Skip to content
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

Development: Add CORS allowed-origin-patterns #9405

Merged
merged 7 commits into from
Nov 18, 2024
Merged

Conversation

janthoXO
Copy link
Contributor

@janthoXO janthoXO commented Oct 1, 2024

Checklist

General

Server

  • Important: I implemented the changes with a very good performance and prevented too many (unnecessary) and too complex database calls.
  • I strictly followed the principle of data economy for all database calls.
  • I strictly followed the server coding and design guidelines.
  • I documented the Java code using JavaDoc style.

Description

Fixes CORS so that configured allowed-origin-patterns are also accepted.
Furthermore adds the VSCode plugin origins to the CORS in application.yml

Steps for Testing

only Config changes so code reviews are appreciated

Testserver States

Note

These badges show the state of the test servers.
Green = Currently available, Red = Currently locked
Click on the badges to get to the test servers.







Review Progress

Test Coverage

Screenshots

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced CORS filter configuration to support a broader range of allowed origins and patterns for API endpoints.
  • Bug Fixes

    • Improved logic for registering the CORS filter to ensure proper handling of origin patterns.

@coderabbitai pause

@janthoXO janthoXO self-assigned this Oct 1, 2024
@janthoXO janthoXO requested a review from a team as a code owner October 1, 2024 19:45
Copy link

coderabbitai bot commented Oct 1, 2024

Walkthrough

The changes in the pull request focus on updating the CORS filter configuration in the WebConfigurer.java file. The method corsFilter() now includes an additional condition that checks if either config.getAllowedOrigins() or config.getAllowedOriginPatterns() is not empty. This enhancement broadens the criteria for CORS filter registration while not altering method signatures, class declarations, or public entities.

Changes

File Change Summary
src/main/java/de/tum/cit/aet/artemis/core/config/WebConfigurer.java Modified the corsFilter() method to include an additional condition for CORS filter registration, checking both allowed origins and allowed origin patterns.

Possibly related PRs

  • Adjust cors config for VSCode Plugin #8784: The changes in this PR also modify the CORS filter configuration in WebConfigurer.java to include checks for both config.getAllowedOrigins() and config.getAllowedOriginPatterns(), directly relating to the updates made in the main PR.

Suggested reviewers

  • SimonEntholzer
  • florian-glombik
  • krusche

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added server Pull requests that update Java code. (Added Automatically!) config-change Pull requests that change the config in a way that they require a deployment via Ansible. core Pull requests that affect the corresponding module labels Oct 1, 2024
@janthoXO janthoXO changed the title let cors accept patterns Config CORS allowed-origin-patterns Oct 1, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between b7a816e and 1ee4259.

⛔ Files ignored due to path filters (1)
  • src/main/resources/config/application.yml is excluded by !**/*.yml
📒 Files selected for processing (1)
  • src/main/java/de/tum/cit/aet/artemis/core/config/WebConfigurer.java (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
src/main/java/de/tum/cit/aet/artemis/core/config/WebConfigurer.java (1)

Pattern src/main/java/**/*.java: naming:CamelCase; principles:{single_responsibility,small_methods,no_duplication}; db:{perf_queries,datetime_not_timestamp}; rest:{stateless,singleton,delegate_logic,http_only,minimal_dtos}; dtos:{java_records,no_entities,min_data,single_resp}; di:constructor_injection; kiss:simple_code; file_handling:os_indep_paths; practices:{least_access,avoid_transactions,code_reuse,static_member_ref,prefer_primitives}; sql:{param_annotation,uppercase,avoid_subqueries};java:avoid_star_imports

iyannsch
iyannsch previously approved these changes Oct 2, 2024
Copy link
Contributor

@iyannsch iyannsch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM, we should maybe test if the App etc are still able to fetch all their data

@janthoXO
Copy link
Contributor Author

janthoXO commented Oct 2, 2024

Code LGTM, we should maybe test if the App etc are still able to fetch all their data

yes that would be good. currently cors is disabled on production and testservers. so we need the cors adjustment on the test servers first

@janthoXO janthoXO changed the title Config CORS allowed-origin-patterns General: CORS allowed-origin-patterns Oct 2, 2024
@janthoXO janthoXO changed the title General: CORS allowed-origin-patterns General: Add CORS allowed-origin-patterns Oct 2, 2024
coderabbitai[bot]
coderabbitai bot previously approved these changes Oct 4, 2024
SimonEntholzer
SimonEntholzer previously approved these changes Oct 5, 2024
Copy link
Contributor

@SimonEntholzer SimonEntholzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code lgtm. How could I test this locally?

@janthoXO
Copy link
Contributor Author

janthoXO commented Oct 5, 2024

Code lgtm. How could I test this locally?

its hard to test. basically you would need a second website with a different URL that makes requests to the server. and then you would set the allowed-origin-patterns in the application.yml

you could open up just any website and then in the inspect console do a fetch(localhost:8080/api/..., {method: POST}) and see if it throws an error. and if you then add the website in the application.yml as a pattern it should not throw an error anymore
or a curl like this curl -v --request OPTIONS 'http://localhost:8080/api/...' -H 'Origin: http://some.origin.here' -H 'Access-Control-Request-Method: GET'

