Skip to content

Commit 99f5cd7

Browse files
committed
fix(crypto): take source URI type and revision into hashing
Hashing only the URL part will lead to dirty caches.
1 parent 1bbc714 commit 99f5cd7

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

acbs/crypto.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ def check_hash_hashlib_inner(chksum_type: str, target_file: str) -> Optional[str
1818
return target_hash
1919

2020

21-
def hash_url(url: str) -> str:
22-
hash_obj = hashlib.new('sha256')
23-
hash_obj.update(url.encode('utf-8'))
24-
return hash_obj.hexdigest()
25-
26-
2721
def check_hash_hashlib(chksum_tuple: Tuple[str, str], target_file: str) -> None:
2822
hash_type, hash_value = chksum_tuple
2923
hash_type = hash_type.lower()

acbs/fetch.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import hashlib
12
import http.client
23
import logging
34
import os
@@ -9,7 +10,7 @@
910
from urllib.parse import urlparse
1011

1112
from acbs.base import ACBSPackageInfo, ACBSSourceInfo
12-
from acbs.crypto import check_hash_hashlib, hash_url
13+
from acbs.crypto import check_hash_hashlib
1314
from acbs.utils import guess_extension_name
1415

1516
fetcher_signature = Callable[[ACBSSourceInfo,
@@ -28,8 +29,8 @@ def fetch_source(info: List[ACBSSourceInfo], source_location: str, package_name:
2829
# in generate mode, we need to fetch all the sources
2930
if not i.enabled and not generate_mode:
3031
logging.info(f'Source {count} skipped.')
31-
url_hash = hash_url(i.url)
32-
fetch_source_inner(i, source_location, url_hash)
32+
uri_hash = hash_source_uri(i)
33+
fetch_source_inner(i, source_location, uri_hash)
3334
return None
3435

3536

@@ -64,13 +65,21 @@ def process_source(info: ACBSPackageInfo, source_name: str) -> None:
6465
return
6566

6667

68+
def hash_source_uri(uri: ACBSSourceInfo) -> str:
69+
hash_obj = hashlib.new("sha256")
70+
hash_obj.update(uri.type.encode("utf-8"))
71+
hash_obj.update(uri.url.encode("utf-8"))
72+
hash_obj.update(uri.revision.encode("utf-8") if uri.revision else b'NONE')
73+
hash_obj.update(uri.branch.encode("utf-8") if uri.branch else b"NONE")
74+
return hash_obj.hexdigest()
75+
76+
6777
# Fetchers implementations
6878
def tarball_fetch(info: ACBSSourceInfo, source_location: str, name: str) -> Optional[ACBSSourceInfo]:
6979
if source_location:
70-
filename = hash_url(info.url)
7180
if not info.chksum[1] and not generate_mode:
7281
raise ValueError('No checksum found. Please specify the checksum!')
73-
full_path = os.path.join(source_location, filename)
82+
full_path = os.path.join(source_location, name)
7483
try:
7584
wget_download(info.url, full_path)
7685
info.source_location = full_path

tests/test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
import unittest.mock
33

4+
import acbs.fetch
45
import acbs.find
56
import acbs.parser
67
import acbs.pm
@@ -157,6 +158,28 @@ def test_guess_extension_name(self):
157158
self.assertEqual(guess_extension_name('test-1.2.3.bin'), '.bin')
158159
self.assertEqual(guess_extension_name('test'), '')
159160

161+
def test_source_uri_hash(self):
162+
self.assertNotEqual(
163+
acbs.fetch.hash_source_uri(
164+
acbs.parser.parse_url_schema('pypi::version=1.0.0::acbs', 'SKIP')
165+
),
166+
acbs.fetch.hash_source_uri(
167+
acbs.parser.parse_url_schema('pypi::version=1.0.1::acbs', 'SKIP')
168+
),
169+
)
170+
self.assertNotEqual(
171+
acbs.fetch.hash_source_uri(
172+
acbs.parser.parse_url_schema(
173+
'tbl::https://github.com/AOSC-Dev/aosc-os-abbs', 'SKIP'
174+
)
175+
),
176+
acbs.fetch.hash_source_uri(
177+
acbs.parser.parse_url_schema(
178+
'git::https://github.com/AOSC-Dev/aosc-os-abbs', 'SKIP'
179+
)
180+
),
181+
)
182+
160183

161184
if __name__ == '__main__':
162185
unittest.main()

0 commit comments

Comments
 (0)