Skip to content

Conversation

ravishanigarapu
Copy link
Member

@ravishanigarapu ravishanigarapu commented Feb 21, 2025

πŸ“‹ Description

JIRA ID:

Cors issue


βœ… Type of Change

  • 🐞 Bug fix (non-breaking change which resolves an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • πŸ”₯ Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • πŸ›  Refactor (change that is neither a fix nor a new feature)
  • βš™οΈ Config change (configuration file or build script updates)
  • πŸ“š Documentation (updates to docs or readme)
  • πŸ§ͺ Tests (adding new or updating existing tests)
  • 🎨 UI/UX (changes that affect the user interface)
  • πŸš€ Performance (improves performance)
  • 🧹 Chore (miscellaneous changes that don't modify src or test files)

ℹ️ Additional Information

Please describe how the changes were tested, and include any relevant screenshots, logs, or other information that provides additional context.

Summary by CodeRabbit

  • New Features
    • Enhanced cookie management with improved support for cross-site functionality and clearer handling of secure connections for a more reliable user session experience.
    • Added support for the SameSite attribute in cookies for better security.
    • Simplified logic for setting the Secure flag in cookies.

Copy link
Contributor

coderabbitai bot commented Feb 21, 2025

Walkthrough

The addJwtTokenToCookie method in the CookieUtil class was updated to manually construct the Set-Cookie header. The changes include introducing a sameSite variable set to "None" for cross-site cookies and explicit handling of the secure flagβ€”setting it to true regardless of the request scheme. This update retains the original method signature while expanding functionality for clearer cookie attribute management.

Changes

File Change Summary
src/main/java/com/iemr/common/utils/CookieUtil.java Modified the addJwtTokenToCookie method to manually build the Set-Cookie header by including a SameSite attribute ("None") and adjusting the Secure flag to always be true.

Sequence Diagram(s)

sequenceDiagram
    participant R as Request
    participant U as CookieUtil
    participant S as Response

    R->>U: Call addJwtTokenToCookie(token, response, request)
    U->>U: Set SameSite value to "None"
    U->>U: Set Secure flag to true
    U->>S: Construct and add custom Set-Cookie header (Path, Max-Age, HttpOnly, SameSite)
    S->>R: Return response with cookie header
Loading

Suggested reviewers

  • helenKaryamsetty

Poem

I'm a little rabbit with a code-filled hop,
Watching cookies get a heart and a proper stop.
With SameSite in the mix and security clear as day,
My paws scurry over changes in a merry display.
Here's to neat headers and code that won't stray!
(_/) πŸ₯•


πŸ“œ Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between dd1dd21 and d2ae95f.

πŸ“’ Files selected for processing (1)
  • src/main/java/com/iemr/common/utils/CookieUtil.java (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/java/com/iemr/common/utils/CookieUtil.java
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Analyze (java)
  • GitHub Check: Build
  • GitHub Check: Package-test

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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 anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

Copy link
Contributor

@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: 2

🧹 Nitpick comments (1)
src/main/java/com/iemr/common/utils/CookieUtil.java (1)

27-67: Add input validation and make cookie settings configurable.

Consider these improvements for better maintainability and debugging:

  1. Add input validation
  2. Make cookie settings configurable
  3. Add logging for debugging CORS issues

Consider adding these enhancements:

+    @Value("${cookie.same-site:Lax}")
+    private String cookieSameSite;
+
+    @Value("${cookie.max-age:86400}")  // 24 hours in seconds
+    private int cookieMaxAge;
+
     public void addJwtTokenToCookie(String Jwttoken, HttpServletResponse response, HttpServletRequest request) {
+        if (Jwttoken == null || Jwttoken.trim().isEmpty()) {
+            throw new IllegalArgumentException("JWT token cannot be null or empty");
+        }
+
+        log.debug("Setting JWT cookie with SameSite={}, Secure={}", cookieSameSite, 
+            "https".equalsIgnoreCase(request.getScheme()));
πŸ“œ Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between e70fc25 and effa7c9.

πŸ“’ Files selected for processing (1)
  • src/main/java/com/iemr/common/utils/CookieUtil.java (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: style-check / checkstyle
  • GitHub Check: Analyze (java)
  • GitHub Check: Package-test
  • GitHub Check: Build
πŸ”‡ Additional comments (1)
src/main/java/com/iemr/common/utils/CookieUtil.java (1)

28-38: LGTM! Basic cookie configuration is secure and well-documented.

The implementation correctly sets essential cookie attributes with appropriate security considerations:

  • HttpOnly flag prevents JavaScript access
  • Reasonable expiration time of 24 hours
  • Root path configuration for application-wide access

Copy link
Contributor

@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

🧹 Nitpick comments (1)
src/main/java/com/iemr/common/utils/CookieUtil.java (1)

28-38: Consider extracting hardcoded values into constants.

For better maintainability and reusability, consider moving hardcoded values to class-level constants:

 public class CookieUtil {
+    private static final String JWT_COOKIE_NAME = "Jwttoken";
+    private static final String COOKIE_PATH = "/";
+    private static final int COOKIE_MAX_AGE_SECONDS = 60 * 60 * 24; // 1 day
 
     public void addJwtTokenToCookie(String Jwttoken, HttpServletResponse response, HttpServletRequest request) {
-        Cookie cookie = new Cookie("Jwttoken", Jwttoken);
+        Cookie cookie = new Cookie(JWT_COOKIE_NAME, Jwttoken);
         cookie.setHttpOnly(true);
-        cookie.setMaxAge(60 * 60 * 24);
-        cookie.setPath("/");
+        cookie.setMaxAge(COOKIE_MAX_AGE_SECONDS);
+        cookie.setPath(COOKIE_PATH);
πŸ“œ Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between effa7c9 and dd1dd21.

πŸ“’ Files selected for processing (1)
  • src/main/java/com/iemr/common/utils/CookieUtil.java (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
πŸ““ Common learnings
Learnt from: sandipkarmakar3
PR: PSMRI/Common-API#162
File: src/main/java/com/iemr/common/utils/CookieUtil.java:52-66
Timestamp: 2025-02-21T07:43:03.828Z
Learning: In the Common-API project's CookieUtil class, the current implementation of addJwtTokenToCookie using both response.addCookie() and manual Set-Cookie header has been tested and confirmed to work as required for their use case. URL encoding of the JWT token is not necessary.
Learnt from: sandipkarmakar3
PR: PSMRI/Common-API#162
File: src/main/java/com/iemr/common/utils/CookieUtil.java:40-47
Timestamp: 2025-02-21T07:42:36.497Z
Learning: In the Common-API project's CookieUtil class, JWT cookies are configured with SameSite=None to support cross-origin requests, which is required for the project's CORS functionality.
src/main/java/com/iemr/common/utils/CookieUtil.java (2)
Learnt from: sandipkarmakar3
PR: PSMRI/Common-API#162
File: src/main/java/com/iemr/common/utils/CookieUtil.java:52-66
Timestamp: 2025-02-21T07:43:03.828Z
Learning: In the Common-API project's CookieUtil class, the current implementation of addJwtTokenToCookie using both response.addCookie() and manual Set-Cookie header has been tested and confirmed to work as required for their use case. URL encoding of the JWT token is not necessary.
Learnt from: sandipkarmakar3
PR: PSMRI/Common-API#162
File: src/main/java/com/iemr/common/utils/CookieUtil.java:40-47
Timestamp: 2025-02-21T07:42:36.497Z
Learning: In the Common-API project's CookieUtil class, JWT cookies are configured with SameSite=None to support cross-origin requests, which is required for the project's CORS functionality.
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Build
  • GitHub Check: Analyze (java)
  • GitHub Check: Package-test
πŸ”‡ Additional comments (1)
src/main/java/com/iemr/common/utils/CookieUtil.java (1)

49-66: Implementation looks good!

The dual approach of using both response.addCookie() and manual Set-Cookie header has been tested and confirmed to work as required for your use case.

Note: Based on the learnings from previous discussions, this implementation is intentional and necessary for proper cookie handling across different browsers.

Copy link

@sandipkarmakar3 sandipkarmakar3 merged commit 7ae846e into PSMRI:develop Feb 21, 2025
7 checks passed
This was referenced Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants