-
Notifications
You must be signed in to change notification settings - Fork 24
fix(core): CORS #2787
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
Merged
Merged
fix(core): CORS #2787
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8e93e49
Add CORS tests for preflight requests and header validation.
pflynn-virtru 72289fe
Add bats test for service CORS validation.
pflynn-virtru 4155e52
Refactor server handler setup to streamline middleware application an…
pflynn-virtru bb3d7e5
Enable CORS in opentdf-dev configuration.
pflynn-virtru a5377ab
Update CORS bats tests to support TLS configurations
pflynn-virtru dc32bf3
Update CORS bats tests to verify HTTP/2 responses
pflynn-virtru 9a97ef3
Update CORS bats tests to refine HTTP response validation
pflynn-virtru 3bcf4a3
Update CORS tests for case-insensitive header validation and prefligh…
pflynn-virtru ff19055
rollback
pflynn-virtru d5ff7b4
explicit
pflynn-virtru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| #!/usr/bin/env bats | ||
|
|
||
| # Tests for validating CORS configuration allows Authorization header | ||
|
|
||
| # Set base URL based on TLS configuration | ||
| BASE_URL="http://localhost:8080" | ||
| CURL_OPTIONS="" | ||
|
|
||
| # Check if TLS is enabled via environment variable | ||
| if [[ "${TLS_ENABLED:-false}" == "true" ]]; then | ||
| BASE_URL="https://localhost:8080" | ||
| CURL_OPTIONS="-k" # Allow insecure connections for self-signed certs | ||
| fi | ||
|
|
||
| @test "CORS: preflight request includes Authorization in allowed headers" { | ||
| run curl -i -X OPTIONS $CURL_OPTIONS \ | ||
| -H "Origin: http://localhost:3000" \ | ||
| -H "Access-Control-Request-Method: POST" \ | ||
| -H "Access-Control-Request-Headers: authorization,content-type,connect-protocol-version" \ | ||
| ${BASE_URL}/policy.namespaces.NamespaceService/GetNamespace | ||
|
|
||
| echo "$output" | ||
|
|
||
| # Verify 200 OK response (HTTP/1.1 or HTTP/2) | ||
| [[ "$output" =~ "HTTP/2 200" ]] || [[ "$output" =~ "HTTP/1.1 200 OK" ]] | ||
|
|
||
| # Verify Access-Control-Allow-Headers includes Authorization | ||
| [[ "$output" =~ [Aa]ccess-[Cc]ontrol-[Aa]llow-[Hh]eaders:.*[Aa]uthorization ]] | ||
|
|
||
| # Verify Access-Control-Allow-Origin is set | ||
| [[ "$output" =~ [Aa]ccess-[Cc]ontrol-[Aa]llow-[Oo]rigin:\ http://localhost:3000 ]] | ||
|
|
||
| # Verify credentials are allowed | ||
| [[ "$output" =~ [Aa]ccess-[Cc]ontrol-[Aa]llow-[Cc]redentials:\ true ]] | ||
|
|
||
| # Verify max-age is set | ||
| [[ "$output" =~ [Aa]ccess-[Cc]ontrol-[Mm]ax-[Aa]ge:\ 3600 ]] | ||
| } | ||
|
|
||
| @test "CORS: preflight request with different headers" { | ||
| run curl -i -X OPTIONS $CURL_OPTIONS \ | ||
| -H "Origin: http://localhost:3000" \ | ||
| -H "Access-Control-Request-Method: POST" \ | ||
| -H "Access-Control-Request-Headers: authorization" \ | ||
| ${BASE_URL}/policy.namespaces.NamespaceService/GetNamespace | ||
|
|
||
| echo "$output" | ||
|
|
||
| # Verify 200 OK response (HTTP/1.1 or HTTP/2) | ||
| [[ "$output" =~ "HTTP/2 200" ]] || [[ "$output" =~ "HTTP/1.1 200 OK" ]] | ||
|
|
||
| # Verify Authorization is in allowed headers | ||
| [[ "$output" =~ [Aa]ccess-[Cc]ontrol-[Aa]llow-[Hh]eaders:.*[Aa]uthorization ]] | ||
| } | ||
|
|
||
| @test "CORS: actual request with Authorization header" { | ||
| run curl -i -X POST $CURL_OPTIONS \ | ||
| -H "Origin: http://localhost:3000" \ | ||
| -H "Authorization: Bearer test-token" \ | ||
| -H "Content-Type: application/json" \ | ||
| -H "Connect-Protocol-Version: 1" \ | ||
| ${BASE_URL}/policy.namespaces.NamespaceService/GetNamespace | ||
|
|
||
| echo "$output" | ||
|
|
||
| # Verify CORS headers are in response (status may be 401 due to invalid token, but CORS should work) | ||
| [[ "$output" =~ [Aa]ccess-[Cc]ontrol-[Aa]llow-[Oo]rigin:\ http://localhost:3000 ]] | ||
| [[ "$output" =~ [Aa]ccess-[Cc]ontrol-[Aa]llow-[Cc]redentials:\ true ]] | ||
| } | ||
|
|
||
| @test "CORS: wildcard origin configuration" { | ||
| run curl -i -X OPTIONS $CURL_OPTIONS \ | ||
| -H "Origin: http://example.com" \ | ||
| -H "Access-Control-Request-Method: POST" \ | ||
| -H "Access-Control-Request-Headers: authorization,content-type" \ | ||
| ${BASE_URL}/policy.namespaces.NamespaceService/GetNamespace | ||
|
|
||
| echo "$output" | ||
|
|
||
| # With wildcard ("*") in config, different origins should work | ||
| # Server should return 200 OK (HTTP/1.1 or HTTP/2) | ||
| [[ "$output" =~ "HTTP/2 200" ]] || [[ "$output" =~ "HTTP/1.1 200 OK" ]] | ||
|
|
||
| # Origin should be reflected back or wildcard | ||
| [[ "$output" =~ [Aa]ccess-[Cc]ontrol-[Aa]llow-[Oo]rigin: ]] | ||
| } | ||
|
|
||
| @test "CORS: verify Content-Type in allowed headers" { | ||
| run curl -i -X OPTIONS $CURL_OPTIONS \ | ||
| -H "Origin: http://localhost:3000" \ | ||
| -H "Access-Control-Request-Method: POST" \ | ||
| -H "Access-Control-Request-Headers: content-type" \ | ||
| ${BASE_URL}/policy.namespaces.NamespaceService/GetNamespace | ||
|
|
||
| echo "$output" | ||
|
|
||
| # Verify Content-Type is in allowed headers | ||
| [[ "$output" =~ [Aa]ccess-[Cc]ontrol-[Aa]llow-[Hh]eaders:.*[Cc]ontent-[Tt]ype ]] | ||
| } | ||
|
|
||
| @test "CORS: verify Connect-Protocol-Version in allowed headers" { | ||
| run curl -i -X OPTIONS $CURL_OPTIONS \ | ||
| -H "Origin: http://localhost:3000" \ | ||
| -H "Access-Control-Request-Method: POST" \ | ||
| -H "Access-Control-Request-Headers: connect-protocol-version" \ | ||
| ${BASE_URL}/policy.namespaces.NamespaceService/GetNamespace | ||
|
|
||
| echo "$output" | ||
|
|
||
| # Verify Connect-Protocol-Version is in allowed headers | ||
| [[ "$output" =~ [Aa]ccess-[Cc]ontrol-[Aa]llow-[Hh]eaders:.*[Cc]onnect-[Pp]rotocol-[Vv]ersion ]] | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.