Fix: URLSanitizer does not validate/limit URL ports and can permit access to sensitive ports - #1661
Merged
Conversation
…cess to sensitive ports
|
@Kirtan-pc is attempting to deploy a commit to the Anuj's projects Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
|
🎉 Thank you for your contribution! Your Pull Request has been merged successfully. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 Description
Summary
Add port validation to
URLSanitizer.validate_url()to prevent SSRF attacks targeting dangerous ports (e.g., SSH:22, MySQL:3306, Redis:6379, Docker:2375) on allowed hosts.Changes
security/url_sanitizer.pyBLOCKED_PORTS: Added class constant with ~80 known dangerous ports spanning SSH, SMB, MySQL, PostgreSQL, Redis, Docker, Memcached, MongoDB, LDAP, SMTP, and more.allowed_portsparameter: Added to__init__— when provided, acts as an allowlist (only these ports are permitted); whenNone, theBLOCKED_PORTSdeny list is used._validate_port(): New method called duringvalidate_url()that checks the parsed port against the blocked set or allowlist. Also validates port range (1–65535).validate_url(): Updated to accept and forwardallowed_ports.tests/test_security.pyAdded 5 new tests:
test_blocked_port— blocked port (SSH:22) on a public hostname is rejectedtest_blocked_port_on_ip_literal— blocked port on an IP literal is rejectedtest_allowed_ports_allowlist— allowlist mode permits specified portstest_allowed_ports_rejects_non_allowed— allowlist rejects unspecified portstest_valid_port_not_blocked— non-blocked port passes validation🔗 Linked Issue
Closes #1542
📋 Contribution Checklist