Repository navigation
feat: report invalid fields in the permissions API - #765
Conversation
The permissions API raises InvalidFieldsProblem for invalid rules, identifiers and pages, naming the field (JSON Pointer or parameter) with a stable code and never the rejected value. validate_rule() and validate_rule_value() raise InvalidRuleError, a ValueError carrying the field and code. Codes are documented in "Invalid requests"; the 0.23 migration guide covers the change. Closes #760 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMpmDCfbACLj3uy6xV
Log rejected rule keys in _check_rule_keys, test InvalidRuleError's field, code and reason directly, and document where each code of the permissions API is reported. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMpmDCfbACLj3uy6xV
Reviewer's GuideData structuresIntroduce
APIReplace plain bad-request failures in the permissions API with structured invalid-field errors for rules, identifiers, and pagination parameters.
DocumentationDocument the permission API's invalid-field codes and locations, show a representative response, and explain the 0.23 migration impact.
TestsVerify rule-validation metadata and the permissions API's reported field, code, and value-safe details for invalid requests.
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 #765 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 74 74
Lines 9472 9517 +45
=========================================
+ Hits 9472 9517 +45
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 aa45d08 #760 feat: report invalid fields in the permissions API · 3 of 3 met
|
There was a problem hiding this comment.
Hey - I've found 4 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="docs/problems/invalid-request.md" line_range="118" />
<code_context>
+| `unknown-policy-type` | `#/policy_type` | `policy_type` is not defined in the app's access control model. |
+| `unknown-property` | `#/rule/<key>` | `rule` has a key other than `v0` to `v5`. |
+| `non-consecutive-keys` | `#/rule` | The keys of `rule` are not consecutive, starting from `v0`. |
+| `wrong-value-count` | `#/rule` | `rule` has more or fewer values than its policy type's definition has fields. |
+| `too-short`, `too-long` | `#/rule/vN` | A value is empty or longer than 1024 characters. |
+| `invalid-character` | `#/rule/vN`, parameter `id` | A value has a comma or line break, or starts with `#`; or `id` has characters other than those of `access_control.id_charset`. |
</code_context>
<issue_to_address>
**Empty rules report the wrong error**
When an HTTP POST or PUT supplies an empty `rule` object, the request-body schema requires `rule.v0`, so `BodyValidator` rejects the request before `_get_rule()` reports `wrong-value-count` at `#/rule`; callers receive `required` at `#/rule/v0` instead of the documented code and location.
Allow an empty `rule` through body validation so `_get_rule()` can return `wrong-value-count` at `#/rule`.
Also at `docs/problems/invalid-request.md:113`.
</issue_to_address>
### Comment 2
<location path="foca/security/access_control/access_control_server.py" line_range="167" />
<code_context>
MAX_RULE_VALUES,
+ InvalidRuleError,
validate_rule,
)
from foca.utils.logging import escape_log_value, log_traffic_if
</code_context>
<issue_to_address>
**Aggregate invalid fields**
The issue describes `errors` listing each invalid field, but `_invalid_field` constructs an `InvalidFieldsProblem` with a single `FieldError`. Requests with multiple invalid fields therefore report only the first one found.
</issue_to_address>
### Comment 3
<location path="docs/problems/invalid-request.md" line_range="112" />
<code_context>
+
+| Code | Reported at | Meaning |
+| --- | --- | --- |
+| `invalid-type` | `#`, `#/rule`, `#/policy_type`, `#/rule/vN` | The body or `rule` is not an object, or `policy_type` or a value is not a string. |
+| `required` | `#/rule`, `#/policy_type` | `rule` or `policy_type` is missing. |
+| `pattern-mismatch` | `#/policy_type` | `policy_type` is not `p` or `g`, optionally followed by digits. |
</code_context>
<issue_to_address>
**Pagination type errors are undocumented**
When a request supplies a non-integer value for `page` or `page_size`, a non-integer `page` or `page_size` is rejected by parameter validation with code `invalid-type` at that parameter, but the table lists only body pointers for this code. Callers cannot use the documented code-location mapping to handle these pagination errors consistently.
Add `page` and `page_size` parameters to the documented `invalid-type` locations.
</issue_to_address>
### Comment 4
<location path="tests/security/access_control/foca_casbin_adapter/test_casbin_rule.py" line_range="195" />
<code_context>
+ exc = exc_info.value
+ assert isinstance(exc, InvalidRuleError)
+ assert (exc.field, exc.code) == (field, code)
+ assert "s3cr3t" not in exc.reason
+
+ def test_reason_defaults_to_message(self) -> None:
</code_context>
<issue_to_address>
**Other rejected values leak unnoticed**
When an error echoes a rejected value other than the literal `s3cr3t`, the fixed-string checks in `TestInvalidRuleError.test_fields` (line 195) and `TestInvalidFields._assert_field` (lines 695–696) still pass if an error echoes a rejected value other than `s3cr3t`, such as the 1025-character value in the too-long case. That leaves value disclosure regressions undetected by these tests.
Pass each rejected value to the assertion helper and verify that value is absent from the client-facing error details.
Also at `tests/security/access_control/test_access_control_server.py:695-696`.
</issue_to_address>Sourcery assessment
Approval pending. 3 findings to address first.
Blocking findings: docs/problems/invalid-request.md:118, foca/security/access_control/access_control_server.py:167, tests/security/access_control/foca_casbin_adapter/test_casbin_rule.py:195
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMpmDCfbACLj3uy6xV
Summary
FOCA's permissions API (
/permissions) now reports invalid rules, identifiers and pages asInvalidFieldsProblem, the same way the app endpoints do. Each response names the field (a JSON Pointer such as#/rule/v0, or the parameterid,pageorpage_size), gives a stable code, and never includes the rejected value.casbin_rule.validate_rule()andvalidate_rule_value()raise the newInvalidRuleError. It subclassesValueErrorand addsfield,codeand areasonthat leaves out the value. Existingexcept ValueErrorcallers are unchanged.docs/problems/invalid-request.md.docs/access-control.mdhas an example response.docs/migration-0.23.md) has a new section for this change: the problem details now come from the mapping forRequestValidationProblem, notBadRequest; withexceptions.public_members, listtype,codeanderrors; and tests should expectInvalidFieldsProblem.Suggested 0.23 release-note bullet (Features): "The permissions API reports invalid rules, identifiers and pages as invalid fields, with stable codes and without the rejected values (#760, PR #765)."
Verification
pytest -m "not mongodb": 4856 passed, 2 skipped. The new tests failed before the change: 93 failed intest_access_control_server.py.foca/cli.pyandfoca/testing/pytest_plugin.pystay below 100% without MongoDB; this is unrelated and covered in CI.ruff check,ruff format --check,mypy,ast-grep scan: all clean.InvalidRuleError, the log flow in_get_rule, and a "Reported at" column in the docs.Declined review findings
reason(medium): kept.InvalidRuleErrormessages also go into logs andValueErrortexts that other callers and tests match, so changing their punctuation would spread further than this PR.total_pagesfrom page 0.test_opt_in_rolemonkeypatches the predicate (low):grants_opt_in_rolehas its own tests. This test only checks the field and code mapping.Open questions
None.
Closes #760
🤖 Generated with Claude Code
https://claude.ai/code/session_011FSoqMpmDCfbACLj3uy6xV
Summary by Sourcery
Expose structured, value-safe validation errors from the permissions API for invalid rules, identifiers, and pagination parameters.
New Features:
Enhancements:
Documentation:
Tests: