-
Notifications
You must be signed in to change notification settings - Fork 534
Corsfilter fix #12151
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
Draft
beepsoft
wants to merge
9
commits into
IQSS:develop
Choose a base branch
from
beepsoft:cors-filter-fix
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−4
Draft
Corsfilter fix #12151
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e7e9f18
Fix CorsFilter invocation inconsistency
beepsoft ccfe364
Fix imports
beepsoft 860bd51
Add CorsIT for minimal CORS header checking
beepsoft c7c3c68
Make it ParameterizedTest, add more endpoints
beepsoft 140a799
Fix code style
beepsoft 81a4e32
Add ERROR and ASYNC dispatcher type and more docs
beepsoft 36c336e
Use idiomatic RestAssured assertions
beepsoft 3b117c1
Merge branch 'IQSS:develop' into cors-filter-fix
beepsoft 2a85be7
Add CorsIT to integration-tests.txt
beepsoft 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package edu.harvard.iq.dataverse.api; | ||
|
|
||
| import io.restassured.RestAssured; | ||
| import io.restassured.response.Response; | ||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| import static io.restassured.RestAssured.given; | ||
| import static org.hamcrest.Matchers.anyOf; | ||
| import static org.hamcrest.Matchers.blankOrNullString; | ||
| import static org.hamcrest.Matchers.is; | ||
| import static org.hamcrest.Matchers.not; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * Integration tests for CORS headers on API endpoints. These tests verify that the expected CORS | ||
| * headers are present and contain the correct values for preflight OPTIONS requests to key | ||
| * API endpoints. | ||
| * | ||
| * For this to work CORS has to be enabled. Eg. in docker-compose-dev.yml add | ||
| * DATAVERSE_CORS_ORIGIN: "*" | ||
| * env to `dev_dataverse`. | ||
| */ | ||
| public class CorsIT { | ||
| private static final String ORIGIN_NULL = "null"; | ||
|
|
||
| @BeforeAll | ||
| public static void setUp() { | ||
| RestAssured.baseURI = UtilIT.getRestAssuredBaseUri(); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "CORS preflight headers on {0}") | ||
| @ValueSource(strings = { | ||
| "/api/dataverses/root/datasets", | ||
| "/api/v1/dataverses/root/datasets", | ||
| "/page_doesnt_exist", | ||
| "/dvn/api/data-deposit/v1.1/swordv2/collection/dataverse/root" | ||
| }) | ||
| public void testPreflightOptionsCorsHeaders(String path) { | ||
| assertPreflightCorsHeaders(path); | ||
| } | ||
|
|
||
| private void assertPreflightCorsHeaders(String path) { | ||
| Response response = given() | ||
| .header("Accept", "*/*") | ||
| .header("Accept-Language", "en-US,en;q=0.9,es;q=0.8,hu;q=0.7") | ||
| .header("Access-Control-Request-Headers", "content-type,x-dataverse-key") | ||
| .header("Access-Control-Request-Method", "POST") | ||
| .header("Origin", ORIGIN_NULL) | ||
| .header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36") | ||
| .when() | ||
| .options(path) | ||
| .then() | ||
| .log().ifValidationFails() | ||
| .statusCode(anyOf(is(200), is(204))) | ||
| .header("Access-Control-Allow-Methods", not(blankOrNullString())) | ||
| .header("Access-Control-Allow-Headers", not(blankOrNullString())) | ||
| .header("Access-Control-Expose-Headers", not(blankOrNullString())) | ||
| .extract() | ||
| .response(); | ||
|
|
||
| assertHeaderSetEquals("Access-Control-Allow-Methods", getExpectedCorsMethods(), response); | ||
| assertHeaderSetEquals("Access-Control-Allow-Headers", getExpectedCorsAllowHeaders(), response); | ||
| assertHeaderSetEquals("Access-Control-Expose-Headers", getExpectedCorsExposeHeaders(), response); | ||
| } | ||
|
|
||
| private static List<String> getExpectedCorsMethods() { | ||
| return List.of("GET", "POST", "OPTIONS", "PUT", "DELETE"); | ||
| } | ||
|
|
||
| private static List<String> getExpectedCorsAllowHeaders() { | ||
| return List.of("Accept", "Content-Type", "X-Dataverse-key", "Range"); | ||
| } | ||
|
|
||
| private static List<String> getExpectedCorsExposeHeaders() { | ||
| return List.of("Accept-Ranges", "Content-Range", "Content-Encoding"); | ||
| } | ||
|
|
||
| private static void assertHeaderSetEquals(String headerName, List<String> expectedTokens, Response response) { | ||
| String headerValue = response.getHeader(headerName); | ||
| assertTrue(headerValue != null && !headerValue.isBlank(), "Missing header: " + headerName); | ||
| Set<String> actual = normalizeTokens(headerValue); | ||
| Set<String> expected = expectedTokens.stream() | ||
| .map(CorsIT::normalizeToken) | ||
| .collect(Collectors.toCollection(HashSet::new)); | ||
| assertEquals(expected, actual, "Unexpected value for header: " + headerName); | ||
| } | ||
|
|
||
| private static Set<String> normalizeTokens(String headerValue) { | ||
| return Arrays.stream(headerValue.split(",")) | ||
| .map(CorsIT::normalizeToken) | ||
| .filter(token -> !token.isEmpty()) | ||
| .collect(Collectors.toCollection(HashSet::new)); | ||
| } | ||
|
|
||
| private static String normalizeToken(String value) { | ||
| return value == null ? "" : value.trim().toLowerCase(Locale.ROOT); | ||
| } | ||
| } |
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 | ||||
|---|---|---|---|---|---|---|
| @@ -1 +1 @@ | ||||||
| DataversesIT,DatasetsIT,SwordIT,AdminIT,BuiltinUsersIT,UsersIT,UtilIT,ConfirmEmailIT,FileMetadataIT,FilesIT,SearchIT,InReviewWorkflowIT,HarvestingServerIT,HarvestingClientsIT,MoveIT,MakeDataCountApiIT,FileTypeDetectionIT,EditDDIIT,ExternalToolsIT,AccessIT,DuplicateFilesIT,DownloadFilesIT,LinkIT,DeleteUsersIT,DeactivateUsersIT,AuxiliaryFilesIT,InvalidCharactersIT,LicensesIT,NotificationsIT,BagIT,MetadataBlocksIT,NetcdfIT,SignpostingIT,FitsIT,LogoutIT,DataRetrieverApiIT,ProvIT,S3AccessIT,OpenApiIT,InfoIT,DatasetFieldsIT,SavedSearchIT,DatasetTypesIT,DataverseFeaturedItemsIT,SendFeedbackApiIT,CustomizationIT,JsonLDExportIT,WorkflowsIT,LDNInboxIT,LocalContextsIT | ||||||
| DataversesIT,DatasetsIT,SwordIT,AdminIT,BuiltinUsersIT,UsersIT,UtilIT,ConfirmEmailIT,FileMetadataIT,FilesIT,SearchIT,InReviewWorkflowIT,HarvestingServerIT,HarvestingClientsIT,MoveIT,MakeDataCountApiIT,FileTypeDetectionIT,EditDDIIT,ExternalToolsIT,AccessIT,DuplicateFilesIT,DownloadFilesIT,LinkIT,DeleteUsersIT,DeactivateUsersIT,AuxiliaryFilesIT,InvalidCharactersIT,LicensesIT,NotificationsIT,BagIT,MetadataBlocksIT,NetcdfIT,SignpostingIT,FitsIT,LogoutIT,DataRetrieverApiIT,ProvIT,S3AccessIT,OpenApiIT,InfoIT,DatasetFieldsIT,SavedSearchIT,DatasetTypesIT,DataverseFeaturedItemsIT,SendFeedbackApiIT,CustomizationIT,JsonLDExportIT,WorkflowsIT,LDNInboxIT,LocalContextsIT, CorsIT | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
No spaces allowed. |
||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just going to put this comment at the top but can you please add a release not snippet that describes the bug being fixed? Thanks! I think you know the drill, but just in case: https://guides.dataverse.org/en/6.9/developers/version-control.html#writing-a-release-note-snippet