Skip to main content

parse_port

Function parse_port 

Source
fn parse_port(port: &str) -> Result<u16, DidError>
Expand description

Reads the port half of a decoded authority, in one spelling only.

u16::from_str is not that: it accepts a leading + and any number of leading zeros, so %3A3000, %3A03000 and %3A+3000 would all decode to port 3000. That is the two-spellings-one-identity problem, and it is not hypothetical here — AccountDid compares and hashes on the DID string it was given, while AccountDid::did_json_url and AccountDid::authority derive from the decoded port, so the spellings are distinct identifiers naming one account. The + form is worse still: validate_did refuses + outright, so accepting it here made this supposedly stricter parser admit a string that is not a syntactically valid DID of any method at all.

Digits only, and no leading zero unless the port is exactly 0 — which is then refused as a port anyway, since nothing listens on it.