Skip to content

Conversation

indraniBan
Copy link
Contributor

@indraniBan indraniBan commented Mar 12, 2025

πŸ“‹ Description

JIRA ID:

Please provide a summary of the change and the motivation behind it. Include relevant context and details.


βœ… 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 handling now automatically applies stricter same-site security settings in production environments for improved protection.
    • Introduced a new configuration property isProduction across various environment files to indicate the application's operational environment.

Copy link
Contributor

coderabbitai bot commented Mar 12, 2025

Walkthrough

The update introduces a new private Boolean field isProduction in the CookieUtil class, populated via configuration. The addJwtTokenToCookie method now conditionally sets the cookie's sameSite attribute to "Strict" when running in a production environment; otherwise, it remains "None". This change directly affects how cookies are managed based on the application's environment settings.

Changes

File(s) Change Summary
src/main/java/com/iemr/common/utils/CookieUtil.java Added a new private Boolean field isProduction with @Value("${isProduction}"). Modified addJwtTokenToCookie to conditionally set sameSite.
src/main/environment/common_ci.properties Added new property isProduction=false.
src/main/environment/common_dev.properties Added new property isProduction=false.
src/main/environment/common_example.properties Added new property isProduction=false.
src/main/environment/common_test.properties Added new property isProduction=false.
src/main/environment/common_uat.properties Added new property isProduction=false.

Sequence Diagram(s)

sequenceDiagram
    participant CU as CookieUtil
    participant Env as Environment Config
    participant Resp as HTTP Response

    CU->>Env: Read isProduction property
    Env-->>CU: Return isProduction value
    CU->>CU: Execute addJwtTokenToCookie
    alt isProduction = true
        CU->>Resp: Set cookie with sameSite="Strict"
    else isProduction = false
        CU->>Resp: Set cookie with sameSite="None"
    end
Loading

Possibly related PRs

  • cookieutil changeΒ #171: Directly related as it also modifies behavior in addJwtTokenToCookie, dealing with cookie management changes.
  • Cors issue resolvedΒ #162: Involves similar changes to the cookie handling mechanism, specifically focusing on the sameSite attribute logic.

Suggested reviewers

  • devikasuresh20
  • helenKaryamsetty

Poem

Hop, hop, here's a note from me,
A clever change as clear as can be.
In production, cookies stay so strict,
Else "None" is usedβ€”the logic picked!
With whiskers twitching and code so neat,
I celebrate changes in our coding beat. 🐰
Happy coding and carrot treats!

Tip

⚑πŸ§ͺ Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.

πŸ“œ 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 d2f4629 and d09ab95.

