Skip to content

Fix consolidated api unauthorized check #40702

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

Closed
wants to merge 1 commit into from

Conversation

dvj1988
Copy link
Contributor

@dvj1988 dvj1988 commented May 20, 2025

Summary

  • return 401 Unauthorized when /consolidated-api/view is called with a defaultPageId but without authentication

Testing

  • mvn test (fails: mvn not found)

Warning

Tests have not run on the HEAD 923e8b4 yet


Tue, 20 May 2025 07:14:48 UTC

Summary by CodeRabbit

  • New Features
    • Added authentication check to the view mode endpoint. Unauthenticated users now receive a 401 Unauthorized response when accessing certain data.
  • Bug Fixes
    • Improved security by ensuring sensitive data is only accessible to authenticated users.

Copy link
Contributor

coderabbitai bot commented May 20, 2025

Walkthrough

The /view endpoint in ConsolidatedAPIController was updated to enforce authentication by using SessionUserService. If a defaultPageId is provided and the user is not authenticated, the endpoint now returns a 401 Unauthorized response. The constructor was updated to inject the new service dependency.

Changes

File(s) Change Summary
app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ConsolidatedAPIController.java Added authentication check to /view endpoint; injected SessionUserService via constructor; returns 401 if unauthenticated and defaultPageId is present.

Poem

In the land of endpoints, a guardian stands,
Checking each user with careful hands.
If you lack a token and seek the view,
A 401 awaits—no data for you!
But if you’re known, proceed with pride,
The page loads swift, with auth as your guide.
🚦🔐

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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.

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.

@dvj1988 dvj1988 requested a review from Copilot May 20, 2025 07:15
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

🔭 Outside diff range comments (1)
app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ConsolidatedAPIController.java (1)

89-176: 💡 Verification agent

🧩 Analysis chain

Verify test coverage for the authentication logic.

Ensure there are unit tests that verify:

  1. Requests with defaultPageId but no authentication return 401
  2. Requests with defaultPageId and valid authentication proceed normally
  3. Requests without defaultPageId proceed regardless of authentication status

The PR mentioned test failures but indicated they were due to environment issues rather than code problems.


🏁 Script executed:

#!/bin/bash
# Search for test files that might cover this controller
echo "Searching for test files related to ConsolidatedAPIController..."
fd -t f ".*Test.*" | grep -i consolidated | grep -i api | grep -i controller

Length of output: 217


Add unit tests for ConsolidatedAPIController authentication logic

I didn’t find any existing tests covering the new auth checks in
app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ConsolidatedAPIController.java (lines 89–176). Please add tests for:

  • Requests with a non-null defaultPageId but no authenticated user → HTTP 401
  • Requests with a non-null defaultPageId and an authenticated user → HTTP 200 + response body
  • Requests when defaultPageId is null (regardless of auth) → HTTP 200 + response body
🧹 Nitpick comments (2)
app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ConsolidatedAPIController.java (2)

116-121: Authentication enforcement implemented correctly.

This correctly implements the requirement to return 401 Unauthorized when a defaultPageId is provided but the user is not authenticated.

Consider adding an error message to the response for better API usability:

-new ResponseDTO<>(HttpStatus.UNAUTHORIZED, null);
+new ResponseDTO<>(HttpStatus.UNAUTHORIZED, null, "Authentication required to access page with ID " + defaultPageId);

113-170: Consider extracting methods to improve readability.

The code structure has become deeply nested, which makes it harder to follow. Consider extracting the authentication logic and response generation into separate private methods to improve readability.

For example:

private Mono<ResponseEntity<ResponseDTO<ConsolidatedAPIResponseDTO>>> handleUnauthenticatedRequest() {
    ResponseDTO<ConsolidatedAPIResponseDTO> responseDTO = new ResponseDTO<>(HttpStatus.UNAUTHORIZED, null);
    return Mono.just(new ResponseEntity<>(responseDTO, HttpStatus.UNAUTHORIZED));
}

private Mono<ResponseEntity<ResponseDTO<ConsolidatedAPIResponseDTO>>> fetchAndProcessPageData(...) {
    // Current implementation from lines 123-169
}

This would make the main method more concise:

