Repository navigation
feat(validation): stricter request checks - #686
Conversation
Connexion 2 lets three kinds of invalid requests through: - Unpaired surrogates in JSON bodies can't be encoded for storage, and fail with a server error: refused with a client error. - The formats date-time, date and uri aren't checked without optional packages of jsonschema: registered on the format checker in use. - Query parameters of operations without any parameters aren't checked with strict_validation: refused via a resolver wrapper. To be revisited with the move to Connexion 3 (#188). Co-Authored-By: Claude Opus 5.5 <[email protected]>
From review: API keys in the query aren't extra parameters, validators of apps are kept, the permissions API checks bodies as well, and the format checks of the service info are public, in one place. Adds tests of responses, dates, resolvers and the permissions API. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The body was refused before the query was looked at. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideIntroduces centralized stricter validation for application and permissions APIs by extending Connexion's body and resolver hooks, registering additional JSON Schema formats, and documenting and testing the resulting 400/500 failure behavior and compatibility exceptions. Sequence diagram for stricter request validationsequenceDiagram
actor Client
participant API
participant BodyValidator
participant StrictParametersResolver
participant Connexion
Client->>API: Send request
API->>BodyValidator: validate_schema(data, url)
alt Unpaired surrogate
BodyValidator-->>Client: 400 BadRequestProblem
else Valid body
BodyValidator->>Connexion: Validate schema and formats
Connexion->>StrictParametersResolver: Resolve operation
alt strict_validation and unexpected query parameter
StrictParametersResolver-->>Client: 400 ExtraParameterProblem
else API key query parameter
StrictParametersResolver->>Connexion: Invoke operation
Connexion-->>Client: API response
end
end
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #686 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 62 63 +1
Lines 8130 8197 +67
=========================================
+ Hits 8130 8197 +67
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:
|
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="foca/api/validation.py" line_range="33" />
<code_context>
+
+# The format checker that Connexion uses
+for _name, _check in (
+ ("date-time", is_date_time),
+ ("date", is_date),
+ ("uri", is_uri),
</code_context>
<issue_to_address>
**Invalid leap seconds pass validation**
When a request or response contains a `date-time` value with seconds `60` at a time that is not a leap second, `is_date_time` accepts the value without verifying that the leap second occurs at the end of the day, so the registered format checker lets invalid `date-time` values pass request and response validation.
Update `is_date_time` to accept seconds `60` only for an actual leap second at the end of a day.
Also at `foca/utils/service_info.py:65`, `foca/api/validation.py:37`.
</issue_to_address>
### Comment 2
<location path="foca/api/validation.py" line_range="37" />
<code_context>
+ ("date", is_date),
+ ("uri", is_uri),
+):
+ Draft4Validator.FORMAT_CHECKER.checks(_name)(_check)
+
+
</code_context>
<issue_to_address>
**Unrelated validators accept malformed URIs**
When another component in the process validates formats using the shared Draft 4 checker, `Draft4Validator.FORMAT_CHECKER.checks` replaces the URI checker on the shared Draft 4 checker with FOCA’s intentionally lenient `is_uri`; other components that reuse that checker then accept URI values their previous checker rejected.
Scope the custom format checker to FOCA’s Connexion validators instead of changing the shared checker.
</issue_to_address>
### Comment 3
<location path="tests/api/test_validation.py" line_range="375-377" />
<code_context>
+
+ resolver = StrictParametersResolver(_resolve)
+ operation = MagicMock(strict_validation=True)
+ resolution = resolver.resolve(operation)
+ assert isinstance(resolution, Resolution)
+ assert len(calls) == 1
+
+
</code_context>
<issue_to_address>
**Custom resolver result goes unchecked**
When the wrapper invokes the custom resolver but fails to preserve its returned function, if `StrictParametersResolver.resolve` calls the configured resolver but discards its returned function, this test still passes: it checks only that the result is a `Resolution` and that the resolver was called once. The app can then dispatch the wrong function without this test catching it.
Assert that invoking the resulting resolution dispatches to the function returned by the custom resolver.
</issue_to_address>Linked issues
#617 feat(validation): stricter request checks · 5 of 5 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Refuse unpaired surrogates in bodies, including nested strings | ✅ Met | BodyValidator checks strings recursively, including dictionary keys and nested values; tests cover both nested strings and keys. |
| 2 | Check JSON Schema formats such as date-time | ✅ Met | The Draft 4 format checker registers checks for date-time, date, and uri; tests cover body and parameter validation, and response validation. |
| 3 | Refuse extra query parameters on parameterless operations with strict_validation | ✅ Met | StrictParametersResolver rejects extras for parameterless operations and exempts query API keys; tests cover these cases. |
| 4 | Test each case, including nested strings, with 100% coverage | ✅ Met | Tests cover the listed validation cases, including nested surrogate strings; the PR description reports 100% coverage. |
| 5 | Document that format checks can make previously passing requests fail | ✅ Met | The API documentation describes the format checks, and the migration guide warns that previously accepted values can now fail. |
Sourcery assessment
Approval pending. 3 findings to address first.
Blocking findings: foca/api/validation.py:33, foca/api/validation.py:37, tests/api/test_validation.py:377
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…dation # Conflicts: # docs/migration-0.21.md
Summary
Closes #617.
Connexion 2 lets three kinds of invalid requests through. New in
foca.api.validation, for the app's APIs and the permissions API:\ud800), also in keys and nested strings, are refused with400instead of failing with500on storage. Only where Connexion checks bodies at all: operations with a schema and only JSON media types.date-time,dateanduriare checked, which jsonschema only does with optional packages. Importing the module registers them on jsonschema's Draft 4 checker for the process, which is the object Connexion uses. So they apply to responses withvalidate_responsestoo, which then fail with500, e.g., for stored timestamps without a time zone.strict_validation, via a resolver wrapper. Extra parameters are handled here, not left to build: add support for Connexion 3+ #188. API keys in the query are exempt.A custom
bodyvalidator invalidator_mapwins, and takes over the surrogate check. To be revisited with the move to Connexion 3 (#188).Docs:
docs/configuration/api.md,docs/migration-0.21.md,docs/reference.md.How it was verified
validator_map, the permissions API without body check, and docs overstating the coverage; all fixed. Round 2 ran a matrix ofapiKeysecurity on Swagger 2 and OpenAPI 3 against real Connexion apps, without findings.Declined review findings
operation._security_schemesis private to Connexion: Connexion has no public accessor, FOCA already readsoperation._operation, and the test of API keys goes through a real app, so a rename would show.*/*aren't checked: Connexion skips them for all body checks; documented.Open questions
None.
🤖 Generated with Claude Code
Summary by Sourcery
Enforce stricter request validation across application and permissions APIs to reject malformed values and undeclared query parameters consistently.
New Features:
Bug Fixes:
Enhancements:
Documentation:
Tests: