Fix SSRF bypass for IPv4-mapped IPv6 in HTML-to-Markdown expansion#788
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3bae4108a3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| def _is_ip_blocked(ip_str: str) -> bool: | ||
| def _normalize_ip_address(ip_str: str) -> ipaddress.IPv4Address | ipaddress.IPv6Address: | ||
| ip = ipaddress.ip_address(ip_str) | ||
| return ip.ipv4_mapped or ip |
There was a problem hiding this comment.
Handle IPv4Address before reading ipv4_mapped
For any plain IPv4 literal, or for a hostname whose DNS answer includes an A record, ipaddress.ip_address() returns an IPv4Address, which does not have an ipv4_mapped attribute. This line therefore raises AttributeError before the blocklist check can return, so URLs like http://93.184.216.34/ and ordinary hostnames resolving to IPv4 addresses can crash the expansion module instead of being allowed or blocked. Guard this with getattr(ip, "ipv4_mapped", None) or an isinstance(ip, ipaddress.IPv6Address) check.
Useful? React with 👍 / 👎.
Motivation
::ffff:127.0.0.1) were not normalized and therefore escaped existing IPv4 blocklist checks.Description
_normalize_ip_addresshelper that converts IPv4-mapped IPv6 addresses to their underlying IPv4 address and return a properipaddressobject._is_ip_blockedto use_normalize_ip_addressso mapped IPv4-in-IPv6 literals are checked againstBLOCKED_RANGES.is_safe_urlreject URLs without a hostname by checkingparsed.hostnamein addition to scheme validation.tests/test_html_to_markdown.pycovering mapped IPv6 literals, public mapped literals, DNS responses returning mapped addresses, and hostless URLs.misp_modules/modules/expansion/html_to_markdown.py; added file:tests/test_html_to_markdown.py.Testing
python -m compileall -q misp_modules/modules/expansion/html_to_markdown.py tests/test_html_to_markdown.pyand then ranpython -m unittest tests.test_html_to_markdown.OK).Codex Task