πŸ“’ Files selected for processing (5)
  • src/main/environment/common_ci.properties (1 hunks)
  • src/main/environment/common_dev.properties (1 hunks)
  • src/main/environment/common_example.properties (1 hunks)
  • src/main/environment/common_test.properties (1 hunks)
  • src/main/environment/common_uat.properties (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (5)
  • src/main/environment/common_dev.properties
  • src/main/environment/common_ci.properties
  • src/main/environment/common_test.properties
  • src/main/environment/common_uat.properties
  • src/main/environment/common_example.properties
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Package-test
  • GitHub Check: Build
  • GitHub Check: Analyze (java)

πŸͺ§ 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.
  • @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: 1

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

45-48: Add null check for the isProduction flag.

The code doesn't handle the case where the isProduction configuration property might be null. If the property isn't correctly defined in your configuration, this could lead to a NullPointerException.

String sameSite = "None"; // Allow cross-site cookies (can be 'Strict', 'Lax', or 'None')
- if(isProduction) {
+ if(Boolean.TRUE.equals(isProduction)) {
    sameSite= "Strict";
}

31-66: Consider logging the cookie security setting.

For debugging and audit purposes, it would be helpful to log which SameSite attribute is being applied, especially when the application is starting up or when the cookie is created.

Add a logger and log statement:

@Service
public class CookieUtil {
+    private static final Logger logger = LoggerFactory.getLogger(CookieUtil.class);
    
    @Value("${isProduction}")
    private Boolean isProduction;
    
    // Existing methods...
    
    public void addJwtTokenToCookie(String Jwttoken, HttpServletResponse response, HttpServletRequest request) {
        // Existing code...
        
        String sameSite = "None";
        if(Boolean.TRUE.equals(isProduction)) {
            sameSite= "Strict";
+            logger.info("Running in PRODUCTION mode: Setting cookie SameSite attribute to STRICT for enhanced security");
+        } else {
+            logger.warn("Running in NON-PRODUCTION mode: Setting cookie SameSite attribute to NONE");
        }
        
        // Remaining existing code...
    }
}

13-17: Consider environment-specific cookie configuration.

While the current implementation is a good start, consider extending this approach to make other cookie attributes configurable based on the environment. For example, cookie expiration time might be shorter in production than in development.

πŸ“œ Review details

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

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 2f8d8a7 and 62ba05a.

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

6-6: Good addition of the Value annotation import.

This import enables the injection of the configuration property needed for conditional SameSite attribute setting.


46-48: Good security enhancement for production environment.

Setting SameSite=Strict in production environments is a security best practice that helps prevent cross-site request forgery (CSRF) attacks. This conditional logic appropriately differentiates between production and non-production environments.

Consider adding a comment explaining the security implications of this change:

+ // In production, use SameSite=Strict for enhanced security against CSRF attacks
+ // In non-production environments, use SameSite=None to facilitate testing
if(isProduction) {
    sameSite= "Strict";
}

Comment on lines +16 to +17
@Value("${isProduction}")
private Boolean isProduction;
Copy link
Contributor

Choose a reason for hiding this comment

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

πŸ’‘ Verification agent

🧩 Analysis chain

Ensure configuration property is defined in all environments.

The @Value("${isProduction}") annotation injects the isProduction property from your configuration files. Make sure this property is properly defined in all environment configurations to avoid potential NullPointerExceptions.

Consider adding a default value in case the property is not defined:

- @Value("${isProduction}")
+ @Value("${isProduction:false}")

🏁 Script executed:

#!/bin/bash
# Verify if the isProduction property is defined in configuration files
echo "Checking for isProduction property in configuration files..."
fd -e yml -e properties -e yaml | xargs grep -l "isProduction" || echo "Warning: isProduction property not found in any configuration files"

Length of output: 338


Action Required: Ensure Default Value for "isProduction" Property

The verification indicates that the isProduction property is not defined in any of the configuration files. To prevent potential NullPointerException issues across environments, please update the injection to provide a default value. For example:

- @Value("${isProduction}")
+ @Value("${isProduction:false}")

Additionally, confirm that any deployment or configuration management setups align with this change.

πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@Value("${isProduction}")
private Boolean isProduction;
@Value("${isProduction:false}")
private Boolean isProduction;

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: 0

🧹 Nitpick comments (2)
src/main/environment/common_test.properties (1)

192-193: Configuration Property Addition: isProduction=false

The new property correctly indicates that the test environment is not a production environment. This will allow the application to conditionally apply stricter cookie settings (e.g., SameSite=Strict in production) in the CookieUtil class. Please ensure that corresponding configuration files for production (and other non-test environments) properly set this property to true where needed, and that the usage in the Java code is consistent with the environment logic.

src/main/environment/common_ci.properties (1)

164-165: Clarify the Environment Setting for isProduction.
The new property isProduction=false has been added. Please verify that this value correctly reflects the intended environment for your CI configuration. In scenarios where cookies need to use SameSite=Strict (typically a production scenario), ensure that the value is set accordingly (usually true in production). Also, confirm that this configuration is consistently applied across all relevant environment files.

πŸ“œ Review details

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

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 62ba05a and d2f4629.

πŸ“’ Files selected for processing (5)
  • src/main/environment/common_ci.properties (1 hunks)
  • src/main/environment/common_dev.properties (1 hunks)
  • src/main/environment/common_example.properties (1 hunks)
  • src/main/environment/common_test.properties (1 hunks)
  • src/main/environment/common_uat.properties (1 hunks)
βœ… Files skipped from review due to trivial changes (2)
  • src/main/environment/common_dev.properties
  • src/main/environment/common_uat.properties
πŸ”‡ Additional comments (1)
src/main/environment/common_example.properties (1)

190-190: New Configuration Property 'isProduction' Added

The new property isProduction=false is correctly added after the existing grievanceAllocationRetryConfiguration property. This enables the application to distinguish between production and non-production environments, which aligns with the PR objective of enforcing a "Strict" sameSite attribute on cookies in production. Please ensure that this flag is updated appropriately in the production configuration files (e.g., common_ci.properties, common_dev.properties, etc.) when deploying to production.

Copy link

Copy link
Member

@ravishanigarapu ravishanigarapu left a comment

Choose a reason for hiding this comment

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

looks fine

@ravishanigarapu ravishanigarapu merged commit 81fc4b0 into PSMRI:develop Mar 17, 2025
7 checks passed
springdoc.swagger-ui.enabled=false


isProduction=false
Copy link
Member

Choose a reason for hiding this comment

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

Hey this can't be false.
We should ideally be taking the variable from CI environment.
Use @@

@ravishanigarapu please check.

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.

3 participants