fix(cors): Allow Bearer token authentication for CORS requests #55878
+40
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix Bearer Token Authentication for CORS Endpoints
Problem
Bearer token authentication with OAuth 2.0/OIDC currently fails for app-specific APIs (Notes, Calendar, Contacts, etc.) with
401 Unauthorizederrors, even though the same Bearer tokens work correctly for OCS APIs.Root Cause
When using Bearer token authentication with CORS-annotated endpoints:
CORSMiddlewaredetects the logged-in session but no CSRF tokenCORSMiddlewarecallssession->logout()to prevent CSRF attacksThis occurs because app-specific APIs (Notes, Calendar, etc.) use the
#[CORS]attribute, which triggersCORSMiddlewaresecurity checks. OCS APIs don't have this attribute and handle Bearer tokens correctly viaSecurityMiddleware::isValidOCSRequest()(lines 234-237).Error Manifestation
Or in logs:
Solution
This PR extends
CORSMiddlewareto accept Bearer token authentication without requiring CSRF tokens, aligning with the existing pattern used bySecurityMiddlewarefor OCS routes.Changes
lib/private/AppFramework/Middleware/Security/CORSMiddleware.phpAuthorization: Bearerheader before CSRF andapp_apicheckstests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.phptestCORSShouldAllowBearerAuth()test to verify Bearer tokens are acceptedImplementation Details
This check is placed before the CSRF token and
app_apichecks to allow Bearer tokens to bypass session-based security requirements.Security Considerations
Why is this safe?
SecurityMiddlewarealready allows Bearer tokens for OCS endpoints (line 234-237)What doesn't change?
app_apiflag) still worksBackward Compatibility
✅ Fully backward compatible
Related Issues & PRs
Configuration
No configuration changes required. Works automatically with any authentication backend that provides Bearer tokens via the
Authorizationheader:user_oidcapp with Bearer token validationapp_apiflag)Documentation Impact
No documentation changes required. This fix makes Bearer token authentication work as users would naturally expect, aligning with OAuth 2.0 best practices.
Checklist
SecurityMiddleware