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

fix middleware bug blocking login #3

Merged
merged 1 commit into from
Jan 17, 2024
Merged

fix middleware bug blocking login #3

merged 1 commit into from
Jan 17, 2024

Conversation

eriktaubeneck
Copy link
Member

@eriktaubeneck eriktaubeneck commented Jan 17, 2024

fixing bug where middleware blocks /login when not logged in

Summary by CodeRabbit

  • New Features

    • Enhanced GitHub OAuth login to support different environments more flexibly.
  • Bug Fixes

    • Updated URL handling to prevent incorrect redirect issues.
    • Improved path validation to ensure only allowed paths are accessible without login.

Copy link

vercel bot commented Jan 17, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
draft ✅ Ready (Inspect) Visit Preview Jan 17, 2024 5:48pm

Copy link
Contributor

coderabbitai bot commented Jan 17, 2024

Walkthrough

The system has been refined to better handle different environments when setting URLs, particularly for the GitHub OAuth process. Additionally, the middleware now employs a regex-based check to determine if a user's requested path is allowed, redirecting to a 404 page when the path does not match the defined criteria.

Changes

File Path Change Summary
.../login/GitHubOAuthComponent.tsx Updated site URL handling for various environments and corrected redirectTo parameter in the GitHub login function.
server/middleware.ts Implemented regex-based check for allowed paths to control user access and redirection.

Poem

🐇 "In the realm of code, where pathways are spun,
🌐 URLs dance, and regex does run.
🚪 Where paths are gatekept, with logic so slick,
🎉 A rabbit hops through, with each clever trick."

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>.
    • Generate unit-tests for this file.
  • 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 tests 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 generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5a001df and 438680b.
Files selected for processing (2)
  • server/app/login/GitHubOAuthComponent.tsx (1 hunks)
  • server/middleware.ts (1 hunks)
Additional comments: 2
server/app/login/GitHubOAuthComponent.tsx (1)
  • 25-25: The redirectTo parameter has been updated to remove the trailing slash from the url variable. This change assumes that the url variable will always have a trailing slash, which is ensured by the previous code. However, it's important to verify that this assumption holds true in all environments where the code will run.
Verification successful

The verification process has confirmed that the url variable in GitHubOAuthComponent.tsx is explicitly checked to ensure it ends with a trailing slash. This is done by appending a slash if one is not present, which aligns with the reviewer's concern and ensures the correct behavior of the redirectTo parameter across all environments.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the `url` variable always ends with a trailing slash in all environments.
# This script is a placeholder for the actual verification steps that would be taken.
echo "Verification needed to ensure that the 'url' variable always ends with a trailing slash in all environments."

Length of output: 232



Script:

#!/bin/bash
# Check the assignment of the `url` variable to ensure it always ends with a trailing slash.
fd "GitHubOAuthComponent.tsx" --exec cat {} +

Length of output: 1521

server/middleware.ts (1)
  • 61-62: The regex pattern for allowed paths has been introduced to prevent redirection to /404 for certain paths. It's important to ensure that this regex pattern is comprehensive and doesn't inadvertently block access to other necessary paths.

Comment on lines +12 to +19
let url =
process?.env?.NEXT_PUBLIC_SITE_URL ?? // Set this to your site URL in production env.
process?.env?.NEXT_PUBLIC_VERCEL_URL ?? // Automatically set by Vercel.
"http://localhost:3000/";
// Make sure to include `https://` when not localhost.
url = url.includes("http") ? url : `https://${url}`;
// Make sure to include a trailing `/`.
url = url.charAt(url.length - 1) === "/" ? url : `${url}/`;
Copy link
Contributor

@coderabbitai coderabbitai bot Jan 17, 2024

Choose a reason for hiding this comment

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

The logic for constructing the url variable has been updated to handle different environments and ensure the URL format is correct. However, there is a potential issue with the check for the "http" substring that could lead to incorrect URL formats in some cases.

- url = url.includes("http") ? url : `https://${url}`;
+ url = url.startsWith("http://") || url.startsWith("https://") ? url : `https://${url}`;

This change ensures that the URL starts with either "http://" or "https://", which is more precise than just checking for the presence of "http".


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.

Suggested change
let url =
process?.env?.NEXT_PUBLIC_SITE_URL ?? // Set this to your site URL in production env.
process?.env?.NEXT_PUBLIC_VERCEL_URL ?? // Automatically set by Vercel.
"http://localhost:3000/";
// Make sure to include `https://` when not localhost.
url = url.includes("http") ? url : `https://${url}`;
// Make sure to include a trailing `/`.
url = url.charAt(url.length - 1) === "/" ? url : `${url}/`;
let url =
process?.env?.NEXT_PUBLIC_SITE_URL ?? // Set this to your site URL in production env.
process?.env?.NEXT_PUBLIC_VERCEL_URL ?? // Automatically set by Vercel.
"http://localhost:3000/";
// Make sure to include `https://` when not localhost.
url = url.startsWith("http://") || url.startsWith("https://") ? url : `https://${url}`;
// Make sure to include a trailing `/`.
url = url.charAt(url.length - 1) === "/" ? url : `${url}/`;

Copy link
Member Author

Choose a reason for hiding this comment

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

don't http:// and https:// both start with http?

Copy link
Contributor

Choose a reason for hiding this comment

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

@eriktaubeneck You're absolutely right; both http:// and https:// start with http. The original check for the substring "http" in the URL is indeed sufficient to cover both cases. Thank you for pointing that out.

@eriktaubeneck eriktaubeneck merged commit fe53e9e into main Jan 17, 2024
2 checks passed
@eriktaubeneck eriktaubeneck deleted the fix-login branch January 17, 2024 18:01
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.

1 participant