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

EmbedAPI: support "pretty URLs" #11811

Merged
merged 2 commits into from
Dec 4, 2024
Merged

EmbedAPI: support "pretty URLs" #11811

merged 2 commits into from
Dec 4, 2024

Conversation

humitos
Copy link
Member

@humitos humitos commented Nov 28, 2024

Tru for the URL ending with index.html if the original URL doesn't exist in the storage.

This makes Sphinx HTMLDir build, Docusaurus and other documentation tools with pretty URLs to work properly.

Tru for the URL ending with `index.html` if the original URL doesn't exist in
the storage.

This makes Sphinx HTMLDir build, Docusaurus and other documentation tools with
pretty URLs to work properly.
@humitos humitos requested a review from a team as a code owner November 28, 2024 15:03
@humitos humitos requested a review from stsewd November 28, 2024 15:03
@humitos
Copy link
Member Author

humitos commented Dec 2, 2024

Failing test readthedocs/rtd_tests/tests/test_domains.py::FormTests::test_valid_domains is unrelated to this PR.

file_path
) as fd: # pylint: disable=invalid-name
return fd.read()
tryfiles = [file_path, os.path.join(file_path, "index.html")]
Copy link
Member

Choose a reason for hiding this comment

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

You should use build_media_storage.join, otherwise this is likely vulnerable to path traversal. Also, I think we already have some logic for handling index files somewhere, we just check if the file ends with / and append index.html, otherwise just treat it as a regular file.

Copy link
Member Author

Choose a reason for hiding this comment

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

I found this code:

# Handle our backend storage not supporting directory indexes,
# so we need to append index.html when appropriate.
if not filename or filename.endswith("/"):
filename += "index.html"
# If the filename starts with `/`, the join will fail,
# so we strip it before joining it.
try:
storage_path = build_media_storage.join(
base_storage_path, filename.lstrip("/")
)

Copy link
Member Author

Choose a reason for hiding this comment

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

I updated this PR following that code 👍🏼

Copy link
Member Author

Choose a reason for hiding this comment

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

we just check if the file ends with / and append index.html

We can't do that in this case. The filename I'm receiving when using Docusaurus doesn't have an ending /:

(Pdb++) filename
'/docs/tutorial-basics/create-a-page'

I'm not finding where in the code we manage this case from El Proxito. These type of URLs are definitely being served correctly.

Copy link
Member Author

@humitos humitos Dec 3, 2024

Choose a reason for hiding this comment

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

I found why it works on El Proxito, we are performing a redirect when the URL doesn't ends with /

▶ curl -sIL http://test-builds.devthedocs.org/en/docusaurus/docs/tutorial-basics/create-a-page | head
HTTP/1.1 302 Found
Server: nginx/1.22.0
Date: Tue, 03 Dec 2024 07:41:12 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
Location: /en/docusaurus/docs/tutorial-basics/create-a-page/
Access-Control-Allow-Origin: *
Content-Language: en
Vary: Accept-Language, Cookie
...

This redirect is done at

log.info("Redirecting to index file.", tryfile=tryfile)

Copy link
Member

Choose a reason for hiding this comment

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

yeah, we always redirect to the correct path instead of trying to serve the file from two different URLs

Copy link
Member

Choose a reason for hiding this comment

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

We can't do that in this case. The filename I'm receiving when using Docusaurus doesn't have an ending /:

Do you mean that Docusaurus creates links without /?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean that Docusaurus creates links without /?

Yes.

There are a few documentation tools that don't use ending /, in particular those that are SPA for some reason.

If you go to https://test-builds.readthedocs.io/en/docusaurus/docs/intro we will redirect you to https://test-builds.readthedocs.io/en/docusaurus/docs/intro/. However, if you click any link, the resulting URL in your browser won't have that ending / -- that's the URL we are sending to the backend, and since we don't find the file in the storage we are returning 404.

Checking for the index.html solves this issue.

@humitos humitos requested a review from stsewd December 3, 2024 16:37
@humitos humitos merged commit 8774e68 into main Dec 4, 2024
8 checks passed
@humitos humitos deleted the humitos/embed-api-try-files branch December 4, 2024 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants