Skip to content

Anchor $ to end of string in patternProperties regexes - #232

Merged
horejsek merged 1 commit into
horejsek:masterfrom
kakkar87d:pattern-properties-end-anchor
Nov 2, 2026
Merged

horejsek merged 1 commit into
horejsek:masterfrom
kakkar87d:pattern-properties-end-anchor

Conversation

@kakkar87d

Copy link
Copy Markdown
Contributor

$ in patternProperties matches before a trailing newline

generate_pattern rewrites an unescaped $ to \Z so it only matches at the end of the string (ECMA 262 semantics), but generate_pattern_properties compiles the regex unchanged, so Python's $ also matches before a final \n:

>>> v = fastjsonschema.compile({'patternProperties': {'^[a-z]+$': {}}, 'additionalProperties': False})
>>> v({'abc\n': 1})                 # accepted
>>> v = fastjsonschema.compile({'pattern': '^[a-z]+$', 'patternProperties': {'^[a-z]+$': {}}})
>>> v('abc\n')                      # accepted

The second case happens because both keywords store the compiled regex in REGEX_PATTERNS under the original pattern string, so the unfixed patternProperties compile overwrites the fixed one used by pattern. Two tests in tests/test_pattern_properties.py cover both cases; they fail on master and pass with this change, and the rest of the suite passes. CHANGELOG.txt has an entry under UNRELEASED.

@horejsek

horejsek commented Nov 1, 2026

Copy link
Copy Markdown
Owner

Could you please rebase? 🙏

generate_pattern replaces an unescaped $ with \Z so it matches only at
the end of the string, as in ECMA 262, but generate_pattern_properties
compiled the regex as is. Python's $ also matches before a trailing
newline, so with patternProperties {'^[a-z]+$': {}} and
additionalProperties false a key 'abc\n' was accepted.

Both keywords store the compiled regex in REGEX_PATTERNS under the
original pattern string, so when a pattern and a patternProperties key
use the same regex the unfixed one overwrote the fixed one and the
pattern keyword accepted 'abc\n' too. Apply the same substitution in
generate_pattern_properties.
@kakkar87d
kakkar87d force-pushed the pattern-properties-end-anchor branch from 1bf6f40 to cc050a2 Compare November 2, 2026 07:26
@kakkar87d

Copy link
Copy Markdown
Contributor Author

Rebased on 2.22.3. The only conflict was the changelog, so the entry is now under a new UNRELEASED section.

@horejsek
horejsek merged commit 4999d6b into horejsek:master Nov 2, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants