Skip to content

fix: Do not let $ in a pattern match before a trailing newline - #1013

Open
harshbhx wants to merge 1 commit into
jsonrainbow:mainfrom
harshbhx:fix/1012-pattern-dollar-newline
Open

harshbhx wants to merge 1 commit into
jsonrainbow:mainfrom
harshbhx:fix/1012-pattern-dollar-newline

Conversation

@harshbhx

@harshbhx harshbhx commented Dec 5, 2026

Copy link
Copy Markdown

Summary

In ECMA-262 $ without the m flag only matches at the very end of the input. In PCRE it also matches just before a final newline unless the D modifier is set. EcmaPatternConverter::toPcre() returns /.../u, so "5\n" passed ^[0-9]+$ and a property named "5\n" was accepted by patternProperties ^[0-9]+$.

Fixes #1012

Problem

{"type":"string","pattern":"^[0-9]+$"} validated against "5\n" was valid. JavaScript's /^[0-9]+$/u.test("5\n") is false, and the JSON Schema spec says patterns follow ECMA-262.

Solution

Return /.../uD from toPcre(). All callers go through it (pattern, patternProperties, additionalProperties, unevaluatedProperties, Draft-06/07/2019 variants), so one change covers them. D only affects $. A newline can still be matched explicitly (^5\n$ matches "5\n").

Changes

File Change
src/JsonSchema/Tool/EcmaPatternConverter.php add the D modifier, note why in the class docblock
tests/Tool/EcmaPatternConverterTest.php 4 converter cases (trailing newline rejected, plain value accepted, shorthand class, explicit \n still matches)
tests/Constraints/Draft07/EcmaPatternTest.php, tests/Constraints/Draft2019/EcmaPatternTest.php pattern and patternProperties end-to-end cases

Before / After

Value, ^[0-9]+$ Before After
"5" valid valid
"5\n" valid invalid

How to test

vendor/bin/phpunit tests/Tool/EcmaPatternConverterTest.php tests/Constraints/Draft07/EcmaPatternTest.php tests/Constraints/Draft2019/EcmaPatternTest.php

Testing done

Full CI matrix (PHP 7.2 to 8.5, 32-bit, PHPStan, lint, style) green on my fork: harshbhx#1 . Not run locally.

Notes

Draft07/PropertiesNamesConstraint builds its propertyNames.pattern regex without EcmaPatternConverter (plain '/' . $pattern . '/'), so it has other ECMA gaps too. Left out to keep this PR to the issue; happy to send a follow-up.

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.

$ in a pattern also matches before a final newline, so "5\n" passes "^[0-9]+$"

1 participant