Skip to content

Bypass authentication for anonymous static resources - #2273

Open
sdelamo wants to merge 9 commits into
5.4.xfrom
issue-2267
Open

Bypass authentication for anonymous static resources#2273
sdelamo wants to merge 9 commits into
5.4.xfrom
issue-2267

Conversation

@sdelamo

@sdelamo sdelamo commented Aug 25, 2026

Copy link
Copy Markdown
Member

Add an opt-in security filter setting that skips authentication fetchers for unrouted GET and HEAD requests resolving to static resources with anonymous access.

Provide a replaceable bypass strategy, preserve authentication for routed and protected resources, and cover enabled, disabled, and custom implementations with tests.

Close: #2267

Add an opt-in security filter setting that skips authentication fetchers for unrouted GET and HEAD requests resolving to static resources with anonymous access.

Provide a replaceable bypass strategy, preserve authentication for routed and protected resources, and cover enabled, disabled, and custom implementations with tests.

Close: #2267
@sdelamo
sdelamo requested review from yawkat and a lite review from Copilot August 25, 2026 13:25
@sdelamo sdelamo added the type: improvement A minor improvement to an existing feature label Aug 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an opt-in mechanism in the security module to skip running AuthenticationFetcher implementations for unrouted GET/HEAD requests that resolve to static resources and are configured for anonymous access, addressing issue #2267’s performance concern for static assets when cookies are present.

Changes:

  • Introduces StaticResourceAuthenticationBypass SPI plus a default implementation that detects unrouted static resources and consults the intercept-url-map for anonymous access.
  • Adds micronaut.security.filter.static-resource-authentication-bypass configuration toggle and wires it into SecurityFilter.
  • Adds tests covering enabled/disabled behavior and custom SPI replacement, plus test static resources.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
security/src/main/java/io/micronaut/security/filters/SecurityFilter.java Adds bypass hook before authentication fetchers; introduces new injected constructor and deprecates old one.
security/src/main/java/io/micronaut/security/filters/SecurityFilterConfiguration.java Adds new config flag accessor for the bypass feature.
security/src/main/java/io/micronaut/security/filters/SecurityFilterConfigurationProperties.java Implements/binds the new bypass configuration property.
security/src/main/java/io/micronaut/security/filters/StaticResourceAuthenticationBypass.java New SPI interface for pluggable bypass decision.
security/src/main/java/io/micronaut/security/filters/DefaultStaticResourceAuthenticationBypass.java Default bypass strategy using route-match absence + static resource resolution + intercept-url-map anonymous access.
security/src/main/java/io/micronaut/security/rules/InterceptUrlMapRule.java Refactors pattern matching into findPattern(...) for reuse.
security/src/main/java/io/micronaut/security/rules/ConfigurationInterceptUrlMapRule.java Adds internal helper isAnonymous(request) for bypass decision.
security/src/test/java/io/micronaut/security/filters/StaticResourceAuthenticationBypassTest.java Tests bypass-enabled behavior (static anonymous vs routed vs protected static).
security/src/test/java/io/micronaut/security/filters/StaticResourceAuthenticationBypassDisabledTest.java Tests that authentication is still resolved when bypass is disabled.
security/src/test/java/io/micronaut/security/filters/StaticResourceAuthenticationBypassCustomImplementationTest.java Tests that a custom SPI bean replaces the default implementation.
security/src/test/resources/static-resource-authentication-bypass/asset.txt Test static resource content for anonymous access case.
security/src/test/resources/static-resource-authentication-bypass/protected.txt Test static resource content for protected static resource case.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

sdelamo and others added 7 commits August 26, 2026 09:54
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
- Do not close the RouteMatch in DefaultStaticResourceAuthenticationBypass.
  It is only inspected here and is executed afterwards; close() is a no-op
  today but closing a route match we do not consume is a latent trap.
- Add StaticResourceAuthenticationBypassRouteTakesPrecedenceTest, which maps
  a controller onto a path that also resolves to a static resource. The
  existing routed test used a path outside the static resource mapping, so
  the route-precedence guard was not actually covered.
- Exercise the custom StaticResourceAuthenticationBypass through the filter
  instead of only asserting the injected bean type, including the case where
  the custom implementation declines to bypass.
- Document the feature in the guide, including the requirement for an
  explicit isAnonymous() intercept URL map entry and the fact that security
  rules are still evaluated, with a null authentication.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@sdelamo
sdelamo requested a review from graemerocher September 2, 2026 15:24
@sonarqubecloud

sonarqubecloud Bot commented Sep 2, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: improvement A minor improvement to an existing feature

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

Provide a way to avoid resolving the authentication for some URL pattern

3 participants