@krusche krusche changed the title General: Add CORS allowed-origin-patterns Development: Add CORS allowed-origin-patterns Oct 5, 2024
magaupp
magaupp previously approved these changes Oct 8, 2024
Copy link
Contributor

@magaupp magaupp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM

src/main/resources/config/application.yml Outdated Show resolved Hide resolved
coderabbitai[bot]
coderabbitai bot previously approved these changes Oct 8, 2024
Co-authored-by: Marcel Gaupp <m.gaupp@tum.de>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range comments (1)
src/main/java/de/tum/cit/aet/artemis/core/config/WebConfigurer.java (1)

Line range hint 121-126: LGTM! Consider extracting the condition for improved readability.

The changes effectively extend the CORS filter registration to include allowedOriginPatterns, which aligns with the PR objectives. The implementation adheres to the single responsibility principle and maintains simplicity. The use of CollectionUtils.isEmpty() is a good practice for null-safe empty checks.

To further improve code readability and adhere to the "small_methods" principle mentioned in the coding guidelines, consider extracting the condition into a separate method:

 @Bean
 public CorsFilter corsFilter() {
     UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
     CorsConfiguration config = jHipsterProperties.getCors();
-    if (!CollectionUtils.isEmpty(config.getAllowedOrigins()) || !CollectionUtils.isEmpty(config.getAllowedOriginPatterns())) {
+    if (shouldRegisterCorsFilter(config)) {
         log.debug("Registering CORS filter");
         source.registerCorsConfiguration("/api/**", config);
         source.registerCorsConfiguration("/management/**", config);
         source.registerCorsConfiguration("/v3/api-docs", config);
     }
     return new CorsFilter(source);
 }

+private boolean shouldRegisterCorsFilter(CorsConfiguration config) {
+    return !CollectionUtils.isEmpty(config.getAllowedOrigins()) || !CollectionUtils.isEmpty(config.getAllowedOriginPatterns());
+}

This refactoring improves readability while maintaining the functionality and adhering to the coding guidelines.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 6f99488 and a2e9e84.

📒 Files selected for processing (1)
  • src/main/java/de/tum/cit/aet/artemis/core/config/WebConfigurer.java (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
src/main/java/de/tum/cit/aet/artemis/core/config/WebConfigurer.java (1)

Pattern src/main/java/**/*.java: naming:CamelCase; principles:{single_responsibility,small_methods,no_duplication}; db:{perf_queries,datetime_not_timestamp}; rest:{stateless,singleton,delegate_logic,http_only,minimal_dtos}; dtos:{java_records,no_entities,min_data,single_resp}; di:constructor_injection; kiss:simple_code; file_handling:os_indep_paths; practices:{least_access,avoid_transactions,code_reuse,static_member_ref,prefer_primitives}; sql:{param_annotation,uppercase,avoid_subqueries};java:avoid_star_imports

🔇 Additional comments (1)
src/main/java/de/tum/cit/aet/artemis/core/config/WebConfigurer.java (1)

27-27: LGTM! Appropriate import added for CollectionUtils.

The addition of import org.springframework.util.CollectionUtils; is correct and necessary for using the CollectionUtils.isEmpty() method in the corsFilter() method. This aligns with the coding guidelines to use appropriate utility classes.

iyannsch
iyannsch previously approved these changes Oct 17, 2024
Copy link
Contributor

@iyannsch iyannsch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM as long as the origins are authentic

magaupp
magaupp previously approved these changes Oct 17, 2024
Copy link
Contributor

@magaupp magaupp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM

@github-actions github-actions bot removed the stale label Oct 17, 2024
Copy link

There hasn't been any activity on this pull request recently. Therefore, this pull request has been automatically marked as stale and will be closed if no further activity occurs within seven days. Thank you for your contributions.

@github-actions github-actions bot added the stale label Oct 24, 2024
@janthoXO janthoXO added ready for review too-long-open !!! This is an antipattern, we should aim for small PRs that are only open for a short time !!! and removed stale labels Oct 25, 2024
@janthoXO janthoXO dismissed stale reviews from magaupp and iyannsch via 7a09d4b October 25, 2024 19:23
@github-actions github-actions bot removed the config-change Pull requests that change the config in a way that they require a deployment via Ansible. label Oct 25, 2024
Copy link

github-actions bot commented Nov 2, 2024

There hasn't been any activity on this pull request recently. Therefore, this pull request has been automatically marked as stale and will be closed if no further activity occurs within seven days. Thank you for your contributions.

@github-actions github-actions bot removed the stale label Nov 7, 2024
Copy link
Contributor

@SimonEntholzer SimonEntholzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Contributor

@eceeeren eceeeren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM

@bassner bassner added this to the 7.7.2 milestone Nov 18, 2024
@bassner bassner merged commit 745c7f7 into develop Nov 18, 2024
23 of 26 checks passed
@bassner bassner deleted the chore/fix-cors-patterns branch November 18, 2024 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Pull requests that affect the corresponding module server Pull requests that update Java code. (Added Automatically!) too-long-open !!! This is an antipattern, we should aim for small PRs that are only open for a short time !!!
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

6 participants