Validate and escape values in --write-link output

See https://github.com/yt-dlp/yt-dlp/security/advisories/GHSA-6v4j-43gg-vj32

Authored by: bashonly
This commit is contained in:
bashonly
2026-07-04 17:04:14 -05:00
parent bed3d58c5c
commit b6590aaa1e
3 changed files with 54 additions and 5 deletions
+17 -1
View File
@@ -4631,8 +4631,21 @@ LINK_TEMPLATES = {
'webloc': DOT_WEBLOC_LINK_TEMPLATE,
}
# Ref: https://specifications.freedesktop.org/desktop-entry/latest/value-types.html
_DESKTOP_ENTRY_TRANS = str.maketrans({
' ': R'\s',
'\n': R'\n',
'\t': R'\t',
'\r': R'\r',
'\\': R'\\',
})
def iri_to_uri(iri):
def _desktop_entry_localestring(s):
return s.translate(_DESKTOP_ENTRY_TRANS)
def iri_to_uri(iri, *, allowed_schemes=('http', 'https')):
"""
Converts an IRI (Internationalized Resource Identifier, allowing Unicode characters) to a URI (Uniform Resource Identifier, ASCII-only).
@@ -4641,6 +4654,9 @@ def iri_to_uri(iri):
iri_parts = urllib.parse.urlparse(iri)
if iri_parts.scheme not in allowed_schemes:
raise ValueError(f'"{iri_parts.scheme}" is not in allowed_schemes: {", ".join(allowed_schemes)}')
if '[' in iri_parts.netloc:
raise ValueError('IPv6 URIs are not, yet, supported.')
# Querying `.netloc`, when there's only one bracket, also raises a ValueError.