From dfab09b17d0799d37d7b504d5c01018cde756993 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sat, 12 Sep 2020 19:46:59 -0700 Subject: [PATCH] markdown: Replace hyperlink requirement with urllib.parse. The previous code only worked by accident and hyperlink 20.0.0 breaks it. >>> hyperlink.parse("example.com").replace(scheme="https") DecodedURL(url=URL.from_text('https:example.com')) Signed-off-by: Anders Kaseorg --- requirements/common.in | 1 - requirements/dev.txt | 2 +- requirements/prod.txt | 6 +----- version.py | 2 +- zerver/lib/markdown/__init__.py | 11 +++++++---- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/requirements/common.in b/requirements/common.in index 9a5053c619..f523022a30 100644 --- a/requirements/common.in +++ b/requirements/common.in @@ -18,7 +18,6 @@ Jinja2 Markdown importlib-metadata;python_version<"3.8" # for Markdown Pygments -hyperlink jsx-lexer # Needed for manage.py diff --git a/requirements/dev.txt b/requirements/dev.txt index aa98b126a3..c4adf38d6e 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -368,7 +368,7 @@ hyperframe==3.2.0 \ hyperlink==19.0.0 \ --hash=sha256:4288e34705da077fada1111a24a0aa08bb1e76699c9ce49876af722441845654 \ --hash=sha256:ab4a308feb039b04f855a020a6eda3b18ca5a68e6d8f8c899cbe9e653721d04f \ - # via -r requirements/common.in, twisted + # via twisted idna==2.8 \ --hash=sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407 \ --hash=sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c \ diff --git a/requirements/prod.txt b/requirements/prod.txt index 10f9daf82b..6bebc8a352 100644 --- a/requirements/prod.txt +++ b/requirements/prod.txt @@ -257,14 +257,10 @@ hyperframe==3.2.0 \ --hash=sha256:05f0e063e117c16fcdd13c12c93a4424a2c40668abfac3bb419a10f57698204e \ --hash=sha256:4dcab11967482d400853b396d042038e4c492a15a5d2f57259e2b5f89a32f755 \ # via h2, hyper -hyperlink==19.0.0 \ - --hash=sha256:4288e34705da077fada1111a24a0aa08bb1e76699c9ce49876af722441845654 \ - --hash=sha256:ab4a308feb039b04f855a020a6eda3b18ca5a68e6d8f8c899cbe9e653721d04f \ - # via -r requirements/common.in idna==2.8 \ --hash=sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407 \ --hash=sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c \ - # via hyperlink, requests + # via requests ijson==3.1.post0 \ --hash=sha256:07cd88c9224392726128673fea7a90276746d3bf56d11635e3457769eda6295a \ --hash=sha256:0ae656f3ca3bd833dc548583f13ba205eaee7c15d27496091549de4523931e00 \ diff --git a/version.py b/version.py index bafce853fe..cb7ee65964 100644 --- a/version.py +++ b/version.py @@ -44,4 +44,4 @@ API_FEATURE_LEVEL = 32 # historical commits sharing the same major version, in which case a # minor version bump suffices. -PROVISION_VERSION = '102.0' +PROVISION_VERSION = '103.0' diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py index af20f7e5e5..132d81c62c 100644 --- a/zerver/lib/markdown/__init__.py +++ b/zerver/lib/markdown/__init__.py @@ -26,6 +26,7 @@ from typing import ( Union, ) from typing.re import Match, Pattern +from urllib.parse import urlsplit from xml.etree import ElementTree as etree from xml.etree.ElementTree import Element, SubElement @@ -36,7 +37,6 @@ import markdown import requests from django.conf import settings from django.db.models import Q -from hyperlink import parse from markdown.extensions import codehilite, nl2br, sane_lists, tables from typing_extensions import TypedDict @@ -2077,9 +2077,12 @@ def topic_links(realm_filters_key: int, topic_name: str) -> List[str]: link_match = re.match(get_web_link_regex(), sub_string) if link_match: url = link_match.group('url') - url_object = parse(url) - if not url_object.scheme: - url = url_object.replace(scheme='https').to_text() + result = urlsplit(url) + if not result.scheme: + if not result.netloc: + i = (result.path + "/").index("/") + result = result._replace(netloc=result.path[:i], path=result.path[i:]) + url = result._replace(scheme="https").geturl() matches.append(url) return matches