fix(backend): harden cross-origin resource sharing controls and prevent wildcard credentials - #756
Open
mertcano wants to merge 1 commit into
Open
fix(backend): harden cross-origin resource sharing controls and prevent wildcard credentials#756mertcano wants to merge 1 commit into
mertcano wants to merge 1 commit into
Conversation
…nt wildcard credentials ### Description This PR addresses a high-severity Cross-Origin Resource Sharing (CORS) misconfiguration within the `deso` workspace's backend API (`backend` repository)[cite: 42]. The previous middleware configuration accepted arbitrary, unvalidated origins for public routes, enabling an application-wide CORS bypass that placed credentialed browser requests at risk[cite: 42]. ### Key Changes * **Explicit Origin Validation (`backend/routes/server.go`):** - Overhauled the `isAllowedOrigin` function to enforce strict syntax validation via `url.Parse` and bounded domain-only matching[cite: 42]. - The middleware now explicitly rejects `*` wildcard origin responses when `Access-Control-Allow-Credentials` is set to `true`, adhering to browser security specifications[cite: 42]. * **Headers Boundary (`backend/routes/server.go`):** - Updated `AddHeaders` to emit `Access-Control-Allow-Origin` and credential headers *only* when the incoming request's `Origin` strictly matches the configured `allowedOrigins` allowlist[cite: 42]. - Added the `Vary: Origin` header to instruct intermediaries to cache responses separately per origin[cite: 42]. - Preserved multipart video/image upload handling without resorting to insecure wildcard header reflections[cite: 42]. * **CORS Wildcard Defaults (`backend/cmd/run.go`):** - Removed wildcard fallback defaults from the `access-control-allow-origins` configuration mapping[cite: 42]. ### Validation & Testing * **Regression Coverage (`backend/routes/server_cors_test.go`):** Added explicit unit test coverage confirming that configured, unconfigured, malformed, and wildcard origins are properly handled and rejected when necessary[cite: 42]. * **Reviewer Action Required:** The localized Go toolchain was unavailable during the offline audit (`go test ./routes` blocked)[cite: 42]. A maintainer must run the native test suite locally to verify the new CORS middleware logic against running backend environments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR addresses a high-severity Cross-Origin Resource Sharing (CORS) misconfiguration within the
desoworkspace's backend API (backendrepository)[cite: 42]. The previous middleware configuration accepted arbitrary, unvalidated origins for public routes, enabling an application-wide CORS bypass that placed credentialed browser requests at risk[cite: 42].Key Changes
backend/routes/server.go):isAllowedOriginfunction to enforce strict syntax validation viaurl.Parseand bounded domain-only matching[cite: 42].*wildcard origin responses whenAccess-Control-Allow-Credentialsis set totrue, adhering to browser security specifications[cite: 42].backend/routes/server.go):AddHeadersto emitAccess-Control-Allow-Originand credential headers only when the incoming request'sOriginstrictly matches the configuredallowedOriginsallowlist[cite: 42].Vary: Originheader to instruct intermediaries to cache responses separately per origin[cite: 42].backend/cmd/run.go):access-control-allow-originsconfiguration mapping[cite: 42].Validation & Testing
backend/routes/server_cors_test.go): Added explicit unit test coverage confirming that configured, unconfigured, malformed, and wildcard origins are properly handled and rejected when necessary[cite: 42].go test ./routesblocked)[cite: 42]. A maintainer must run the native test suite locally to verify the new CORS middleware logic against running backend environments.