File size: 3,874 Bytes
0e239c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import hashlib
import io
import json
import os
from pathlib import Path
import tarfile
import tempfile
import unittest
from materialize_agent_context import materialize


def fixture(root,links=(),parts=False):
    archive=root/'context.tar.gz';entries=[]
    with tarfile.open(archive,'w:gz') as tar:
        body=b'original upstream instructions\n';name='testbed/repo/AGENTS.md'
        info=tarfile.TarInfo(name);info.size=len(body);info.mode=0o644;tar.addfile(info,io.BytesIO(body))
        entries.append({'path':name,'bytes':len(body),'sha256':hashlib.sha256(body).hexdigest(),'mode':0o644})
        for name,target in links:
            info=tarfile.TarInfo(name);info.type=tarfile.SYMTYPE;info.linkname=target;info.mode=0o777;tar.addfile(info)
            data=target.encode();entries.append({'path':name,'kind':'symlink','target':target,'bytes':len(data),'sha256':hashlib.sha256(data).hexdigest(),'mode':0o777})
    raw=archive.read_bytes();reference={'path':archive.name,'bytes':len(raw),'sha256':hashlib.sha256(raw).hexdigest()}
    if parts:
        offset=0;reference['parts']=[]
        for index,block in enumerate([raw[:len(raw)//2],raw[len(raw)//2:]]):
            path=root/f'context.tar.gz.part-{index:04d}';path.write_bytes(block)
            reference['parts'].append({'path':path.name,'offset':offset,'bytes':len(block),'sha256':hashlib.sha256(block).hexdigest()});offset+=len(block)
    manifest=root/'context-manifest.json';manifest.write_text(json.dumps({'archive':reference,'files':entries}))
    return manifest


class ContextV2Tests(unittest.TestCase):
    def test_internal_upstream_link_remains_a_link(self):
        with tempfile.TemporaryDirectory() as temporary:
            root=Path(temporary);manifest=fixture(root,[('testbed/repo/CLAUDE.md','AGENTS.md')])
            try:materialize(manifest,root/'out')
            except ValueError as error:self.fail('Valid original source symlink rejected: '+str(error))
            path=root/'out/testbed/repo/CLAUDE.md'
            self.assertTrue(path.is_symlink());self.assertEqual(os.readlink(path),'AGENTS.md')

    def test_escape_link_is_rejected(self):
        with tempfile.TemporaryDirectory() as temporary:
            root=Path(temporary);manifest=fixture(root,[('escape','../outside')])
            with self.assertRaises(ValueError):materialize(manifest,root/'out')

    def test_indirect_escape_via_another_link_is_rejected(self):
        with tempfile.TemporaryDirectory() as temporary:
            root=Path(temporary);manifest=fixture(root,[('d1/a','..'),('d2/b','../d1/a/../outside')])
            with self.assertRaises(ValueError):materialize(manifest,root/'out')
            self.assertFalse((root/'out').exists())

    def test_archive_cannot_write_through_a_link_parent(self):
        with tempfile.TemporaryDirectory() as temporary:
            root=Path(temporary);manifest=fixture(root,[('testbed','inside')])
            with self.assertRaises(ValueError):materialize(manifest,root/'out')

    def test_parts_are_sufficient_without_full_archive(self):
        with tempfile.TemporaryDirectory() as temporary:
            root=Path(temporary);manifest=fixture(root,parts=True);(root/'context.tar.gz').unlink()
            try:materialize(manifest,root/'out')
            except (OSError,ValueError) as error:self.fail('Closed archive parts were not restored: '+str(error))
            self.assertTrue((root/'out/testbed/repo/AGENTS.md').is_file())

    def test_part_tampering_cannot_be_ignored_when_full_archive_exists(self):
        with tempfile.TemporaryDirectory() as temporary:
            root=Path(temporary);manifest=fixture(root,parts=True)
            with (root/'context.tar.gz.part-0000').open('ab') as stream:stream.write(b'changed')
            with self.assertRaises(ValueError):materialize(manifest,root/'out')


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