`Metadata.from_email(validate=True)` accepts malformed `Project-URL` values and fabricates a label/URL pair
Metadata.from_email(..., validate=True) accepts malformed Project-URL values and returns a fabricated label/URL pair instead of failing.
What happens
_parse_project_urls documents that malformed pairs are left for validation to catch:
# The main thing that we have to worry about, is data that does not have a
# ',' at all to split the label from the Value. There isn't a singular right
# answer here, and we will fail validation later on (if the caller is
# validating) so it doesn't *really* matter, but ...
But project_urls is a plain validator (_Validator(added="1.2"), src/packaging/metadata.py:993) with no processing step, so nothing fails. The malformed value is accepted and a mapping is invented for it.
>>> from packaging.metadata import Metadata
>>> raw = "Metadata-Version: 2.4\nName: x\nVersion: 1\nProject-URL: just-a-url\n"
>>> Metadata.from_email(raw, validate=True).project_urls
{'just-a-url': ''}
Other inputs accepted on main with validate=True:
Project-URL value |
resulting project_urls |
|---|---|
just-a-url |
{'just-a-url': ''} |
Docs, (trailing comma, empty URL) |
{'Docs': ''} |
, https://x.example (empty label) |
{'': 'https://x.example'} |
| a 33-character label | accepted unchanged |
Duplicate labels are the only malformed case that is rejected — that path marks the field as unparsed, and from_email raises.
Why it matters
The spec describes the field as "a string containing a browsable URL for the project and a label for it, separated by a comma", with the label "limited to 32 characters". A caller that asked for validation currently gets a dict entry whose value is '' and no exception, so the broken link surfaces far from the metadata that caused it — the same shape as a crash-free wrong answer.
Possible fix
Validate in _parse_project_urls and raise KeyError to reuse the existing "field is unparsed" path, for at least: no comma, empty label, empty URL. Whether the 32-character label limit should be enforced is less clear to me — I'd rather ask than guess, since enforcing it can reject metadata that indexes already accept.
Happy to send a patch with tests for whichever set of rules you want enforced, as errors or as warnings.