-
Notifications
You must be signed in to change notification settings - Fork 268
Fix SSRF bypass for IPv4-mapped IPv6 in HTML-to-Markdown expansion #788
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #!/usr/bin/env python3 | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| from misp_modules.modules.expansion.html_to_markdown import is_safe_url | ||
|
|
||
|
|
||
| class TestHtmlToMarkdownUrlSafety(unittest.TestCase): | ||
|
|
||
| def test_blocks_ipv4_mapped_ipv6_literals_for_blocked_ipv4_ranges(self): | ||
| blocked_urls = ( | ||
| "http://[::ffff:127.0.0.1]/", | ||
| "http://[::ffff:10.0.0.1]/", | ||
| "http://[::ffff:172.16.0.1]/", | ||
| "http://[::ffff:192.168.0.1]/", | ||
| "http://[::ffff:169.254.169.254]/", | ||
| ) | ||
|
|
||
| for url in blocked_urls: | ||
| with self.subTest(url=url): | ||
| self.assertFalse(is_safe_url(url)) | ||
|
|
||
| def test_allows_public_ipv4_mapped_ipv6_literal(self): | ||
| self.assertTrue(is_safe_url("http://[::ffff:93.184.216.34]/")) | ||
|
|
||
| def test_blocks_hostnames_resolving_to_ipv4_mapped_blocked_addresses(self): | ||
| with patch( | ||
| "misp_modules.modules.expansion.html_to_markdown.socket.getaddrinfo", | ||
| return_value=[(None, None, None, None, ("::ffff:127.0.0.1", 0, 0, 0))], | ||
| ): | ||
| self.assertFalse(is_safe_url("http://example.test/")) | ||
|
|
||
| def test_rejects_url_without_hostname(self): | ||
| self.assertFalse(is_safe_url("http:///missing-host")) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.
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.
For any plain IPv4 literal, or for a hostname whose DNS answer includes an A record,
ipaddress.ip_address()returns anIPv4Address, which does not have anipv4_mappedattribute. This line therefore raisesAttributeErrorbefore the blocklist check can return, so URLs likehttp://93.184.216.34/and ordinary hostnames resolving to IPv4 addresses can crash the expansion module instead of being allowed or blocked. Guard this withgetattr(ip, "ipv4_mapped", None)or anisinstance(ip, ipaddress.IPv6Address)check.Useful? React with 👍 / 👎.