Skip to content

Improve return-url implementation in Boilerplate (#9854)#9856

Merged
msynk merged 1 commit intobitfoundation:developfrom
yasmoradi:9854
Feb 11, 2025
Merged

Improve return-url implementation in Boilerplate (#9854)#9856
msynk merged 1 commit intobitfoundation:developfrom
yasmoradi:9854

Conversation

@yasmoradi
Copy link
Member

@yasmoradi yasmoradi commented Feb 10, 2025

closes #9854

Summary by CodeRabbit

  • Bug Fixes

    • Improved URL navigation in authentication workflows by ensuring all return URL values are safely encoded, preventing issues with special characters in sign-in, sign-up, and email confirmation links.
  • Tests

    • Updated test validations to confirm the enhanced handling of URL parameters across identity functions.

@yasmoradi yasmoradi requested a review from msynk February 10, 2025 19:29
@coderabbitai
Copy link

coderabbitai bot commented Feb 10, 2025

Walkthrough

The pull request updates multiple components across the client, server, and test projects to ensure that return URL parameters are safely encoded. The changes uniformly apply Uri.EscapeDataString to URL constructions in sign-up, sign-in, email confirmation, and related test assertions. This enhancement prevents issues with special characters without altering the overall control or navigation flow.

Changes

File(s) Change Summary
src/.../Client.Core/Components/Layout/IdentityHeader.razor
src/.../Pages/Identity/SignIn/SignInPanel.razor
src/.../Pages/Identity/SignUp/SignUpPage.razor
src/.../Pages/NotAuthorizedPage.razor.cs
Updated URL constructions by wrapping return URL parameters with Uri.EscapeDataString, including handling of potential null values.
src/.../Server.Api/Controllers/Identity/IdentityController.EmailConfirmation.cs
src/.../Identity/IdentityController.cs
Modified URI generation to encode the returnUrl parameter; applied double escaping where needed for safe URL parsing.
src/.../Tests/PageTests/PageModels/Identity/SignInPage.cs Adjusted test URL assertions to incorporate Uri.EscapeDataString for robust URL encoding.

Assessment against linked issues

Objective (from linked issue) Addressed Explanation
Boilerplate project template must encode return urls (#9854)

Poem

I'm a little bunny, code in my sight,
Hopping through URLs both day and night.
Escaping each string with a playful delight,
I make sure every link is encoded just right.
Cheers from the burrow—happy coding in flight!


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.

@yasmoradi yasmoradi marked this pull request as ready for review February 10, 2025 19:32
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

🧹 Nitpick comments (2)
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Controllers/Identity/IdentityController.cs (2)

415-415: Double escaping enhances URL parameter security.

The double escaping of the return URL parameter is a good security practice that prevents URL injection attacks and ensures special characters are properly handled.

Consider adding URL validation to prevent open redirect vulnerabilities:

 if (string.IsNullOrEmpty(returnUrl) is false)
 {
+    if (!Uri.TryCreate(returnUrl, UriKind.RelativeOrAbsolute, out var uri) || 
+        (uri.IsAbsoluteUri && !uri.IsWellFormedOriginalString()))
+    {
+        throw new BadRequestException(Localizer[nameof(AppStrings.InvalidReturnUrl)]);
+    }
     qs += $"&return-url={Uri.EscapeDataString(Uri.EscapeDataString(returnUrl))}";
 }

411-419: Enhance URL construction safety and consistency.

While the return URL is properly escaped, consider improving the overall URL construction:

Apply this diff to use UriBuilder for safer URL construction and ensure consistent parameter escaping:

-var qs = $"userName={Uri.EscapeDataString(user.UserName!)}";
-
-if (string.IsNullOrEmpty(returnUrl) is false)
-{
-    qs += $"&return-url={Uri.EscapeDataString(Uri.EscapeDataString(returnUrl))}";
-}
-
-var url = $"{Urls.SignInPage}?otp={Uri.EscapeDataString(token)}&{qs}&culture={CultureInfo.CurrentUICulture.Name}";
+var builder = new UriBuilder(Urls.SignInPage);
+var query = HttpUtility.ParseQueryString(string.Empty);
+query["userName"] = user.UserName;
+query["otp"] = token;
+query["culture"] = CultureInfo.CurrentUICulture.Name;
+
+if (string.IsNullOrEmpty(returnUrl) is false)
+{
+    if (!Uri.TryCreate(returnUrl, UriKind.RelativeOrAbsolute, out var uri) || 
+        (uri.IsAbsoluteUri && !uri.IsWellFormedOriginalString()))
+    {
+        throw new BadRequestException(Localizer[nameof(AppStrings.InvalidReturnUrl)]);
+    }
+    query["return-url"] = Uri.EscapeDataString(returnUrl);
+}
+
+builder.Query = query.ToString();
+var url = builder.Uri.ToString();
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 679f177 and 5074d5b.

📒 Files selected for processing (7)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/IdentityHeader.razor (1 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Pages/Identity/SignIn/SignInPanel.razor (1 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Pages/Identity/SignUp/SignUpPage.razor (1 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Pages/NotAuthorizedPage.razor.cs (2 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Controllers/Identity/IdentityController.EmailConfirmation.cs (1 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Controllers/Identity/IdentityController.cs (1 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Tests/PageTests/PageModels/Identity/SignInPage.cs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build and test
🔇 Additional comments (7)
src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Pages/NotAuthorizedPage.razor.cs (1)

47-47: LGTM! Proper URL encoding implementation.

The changes correctly implement URL encoding for the return URL parameter using Uri.EscapeDataString, preventing potential issues with special characters in the URL.

Also applies to: 61-61

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/IdentityHeader.razor (1)

7-7: LGTM! Consistent URL encoding in navigation links.

The changes properly encode the return URL parameter in both sign-up and sign-in button links using Uri.EscapeDataString, maintaining consistency with the application's URL encoding strategy.

Also applies to: 10-10

src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Controllers/Identity/IdentityController.EmailConfirmation.cs (1)

81-81: LGTM! Comprehensive URL parameter encoding.

The changes properly encode all sensitive parameters (email, token, and return URL) in the URI construction using Uri.EscapeDataString, ensuring robust handling of special characters in the confirmation link.

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Pages/Identity/SignIn/SignInPanel.razor (1)

81-81: LGTM! Proper URL encoding with null handling.

The change correctly implements URL encoding for the return URL parameter using Uri.EscapeDataString and handles null values appropriately with the null coalescing operator (?? "").

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Pages/Identity/SignUp/SignUpPage.razor (2)

79-79: LGTM! URL encoding properly implemented for sign-in link.

The change correctly uses Uri.EscapeDataString to encode the return URL and handles null values appropriately.


81-81: LGTM! URL encoding properly implemented for confirmation link.

The change correctly uses Uri.EscapeDataString to encode all URL parameters (email, phoneNumber, return-url) and handles null values appropriately.

src/Templates/Boilerplate/Bit.Boilerplate/src/Tests/PageTests/PageModels/Identity/SignInPage.cs (1)

124-124: LGTM! Test assertion updated to match URL encoding implementation.

The test assertion correctly uses Uri.EscapeDataString to encode the return URL, ensuring it matches the implementation behavior.

@msynk msynk changed the title Improve Boilerplate return url code (#9854) Improve return-url implementation in Boilerplate (#9854) Feb 11, 2025
@msynk msynk merged commit 4fcf1a9 into bitfoundation:develop Feb 11, 2025
3 checks passed
@yasmoradi yasmoradi deleted the 9854 branch February 11, 2025 11:47
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.

Boilerplate project template must encode return urls

2 participants