Skip to content

[WEB-4943]: add url has allowed host or scheme for validating valid redirections #7809

Merged
sriramveeraghanta merged 3 commits intopreviewfrom
fix-auth-valid-redirections
Sep 16, 2025
Merged

[WEB-4943]: add url has allowed host or scheme for validating valid redirections #7809
sriramveeraghanta merged 3 commits intopreviewfrom
fix-auth-valid-redirections

Conversation

@pablohashescobar
Copy link
Member

@pablohashescobar pablohashescobar commented Sep 16, 2025

Description

  • Added get_allowed_hosts function to retrieve allowed hosts from settings.
  • Updated get_safe_redirect_url to validate URLs against allowed hosts.
  • Improved URL construction logic for safer redirection handling.
  • Updated get_allowed_hosts to extract only the host from ADMIN_BASE_URL and SPACE_BASE_URL settings for better URL validation.
  • Enhanced overall safety and clarity in allowed hosts retrieval.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Test Scenarios

  • test space endpoints for redirections

References

WEB-4943

Summary by CodeRabbit

  • Bug Fixes
    • Hardened redirect handling in sign-up and magic link flows to prevent redirects to untrusted hosts.
    • If an invalid or external next URL is provided, users are safely returned to the default site instead of being redirected elsewhere.
    • Improves security and reliability of post-authentication navigation.
    • Sign-in flow behavior remains unchanged.

* Added get_allowed_hosts function to retrieve allowed hosts from settings.
* Updated get_safe_redirect_url to validate URLs against allowed hosts.
* Improved URL construction logic for safer redirection handling.
* Added url_has_allowed_host_and_scheme checks in SignUpAuthSpaceEndpoint and MagicSignInSpaceEndpoint for safer redirection.
* Updated redirect logic to fallback to base host if the constructed URL is not allowed.
* Improved overall URL safety and handling in authentication flows.
* Updated get_allowed_hosts to extract only the host from ADMIN_BASE_URL and SPACE_BASE_URL settings for better URL validation.
* Enhanced overall safety and clarity in allowed hosts retrieval.
Copilot AI review requested due to automatic review settings September 16, 2025 16:06
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 16, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Introduces host-allowlist validation for redirect URLs in space authentication flows. Updates SignUp and magic-link endpoints to validate constructed redirect URLs and fall back to a base host when disallowed. Adds get_allowed_hosts utility and updates get_safe_redirect_url to enforce host checks.

Changes

Cohort / File(s) Summary
Space auth endpoints
apps/api/plane/authentication/views/space/email.py, apps/api/plane/authentication/views/space/magic.py
Validate constructed redirect URLs using Django’s url_has_allowed_host_and_scheme with get_allowed_hosts(); if invalid, redirect to base host. SignUp endpoint updated; multiple magic-link endpoints updated similarly.
Path validation utilities
apps/api/plane/utils/path_validator.py
Added get_allowed_hosts() building an allowlist from settings; updated get_safe_redirect_url to verify final URL host against the allowlist and fall back to base URL if disallowed; retained next-path validation.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant E as Auth Endpoint (SignUp / Magic)
    participant PV as Path Validator
    participant DJ as Django Host Validator

    U->>E: Request with next_path
    E->>PV: get_safe_redirect_url(base_url, next_path)
    PV->>PV: validate_next_path(next_path)
    PV->>DJ: url_has_allowed_host_and_scheme(url, allowed_hosts)
    alt Host allowed
        DJ-->>PV: true
        PV-->>E: safe redirect URL
        E-->>U: HTTP 302 to next URL
    else Host not allowed
        DJ-->>PV: false
        PV-->>E: base_url
        E-->>U: HTTP 302 to base host
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

🛡️security

Suggested reviewers

  • dheeru0198
  • sriramveeraghanta

Poem

A hop, a skip, a guarded leap—
I check each host before I peep.
If paths look bright and doors allowed,
I bound ahead, secure and proud.
But if they stray to fields unknown,
I scurry back to base—my home. 🐇🔐

✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-auth-valid-redirections

📜 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 d521eab and 9fbceaa.

📒 Files selected for processing (3)
  • apps/api/plane/authentication/views/space/email.py (2 hunks)
  • apps/api/plane/authentication/views/space/magic.py (4 hunks)
  • apps/api/plane/utils/path_validator.py (3 hunks)

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


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

Comment @coderabbitai help to get the list of available commands and usage tips.

@makeplane
Copy link

makeplane bot commented Sep 16, 2025

Pull Request Linked with Plane Work Items

Comment Automatically Generated by Plane

Copy link
Contributor

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 implements URL validation for redirection safety by introducing host and scheme validation to prevent open redirect vulnerabilities. The changes add a new function to retrieve allowed hosts from settings and integrate Django's built-in URL validation across authentication endpoints.

  • Added get_allowed_hosts() function to extract allowed hosts from application settings
  • Enhanced get_safe_redirect_url() with URL validation using Django's url_has_allowed_host_and_scheme
  • Updated space authentication endpoints to validate redirect URLs before performing redirections

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
apps/api/plane/utils/path_validator.py Added host validation logic and enhanced redirect URL safety checks
apps/api/plane/authentication/views/space/magic.py Integrated URL validation in magic link authentication endpoints
apps/api/plane/authentication/views/space/email.py Added URL validation to email authentication endpoint

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +48 to +57
base_origin = settings.WEB_URL or settings.APP_BASE_URL
allowed_hosts = [base_origin]
if settings.ADMIN_BASE_URL:
# Get only the host
host = urlparse(settings.ADMIN_BASE_URL).netloc
allowed_hosts.append(host)
if settings.SPACE_BASE_URL:
# Get only the host
host = urlparse(settings.SPACE_BASE_URL).netloc
allowed_hosts.append(host)
Copy link

Copilot AI Sep 16, 2025

Choose a reason for hiding this comment

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

Inconsistent host extraction logic. base_origin is added as a full URL while ADMIN_BASE_URL and SPACE_BASE_URL are parsed to extract only the netloc. This creates a mismatch where base_origin should also be parsed to extract only the host portion for consistent validation.

Copilot uses AI. Check for mistakes.
from django.core.validators import validate_email
from django.http import HttpResponseRedirect
from django.views import View
from django.utils.http import url_has_allowed_host_and_scheme
Copy link

Copilot AI Sep 16, 2025

Choose a reason for hiding this comment

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

Missing import for get_allowed_hosts function. The function is used on line 204 but not imported, which will cause a NameError at runtime.

Copilot uses AI. Check for mistakes.
@sriramveeraghanta sriramveeraghanta merged commit 6d3d9e6 into preview Sep 16, 2025
5 of 10 checks passed
@sriramveeraghanta sriramveeraghanta deleted the fix-auth-valid-redirections branch September 16, 2025 16:07
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

This is the final PR Bugbot will review for you during this billing cycle

Your free Bugbot reviews will reset on September 20

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

# Get only the host
host = urlparse(settings.SPACE_BASE_URL).netloc
allowed_hosts.append(host)
return allowed_hosts
Copy link

Choose a reason for hiding this comment

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

Bug: Inconsistent Host Handling in URL Validation

The get_allowed_hosts function inconsistently populates the allowed_hosts list. It adds base_origin as a full URL, but extracts only the hostname for ADMIN_BASE_URL and SPACE_BASE_URL. This causes url_has_allowed_host_and_scheme to fail validation, as it expects only hostnames.

Fix in Cursor Fix in Web

yarikoptic pushed a commit to yarikoptic/plane that referenced this pull request Oct 1, 2025
…edirections (makeplane#7809)

* feat: enhance path validation and URL safety in path_validator.py

* Added get_allowed_hosts function to retrieve allowed hosts from settings.
* Updated get_safe_redirect_url to validate URLs against allowed hosts.
* Improved URL construction logic for safer redirection handling.

* feat: enhance URL validation in authentication views

* Added url_has_allowed_host_and_scheme checks in SignUpAuthSpaceEndpoint and MagicSignInSpaceEndpoint for safer redirection.
* Updated redirect logic to fallback to base host if the constructed URL is not allowed.
* Improved overall URL safety and handling in authentication flows.

* fix: improve host extraction in get_allowed_hosts function

* Updated get_allowed_hosts to extract only the host from ADMIN_BASE_URL and SPACE_BASE_URL settings for better URL validation.
* Enhanced overall safety and clarity in allowed hosts retrieval.
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