Skip to content

Commit 67eeb63

Browse files
committed
Simplify custom_download_handler requirements
Remove the requirement from custom_download_handler to download a file with exact length and to check for a slow retrieval attack. The needed file verification is already performed by TUF after the file download step. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent ca6f280 commit 67eeb63

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

tuf/client/updater.py

+15-26
Original file line numberDiff line numberDiff line change
@@ -1712,17 +1712,18 @@ def _get_file(self, filepath, verify_file_function, file_type, file_length,
17121712
file_mirror_errors = {}
17131713
file_object = None
17141714

1715-
if custom_download_handler is not None:
1716-
safe_download = custom_download_handler
1717-
1718-
else:
1719-
safe_download = tuf.download.safe_download
1720-
17211715
for file_mirror in file_mirrors:
17221716
try:
1723-
# Eensure the length of the downloaded file matches 'file_length'
1724-
# exactly.
1725-
file_object = safe_download(file_mirror, file_length)
1717+
if custom_download_handler is not None:
1718+
# When an external download handler is used, file length verification
1719+
# is not expected. It is performed by verify_file_function()
1720+
file_object = custom_download_handler(file_mirror)
1721+
1722+
else:
1723+
# Ensure the length of the downloaded file matches 'file_length'
1724+
# exactly even though it will be redundantly verified one more time
1725+
# by verify_file_function().
1726+
file_object = tuf.download.safe_download(file_mirror, file_length)
17261727

17271728
# Verify 'file_object' according to the callable function.
17281729
# 'file_object' is also verified if decompressed above (i.e., the
@@ -3252,33 +3253,21 @@ def download_target(self, target, destination_directory,
32523253
In order to comply with the TUF specification, the function implementation
32533254
should match the following description:
32543255
3255-
def download_handler_func(url, required_length)
3256+
def download_handler_func(url)
32563257
<Purpose>
3257-
Given the 'url' and 'required_length' of the desired file, open a connection
3258-
to 'url', download it, and return the contents of the file. Also ensure
3259-
the length of the downloaded file matches 'required_length' exactly.
3258+
Given the 'url' of the desired file,
3259+
open a connection to 'url', download it, and return the contents
3260+
of the file.
32603261
32613262
<Arguments>
32623263
url:
32633264
A URL string that represents the location of the file.
32643265
3265-
required_length:
3266-
An integer value representing the length of the file. This is an exact
3267-
limit.
3268-
32693266
<Side Effects>
32703267
A temprorary file object is created to store the contents of 'url'.
32713268
3272-
<Exceptions>
3273-
DownloadLengthMismatchError, if there was a
3274-
mismatch of observed vs expected lengths while downloading the file.
3275-
3276-
SlowRetrievalError, if the total downloaded was
3277-
done in less than the acceptable download speed (as set in
3278-
tuf.settings.py).
3279-
32803269
<Returns>
3281-
A temporay file object that points to the contents of 'url'.
3270+
A temporary file object that points to the contents of 'url'.
32823271
32833272
If None, tuf.download.safe_download is used.
32843273

0 commit comments

Comments
 (0)