Skip to content

Check if another file has been uploaded with the same blake2 digest #2928

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

Closed
wants to merge 1 commit into from
Closed
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
50 changes: 50 additions & 0 deletions tests/unit/forklift/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,56 @@ def test_upload_fails_with_existing_filename_diff_content(self,
assert resp.status_code == 400
assert resp.status == "400 File already exists."

def test_upload_fails_with_diff_filename_same_blake2(self,
pyramid_config,
db_request):
pyramid_config.testing_securitypolicy(userid=1)

user = UserFactory.create()
project = ProjectFactory.create()
release = ReleaseFactory.create(project=project, version="1.0")
RoleFactory.create(user=user, project=project)

filename = "{}-{}.tar.gz".format(project.name, release.version)
file_content = io.BytesIO(b"A fake file.")

db_request.POST = MultiDict({
"metadata_version": "1.2",
"name": project.name,
"version": release.version,
"filetype": "sdist",
"md5_digest": hashlib.md5(file_content.getvalue()).hexdigest(),
"content": pretend.stub(
filename="{}-fake.tar.gz".format(project.name),
file=file_content,
type="application/tar",
),
})

db_request.db.add(
File(
release=release,
filename=filename,
md5_digest=hashlib.md5(file_content.getvalue()).hexdigest(),
sha256_digest=hashlib.sha256(
file_content.getvalue()
).hexdigest(),
blake2_256_digest=hashlib.blake2b(
file_content.getvalue(),
digest_size=256 // 8
).hexdigest(),
path="source/{name[0]}/{name}/{filename}".format(
name=project.name,
filename=filename,
),
),
)

resp = legacy.file_upload(db_request)

assert resp.status_code == 400
assert resp.status == "400 File already exists."

def test_upload_fails_with_wrong_filename(self, pyramid_config,
db_request):
pyramid_config.testing_securitypolicy(userid=1)
Expand Down
23 changes: 20 additions & 3 deletions warehouse/forklift/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,19 @@ def _is_valid_dist_file(filename, filetype):
return True


def _blake2_matches(db_session, blake2_hash):
"""
Check to see if a file with the same blake2 hash already exists
"""
file_ = (
db_session.query(File)
.filter(File.blake2_256_digest == blake2_hash)
.first()
)

return file_ is not None


def _is_duplicate_file(db_session, filename, hashes):
"""
Check to see if file already exists, and if it's content matches
Expand All @@ -631,9 +644,13 @@ def _is_duplicate_file(db_session, filename, hashes):
)

if file_ is not None:
return (file_.sha256_digest == hashes["sha256"] and
file_.md5_digest == hashes["md5"] and
file_.blake2_256_digest == hashes["blake2_256"])
content_matches = (
file_.sha256_digest == hashes["sha256"] and
file_.md5_digest == hashes["md5"] and
file_.blake2_256_digest == hashes["blake2_256"]
)
return (content_matches or
_blake2_matches(db_session, hashes["blake2_256"]))

return None

Expand Down