pub fn extract_git_authority(url: &str) -> Option<String>Expand description
Extract the canonical authority from a git remote URL for host-scoping.
The authority is the host[:port] portion of the URL, canonicalized per
the WHATWG URL Standard’s host-parsing algorithm (via the url crate):
- Domain names are IDNA-processed and ASCII case-folded.
- IPv4 addresses (including non-canonical forms like octal/hex octets) are normalized to canonical dotted-decimal.
- IPv6 literals are normalized and returned in bracketed form
(
[2001:db8::cafe]). - Default scheme ports are stripped (
:443/:80for HTTPS/HTTP,:22for SSH,:9418for the git protocol); any other port is preserved.
Security-critical / standards-based: the URL is parsed structurally
by url::Url::parse — the authority (scheme, userinfo, host, port) is
separated from the path, query, and fragment by the same state machine
browsers use, so an @, /, ?, or # appearing in the path or query
can never be misread as part of the authority. This closes the Sol P1
path/query @ confusion finding (#1429), which affected the previous
hand-written parser.
url::Url::parse only applies IDNA/case canonicalization to the host
automatically for its built-in “special” schemes (http/https); ssh
and git are not “special” to the url crate, so their host is
re-canonicalized explicitly via url::Host::parse — the same
standards-based host algorithm, applied uniformly regardless of scheme.
Backslash is rejected outright (fail closed), not parsed. The WHATWG
URL Standard treats \ as equivalent to / within a “special” scheme
(http/https), ending the authority early — but the pinned libgit2
1.9.x C parser treats \ as an ordinary authority character and keeps
splitting userinfo at the last @. The same input string is therefore
assigned a different authority by this function than by the transport
that actually opens the connection: https://github.com\@evil.example/x
resolves to github.com here but libgit2 connects to evil.example.
Any input containing \ is a parser-differential attack surface, not a
case this function can resolve by parsing more cleverly — it returns
None unconditionally rather than risk agreeing with the wrong parser.
See #1429 Kimi-K3 r3 P1.
Returns None for local paths (file://, bare paths), malformed URLs,
URLs using a scheme outside [ALLOWED_SCHEMES], URLs containing a
backslash, or URLs where the authority cannot be reliably determined.
Callers must treat None as “authority unknown” and refuse the
credential, not fall back to an unscoped release.
§Examples
https://github.com/user/repo.git → "github.com"
https://USER@github.com/user/repo.git → "github.com"
https://github.com:443/user/repo.git → "github.com" (default port stripped)
https://github.com:8443/user/repo.git → "github.com:8443"
https://evil.example/repo@github.com → "evil.example" (path @ ignored)
ssh://git@github.com:22/user/repo.git → "github.com"
git@github.com:user/repo.git → "github.com" (scp-like)
https://[2001:db8::cafe]/repo.git → "[2001:db8::cafe]"
https://github.com\@evil.example/x.git → None (backslash rejected)