return isAuthenticatedMono
    .flatMap(isAuthenticated -> {
        if (StringUtils.hasLength(defaultPageId) && !isAuthenticated) {
            return handleUnauthenticatedRequest();
        }
        return fetchAndProcessPageData(...);
    })
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 338357d and 923e8b4.

📒 Files selected for processing (1)
  • app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ConsolidatedAPIController.java (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • 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/controllers/ConsolidatedAPIController.java (3)

11-11: Appropriate import for authentication.

The SessionUserService import is correctly added to support the authentication check implementation.


39-39: Constructor dependency injection looks good.

Proper constructor injection for the SessionUserService dependency. This follows Spring's recommended dependency injection pattern.

Also applies to: 41-47


108-112: Well-implemented reactive authentication check.

The authentication check is implemented properly using reactive programming patterns. The getCurrentUser() followed by mapping to a boolean and providing a default value handles both authenticated and unauthenticated scenarios correctly.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds an authentication guard to the consolidated API endpoint to return a 401 when defaultPageId is provided but no user is authenticated.

  • Injects SessionUserService into ConsolidatedAPIController
  • Wraps the existing API call in a check that returns 401 for unauthenticated requests with a defaultPageId
  • Preserves original ETag computation and response logic under authenticated or no-defaultPageId conditions
Comments suppressed due to low confidence (1)

app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ConsolidatedAPIController.java:116

  • Add unit and integration tests covering the unauthorized scenario when a defaultPageId is provided but the user is unauthenticated, to verify a 401 status is returned.
if (StringUtils.hasLength(defaultPageId) && !isAuthenticated) {

return isAuthenticatedMono
.flatMap(
isAuthenticated -> {
if (StringUtils.hasLength(defaultPageId) && !isAuthenticated) {
Copy link
Preview

Copilot AI May 20, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider using StringUtils.hasText(...) instead of hasLength to avoid treating strings with only whitespace as a valid defaultPageId.

Suggested change
if (StringUtils.hasLength(defaultPageId) && !isAuthenticated) {
if (StringUtils.hasText(defaultPageId) && !isAuthenticated) {

Copilot uses AI. Check for mistakes.

Comment on lines +116 to +121
if (StringUtils.hasLength(defaultPageId) && !isAuthenticated) {
ResponseDTO<ConsolidatedAPIResponseDTO> responseDTO =
new ResponseDTO<>(HttpStatus.UNAUTHORIZED, null);
return Mono.just(
new ResponseEntity<>(responseDTO, HttpStatus.UNAUTHORIZED));
}
Copy link
Preview

Copilot AI May 20, 2025

Choose a reason for hiding this comment

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

[nitpick] The nested flatMap→map structure is quite deep; consider extracting the unauthorized check and main service call into helper methods to improve readability.

Suggested change
if (StringUtils.hasLength(defaultPageId) && !isAuthenticated) {
ResponseDTO<ConsolidatedAPIResponseDTO> responseDTO =
new ResponseDTO<>(HttpStatus.UNAUTHORIZED, null);
return Mono.just(
new ResponseEntity<>(responseDTO, HttpStatus.UNAUTHORIZED));
}
return handleUnauthorizedCheck(defaultPageId, isAuthenticated)
.switchIfEmpty(
handleConsolidatedServiceCall(
defaultPageId,
applicationId,
refType,
refName,
ifNoneMatch));

Copilot uses AI. Check for mistakes.

@dvj1988 dvj1988 requested a review from trishaanand May 20, 2025 07:17
})
Mono<Boolean> isAuthenticatedMono = sessionUserService
.getCurrentUser()
.map(user -> true)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is incorrect.You would always be returned a user (either the logged in user, or a user object whose email is anonymousUser

Copy link
Contributor Author

@dvj1988 dvj1988 May 20, 2025

Choose a reason for hiding this comment

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

@trishaanand In that case can we send a 401 for anonymous users? We should able to distinguish an anonymous user from a logged in user right?

Copy link

This PR has not seen activitiy for a while. It will be closed in 7 days unless further activity is detected.

@github-actions github-actions bot added the Stale label May 27, 2025
Copy link

github-actions bot commented Jun 3, 2025

This PR has been closed because of inactivity.

@github-actions github-actions bot closed this Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants