normalize_url.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from __future__ import annotations
  2. from collections.abc import Callable
  3. from contextlib import suppress
  4. import re
  5. from urllib.parse import quote, unquote, urlparse, urlunparse # noqa: F401
  6. import mdurl
  7. from .. import _punycode
  8. RECODE_HOSTNAME_FOR = ("http:", "https:", "mailto:")
  9. def normalizeLink(url: str) -> str:
  10. """Normalize destination URLs in links
  11. ::
  12. [label]: destination 'title'
  13. ^^^^^^^^^^^
  14. """
  15. parsed = mdurl.parse(url, slashes_denote_host=True)
  16. # Encode hostnames in urls like:
  17. # `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
  18. #
  19. # We don't encode unknown schemas, because it's likely that we encode
  20. # something we shouldn't (e.g. `skype:name` treated as `skype:host`)
  21. #
  22. if parsed.hostname and (
  23. not parsed.protocol or parsed.protocol in RECODE_HOSTNAME_FOR
  24. ):
  25. with suppress(Exception):
  26. parsed = parsed._replace(hostname=_punycode.to_ascii(parsed.hostname))
  27. return mdurl.encode(mdurl.format(parsed))
  28. def normalizeLinkText(url: str) -> str:
  29. """Normalize autolink content
  30. ::
  31. <destination>
  32. ~~~~~~~~~~~
  33. """
  34. parsed = mdurl.parse(url, slashes_denote_host=True)
  35. # Encode hostnames in urls like:
  36. # `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
  37. #
  38. # We don't encode unknown schemas, because it's likely that we encode
  39. # something we shouldn't (e.g. `skype:name` treated as `skype:host`)
  40. #
  41. if parsed.hostname and (
  42. not parsed.protocol or parsed.protocol in RECODE_HOSTNAME_FOR
  43. ):
  44. with suppress(Exception):
  45. parsed = parsed._replace(hostname=_punycode.to_unicode(parsed.hostname))
  46. # add '%' to exclude list because of https://github.com/markdown-it/markdown-it/issues/720
  47. return mdurl.decode(mdurl.format(parsed), mdurl.DECODE_DEFAULT_CHARS + "%")
  48. BAD_PROTO_RE = re.compile(r"^(vbscript|javascript|file|data):")
  49. GOOD_DATA_RE = re.compile(r"^data:image\/(gif|png|jpeg|webp);")
  50. def validateLink(url: str, validator: Callable[[str], bool] | None = None) -> bool:
  51. """Validate URL link is allowed in output.
  52. This validator can prohibit more than really needed to prevent XSS.
  53. It's a tradeoff to keep code simple and to be secure by default.
  54. Note: url should be normalized at this point, and existing entities decoded.
  55. """
  56. if validator is not None:
  57. return validator(url)
  58. url = url.strip().lower()
  59. return bool(GOOD_DATA_RE.search(url)) if BAD_PROTO_RE.search(url) else True