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

chore: Custom cooke resolver and logout on session invalidation #39695

Open
wants to merge 3 commits into
base: release
Choose a base branch
from

Conversation

nidhi-nair
Copy link
Contributor

@nidhi-nair nidhi-nair commented Mar 12, 2025

Description

Tip

Add a TL;DR when the description is longer than 500 words or extremely technical (helps the content, marketing, and DevRel team).

Please also include relevant motivation and context. List any dependencies that are required for this change. Add links to Notion, Figma or any other documents that might be relevant to the PR.

Fixes #Issue Number
or
Fixes Issue URL

Warning

If no issue exists, please create an issue first, and check with the maintainers if the issue is valid.

Automation

/ok-to-test tags="@tag.Sanity"

🔍 Cypress test results

Tip

🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/13813722166
Commit: 888e9da
Cypress dashboard.
Tags: @tag.Sanity
Spec:


Wed, 12 Mar 2025 16:07:33 UTC

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

  • New Features
    • Introduced a custom session cookie management system, allowing for enhanced handling of SameSite attributes based on organization settings.
    • Added specific error handling for session invalidation, ensuring a clear unauthorized response when a session issue occurs.

@nidhi-nair nidhi-nair requested a review from sharat87 March 12, 2025 12:55
Copy link
Contributor

coderabbitai bot commented Mar 12, 2025

Walkthrough

This pull request introduces a custom session cookie resolver and updates session handling. A new class, CustomCookieWebSessionIdResolver, extends the default resolver to configure the session cookie with a "Lax" SameSite attribute. The SecurityConfig file is updated to return an instance of this custom resolver instead of the default. Additionally, GlobalExceptionHandler now includes a method to handle IllegalStateException specifically when session invalidation occurs, returning an HTTP 401 Unauthorized response.

Changes

File(s) Change Summary
app/.../CustomCookieWebSessionIdResolver.java, app/.../ce/CustomCookieWebSessionIdResolverCE.java Introduces CustomCookieWebSessionIdResolver and CustomCookieWebSessionIdResolverCE, customizing cookie attributes for session management.
app/.../SecurityConfig.java Updates the webSessionIdResolver method to return an instance of CustomCookieWebSessionIdResolver.
app/.../GlobalExceptionHandler.java Adds a new method handleSessionInvalidation for handling IllegalStateException related to session invalidation, returning an HTTP 401 Unauthorized response.

Suggested labels

Enhancement, Bug

Suggested reviewers

  • sharat87
  • mohanarpit
  • abhvsn

Poem

In the code where sessions now reside,
Cookies take a custom ride.
With secure flags and paths laid true,
Errors catch with a graceful cue.
Cheers to progress—onward we stride!
🎉


🪧 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 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.

@nidhi-nair nidhi-nair added the ok-to-test Required label for CI label Mar 12, 2025
@github-actions github-actions bot added the skip-changelog Adding this label to a PR prevents it from being listed in the changelog label Mar 12, 2025
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 (2)
app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/ce/CustomCookieWebSessionIdResolverCE.java (2)

17-17: Consider using an enum for SameSite values

Using string constants like this is fine, but for better type safety and IDE support, consider using an enum to represent SameSite values (None, Lax, Strict).


10-14: Document where the cross-site embedding configuration comes from

The class comment mentions setting SameSite "based on organization configuration" but the implementation doesn't show where this configuration comes from or how it's used.

Add more specific documentation about how the organization configuration will be used, or consider adding a method parameter to make this configurable:

/**
 * This class is a custom implementation of the CookieWebSessionIdResolver class.
 * It allows us to set the SameSite attribute of the session cookie.
 * Currently set to "Lax" by default, but future implementations will
 * allow customization based on organization-specific embedding requirements.
 */
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 6ddb534 and 888e9da.

📒 Files selected for processing (2)
  • app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/CustomCookieWebSessionIdResolver.java (1 hunks)
  • app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/ce/CustomCookieWebSessionIdResolverCE.java (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/CustomCookieWebSessionIdResolver.java
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: perform-test / rts-build / build
  • GitHub Check: perform-test / client-build / client-build
  • GitHub Check: perform-test / server-build / server-unit-tests
  • GitHub Check: server-unit-tests / server-unit-tests
  • GitHub Check: server-spotless / spotless-check
🔇 Additional comments (3)
app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/ce/CustomCookieWebSessionIdResolverCE.java (3)

1-15: LGTM - Documentation clearly explains the purpose of this custom resolver

The class documentation accurately describes the purpose of extending the default cookie resolver to support customized SameSite attributes for cross-site embedding scenarios.


19-26: LGTM - Good cookie configuration with helpful comments

Setting a 30-day max age and root path are sensible defaults. The comments explaining the rationale for preventing cookie expiration on browser close are helpful.


28-32: LGTM - Proper method override pattern

Correctly calls the custom initializer method before delegating to the parent implementation.

Comment on lines +34 to +37
protected void addCookieInitializers() {
// Add the appropriate SameSite attribute based on the exchange attribute
addCookieInitializer((builder) -> builder.sameSite(LAX).secure(true));
}
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

❓ Verification inconclusive

Inconsistency between implementation and documentation

The method comment suggests the SameSite attribute should be based on an exchange attribute, but the implementation hard-codes it to "Lax". Either:

  1. Update the implementation to actually use the exchange attribute
  2. Update the comment to reflect the current implementation

Additionally, consider adding a comment explaining why "Lax" was chosen over "Strict" or "None" as it has security implications.

protected void addCookieInitializers() {
-    // Add the appropriate SameSite attribute based on the exchange attribute
+    // Set SameSite to "Lax" which provides a balance between security and functionality
+    // "Lax" allows the cookie to be sent when users navigate to the site from external links
     addCookieInitializer((builder) -> builder.sameSite(LAX).secure(true));
}

Action: Update the method documentation to reflect the deliberate use of a fixed SameSite value.

  • The implementation is hard-coded to use "Lax" rather than basing the attribute on an exchange value like the comment suggests.
  • Update the comment to clearly state that using "Lax" is an intentional choice to balance security (e.g., CSRF protections) with cookie availability during top-level navigations.
  • Optionally, add clarification on why "Lax" was preferred over "Strict" or "None" given your security considerations.
protected void addCookieInitializers() {
-    // Add the appropriate SameSite attribute based on the exchange attribute
+    // Set SameSite to "Lax" which strikes a balance between security and cross-origin functionality.
+    // "Lax" allows the cookie to be sent on top-level navigations while providing some CSRF protection.
    addCookieInitializer((builder) -> builder.sameSite(LAX).secure(true));
}
📝 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
protected void addCookieInitializers() {
// Add the appropriate SameSite attribute based on the exchange attribute
addCookieInitializer((builder) -> builder.sameSite(LAX).secure(true));
}
protected void addCookieInitializers() {
// Set SameSite to "Lax" which strikes a balance between security and cross-origin functionality.
// "Lax" allows the cookie to be sent on top-level navigations while providing some CSRF protection.
addCookieInitializer((builder) -> builder.sameSite(LAX).secure(true));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ok-to-test Required label for CI skip-changelog Adding this label to a PR prevents it from being listed in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant