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: add resolvability check before redirecting to prevent insecure redirects after publishing #436

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

theShinigami
Copy link

@theShinigami theShinigami commented Dec 6, 2024

Description

After publishing, if there is a requested URL, it first checks if the requested URL is resolvable. If it is not, it defaults to the version list URL.

Related resources

Checklist

  • I have opened this pull request against master
  • I have added or modified the tests when changing logic
  • I have followed the conventional commits guidelines to add meaningful information into the changelog
  • I have read the contribution guidelines and I have joined #workgroup-pr-review on
    Slack to find a “pr review buddy” who is going to review my pull request.

Summary by Sourcery

Implement a resolvability check for requested URLs in the publish view to prevent insecure redirects after publishing. Update tests to cover scenarios with resolvable and non-resolvable URLs.

Bug Fixes:

  • Add a resolvability check for requested URLs before redirecting after publishing to prevent insecure redirects.

Tests:

  • Introduce a test to verify the behavior of the publish view when handling resolvable and non-resolvable redirect URLs.

Copy link

sourcery-ai bot commented Dec 6, 2024

Reviewer's Guide by Sourcery

This PR implements a security enhancement for the publish view redirect functionality. It adds a URL resolvability check before performing redirects after publishing content to prevent potential insecure redirects. If a requested redirect URL is not resolvable within the application, it falls back to the version list URL.

Sequence diagram for URL resolvability check before redirect

sequenceDiagram
    actor User
    participant Application
    participant URLResolver

    User->>Application: Publish content
    Application->>URLResolver: Check if requested URL is resolvable
    URLResolver-->>Application: URL is resolvable
    Application->>User: Redirect to requested URL

    alt URL not resolvable
        URLResolver-->>Application: URL not resolvable
        Application->>User: Redirect to version list URL
    end
Loading

File-Level Changes

Change Details Files
Added URL resolution validation in the publish view
  • Added URL resolvability check using resolve() function
  • Implemented fallback to version_list_url when URL is not resolvable
  • Maintained existing redirect behavior for resolvable URLs
djangocms_versioning/admin.py
Added comprehensive tests for URL redirect scenarios
  • Added test for publish view with no redirect URL
  • Added test for publish view with resolvable redirect URL
  • Added test for publish view with non-resolvable redirect URL
tests/test_admin.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @theShinigami - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟡 Testing: 2 issues found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

djangocms_versioning/admin.py Show resolved Hide resolved
Comment on lines 1388 to 1391
def test_publish_resolvable_redirect_url(self):
from djangocms_versioning import conf

conf.ON_PUBLISH_REDIRECT = "published"
Copy link

Choose a reason for hiding this comment

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

issue (testing): Missing test cleanup for ON_PUBLISH_REDIRECT setting

The test modifies a global setting but doesn't restore it after the test. This could affect other tests. Consider using setUp/tearDown or storing the original value and restoring it at the end of the test, similar to how test_publish_view_redirects_according_to_settings does it.

Comment on lines 1414 to 1420
# when the requested url is not resolvable, should default to version list url
not_resolvable_url = url + "?next=http://example.com"

with self.login_user_context(user):
response = self.client.post(not_resolvable_url)

self.assertEqual(response.url, version_list_url(poll_version.content))
Copy link

Choose a reason for hiding this comment

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

issue (testing): Missing security-related test cases for redirect validation

Given that this is a security-related change to prevent insecure redirects, consider adding test cases for potentially malicious URLs (e.g., URLs with different schemes, URLs to external domains, URLs with special characters). This would help ensure the security aspect is properly tested.

Copy link

codecov bot commented Dec 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.23%. Comparing base (f90c5b2) to head (0ad9e12).
Report is 35 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #436      +/-   ##
==========================================
+ Coverage   90.88%   91.23%   +0.35%     
==========================================
  Files          72       72              
  Lines        2546     2671     +125     
  Branches      361      308      -53     
==========================================
+ Hits         2314     2437     +123     
+ Misses        168      163       -5     
- Partials       64       71       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@fsbraun fsbraun left a comment

Choose a reason for hiding this comment

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

Hey @theShinigami ! Thanks for the PR! That's a good step in the right direction.

I have two comments:

  • In the case of conf.ON_PUBLISH_REDIRECT not in ("preview", "published") the requested_redirect is not validated
  • Also the published object's absolute url in line 1067 needs to be validated: Some custom object could return an invalid url.

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.

2 participants