Skip to content

Commit f72dc4b

Browse files
committed
test(431): guard the field-threshold blob with a schema-derived field name
Review found that reverting only thresholds_json left every test green, though that path runs injected script in Chrome via a JSON Schema property name. Add that case, and name the route in the CHANGELOG.
1 parent 19f8618 commit f72dc4b

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ Each release links to full notes on the
218218
ends the block at the first `</script`, so a ground-truth or predicted value
219219
carrying `</script><script>...</script>` ran as its own script when the
220220
report was opened. Values are model output or third-party documents, so this
221-
needed no access to stickler or the host. `<`, `>`, `&`, U+2028 and U+2029
221+
needed no access to stickler or the host. A JSON Schema property name reached
222+
the same block through the field thresholds. `<`, `>`, `&`, U+2028 and U+2029
222223
are now written as `\uXXXX`, which the page's JavaScript reads as the same
223224
characters. Present since 0.1.3
224225
([#431](https://github.com/awslabs/stickler/issues/431))

‎tests/reporting/html/test_script_embedding.py‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,20 @@ def handle_data(self, data):
5252
self.bodies[-1] += data
5353

5454

55-
def _report(tmp_path, predicted_vendor):
55+
def _report(tmp_path, predicted_vendor, model=Invoice, gt=None, pred=None):
5656
jsonl = tmp_path / "results.jsonl"
5757
jsonl.touch()
5858
evaluator = BulkStructuredModelEvaluator(
59-
target_schema=Invoice, individual_results_jsonl=str(jsonl)
59+
target_schema=model, individual_results_jsonl=str(jsonl)
6060
)
61-
evaluator.update(Invoice(vendor="Acme"), Invoice(vendor=predicted_vendor), doc_id="doc1")
61+
gt = gt if gt is not None else model(vendor="Acme")
62+
pred = pred if pred is not None else model(vendor=predicted_vendor)
63+
evaluator.update(gt, pred, doc_id="doc1")
6264
out = tmp_path / "report.html"
6365
result = EvaluationHTMLReporter().generate_report(
6466
evaluator.compute(),
6567
str(out),
66-
model_schema=Invoice,
68+
model_schema=model,
6769
individual_results_jsonl_path=str(jsonl),
6870
)
6971
assert result.success
@@ -84,6 +86,29 @@ def test_a_document_value_cannot_end_the_script_block(tmp_path):
8486
assert PAYLOAD not in page
8587

8688

89+
def test_a_field_name_cannot_end_the_script_block(tmp_path):
90+
"""A JSON Schema property name reaches the page through the field
91+
thresholds, the second blob in the same block, so it is guarded too."""
92+
name = "a" + PAYLOAD
93+
model = StructuredModel.from_json_schema(
94+
{"type": "object", "properties": {"vendor": {"type": "string"}, name: {"type": "string"}}}
95+
)
96+
plain = tmp_path / "plain"
97+
plain.mkdir()
98+
_, baseline = _report(plain, "Acme Corp")
99+
page, bodies = _report(
100+
tmp_path,
101+
None,
102+
model=model,
103+
gt=model(**{"vendor": "Acme", name: "x"}),
104+
pred=model(**{"vendor": "Acme", name: "y"}),
105+
)
106+
107+
assert len(bodies) == len(baseline)
108+
assert not any(body.strip() == "alert(1)" for body in bodies)
109+
assert PAYLOAD not in page
110+
111+
87112
def test_the_value_still_reaches_the_page_data_unchanged(tmp_path):
88113
page, _ = _report(tmp_path, PAYLOAD)
89114

0 commit comments

Comments
 (0)