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

Make filename check more strict #14027

Merged
merged 6 commits into from
Jun 26, 2023
Merged
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
34 changes: 28 additions & 6 deletions tests/unit/forklift/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2310,31 +2310,53 @@ def test_upload_fails_with_diff_filename_same_blake2(
"400 File already exists. See /the/help/url/ for more information."
)

def test_upload_fails_with_wrong_filename(self, pyramid_config, db_request):
@pytest.mark.parametrize(
"filename, project_name",
[
# completely different
("nope-{version}.tar.gz", "something_else"),
("nope-{version}-py3-none-any.whl", "something_else"),
# starts with same prefix
("nope-{version}.tar.gz", "no"),
("nope-{version}-py3-none-any.whl", "no"),
# starts with same prefix with hyphen
("no-way-{version}.tar.gz", "no"),
("no_way-{version}-py3-none-any.whl", "no"),
],
)
def test_upload_fails_with_wrong_filename(
self, pyramid_config, db_request, metrics, filename, project_name
):
user = UserFactory.create()
pyramid_config.testing_securitypolicy(identity=user)
db_request.user = user
db_request.user_agent = "warehouse-tests/6.6.6"
EmailFactory.create(user=user)
project = ProjectFactory.create()
project = ProjectFactory.create(name=project_name)
release = ReleaseFactory.create(project=project, version="1.0")
RoleFactory.create(user=user, project=project)

filename = f"nope-{release.version}.tar.gz"
storage_service = pretend.stub(store=lambda path, filepath, meta: None)
db_request.find_service = lambda svc, name=None, context=None: {
IFileStorage: storage_service,
IMetricsService: metrics,
}.get(svc)

db_request.POST = MultiDict(
{
"metadata_version": "1.2",
"name": project.name,
"version": release.version,
"filetype": "sdist",
"md5_digest": "nope!",
"md5_digest": _TAR_GZ_PKG_MD5,
Copy link
Member

Choose a reason for hiding this comment

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

question: ‏now that we're paramtrizing .whl files in the test case, does the same TAR_GZ behavior name apply to those files as well? Or is this 100% a testing-only behavior and we're unlikely to hit a whl-specific condition on prod? I suspect it's fine, but wanted to ask.

Copy link
Member Author

Choose a reason for hiding this comment

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

The file contents don't really matter here as long as the hashes match, but I agree this could be a little confusing to future travelers.

"content": pretend.stub(
filename=filename,
file=io.BytesIO(b"a" * (legacy.MAX_FILESIZE + 1)),
filename=filename.format(version=release.version),
file=io.BytesIO(_TAR_GZ_PKG_TESTDATA),
type="application/tar",
),
}
)
db_request.help_url = lambda **kw: "/the/help/url/"

with pytest.raises(HTTPBadRequest) as excinfo:
legacy.file_upload(db_request)
Expand Down
14 changes: 12 additions & 2 deletions warehouse/forklift/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,10 +1208,20 @@ def file_upload(request):
# Ensure the filename doesn't contain any characters that are too 🌶️spicy🥵
_validate_filename(filename)

# Extract the project name from the filename and normalize it.
filename_prefix = pkg_resources.safe_name(
Copy link
Member

Choose a reason for hiding this comment

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

question: ‏this is using the deprecated pkg_resources package - should we take the time now to replace, or do you want for forge ahead and let this get bundled into #13991 (or another)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I'd like it to get bunded into the fix for #7811, this is sort of orthogonal.

# For wheels, the project name is normalized and won't contain hyphens, so
# we can split on the first hyphen.
filename.partition("-")[0]
if filename.endswith(".whl")
# For source releases, we know that the version should not contain any
# hypens, so we can split on the last hypen to get the project name.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# hypens, so we can split on the last hypen to get the project name.
# hyphens, so we can split on the last hyphen to get the project name.

else filename.rpartition("-")[0]
).lower()

# Make sure that our filename matches the project that it is being uploaded
# to.
prefix = pkg_resources.safe_name(project.name).lower()
if not pkg_resources.safe_name(filename).lower().startswith(prefix):
if (prefix := pkg_resources.safe_name(project.name).lower()) != filename_prefix:
raise _exc_with_message(
HTTPBadRequest,
f"Start filename for {project.name!r} with {prefix!r}.",
Expand Down