-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix(cors): Allow Bearer token authentication for CORS requests #55878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
fix(cors): Allow Bearer token authentication for CORS requests #55878
Conversation
Bearer token authentication with OAuth 2.0/OIDC currently fails for app-specific APIs (Notes, Calendar, Contacts, etc.) with 401 errors, even though it works correctly for OCS APIs. Root cause: - Bearer token validation successfully authenticates the user - A session is created for the authenticated user - CORSMiddleware detects the logged-in session but no CSRF token - CORSMiddleware calls session->logout() to prevent CSRF attacks - The logout invalidates the session, breaking the API request This fix allows Bearer token authentication to bypass the CSRF check and logout logic in CORSMiddleware, as Bearer tokens are stateless and don't require CSRF protection. This aligns with how SecurityMiddleware already handles Bearer tokens for OCS routes (line 234-237). The fix adds a check for the Authorization: Bearer header before the CSRF and app_api checks, allowing Bearer-authenticated requests to proceed without triggering session logout. This enables proper Bearer token authentication for all Nextcloud APIs including app-specific APIs that use the #[CORS] attribute. Related: nextcloud#44365 Related: nextcloud/user_oidc#836 Signed-off-by: Chris Coutinho <[email protected]>
Update oauth-upstream-status.md to clarify patch requirements and document completed upstream work: **Clarifications:** - CORSMiddleware patch is for Nextcloud core server (not user_oidc app) - Root cause: CORS middleware logs out sessions without CSRF tokens - Solution: Allow Bearer tokens to bypass CORS/CSRF checks - Updated all references with actual PR number: nextcloud/server#55878 **Completed oidc app PRs (now documented):** - ✅ H2CK/oidc#586: User consent management (v1.11.0+) - ✅ H2CK/oidc#585: JWT tokens, introspection, scope validation (v1.10.0+) - ✅ H2CK/oidc#584: PKCE support (RFC 7636) (v1.10.0+) **Updated sections:** - "What Works Without Patches" - Added JWT, scopes, consent features - "Upstream PRs Status" - Added completed PRs table - "Monitoring Upstream Progress" - Focus on remaining work - Last updated date: 2025-11-02 All OAuth features except app-specific APIs now work out of the box with oidc app v1.10.0+. Only CORSMiddleware patch remains pending. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Update oauth-upstream-status.md to clarify patch requirements and document completed upstream work: **Clarifications:** - CORSMiddleware patch is for Nextcloud core server (not user_oidc app) - Root cause: CORS middleware logs out sessions without CSRF tokens - Solution: Allow Bearer tokens to bypass CORS/CSRF checks - Updated all references with actual PR number: nextcloud/server#55878 **Completed oidc app PRs (now documented):** - ✅ H2CK/oidc#586: User consent management (v1.11.0+) - ✅ H2CK/oidc#585: JWT tokens, introspection, scope validation (v1.10.0+) - ✅ H2CK/oidc#584: PKCE support (RFC 7636) (v1.10.0+) **Updated sections:** - "What Works Without Patches" - Added JWT, scopes, consent features - "Upstream PRs Status" - Added completed PRs table - "Monitoring Upstream Progress" - Focus on remaining work - Last updated date: 2025-11-02 All OAuth features except app-specific APIs now work out of the box with oidc app v1.10.0+. Only CORSMiddleware patch remains pending. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
|
Hello there, We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process. Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6 Thank you for contributing to Nextcloud and we hope to hear from you soon! (If you believe you should not receive this message, you can add yourself to the blocklist.) |
|
Hi @ArtificialOwl @Altahrim @sorbaugh - friendly ping. Could you possibly review this PR? |
|
@oleksandr-nc @nickvergessen @blizzz @juliusknorr any chance some of you can review this pr? |
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