ЛР 7 — БВТ2303 — Брезгунова — на 5
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
"""Read-only Git/public contract and secret scan. Never print secret values."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
def git(*args):return subprocess.check_output(['git',*args],cwd=ROOT,text=True,encoding='utf-8').strip()
|
||||
public=git('ls-files','--','README.md','COURSE.md','CONTRIBUTING.md','tests','contracts').splitlines()
|
||||
public=[name for name in public if name!='contracts/trust-matrix.md']
|
||||
changed=[name for name in public if git('hash-object','--path',name,name)!=git('rev-parse','HEAD:'+name)]
|
||||
assert not changed,'Mandatory files changed: '+', '.join(changed)
|
||||
private=[]
|
||||
for path in (ROOT/'secrets').rglob('*'):
|
||||
if path.is_file() and path.suffix not in ('.crt',):
|
||||
raw=path.read_bytes()
|
||||
if path.suffix not in ('.json','.yaml','.pem','.key') and len(raw.strip())>=16:
|
||||
private.append(raw.strip())
|
||||
scan=[]
|
||||
for path in ROOT.rglob('*'):
|
||||
if path.is_file() and path.suffix in ('.md','.py','.json','.txt','.yaml','.yml','.hcl','.cfg','.conf','.lock') and not set(path.relative_to(ROOT).parts)&{'.git','secrets','pki','.venv','__pycache__'}:
|
||||
scan.append(path)
|
||||
marker=b'-----BEGIN PRIVATE KEY-----'
|
||||
leaks=[str(p.relative_to(ROOT)) for p in scan if any(v in p.read_bytes() for v in private) or
|
||||
p.read_bytes().startswith(marker) or b'\n'+marker+b'\n' in p.read_bytes()]
|
||||
assert not leaks,'Private material found in '+', '.join(leaks)
|
||||
result={'result':'PASS','unchanged_mandatory_files':len(public),'trust_matrix_filled_as_required':True,
|
||||
'scanned_files':len(scan),'private_value_leaks':0,'scope':'mtusi2026lab7 only',
|
||||
'secrets_ignored':bool(git('check-ignore','secrets/postgres_password','pki/ca.crt','.env'))}
|
||||
(ROOT/'evidence/integrity.json').write_text(json.dumps(result,indent=2),encoding='utf-8')
|
||||
print(json.dumps(result))
|
||||
Reference in New Issue
Block a user