Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unresolver: allow a full URL when un-resolving a domain #11632

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion readthedocs/core/unresolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def unresolve_url(self, url, append_indexhtml=True):
parsed_url = urlparse(url)
if parsed_url.scheme not in ["http", "https"]:
raise InvalidSchemeError(parsed_url.scheme)
domain = self.get_domain_from_host(parsed_url.netloc)
domain = parsed_url.hostname
unresolved_domain = self.unresolve_domain(domain)
return self._unresolve(
unresolved_domain=unresolved_domain,
Expand Down Expand Up @@ -551,13 +551,23 @@ def unresolve_domain(self, domain):
Unresolve domain by extracting relevant information from it.

:param str domain: Domain to extract the information from.
It can be a full URL, in that case, only the domain is used.
:returns: A UnresolvedDomain object.
"""
parsed_domain = urlparse(domain)
if parsed_domain.scheme:
if parsed_domain.scheme not in ["http", "https"]:
raise InvalidSchemeError(parsed_domain.scheme)
domain = parsed_domain.hostname
stsewd marked this conversation as resolved.
Show resolved Hide resolved

public_domain = self.get_domain_from_host(settings.PUBLIC_DOMAIN)
external_domain = self.get_domain_from_host(
settings.RTD_EXTERNAL_VERSION_DOMAIN
)

if not domain:
raise InvalidSubdomainError(domain)

subdomain, *root_domain = domain.split(".", maxsplit=1)
root_domain = root_domain[0] if root_domain else ""

Expand Down
26 changes: 26 additions & 0 deletions readthedocs/rtd_tests/tests/test_unresolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
InvalidExternalVersionError,
InvalidPathForVersionedProjectError,
InvalidSchemeError,
InvalidSubdomainError,
SuspiciousHostnameError,
TranslationNotFoundError,
TranslationWithoutVersionError,
VersionNotFoundError,
unresolve,
unresolver,
)
from readthedocs.projects.constants import SINGLE_VERSION_WITHOUT_TRANSLATIONS
from readthedocs.projects.models import Domain
Expand Down Expand Up @@ -372,8 +374,32 @@ def test_unresolve_invalid_scheme(self):
"fttp://pip.readthedocs.io/en/latest/",
"fttps://pip.readthedocs.io/en/latest/",
"ssh://pip.readthedocs.io/en/latest/",
"javascript://pip.readthedocs.io/en/latest/",
"://pip.readthedocs.io/en/latest/",
]
for url in invalid_urls:
with pytest.raises(InvalidSchemeError):
unresolve(url)

with pytest.raises(InvalidSubdomainError):
unresolve("https:///pip.readthedocs.io/en/latest/")
Comment on lines +384 to +385
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this raises InvalidSubdomainError?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A tripe /// is interpreted as a URL without a domain.


def test_unresolve_domain_with_full_url(self):
result = unresolver.unresolve_domain("https://pip.readthedocs.io/en/latest/")
self.assertIsNone(result.domain)
self.assertEqual(result.project, self.pip)
self.assertTrue(result.is_from_public_domain)
self.assertEqual(result.source_domain, "pip.readthedocs.io")

def test_unresolve_domain_with_full_url_invalid_protocol(self):
invalid_protocols = [
"fttp",
"fttps",
"ssh",
"javascript",
]
for protocol in invalid_protocols:
with pytest.raises(InvalidSchemeError):
unresolver.unresolve_domain(
f"{protocol}://pip.readthedocs.io/en/latest/"
)