Skip to content

feat(openapi): filter the OpenAPI document per caller, cached per grant set (0.9.0) - #26

Merged
schmunk42 merged 6 commits into
masterfrom
feature/utils-openapi-filter
Nov 30, 2026
Merged

schmunk42 merged 6 commits into
masterfrom
feature/utils-openapi-filter

Conversation

@schmunk42

Copy link
Copy Markdown
Member

Summary

Part of #21 (OpenAPI part; the Hydra documentation /api/docs.jsonld follows in a later release, the issue stays open).

The OpenAPI document (/api/docs.jsonopenapi, Swagger UI) is filtered per caller: an operation is listed only if both its security expression and the firewall's access_control rule for its path and method allow the current token. On by default, release 0.9.0.

  • refactor: switch-free OperationSecurityEvaluator extracted from OperationGrantChecker; Hydra behaviour, constructor and service definition unchanged, existing tests untouched.
  • SecurityFilteredOpenApiFactory decorates api_platform.openapi.factory at priority -1000 (outermost). Denied operations and empty paths are removed, schemas that became unreachable and unused tags are pruned; components.securitySchemes and the global security stay for every caller. Paths without an API Platform operation are decided by access_control alone. Without a request (console export) the document is unfiltered.
  • Fail closed: unparsable expressions, expressions with variables such as request, a throwing access checker, missing expression-language or security component hide operations that carry an expression. Object-dependent operations are listed by default (openapi.show_object_dependent_operations).
  • Cache per grant set in a pool derived from cache.system, keyed by the container build id, openapi.cache_lifetime (default 3600, 0 disables); filter_tags bypasses it.
  • Cache-Control: private and Vary: Authorization on the OpenAPI and Swagger UI responses; /api/docs.jsonld untouched.
  • Swagger UI on a resource URL whose operation is hidden although the request is allowed rendered a 500; it now renders without a pre-selected operation.
  • Shipped guard test for apps under src/Test (OpenApiGuardTestCase, OpenApiGuardTrait): no documented operation may answer 403 for a configured caller. Reading methods only by default.

Consumer action

  • Anonymous and low-privilege clients see fewer operations in the OpenAPI document.
  • Apps without symfony/security-bundle or symfony/expression-language lose operations carrying a security expression from the document until they switch the filter off.
  • Own decorators of api_platform.openapi.factory must stay at a priority above -1000.
  • openapi.filter_operations_by_security: false restores the previous document, registers none of the new services and changes no header.

Test plan

  • Full suite green twice in the PHP 8.4 container: 506 tests, 1717 assertions (278 before)
  • Unit tests at the filter alone, kernel tests through a real firewall (callers, path mapping against the real build, decorator order, pruning, cache, headers, console, Swagger UI), guard test with its own red case
  • GitHub Actions green
  • Not verified: API Platform 3.x (^3.0 is allowed by the constraint, only 4.x is installed and tested)

🤖 Generated with Claude Code

schmunk42 and others added 6 commits November 30, 2026 19:49
…om OperationGrantChecker [*]

The grant decision on an operation's security expression moves into a
switch-free evaluator so a second consumer can use it under its own
switch. OperationGrantChecker delegates to it; constructor, service
definition and Hydra behaviour are unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… access_control [*]

A decorator of api_platform.openapi.factory that runs last removes every
operation the current token could not execute, judged by the operation's
security expression and the firewall's access_control rule for its path
and method. Paths without a remaining operation disappear, unreachable
component schemas and unused tags are pruned, securitySchemes and the
global security stay for every caller. On by default
(openapi.filter_operations_by_security); object-dependent operations are
listed by default (openapi.show_object_dependent_operations). Without a
request (console) the document stays unfiltered.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… docs response private [*]

The unfiltered document is cached per base URL and spec version, the
filtered one per grant set, in a pool derived from cache.system and
keyed by the container build id; openapi.cache_lifetime (default 3600,
0 disables) bounds an entry. A request with filter_tags bypasses the
cache. A late kernel.response listener forces Cache-Control: private
and adds Vary: Authorization on the OpenAPI and Swagger UI responses;
/api/docs.jsonld is untouched.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A reusable building block under src/Test that, per configured caller,
reads the OpenAPI document and calls every documented operation: a 403
on a documented operation is a violation. Only reading methods are
called by default; writing methods need an explicit opt-in because they
execute real requests in the app's test environment.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…, fix the Swagger UI 500 [*]

Kernel tests with SecurityBundle cover callers, path mapping against the
real document build, decorator order, object-dependent operations,
pruning, the cache per grant set, response headers and the console
export. Swagger UI on a resource URL whose operation the filtered
document hides although the request is allowed answered 500; a decorator
of api_platform.swagger_ui.processor now renders it without a
pre-selected operation.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
README section for the OpenAPI filter and the guard test, CHANGELOG
entry with the consumer actions. Part of issue #21.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@schmunk42 schmunk42 self-assigned this Nov 30, 2026
@schmunk42
schmunk42 merged commit 2173227 into master Nov 30, 2026
2 checks passed
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.

1 participant