61 lines
2.5 KiB
Python
61 lines
2.5 KiB
Python
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
spec = importlib.util.spec_from_file_location('audit', ROOT / 'scripts/audit_repository.py')
|
|
audit = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(audit)
|
|
|
|
|
|
class RepositorySafetyTests(unittest.TestCase):
|
|
def test_repository_guardrails(self):
|
|
self.assertEqual(audit.audit(), [])
|
|
|
|
def test_private_addresses_rejected(self):
|
|
private = '.'.join(('192', '168', '23', '8'))
|
|
self.assertIn('private IP address included', audit.findings('config.json', private))
|
|
|
|
def test_private_config_rejected_even_when_forced_into_git(self):
|
|
self.assertTrue(audit.findings('.local/config.json', '{}'))
|
|
self.assertTrue(audit.findings('.env.production', ''))
|
|
|
|
def test_embedded_url_credentials_rejected(self):
|
|
self.assertIn('URL credentials included', audit.findings('settings', 'https://' + 'user:secret' + '@example.test'))
|
|
|
|
def test_key_material_rejected(self):
|
|
sample = '-----BEGIN ' + 'PRIVATE KEY-----'
|
|
self.assertIn('private key material included', audit.findings('bad.txt', sample))
|
|
|
|
def test_documentation_versions_are_not_private_addresses(self):
|
|
self.assertEqual(audit.findings('versions', '14.371 2.10.6 5.0.0'), [])
|
|
|
|
def test_private_paths_are_ignored(self):
|
|
paths = ['.local/environment.json', '.env', '.env.production', 'service.key']
|
|
for path in paths:
|
|
result = subprocess.run(['git', 'check-ignore', '-q', path], cwd=ROOT)
|
|
self.assertEqual(result.returncode, 0, path)
|
|
|
|
def test_provenance_pins_have_hashes(self):
|
|
data = json.loads((ROOT / 'evidence/upstream.json').read_text())
|
|
self.assertEqual(len(data['git_sources']), 5)
|
|
for source in data['git_sources']:
|
|
self.assertRegex(source['commit'], r'^[a-f0-9]{40}$')
|
|
self.assertTrue(source['files'])
|
|
for digest in source['files'].values():
|
|
self.assertRegex(digest, r'^[a-f0-9]{64}$')
|
|
self.assertEqual(data['pypi_sources'][0]['version'], '9.1.1')
|
|
|
|
def test_network_evidence_is_unauthenticated(self):
|
|
data = json.loads((ROOT / 'evidence/connectivity.json').read_text())
|
|
self.assertFalse(data['authenticated'])
|
|
self.assertEqual([r['path'] for r in data['results']], ['/', '/info'])
|
|
for result in data['results']:
|
|
self.assertEqual(result['status'], 200)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|