-
-
Notifications
You must be signed in to change notification settings - Fork 226
Fixed rebuild_safe_url function #3832
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
Conversation
PR Reviewer Guide 🔍(Review updated until commit 0ae056d)Here are some key observations to aid the review process:
|
PR Code Suggestions ✨ |
Persistent review updated to latest commit 0ae056d |
PR Code Suggestions ✨ |
This fixes the issue for the rebuild_safe_url which was introducing SSRF vulnerabilities in the /website/views/issue.py at file issues.py and company.py by improperly sanitizing the url. My changes to the code bring enhanced protection from SSRF, CRLF and path traversal vulnerabilities by properly sanitizing and blocking requests to internal ip address. The dns_safe is another function added which first resolves the input url from the user and then resolves it's ip address before processing it any further [Protects from dns rebinding] . It is advisable to use this version of rebuild_safe_url wherever there;s a need to take user input. |
@DonnieBLT I have added the test case, please check if any improvements are needed or edge case I might've missed. |
Can this be merged? @DonnieBLT |
WalkthroughThe pull request improves security and reliability in multiple parts of the application. In the education template and view, deletion actions now enforce POST submissions with CSRF protection. In the URL utilities, a new DNS safety check is added to the URL rebuilding process while deprecating an older safety method. Additionally, comprehensive tests have been introduced to verify the updated URL safety functionality. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Form
participant Server
participant Database
Client->>Form: Submit delete request (POST with CSRF)
Form->>Server: Forward request to delete endpoint
Server->>Database: Fetch and delete section
Database-->>Server: Confirmation of deletion
Server->>Client: Return response
sequenceDiagram
participant Caller
participant rebuild_safe_url
participant is_dns_safe
Caller->>rebuild_safe_url: Pass URL
rebuild_safe_url->>is_dns_safe: Validate hostname safety
is_dns_safe-->>rebuild_safe_url: Return safety status
rebuild_safe_url-->>Caller: Return safe URL or None
Poem
Tip ⚡🧪 Multi-step agentic review comment chat (experimental)
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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 (1)
website/test_api.py (1)
5-38
: Comprehensive test coverage forrebuild_safe_url
.
- It's great to see each input being tested in a subTest, providing clear insights into which URL variations might fail.
- Consider removing or converting the debug print statement (line 17) to logging if needed, as excessive console prints can clutter test outputs.
- You might also want to test edge cases like empty URLs or non-HTTP(S) schemes in the future to further strengthen coverage.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
website/templates/education/content_management.html
(1 hunks)website/test_api.py
(1 hunks)website/utils.py
(2 hunks)website/views/education.py
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
- GitHub Check: Run Tests
- GitHub Check: docker-test
- GitHub Check: Analyze (python)
🔇 Additional comments (4)
website/views/education.py (1)
242-242
: Good addition of @require_POST for secure deletion handling.Requiring a POST request for
delete_section
helps prevent unintended or malicious GET requests from triggering destructive actions. Ensure that the frontend form is correctly using thePOST
method with a CSRF token to fully protect against CSRF.website/templates/education/content_management.html (1)
395-407
: Proper use of POST method and CSRF token in the deletion form.Adding
method="post"
and{% csrf_token %}
aligns with best practices for destructive actions, helping to prevent CSRF attacks. The clear separation of “Cancel” and “Delete” buttons further improves UX and reduces accidental deletions.website/utils.py (2)
134-148
: DNS safety checks are well-structured.Using
socket.getaddrinfo
and iterating addresses to reject private/loopback/reserved networks greatly reduces SSRF risks. If performance concerns arise under high volume, consider an optimized DNS resolution strategy or in-memory caching.
150-185
: Enhanced URL sanitization inrebuild_safe_url
.
- Validating scheme (HTTP/HTTPS) and removing user credentials (@) is a robust measure for SSRF mitigation.
- Checking IP addresses (private, loopback) plus
is_dns_safe
ensures that unauthorized internal hosts become inaccessible.- Collapsing multiple slashes and removing CR/LF is excellent for path normalization.
- Consider whether you need to preserve or sanitize query parameters/fragment if they are used elsewhere, as they are currently being dropped.
Overall, well-implemented SSRF defense.
@CodeRabbit review once more and approve if it looks good |
🧩 Analysis chain🏁 Script executed:
Length of output: 46965 @DonnieBLT I've reviewed the PR and the changes look excellent. The improved
The new The test cases are comprehensive and cover all the security improvements. This PR effectively addresses the SSRF vulnerabilities in issue.py and company.py. I recommend approving this PR. ✅ Actions performedComments resolved and changes approved. |
User description
Now the rebuild_safe_url checks for potential ssrf payload and returns None in case such url is provided, also I have made the path sanitization better since the previous one only removed the query and fragment part from the path but now it checks furtherfor carriage return and line feed and other misused paths and normalizes them.
fixes #3797
fixes #3923
PR Type
Bug fix, Tests
Description
Enhanced
rebuild_safe_url
to sanitize URLs against SSRF vulnerabilities.Added
is_dns_safe
function to validate DNS safety of URLs.Introduced comprehensive test cases for
rebuild_safe_url
function.Removed deprecated
is_safe_url
function to streamline URL validation.Changes walkthrough 📝
test_api.py
Add test cases for `rebuild_safe_url` function
website/test_api.py
RebuildSafeUrlTestCase
.rebuild_safe_url
.traversal.
utils.py
Improve URL sanitization and add DNS safety checks
website/utils.py
rebuild_safe_url
to sanitize URLs against SSRFvulnerabilities.
is_dns_safe
function to validate DNS safety of hostnames.rebuild_safe_url
.is_safe_url
function.Summary by CodeRabbit