Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/test_source_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,43 @@ def test_public_git_source_ref_rejects_sandbox_and_noncanonical_repo(self):
with self.assertRaises(ContractError): validate_source_ref(git_ref(repository=repo))
def test_source_ref_rejects_case_variant_sandbox_path(self):
with self.assertRaisesRegex(ContractError,'sandbox'): validate_source_ref(git_ref(path='Sandbox/private.txt'))
def test_git_source_ref_rejects_nul_at_each_position(self):
for path in ('\x00docs/proof.md', 'docs/\x00proof.md', 'docs/proof.md\x00'):
with self.subTest(path=path):
row = git_ref(path=path)
before = json.loads(json.dumps(row))
with self.assertRaisesRegex(ContractError, '^unsafe path$'):
validate_source_ref(row)
self.assertEqual(row, before)

def test_source_manifest_rejects_nul_at_each_position(self):
for path in ('\x00docs/proof.md', 'docs/\x00proof.md', 'docs/proof.md\x00'):
with self.subTest(path=path):
row = {'schema_version':'1.0', 'distribution':'synthetic', 'version':'0.1.0',
'repository':'d6g8k5htny-coder/Math-', 'commit':'1'*40,
'scientific_status_authority':False,
'files':[{'path':path, 'sha256':'2'*64, 'bytes':0}]}
before = json.loads(json.dumps(row))
with self.assertRaisesRegex(ContractError, '^unsafe path$'):
validate_source_manifest(row)
self.assertEqual(row, before)

def test_source_paths_preserve_ordinary_names_and_inputs(self):
for path in ('docs/proof.md', 'docs/proof notes.md', 'docs/\u03b1.md'):
reference = git_ref(path=path)
manifest = {'schema_version':'1.0', 'distribution':'synthetic', 'version':'0.1.0',
'repository':'d6g8k5htny-coder/Math-', 'commit':'1'*40,
'scientific_status_authority':False,
'files':[{'path':path, 'sha256':'2'*64, 'bytes':0}]}
for validator, row in ((validate_source_ref, reference),
(validate_source_manifest, manifest)):
with self.subTest(path=path, validator=validator.__name__):
before = json.loads(json.dumps(row))
result = validator(row)
self.assertEqual(result, before)
self.assertIsNot(result, row)
self.assertEqual(row, before)

def test_external_frozen_source_ref(self):
row={'schema_version':'1.0','kind':'external','provider':'google-drive','source_id':'abc','sha256':'4'*64,'size_bytes':12,'monitorability':'external-frozen','role':'historical-source','visibility':'public'}
self.assertEqual(validate_source_ref(row)['provider'],'google-drive')
Expand Down
2 changes: 1 addition & 1 deletion tools/contract_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _copy(data: Any) -> dict:
return dict(data)

def _safe_path(text: Any) -> str:
if not isinstance(text,str) or not text or '\\' in text or ':' in text: raise ContractError('unsafe path')
if not isinstance(text,str) or not text or '\\' in text or ':' in text or '\x00' in text: raise ContractError('unsafe path')
p=PurePosixPath(text)
if p.is_absolute() or '..' in p.parts or str(p)!=text: raise ContractError('unsafe path')
if any(part.casefold()=='sandbox' for part in p.parts): raise ContractError('private sandbox path refused')
Expand Down
Loading