Skip to content

ENT-12997: Strict checksums and insecure flag #141

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 3 commits into from

Conversation

luterlassus
Copy link

No description provided.

@larsewi larsewi requested review from victormlg and jakub-nt June 11, 2025 11:48
…y. Seperated lockfiles to other tempfiles. Wrong checksum on pre-existing file now triggers warning, deletion of pre-existing file and re-download rather than raising of error.
…dded flags for supplying checksums for installing packages and masterfiles specified with url. All calls for download_package now allows for specifying checksums, and ignoring them
Copy link
Contributor

@larsewi larsewi left a comment

Choose a reason for hiding this comment

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

Great work so far 😄 Added some comments for you to address


with open(filepath, "rb") as f:
content = f.read()
return is_different_checksum(checksum, content)
Copy link
Contributor

Choose a reason for hiding this comment

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

No big deal. But this return statement does not have to be indented

mkdir(tempfolder)
# Check if other process is handeling this - lockfile
lockfile = os.path.join(tempfolder, f"{filename}.lock")
with open(lockfile, "w") as lf:
Copy link
Contributor

Choose a reason for hiding this comment

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

Opening the file with in "w" mode may truncate the file while another process is writing to it.

)

# Copy over the tempfile and remove it
copy_file(tempfilename, path)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would just use rename, because it is atomic. The copy_file function introduces a new race condition where two processes may be writing to ".{}.tmp".format(filename) at the same time (see CFE-4549). Furthermore, there is no need to copy the file, because we don't want to keep the original.

log.warning("Pre-existing file did not match existing checksum. File is now deleted.")
else:
# Checksum was correct, skip download
return path
Copy link
Contributor

Choose a reason for hiding this comment

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

Should probably release the lock before returning

cf_remote/web.py Outdated
tempfilename, header = urllib.request.urlretrieve(url)
if file_has_different_checksum(tempfilename, checksum):
urllib.request.urlcleanup()
raise ChecksumError(
Copy link
Contributor

Choose a reason for hiding this comment

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

Should probably release the lock here as well

Comment on lines +124 to +135
# Find the checksum
name = os.path.basename(package_url)
checksum = None
if package_url in package_checksums:
# Checksum provided for the specified url
checksum = package_checksums[package_url]
elif name in package_checksums:
# Checksum provided for the specified name
checksum = package_checksums[name]
elif "any" in package_checksums:
# Single checksum or no checksum privided
checksum = package_checksums["any"]
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be in a separate function, and you could add some unit tests testing different checksum files

return SHA256_RE.match(checksum)


def parse_checksums_txt(checksums):
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add some unit tests for this function

Comment on lines 53 to 61
tempfolder = os.path.join(tempfile.gettempdir(), "cf-engine")
tempfolder = os.path.join(tempfile.gettempdir(), "cf-remote")
mkdir(tempfolder)
# Check if other process is handeling this - lockfile
# Wait for other process if lockfile is taken. Otherwise lock it
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like these changes belong in one of the previous commits

Comment on lines 84 to 85
# Unlock the lockfile
fcntl.flock(lf.fileno(), fcntl.LOCK_UN)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why remove this part?

@olehermanse
Copy link
Member

Closing until August.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants