-
Notifications
You must be signed in to change notification settings - Fork 36
Adding Cors handler to http request handler in netty server #756
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
edc4aca
Adding cors handler to netty server request handler
dpacheco-groupieticket dd792e8
Resolving conflicts
dpacheco-groupieticket 36ad4f1
Merge pull request #1 from Groupie-Ticket/server-netty-cors-handler
dpacheco-groupieticket af8d4d8
Addign cors headers unit tests
dpacheco-groupieticket 5b1302a
Merge pull request #2 from Groupie-Ticket/server-netty-cors-handler
dpacheco-groupieticket 6a1f11d
Adding cors headers directly to the job
dpacheco-groupieticket bcd42f0
Return null if the origin trait do not exist
dpacheco-groupieticket e7ee370
Merge pull request #3 from Groupie-Ticket/server-netty-cors-handler
dpacheco-groupieticket 78c8446
Fixing build
dpacheco-groupieticket f51f294
Merge pull request #4 from Groupie-Ticket/server-netty-cors-handler
dpacheco-groupieticket 2078eea
Merge branch 'main' into main
dpacheco-groupieticket 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
77 changes: 77 additions & 0 deletions
77
server/server-core/src/main/java/software/amazon/smithy/java/server/core/CorsHeaders.java
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,77 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package software.amazon.smithy.java.server.core; | ||
|
|
||
| import static software.amazon.smithy.java.core.schema.TraitKey.CORS_TRAIT; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public final class CorsHeaders { | ||
|
|
||
| private CorsHeaders() {} | ||
|
|
||
| private static final Map<String, List<String>> BASE_CORS_HEADERS = Map.of( | ||
| "Access-Control-Allow-Methods", | ||
| List.of("GET, POST, PUT, DELETE, OPTIONS"), | ||
| "Access-Control-Allow-Headers", | ||
| List.of("*,Access-Control-Allow-Headers,Access-Control-Allow-Methods,Access-Control-Allow-Origin,Amz-Sdk-Invocation-Id,Amz-Sdk-Request,Authorization,Content-Length,Content-Type,X-Amz-User-Agent,X-Amzn-Trace-Id"), | ||
| "Access-Control-Max-Age", | ||
| List.of("600")); | ||
|
|
||
| public static void addCorsHeaders(HttpJob job) { | ||
| if (!shouldAddCorsHeaders(job)) { | ||
| return; | ||
| } | ||
|
|
||
| String requestOrigin = job.request().headers().firstValue("origin"); | ||
| String configuredOrigin = getConfiguredOrigin(job); | ||
|
|
||
| if (!isOriginAllowed(configuredOrigin, requestOrigin)) { | ||
| return; | ||
| } | ||
|
|
||
| job.response().headers().putHeaders(BASE_CORS_HEADERS); | ||
| job.response().headers().putHeader("Access-Control-Allow-Origin", List.of(requestOrigin)); | ||
| } | ||
|
|
||
| private static boolean shouldAddCorsHeaders(HttpJob job) { | ||
| if (job.operation().getApiOperation().service() == null || | ||
| job.operation().getApiOperation().service().schema() == null | ||
| || | ||
| !job.operation().getApiOperation().service().schema().hasTrait(CORS_TRAIT)) { | ||
| return false; | ||
| } | ||
| return job.request().headers().hasHeader("origin"); | ||
| } | ||
|
|
||
| private static String getConfiguredOrigin(HttpJob job) { | ||
| var corsTrait = job.operation() | ||
| .getApiOperation() | ||
| .service() | ||
| .schema() | ||
| .getTrait(CORS_TRAIT); | ||
| if (corsTrait != null) { | ||
| return corsTrait.getOrigin(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static boolean isOriginAllowed(String configuredOrigin, String requestOrigin) { | ||
| if (configuredOrigin == null || requestOrigin == null) { | ||
| return false; | ||
| } | ||
|
|
||
| if (configuredOrigin.equals("*")) { | ||
| return true; | ||
| } | ||
|
|
||
| return Arrays.stream(configuredOrigin.split(",")) | ||
| .map(String::trim) | ||
| .anyMatch(origin -> origin.equalsIgnoreCase(requestOrigin)); | ||
| } | ||
| } | ||
126 changes: 126 additions & 0 deletions
126
...er/server-core/src/test/java/software/amazon/smithy/java/server/core/CorsHeadersTest.java
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,126 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package software.amazon.smithy.java.server.core; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Stream; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| import software.amazon.smithy.java.core.schema.ApiService; | ||
| import software.amazon.smithy.java.core.schema.Schema; | ||
| import software.amazon.smithy.java.http.api.HttpHeaders; | ||
| import software.amazon.smithy.java.http.api.ModifiableHttpHeaders; | ||
| import software.amazon.smithy.java.server.Operation; | ||
| import software.amazon.smithy.model.traits.CorsTrait; | ||
|
|
||
| public class CorsHeadersTest { | ||
|
|
||
| record TestCase( | ||
| String name, | ||
| String configuredOrigin, | ||
| String requestOrigin, | ||
| boolean shouldHaveHeaders, | ||
| String expectedAllowOrigin) {} | ||
|
|
||
| private static Stream<TestCase> corsTestCases() { | ||
| return Stream.of( | ||
| new TestCase( | ||
| "No request origin header", | ||
| "http://allowed-origin.com", | ||
| null, | ||
| false, | ||
| null), | ||
| new TestCase( | ||
| "Not allowed origin", | ||
| "http://allowed-origin.com", | ||
| "http://not-allowed-origin.com", | ||
| false, | ||
| null), | ||
| new TestCase( | ||
| "Multiple allowed origins", | ||
| "http://allowed-origin.com,http://other-allowed-origin.com", | ||
| "http://allowed-origin.com", | ||
| true, | ||
| "http://allowed-origin.com"), | ||
| new TestCase( | ||
| "Wildcard origin", | ||
| "*", | ||
| "http://any-origin.com", | ||
| true, | ||
| "http://any-origin.com"), | ||
| new TestCase( | ||
| "Exact match origin", | ||
| "http://allowed-origin.com", | ||
| "http://allowed-origin.com", | ||
| true, | ||
| "http://allowed-origin.com")); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "{0}") | ||
| @MethodSource("corsTestCases") | ||
| void testCorsHeaders(TestCase testCase) throws URISyntaxException { | ||
| // Create operation with configured CORS origin | ||
| Operation testOperation = Operation.of( | ||
| "TestOperation", | ||
| (input, context) -> new TestStructs.TestOutput(), | ||
| new TestStructs.TestApiOperation(new ApiService() { | ||
| @Override | ||
| public Schema schema() { | ||
| return Schema.structureBuilder( | ||
| CorsTrait.ID, | ||
| CorsTrait.builder().origin(testCase.configuredOrigin).build()).build(); | ||
| } | ||
| }), | ||
| new TestStructs.TestService()); | ||
|
|
||
| // Create request headers | ||
| Map<String, List<String>> headerMap = | ||
| testCase.requestOrigin != null ? Map.of("Origin", List.of(testCase.requestOrigin)) : Map.of(); | ||
| HttpHeaders requestHeaders = HttpHeaders.of(headerMap); | ||
|
|
||
| // Create request | ||
| HttpRequest request = new HttpRequest( | ||
| requestHeaders, | ||
| new URI("http://test.com"), | ||
| "GET"); | ||
|
|
||
| // Create response and job | ||
| HttpResponse response = new HttpResponse(new TestStructs.TestModifiableHttpHeaders()); | ||
| ServerProtocol protocol = new TestStructs.TestServerProtocol(List.of()); | ||
| HttpJob job = new HttpJob(testOperation, protocol, request, response); | ||
|
|
||
| // Apply CORS headers | ||
| CorsHeaders.addCorsHeaders(job); | ||
|
|
||
| // Verify headers | ||
| if (testCase.shouldHaveHeaders) { | ||
| assertContainsHeader(job.response().headers(), testCase.expectedAllowOrigin); | ||
| } else { | ||
| assertDoNotContainsHeader(job.response().headers()); | ||
| } | ||
| } | ||
|
|
||
| private void assertContainsHeader(ModifiableHttpHeaders responseHeaders, String allowedOrigin) { | ||
| assertTrue(responseHeaders.hasHeader("Access-Control-Allow-Methods")); | ||
| assertTrue(responseHeaders.hasHeader("Access-Control-Allow-Headers")); | ||
| assertTrue(responseHeaders.hasHeader("Access-Control-Max-Age")); | ||
| assertTrue(responseHeaders.hasHeader("Access-Control-Allow-Origin")); | ||
| assertTrue(responseHeaders.allValues("Access-Control-Allow-Origin").contains(allowedOrigin)); | ||
| } | ||
|
|
||
| private void assertDoNotContainsHeader(ModifiableHttpHeaders responseHeaders) { | ||
| assertFalse(responseHeaders.hasHeader("Access-Control-Allow-Methods")); | ||
| assertFalse(responseHeaders.hasHeader("Access-Control-Allow-Headers")); | ||
| assertFalse(responseHeaders.hasHeader("Access-Control-Max-Age")); | ||
| assertFalse(responseHeaders.hasHeader("Access-Control-Allow-Origin")); | ||
| } | ||
| } |
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.