Repository navigation
Conversation
545b1f0 to
8a6ba78
Compare
|
Thanks for the quick fix in #956, and for the heads-up! Rebased on main. One thing worth mentioning: the two |
DannyvdSluijs
left a comment
There was a problem hiding this comment.
Thanks for the additions. A few remarks and there was a merge conflict that should be addressed as well.
There was a problem hiding this comment.
Nitpick: This could use an update since the logic is changing.
There was a problem hiding this comment.
Updated in ce27bfd, it now points to holdsSchemas() instead of talking about enum and const.
| @@ -711,12 +705,6 @@ private function shouldNotYieldTest(string $name): bool | |||
| '[draft2019-09/optional/format/uri-template.json]: format: uri-template: a space in a literal is invalid is expected to be invalid', | |||
| '[draft2019-09/optional/format/uri-template.json]: format: uri-template: a delete character in a literal is invalid is expected to be invalid', | |||
| // Optional unknown keyword: Not yet implemented | |||
There was a problem hiding this comment.
Nitpick: Seems like all below items are removed and the comment is left dangling. Could you remove it as well?
There was a problem hiding this comment.
Removed, thanks. The conflict was in that same block, so it went away with the rebase on main.
8a6ba78 to
ce27bfd
Compare
DannyvdSluijs
left a comment
There was a problem hiding this comment.
I overlooked the expand refs also works inside data containers. I guess that change needs to be reverted.
b81e3c0 to
6796d2f
Compare
|
Rebased on main to resolve the conflict with #970 (which is also why CI never ran on the previous push). Its anchor handling is kept as is, |
Roman3349
left a comment
There was a problem hiding this comment.
Thanks for the fix! Two issues I'd address before merging, plus one pre-existing one that the PR makes more visible:
expandRefs() (pre-existing, SchemaStorage.php ~L147): it still descends into default, examples and unknown keywords. An $id there still shifts the base URI for $refs beneath it, although this PR says such an id "identifies nothing". And $ref strings inside default/examples data get rewritten into absolute pointers, so with CHECK_MODE_APPLY_DEFAULTS the rewritten value ends up in the user's data. Example: {"definitions":{"int":{"type":"integer"}},"properties":{"a":{"$ref":"#/x-wrap/inner"}},"x-wrap":{"$id":"http://other.invalid/s.json","inner":{"$ref":"#/definitions/int"}}}: the inner $ref becomes http://other.invalid/s.json#/definitions/int and validation tries a network fetch. The two walkers now disagree about what counts as a schema. Could be a follow-up issue.
| * keyword is a schema there, which is what $parentProperty decides. | ||
| */ | ||
| private static function holdsSchemas(string $propertyName, string $parentProperty): bool | ||
| { |
There was a problem hiding this comment.
This still leaks ids from data positions when a subschema's name is a map keyword. scanMembersForSubschemas() passes each map member's own name as $parentProperty, so the members of a property called properties, definitions, $defs, dependencies, patternProperties or dependentSchemas are scanned with that name and holdsSchemas() returns true for all of them.
Repro:
$storage->addSchema('http://example.org/root/', json_decode('{"properties": {"properties": {"type":"object", "default": {"id": "leaked.json"}, "enum": [{"id": "e.json"}], "x-unknown": {"id": "u.json"}}}}'));This registers http://example.org/root/leaked.json, e.json and u.json. A property named properties is common (e.g. GeoJSON).
Suggestion: track whether the container being scanned is a schema map or a schema, e.g. pass a flag or an empty $parentProperty when recursing into a map member's schema, instead of reusing the member's name.
There was a problem hiding this comment.
Good catch, thanks! Confirmed with your repro (it leaks on main too, but this PR should close it). Fixed in 71f2003: a member of a schema map is now scanned as a schema, so its own name no longer decides what its members hold. Your case is in the data provider and fails without the fix.
| // into. Data positions such as 'default', 'examples', 'enum', 'const' and any unknown | ||
| // keyword are left alone, and an id written there identifies nothing, | ||
| // see https://github.com/json-schema-org/JSON-Schema-Test-Suite/pull/471 | ||
| if (!self::holdsSchemas((string) $propertyName, $parentProperty)) { |
There was a problem hiding this comment.
This is a behaviour change: ids declared under a custom container keyword (OpenAPI-style components, x-definitions, ...) are no longer registered, so $refs to them now fall through to a remote fetch.
Repro: schema {"properties":{"a":{"$ref":"http://example.invalid/c.json"}},"components":{"C":{"$id":"http://example.invalid/c.json","type":"integer"}}} with data {"a":"x"}. On main this reports a type error; with this PR it throws ResourceNotFoundException for http://example.invalid/c.json.
This is more spec-correct, but it breaks setups that work today. Could we either note it in the CHANGELOG as a BC break, or keep a fallback (e.g. register such ids only when nothing else claims the URI)?
There was a problem hiding this comment.
Confirmed: main reports the type error, this PR throws ResourceNotFoundException. That's the behaviour #949 asks for (an id under an unknown keyword identifies nothing, as in the test suite's unknownKeyword.json), but you're right that it breaks setups working today. @DannyvdSluijs your call: keep it and flag it in the release notes, or register such ids as a fallback only when no real schema claims the URI? Happy to do either.
|
On |
Fixes #949
Problem
SchemaStoragewalks a document looking forid/$idso it can register subschemas under their canonical URI. It descends into every member it meets, so an$idwritten in a position that holds data rather than a schema gets registered as though it identified a schema. When that value collides with a real$idelsewhere in the document, the fake one can win, and a$refto that URI then resolves to the wrong schema.That is exactly what
unknownKeyword.jsonin the official suite pins down: an$idburied under an unknown keyword must not shadow the real one.Cause
scanMembersForSubschemas()skippedenumandconstby name. That covers two data positions out of many:default,examplesand any unknown keyword were still descended into.Fix
Instead of listing the positions to avoid, which is open ended, the scanner now only descends into positions that actually hold schemas. Two classification lists sit beside the existing
SCHEMA_MAP_KEYWORDS:SCHEMA_KEYWORDS, whose value is a schema (not,if,additionalProperties, and so on)SCHEMA_ARRAY_KEYWORDS, whose value is a list of schemas (allOf,anyOf,prefixItems, and so on)Everything else is data, and an
idinside it identifies nothing.Members of a schema map (
properties,$defs,definitions, ...) remain schemas whatever their name, which is what the existing$parentPropertycheck already guaranteed: a subschema legitimately namedenumis still traversed.The classification is deliberately draft agnostic, since
SchemaStorageis never told which dialect it is reading. A keyword counts if it holds schemas in any draft. That is also the safer direction: treating a keyword unknown to the current draft as a schema position is simply the behaviour every keyword had before.Tests
Six cases added to
subschemaRegistrationProvider(). Four fail without the source change (default,examples, an unknown keyword holding a list, an unknown keyword holding a map). The remaining two guard against over narrowing in the other direction, checking that anidundernotand under anitemstuple is still registered.The six now obsolete
unknownKeyword.jsonentries inshouldNotYieldTest()are removed.Notes
expandRefs()carries the sameenum/constspecial case. I left it untouched: the fixture does not exercise it, and measuring confirmed the scanner change alone is sufficient. Glad to align the two walkers in a follow up if you would prefer they agree.One caveat on how far I could verify this.
JsonSchemaTestSuiteTestcurrently yields no tests at all, because the upstream suite added atests/v1/directory thatDraftIdentifiers::fromConstraintName()rejects, and PHPUnit reports that as a warning while still exiting 0. I opened #953 about it. To exercise theunknownKeywordcases here I skippedv1locally, which is not part of this PR. With the suite unblocked that way, this change takes the failure count from 364 down to 354, repairing 6 suite cases plus the 4 new unit cases, and breaking none.PHPStan reports no errors. Thanks for the library, and for the very readable
SchemaStorage, which made this one pleasant to chase. 🙂