Skip to content

Commit dc7ba49

Browse files
committed
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>
1 parent 4bfb4a3 commit dc7ba49

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,19 @@ 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: the maximum length of a `let` binding's `<UserIdentifier>`.
925+
#
926+
# Flat 512, deliberately not the §7.1 `<Identifier>` cap that
927+
# `NameIdentifierLengthMixin` and the inline
928+
# `512 if "FEATURE_BUNDLE_1" in context.extensions else 64` checks apply. §3.6.1
929+
# states one maximum for a `<UserIdentifier>` and does not gate it on an
930+
# extension, and the conformance pair proves the flat reading:
931+
# `EXPR/job_templates/3.6--let-boundary-edges.yaml` declares `EXPR` alone and
932+
# requires a 512-character name to be accepted, so borrowing the §7.1 cap would
933+
# limit it to 64 and reject a template the spec permits. EXPR gates whether
934+
# `let` exists at all and moves neither cap.
935+
LET_MAX_IDENTIFIER_LEN = 512
936+
924937

925938
def parse_let_bindings(value: Any) -> list[tuple[str, str]]:
926939
"""Parse a ``let`` field value (list of ``"name = expression"`` strings)
@@ -941,6 +954,12 @@ def parse_let_bindings(value: Any) -> list[tuple[str, str]]:
941954
expr = expr.strip()
942955
if not _LET_NAME_RE.match(name):
943956
raise ValueError(f"A 'let' binding name must be a valid identifier: {name!r}")
957+
# The name is not interpolated into the message: at 513 characters it
958+
# would dwarf the diagnostic.
959+
if len(name) > LET_MAX_IDENTIFIER_LEN:
960+
raise ValueError(
961+
f"A 'let' binding name must be at most {LET_MAX_IDENTIFIER_LEN} characters long"
962+
)
944963
if not expr:
945964
raise ValueError(f"A 'let' binding must define an expression: {binding!r}")
946965
result.append((name, expr))

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ 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, because the cap does not depend on FEATURE_BUNDLE_1.
46+
def test_name_512_chars(self):
47+
name = "a" * 512
48+
_decode(_job([{"name": "S", "let": [f"{name} = 1"], "script": _onrun("hi")}]))
49+
50+
def test_name_512_chars_with_fb1(self):
51+
name = "a" * 512
52+
_decode(
53+
_job(
54+
[{"name": "S", "let": [f"{name} = 1"], "script": _onrun("hi")}],
55+
extensions=("EXPR", "FEATURE_BUNDLE_1"),
56+
)
57+
)
58+
59+
def test_name_512_chars_script(self):
60+
name = "a" * 512
61+
_decode(_job([{"name": "S", "script": {"let": [f"{name} = 1"], **_onrun("hi")}}]))
62+
4463
def test_chained_and_functions(self):
4564
_decode(
4665
_job(
@@ -98,6 +117,34 @@ def test_self_reference(self):
98117
with pytest.raises(DecodeValidationError, match="cannot reference itself"):
99118
_decode(_job([{"name": "S", "let": ["x = x + 1"], "script": _onrun("hi")}]))
100119

120+
# §3.6.1: a `<UserIdentifier>` is at most 512 characters. The cap is flat,
121+
# not the FEATURE_BUNDLE_1-gated §7.1 `<Identifier>` cap, so the accept case
122+
# must hold with EXPR alone. `_job` declares EXPR only by default, which is
123+
# what the conformance fixture
124+
# `EXPR/job_templates/3.6--let-boundary-edges.yaml` asserts; the
125+
# `_with_fb1` variants pin that declaring FEATURE_BUNDLE_1 does not move it.
126+
def test_name_513_chars(self):
127+
name = "a" * 513
128+
with pytest.raises(DecodeValidationError, match="at most 512 characters"):
129+
_decode(_job([{"name": "S", "let": [f"{name} = 1"], "script": _onrun("hi")}]))
130+
131+
def test_name_513_chars_with_fb1(self):
132+
name = "a" * 513
133+
with pytest.raises(DecodeValidationError, match="at most 512 characters"):
134+
_decode(
135+
_job(
136+
[{"name": "S", "let": [f"{name} = 1"], "script": _onrun("hi")}],
137+
extensions=("EXPR", "FEATURE_BUNDLE_1"),
138+
)
139+
)
140+
141+
def test_name_513_chars_script(self):
142+
name = "a" * 513
143+
with pytest.raises(DecodeValidationError, match="at most 512 characters"):
144+
_decode(
145+
_job([{"name": "S", "script": {"let": [f"{name} = 1"], **_onrun("hi")}}]),
146+
)
147+
101148
def test_comprehension_shadows_let(self):
102149
with pytest.raises(DecodeValidationError, match="shadows"):
103150
_decode(

0 commit comments

Comments
 (0)