File size: 4,015 Bytes
a25598f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Safely restore the simulator archive, retaining existing identical files."""
import hashlib
import json
import os
from pathlib import Path, PurePosixPath
import tarfile

ROOT = Path(__file__).resolve().parents[1]


def sha256(path):
    value = hashlib.sha256()
    with path.open("rb") as stream:
        for chunk in iter(lambda: stream.read(8 * 1024 * 1024), b""):
            value.update(chunk)
    return value.hexdigest()


def safe_relative(name):
    path = PurePosixPath(name)
    if path.is_absolute() or ".." in path.parts or not path.parts:
        raise ValueError(f"Unsafe archive path: {name}")
    if path.as_posix() != name:
        raise ValueError(f"Non-canonical archive path: {name}")
    if path.parts[0] not in ("third_party", "runtime_assets"):
        raise ValueError(f"Unexpected archive root: {name}")
    return path


def resource_path(root, name):
    relative = safe_relative(name)
    current = root.resolve()
    for part in relative.parts:
        current = current / part
        if current.is_symlink():
            raise ValueError(f"Symlink in resource path: {name}")
    return current


def unpack(root=ROOT):
    inventory = json.loads((root / "environment/resource_manifest.json").read_text())
    archive = root / "archives/simulation_assets.tar.gz"
    expected = inventory["files"]
    if archive.exists():
        if sha256(archive) != inventory["archive_sha256"]:
            raise ValueError("Simulator archive SHA-256 mismatch.")
        seen = set()
        with tarfile.open(archive, "r:gz") as source:
            for member in source:
                relative = safe_relative(member.name)
                destination = resource_path(root, member.name)
                if member.isdir():
                    continue
                if not member.isfile() or member.name not in expected or member.name in seen:
                    raise ValueError(f"Unexpected or duplicate archive entry: {member.name}")
                seen.add(member.name)
                # Resolve before creating anything; pre-existing symlinks must not redirect writes.
                if root.resolve() not in destination.resolve().parents or destination.is_symlink():
                    raise ValueError(f"Unsafe extraction target: {member.name}")
                if destination.exists():
                    if sha256(destination) != expected[member.name]:
                        raise ValueError(f"Refusing to overwrite modified resource: {member.name}")
                    continue
                destination.parent.mkdir(parents=True, exist_ok=True)
                data = source.extractfile(member)
                temp = destination.with_name(destination.name + ".extracting")
                value = hashlib.sha256()
                created = False
                try:
                    with temp.open("xb") as output:
                        created = True
                        for chunk in iter(lambda: data.read(8 * 1024 * 1024), b""):
                            value.update(chunk)
                            output.write(chunk)
                    if value.hexdigest() != expected[member.name]:
                        raise ValueError(f"Resource hash mismatch: {member.name}")
                    temp.chmod(member.mode & 0o777)
                    # Atomic no-clobber installation if another process created the target.
                    os.link(temp, destination)
                finally:
                    if created and temp.exists():
                        temp.unlink()
        if seen != set(expected):
            raise ValueError("Simulator archive is incomplete.")
    for name, expected_hash in expected.items():
        path = resource_path(root, name)
        if root.resolve() not in path.resolve().parents or path.is_symlink() or sha256(path) != expected_hash:
            raise ValueError(f"Resource verification failed: {name}")
    print(f"Verified {len(expected)} simulator resource files.")


if __name__ == "__main__":
    unpack()