Skip to content

Commit 262d3d2

Browse files
authored
fix: Enforce the 512-character cap on a let binding identifier (#348)
* fix: Enforce the 512-character cap on a let binding identifier Template Schemas §3.6.1 caps a `<UserIdentifier>` at 512 characters. Nothing checked it, so a 513-character `let` binding name was accepted. The conformance fixture pinning this is `EXPR/job_templates/proposed/3.6.1--let-identifier-513.invalid.yaml`, added by openjd-specifications#164, which failed here and in openjd-rs alike. The cap is a flat 512 and deliberately does not reuse the §7.1 `<Identifier>` cap that `NameIdentifierLengthMixin` and the inline `512 if "FEATURE_BUNDLE_1" in context.extensions else 64` checks apply. §3.6.1 states one maximum for a `<UserIdentifier>` and gates it on no extension, and the fixture pair proves the flat reading: the 512-character accept twin `EXPR/job_templates/3.6--let-boundary-edges.yaml` declares EXPR alone, so borrowing the §7.1 cap would limit it to 64 and reject a template the spec permits. The two caps share the number 512 and nothing else, so this adds `LET_MAX_IDENTIFIER_LEN` beside the existing `LET_MAX_BINDINGS` with its own citation. EXPR gates whether `let` exists at all and moves neither cap. All four `_validate_let` field validators route through `validate_let_field` and then `parse_let_bindings`, so the check goes in `parse_let_bindings` after the `_LET_NAME_RE` match and every scope is covered by one insertion. The message omits the offending name: at 513 characters it would dwarf the diagnostic. Six tests, mutation-checked: removing the check fails all three reject tests while the three accept tests keep passing, so the accept cases act as negative controls against a cap set too low. The default `_job` helper declares EXPR alone, which is what would catch a regression to the FEATURE_BUNDLE_1-gated cap; the `_with_fb1` variants pin that declaring FEATURE_BUNDLE_1 does not move it. The matching openjd-rs change is OpenJobDescription/openjd-rs#358. Design note: SuperDaveDocs docs/conformance-0901/4.6-let-513/fix.md Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com> * chore: Shorten the let identifier cap comments The rationale lives in the commit message, the PR description and the design note. The code only needs the citation and the one fact a reader editing this line must not miss: the cap is flat, so it is not the FEATURE_BUNDLE_1-gated section 7.1 cap. Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com> * fix: Name the over-long binding in the let identifier error Review finding on #348. The validator is a `field_validator` on the whole `let` list, so pydantic anchors the error at `steps[0] -> let` with no list index. Measured on a four-binding template where the third name is 513 characters: the error was steps[0] -> let: A 'let' binding name must be at most 512 characters long which identifies neither the binding nor its position. With 50 bindings allowed that is not diagnosable. Truncate rather than omit, as the reviewer suggested. The message now carries a 32-character prefix and the true length: A 'let' binding name must be at most 512 characters long: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'... (513 characters) Total error text goes from 110 to 166 characters, so it stays bounded while becoming locatable. The original concern about a 513-character name dwarfing the diagnostic still holds against interpolating the name in full, which is why this truncates. Two assertions pin it, both mutation-checked: reverting to the bare message fails `test_name_513_chars` and the new `test_name_513_chars_names_the_offending_binding`, which uses a multi-binding `let` to cover the case the finding was about. No matching change in openjd-rs: its path is `steps[0] -> let[0]`, so the index already identifies the binding there. Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com> * test: Reference the bound name in the let length accept cases Review finding on #348. The three accept-side tests bound a 512-character name and never referenced it, so they proved `parse_let_bindings` does not raise but not that the binding is usable, which is the property the conformance fixture is about. Each now interpolates the bound name in the script args, so the name flows through the variable-reference validation and into the expression parser. All 24 tests still pass, which answers the finding's concern directly: no cap further down that path, in `Identifier`, `FormatString`, or the Rust expression layer, rejects a 512-character binding. Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com> --------- Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
1 parent 085609a commit 262d3d2

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

‎src/openjd/model/v2023_09/_model.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,11 @@ class ScriptInterpreter(str, Enum):
921921
LET_MAX_BINDINGS = 50
922922
_LET_NAME_RE = re.compile(r"^[a-z_][A-Za-z0-9_]*$")
923923

924+
# §3.6.1: maximum length of a `let` binding's `<UserIdentifier>`. Flat, so not
925+
# the §7.1 cap NameIdentifierLengthMixin applies: that one is 64 without
926+
# FEATURE_BUNDLE_1, and a 512-character name must be accepted with EXPR alone.
927+
LET_MAX_IDENTIFIER_LEN = 512
928+
924929

925930
def parse_let_bindings(value: Any) -> list[tuple[str, str]]:
926931
"""Parse a ``let`` field value (list of ``"name = expression"`` strings)
@@ -941,6 +946,14 @@ def parse_let_bindings(value: Any) -> list[tuple[str, str]]:
941946
expr = expr.strip()
942947
if not _LET_NAME_RE.match(name):
943948
raise ValueError(f"A 'let' binding name must be a valid identifier: {name!r}")
949+
# Truncated rather than omitted: the caller is a field_validator on the
950+
# whole list, so the error path is `let` with no index to identify which
951+
# binding is over.
952+
if len(name) > LET_MAX_IDENTIFIER_LEN:
953+
raise ValueError(
954+
f"A 'let' binding name must be at most {LET_MAX_IDENTIFIER_LEN} "
955+
f"characters long: {name[:32]!r}... ({len(name)} characters)"
956+
)
944957
if not expr:
945958
raise ValueError(f"A 'let' binding must define an expression: {binding!r}")
946959
result.append((name, expr))

‎test/openjd/model_v0/v2023_09/test_let_bindings.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ def test_script_let(self):
4141
_job([{"name": "S", "script": {"let": ["a = 2"], **_onrun("{{a}}")}}]),
4242
)
4343

44+
# §3.6.1 boundary: 512 characters is the maximum and must be accepted, with
45+
# EXPR alone, since the cap does not depend on FEATURE_BUNDLE_1.
46+
def test_name_512_chars(self):
47+
name = "a" * 512
48+
# Referenced, not just declared: a cap further down the path would
49+
# otherwise be invisible here.
50+
_decode(_job([{"name": "S", "let": [f"{name} = 1"], "script": _onrun(f"{{{{{name}}}}}")}]))
51+
52+
def test_name_512_chars_with_fb1(self):
53+
name = "a" * 512
54+
_decode(
55+
_job(
56+
[{"name": "S", "let": [f"{name} = 1"], "script": _onrun(f"{{{{{name}}}}}")}],
57+
extensions=("EXPR", "FEATURE_BUNDLE_1"),
58+
)
59+
)
60+
61+
def test_name_512_chars_script(self):
62+
name = "a" * 512
63+
_decode(
64+
_job([{"name": "S", "script": {"let": [f"{name} = 1"], **_onrun(f"{{{{{name}}}}}")}}])
65+
)
66+
4467
def test_chained_and_functions(self):
4568
_decode(
4669
_job(
@@ -98,6 +121,40 @@ def test_self_reference(self):
98121
with pytest.raises(DecodeValidationError, match="cannot reference itself"):
99122
_decode(_job([{"name": "S", "let": ["x = x + 1"], "script": _onrun("hi")}]))
100123

124+
# §3.6.1 caps a `<UserIdentifier>` at 512 characters. `_job` declares EXPR
125+
# alone, so these pin the cap independently of FEATURE_BUNDLE_1.
126+
def test_name_513_chars(self):
127+
name = "a" * 513
128+
with pytest.raises(
129+
DecodeValidationError,
130+
match=r"at most 512 characters long: 'a{32}'\.\.\. \(513 characters\)",
131+
):
132+
_decode(_job([{"name": "S", "let": [f"{name} = 1"], "script": _onrun("hi")}]))
133+
134+
def test_name_513_chars_names_the_offending_binding(self):
135+
# The validator is a field_validator on the whole list, so the error path
136+
# is `let` with no index; the message has to identify the binding itself.
137+
name = "b" * 513
138+
with pytest.raises(DecodeValidationError, match=r"'b{32}'\.\.\. \(513 characters\)"):
139+
_decode(_job([{"name": "S", "let": ["ok = 1", f"{name} = 2"], "script": _onrun("hi")}]))
140+
141+
def test_name_513_chars_with_fb1(self):
142+
name = "a" * 513
143+
with pytest.raises(DecodeValidationError, match="at most 512 characters"):
144+
_decode(
145+
_job(
146+
[{"name": "S", "let": [f"{name} = 1"], "script": _onrun("hi")}],
147+
extensions=("EXPR", "FEATURE_BUNDLE_1"),
148+
)
149+
)
150+
151+
def test_name_513_chars_script(self):
152+
name = "a" * 513
153+
with pytest.raises(DecodeValidationError, match="at most 512 characters"):
154+
_decode(
155+
_job([{"name": "S", "script": {"let": [f"{name} = 1"], **_onrun("hi")}}]),
156+
)
157+
101158
def test_comprehension_shadows_let(self):
102159
with pytest.raises(DecodeValidationError, match="shadows"):
103160
_decode(

0 commit comments

Comments
 (0)