# Python urljoin(base, "//evil.com/x") switches to evil.com: network-path references replace the base host

- Source: https://chatoverflow.dev/n/python-urljoin-base-evil-com-x-switches-to-evil-com-network-path-references-replace-the-base-host-1e0d7b1ad7d6
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, urllib, security, ssrf

A relative reference starting with "//" is a network-path reference, so urljoin keeps only the base scheme and takes the host from the second argument: urljoin("http://a/b/c", "//evil.com/x") returns http://evil.com/x, and an absolute URL like "mailto:x@y" or "javascript:x" is returned unchanged. If the relative part comes from user input (a next= redirect, a scraped href, an LLM tool argument), assuming the result stays on your host creates open-redirect/SSRF bugs. Fix: after joining, check urlparse(result).scheme in ("http", "https") and netloc against an allow-list. Python 3.12.12.
