Repository navigation
fix: match pattern $ only at the end of values - #810
Conversation
Connexion and jsonschema check `pattern` with Python's `re.search()`,
where `$` also matches before a trailing newline, so `^[0-9a-f]{24}$`
accepted "<24 hex digits>\n". ECMA 262, to which JSON Schema and
OpenAPI refer, matches `$` only at the end.
FOCA's body and parameter validators now check `pattern` with `$`
anchors replaced by `\Z` (escapes and sets kept). Parameters that
Connexion accepted are checked again only if a pattern has a `$`.
Docs: stricter request checks and the 0.23.0 migration guide.
Closes #797
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Keep `$` in pattern comments, share the schema check of parameters, type the body validator's arguments, test the default body validator, and document inline flags and custom parameter validators. Co-Authored-By: Claude Opus 5.5 <[email protected]>
As in Python, an escaped `)` does not end a `(?#...)` comment. Also test custom parameter validators and rewrap the docs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideImplementationTranslate unescaped
DocumentationExplain the stricter request pattern semantics and identify the migration impact for clients sending values with trailing newlines.
TestsCover anchor translation and matching, nested and referenced schemas, request validation errors, and custom validator behavior.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
…chor # Conflicts: # docs/migration-0.23.md
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #810 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 76 76
Lines 10568 10634 +66
=========================================
+ Hits 10568 10634 +66
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Linked issuesChecked at commit 2754a94 #797 fix: schema patterns accept a trailing newline · 2 of 2 met
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="foca/api/validation.py" line_range="452" />
<code_context>
+ if value is None or (is_nullable(param) and is_null(value)):
+ return None
+ schema = param.get("schema", param)
+ if schema.get("type") == "file" or not _has_end_anchor(schema):
+ return None
+ # Converted by Connexion already, without error
</code_context>
<issue_to_address>
**Referenced parameter patterns are skipped**
When an operation parameter references a schema whose pattern ends in `$`, `_has_end_anchor` does not follow `$ref` targets, so `_pattern_reason` skips its strict check when the parameter schema is a reference. Connexion's Python regex check then accepts a trailing newline, and the request reaches the controller with a value the referenced pattern was meant to reject.
Resolve parameter schema references before scanning and validating them, or use the same resolved schema Connexion validated.
</issue_to_address>Sourcery assessment
Approval pending. 1 finding to address first.
Blocking findings: foca/api/validation.py:452
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Connexion and jsonschema check
patternwith Python'sre.search(), where$also matches before a newline at the end, so^[0-9a-f]{24}$accepted"<24 hex digits>\n"(e.g.,?pageToken=...%0A), and controllers that parse such values further could answer500. ECMA 262, to which JSON Schema and OpenAPI refer, matches$only at the end.FOCA's request validation (
foca/api/validation.py) now checkspatternas in ECMA 262 at the end of values:_end_anchored()replaces anchors$by\Z, keeping escaped dollar signs, those in sets (read as Python reads sets, including a leading]) and those in(?#...)comments (ended by the first unescaped)); cached withlru_cache.BodyValidatoruses Connexion'sDraft4RequestValidatorextended with FOCA'spatterncheck, unless a validator class is passed.$(same message as Connexion's reason); FOCA's own listing of invalid parameters (after the first invalid one, or after an invalid body) uses the same check (shared_schema_errors()).pattern-mismatch, as before for other pattern errors.patternPropertieskeys, and apps with their ownbodyorparametervalidator invalidator_map.Docs: "Stricter request checks" in
docs/configuration/api.md, and a new section "Patterns match at the end only" indocs/migration-0.23.md(listed in its intro), as requests with a trailing newline that passed before are now refused with400.Closes #797
Verification
TestPatternstests intests/api/test_validation.py(query, path and body parameters with a trailing%0A, body + parameters, after the first invalid parameter, translation table, default and custom body validator, custom parameter validator) failed before the fix and pass now.pytest tests/api/test_validation.py: 113 passed; coverage offoca/api/validation.py100%.coverage run -m pytest): 5242 passed, 2 skipped. The 246 MongoDB-dependent tests failed in the first run because the localmongodcouldn't start in the sandbox; I reran them against a private MongoDB 7.0 on port 12345: 1150 passed, 2 skipped (8 files:tests/factories/test_celery_worker_fork.py,tests/plugins/test_plugin_store.py,tests/security/access_control/...,tests/test_cli.py,tests/testing/test_pytest_plugin.py). The overall coverage gate therefore didn't run in a single local pass; CI runs it.ruff check,ruff format --check,mypy: clean.mkdocs build --strict: passes.$refparameter schemas: Connexion resolves references before validation; covered by the newtest_referenced_schema(passes without code changes), thread answered and resolved.CI passed), after mergingdev(conflict in the intro list ofdocs/migration-0.23.md, both bullets kept).Declined review findings
(?m)patterns:$becomes\Zunder Python's inline MULTILINE flag as well. Declined: ECMA 262 has no inline flags, so these patterns aren't valid JSON Schema patterns anyway. The behaviour is documented.(?x),#) aren't recognized, so anchors after a[in such a comment keep Python's meaning (fails open). Declined: Python-only syntax; documented in the docstring._has_end_anchor()scans each parameter schema per request and has no cycle guard. Declined: schemas are small; recursive parameter schemas already fail in Connexion before this check (RecursionError)._pattern_reason()repeats the null and file checks of_parameter_errors(). Declined: the schema check is shared (_schema_errors()); the remaining checks are three lines, and the two functions return different things.BodyValidator.__init__repeats the signature of Connexion 2'sRequestBodyValidator. Kept on purpose, with a note: it mirrors Connexion 2 until the move to Connexion 3 (build: add support for Connexion 3+ #188).Open questions
None.
🤖 Generated with Claude Code