@@ -255,13 +255,36 @@ def main(argv: list[str] | None = None) -> int:
255255 args_i2 .registry .rstrip ("/" )
256256 + f"/resolve?name={ name } &version={ ver or '*' } "
257257 )
258- with _u .urlopen (meta_url ) as r :
258+ headers = {}
259+ token = os .environ .get ("REGISTRY_TOKEN" )
260+ if token :
261+ headers ["Authorization" ] = f"Bearer { token } "
262+ req = _u .Request (meta_url , headers = headers )
263+ with _u .urlopen (req ) as r :
259264 if r .getcode () // 100 != 2 :
260265 raise RuntimeError ("Registry resolve failed" )
261266 meta = _json .loads (r .read ().decode ("utf-8" ))
262267 src_code = meta .get ("source" , "" )
268+ digest = meta .get ("sha256" )
263269 if not src_code :
264270 raise RuntimeError ("Registry returned empty source" )
271+ # Verify integrity if digest present
272+ if digest :
273+ import hashlib as _hh
274+
275+ h = _hh .sha256 (src_code .encode ("utf-8" )).hexdigest ()
276+ if h != digest :
277+ raise RuntimeError ("Integrity check failed: sha256 mismatch" )
278+ # Optional HMAC verification if shared secret configured
279+ hmac_given = meta .get ("hmac" )
280+ hmac_secret = os .environ .get ("SUP_REGISTRY_HMAC" )
281+ if hmac_given and hmac_secret :
282+ import hmac as _h
283+ import hashlib as _hh2
284+
285+ calc = _h .new (hmac_secret .encode ("utf-8" ), src_code .encode ("utf-8" ), _hh2 .sha256 ).hexdigest ()
286+ if calc != hmac_given :
287+ raise RuntimeError ("Integrity check failed: hmac mismatch" )
265288 else :
266289 reg_dir = os .path .abspath (args_i2 .registry )
267290 cand = os .path .join (reg_dir , f"{ name } .sup" )
@@ -409,9 +432,23 @@ def main(argv: list[str] | None = None) -> int:
409432 body = json .dumps (
410433 {"name" : name , "version" : version , "sha256" : digest }
411434 ).encode ("utf-8" )
412- req = _u .Request (
413- url , data = body , headers = {"Content-Type" : "application/json" }
414- )
435+ headers = {"Content-Type" : "application/json" }
436+ token = os .environ .get ("REGISTRY_TOKEN" )
437+ if token :
438+ headers ["Authorization" ] = f"Bearer { token } "
439+ # Add optional HMAC of tarball for verification
440+ hmac_secret = os .environ .get ("SUP_REGISTRY_HMAC" )
441+ if hmac_secret :
442+ import hmac as _h
443+ import hashlib as _hh2
444+
445+ with open (tar_path , "rb" ) as rf :
446+ tar_bytes = rf .read ()
447+ hmac_hex = _h .new (hmac_secret .encode ("utf-8" ), tar_bytes , _hh2 .sha256 ).hexdigest ()
448+ payload = json .loads (body .decode ("utf-8" ))
449+ payload ["hmac" ] = hmac_hex
450+ body = json .dumps (payload ).encode ("utf-8" )
451+ req = _u .Request (url , data = body , headers = headers )
415452 with _u .urlopen (req ) as r :
416453 if r .getcode () // 100 != 2 :
417454 raise RuntimeError ("Registry upload failed" )
0 commit comments