Issue #3412
closedTrailing slash in distribution base_path causes breakage (Pulp 3)
Description
Let's say I create a distribution by making an HTTP POST request to /api/v3/distributions/
, with the following JSON body:
{
"base_path": "my-base-path",
"http": true,
"https": true,
"name": "distribution-name",
"publication": "http://pulp-3.example.com:8000/api/v3/publications/publication-id/",
}
This will return a distribution:
{
"_href": "http://pulp-3.example.com:8000/api/v3/distributions/distribution-name/",
"base_path": "my-base-path",
"base_url": "pulp-3.example.com:8000/content/my-base-path",
"http": true,
"https": true,
"name": "distribution-name",
"publication": "http://pulp-3.example.com:8000/api/v3/publications/publication-id/",
"publisher": null,
"repository": null
}
Furthermore, let's say that the publication at …/api/v3/publications/publication-id/
contains a file named 1.iso.
At which URLs are this file available? The following:
http://pulp-3.example.com:8000/content/my-base-path/1.iso
https://pulp-3.example.com:8000/content/my-base-path/1.iso
Before going any further, note that the process of generating these paths is awkward, because the data in the distributor is awkward. Let's say that I write some Python code to come up with the path to 1.iso
. A naive attempt will totally fail:
from urllib.parse import urljoin
distribution = {
'_href': 'http://pulp-3.example.com:8000/api/v3/distributions/distribution-name/',
'base_path': 'my-base-path',
'base_url': 'pulp-3.example.com:8000/content/my-base-path',
'http': True,
'https': True,
'name': 'distribution-name',
'publication': 'http://pulp-3.example.com:8000/api/v3/publications/publication-id/',
'publisher': None,
'repository': None
}
basename = '1.iso'
# ['1.iso', '1.iso']
urls = [
urljoin(distribution['base_url'], basename)
for scheme in ('http', 'https')
if distribution[scheme]
]
# ['http://pulp-3.example.com:8000/content/1.iso', 'https://pulp-3.example.com:8000/content/1.iso']
urls = [
urljoin(scheme + '://' + distribution['base_url'], basename)
for scheme in ('http', 'https')
if distribution[scheme]
]
# ['http://pulp-3.example.com:8000/content/my-base-path/1.iso', 'https://pulp-3.example.com:8000/content/my-base-path/1.iso']
urls = [
urljoin(scheme + '://' + distribution['base_url'] + '/', basename)
for scheme in ('http', 'https')
if distribution[scheme]
]
This user experience sucks. But that's not what this bug is about. This bug is really about the fact Pulp accepts base_path
s that it subsequently barfs on. Let's say that I create the exact same distribution as before, but with a trailing slash on the base_path
:
{
"base_path": "my-base-path/",
"http": true,
"https": true,
"name": "distribution-name",
"publication": "http://pulp-3.example.com:8000/api/v3/publications/publication-id/",
}
Pulp will accept the trailing slash:
{
"_href": "http://pulp-3.example.com:8000/api/v3/distributions/distribution-name/",
"base_path": "my-base-path/",
"base_url": "pulp-3.example.com:8000/content/my-base-path/",
"http": true,
"https": true,
"name": "distribution-name",
"publication": "http://pulp-3.example.com:8000/api/v3/publications/publication-id/",
"publisher": null,
"repository": null
}
The file named 1.iso
should be available at the following URLs:
http://pulp-3.example.com:8000/content/my-base-path/1.iso
https://pulp-3.example.com:8000/content/my-base-path/1.iso
Unfortunately, HTTP requests to these URLs will produce HTTP 404s.
Please do one of the following:
- Reject
base_url
s that have a trailing slash. - Accept
base_url
s that have a trailing slash, and handle them properly.
When deciding upon a solution, please consider the user story outlined above.
Validate distribution base paths
Validate that distribution base paths contain valid urls and don't end or begin with slashes
fixes #3412 https://pulp.plan.io/issues/3412