Skip to content

Commit

Permalink
Merge pull request #276 from vladimir-v-diaz/supported-uri-schemes
Browse files Browse the repository at this point in the history
Add Configuration Option for Supported URI Schemes
  • Loading branch information
vladimir-v-diaz committed May 4, 2015
2 parents fb7dfaa + 16685d2 commit fccce96
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
12 changes: 10 additions & 2 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,12 @@ def test_download_url_to_tempfileobj_and_performance(self):
def test_download_url_to_tempfileobj_and_urls(self):

download_file = download.safe_download
unsafe_download_file = download.unsafe_download

self.assertRaises(tuf.FormatError,
download_file, None, self.target_data_length)

self.assertRaises(ValueError,
self.assertRaises(tuf.FormatError,
download_file,
self.random_string(), self.target_data_length)

Expand All @@ -177,8 +178,15 @@ def test_download_url_to_tempfileobj_and_urls(self):
download_file,
'http://localhost:' + str(self.PORT+1) + '/' + self.random_string(),
self.target_data_length)


# Specify an unsupported URI scheme.
url_with_unsupported_uri = self.url.replace('http', 'file')
self.assertRaises(tuf.FormatError, download_file, url_with_unsupported_uri,
self.target_data_length)
self.assertRaises(tuf.FormatError, unsafe_download_file,
url_with_unsupported_uri, self.target_data_length)



def test__get_opener(self):
# Test normal case.
Expand Down
7 changes: 7 additions & 0 deletions tuf/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,10 @@
# to generate the digests listed in metadata and prepended to the filenames of
# consistent snapshots.
REPOSITORY_HASH_ALGORITHMS = ['sha256']

# Software updaters that integrate the framework are required to specify
# the URL prefix for the mirrors that clients can contact to download updates.
# The following URI schemes are those that download.py support. By default,
# the ['http', 'https'] URI schemes are supported, but may be modified by
# integrators to schemes that they wish to support for their integration.
SUPPORTED_URI_SCHEMES = ['http', 'https']
44 changes: 42 additions & 2 deletions tuf/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def safe_download(url, required_length):
<Arguments>
url:
A URL string that represents the location of the file.
A URL string that represents the location of the file. The URI scheme
component must be one of 'tuf.conf.SUPPORTED_URI_SCHEMES'.
required_length:
An integer value representing the length of the file. This is an exact
Expand All @@ -92,6 +93,25 @@ def safe_download(url, required_length):
A 'tuf.util.TempFile' file-like object that points to the contents of 'url'.
"""

# Do all of the arguments have the appropriate format?
# Raise 'tuf.FormatError' if there is a mismatch.
tuf.formats.URL_SCHEMA.check_match(url)
tuf.formats.LENGTH_SCHEMA.check_match(required_length)

# Ensure 'url' specifies one of the URI schemes in
# 'tuf.conf.SUPPORTED_URI_SCHEMES'. Be default, ['http', 'https'] is
# supported. If the URI scheme of 'url' is empty or "file", files on the
# local system can be accessed. Unexpected files may be accessed by
# compromised metadata (unlikely to happen if targets.json metadata is signed
# with offline keys).
parsed_url = six.moves.urllib.parse.urlparse(url)

if parsed_url.scheme not in tuf.conf.SUPPORTED_URI_SCHEMES:
message = \
repr(url) + ' specifies an unsupported URI scheme. Supported ' + \
' URI Schemes: ' + repr(tuf.conf.SUPPORTED_URI_SCHEMES)
raise tuf.FormatError(message)

return _download_file(url, required_length, STRICT_REQUIRED_LENGTH=True)


Expand All @@ -114,7 +134,8 @@ def unsafe_download(url, required_length):
<Arguments>
url:
A URL string that represents the location of the file.
A URL string that represents the location of the file. The URI scheme
component must be one of 'tuf.conf.SUPPORTED_URI_SCHEMES'.
required_length:
An integer value representing the length of the file. This is an upper
Expand All @@ -136,6 +157,25 @@ def unsafe_download(url, required_length):
A 'tuf.util.TempFile' file-like object that points to the contents of 'url'.
"""

# Do all of the arguments have the appropriate format?
# Raise 'tuf.FormatError' if there is a mismatch.
tuf.formats.URL_SCHEMA.check_match(url)
tuf.formats.LENGTH_SCHEMA.check_match(required_length)

# Ensure 'url' specifies one of the URI schemes in
# 'tuf.conf.SUPPORTED_URI_SCHEMES'. Be default, ['http', 'https'] is
# supported. If the URI scheme of 'url' is empty or "file", files on the
# local system can be accessed. Unexpected files may be accessed by
# compromised metadata (unlikely to happen if targets.json metadata is signed
# with offline keys).
parsed_url = six.moves.urllib.parse.urlparse(url)

if parsed_url.scheme not in tuf.conf.SUPPORTED_URI_SCHEMES:
message = \
repr(url) + ' specifies an unsupported URI scheme. Supported ' + \
' URI Schemes: ' + repr(tuf.conf.SUPPORTED_URI_SCHEMES)
raise tuf.FormatError(message)

return _download_file(url, required_length, STRICT_REQUIRED_LENGTH=False)


Expand Down

0 comments on commit fccce96

Please sign in to comment.