@@ -5,34 +5,97 @@ mode="${1:?mode is required}"
55sticky_root=" ${2:? sticky root is required} "
66workspace=" ${3:? workspace is required} "
77archive=" $sticky_root /importer-node-modules.tar"
8+ archive_checksum=" $sticky_root /.openclaw-importer-archive.sha256"
9+ importer_manifest=" $sticky_root /importer-node-modules.manifest"
810marker=" $sticky_root /.openclaw-deps-fingerprint"
11+ script_dir=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
12+
13+ archive_sha256 () {
14+ if command -v sha256sum > /dev/null 2>&1 ; then
15+ sha256sum -- " $1 " | awk ' {print $1}'
16+ else
17+ shasum -a 256 " $1 " | awk ' {print $1}'
18+ fi
19+ }
20+
21+ clear_importers () {
22+ (
23+ cd " $workspace "
24+ find . \( -type d -o -type l \) -name node_modules -prune \
25+ ! -path ./node_modules -exec rm -rf -- {} +
26+ )
27+ }
28+
29+ verify_importers () {
30+ node " $script_dir /verify-importers.mjs" " $workspace " " $1 "
31+ }
932
1033case " $mode " in
1134 capture)
1235 fingerprint=" ${4:? fingerprint is required} "
1336 mkdir -p " $sticky_root "
1437 list_file=" $( mktemp) "
1538 temp_archive=" $archive .tmp.$$ "
39+ temp_checksum=" $archive_checksum .tmp.$$ "
40+ temp_manifest=" $importer_manifest .tmp.$$ "
41+ temp_marker=" $marker .tmp.$$ "
1642 cleanup () {
17- rm -f " $list_file " " $temp_archive "
43+ rm -f " $list_file " " $temp_archive " " $temp_checksum " " $temp_manifest " " $temp_marker "
1844 }
1945 trap cleanup EXIT
46+ # Do not publish a fingerprint for an install whose importer resolution is
47+ # already falling through to a wrong hoisted version.
48+ rm -f " $marker " " $archive_checksum " " $importer_manifest "
2049 (
2150 cd " $workspace "
22- find . -type d -name node_modules -prune ! -path ./node_modules -print0 > " $list_file "
51+ find . \( -type d -o -type l \) -name node_modules -prune \
52+ ! -path ./node_modules -print0 > " $list_file "
2353 tar --create --file " $temp_archive " --null --files-from " $list_file "
2454 )
55+ tr ' \0' ' \n' < " $list_file " > " $temp_manifest "
56+ # Record the exact importer set and check its live resolution before a
57+ # writer can publish the marker.
58+ verify_importers " $temp_manifest "
59+ {
60+ archive_sha256 " $temp_archive "
61+ archive_sha256 " $temp_manifest "
62+ } > " $temp_checksum "
2563 mv " $temp_archive " " $archive "
26- # The marker lands last: a fingerprint is only ever visible next to the
27- # importer archive it describes, so consumers cannot restore a torn pair.
28- printf ' %s\n' " $fingerprint " > " $marker "
64+ mv " $temp_manifest " " $importer_manifest "
65+ mv " $temp_checksum " " $archive_checksum "
66+ # The marker lands last. Consumers also verify the archive bytes and every
67+ # registry-backed importer resolution before trusting this snapshot.
68+ printf ' %s\n' " $fingerprint " > " $temp_marker "
69+ mv " $temp_marker " " $marker "
2970 ;;
3071 restore)
31- if [[ ! -f " $archive " ]]; then
32- echo " ::error::sticky importer node_modules archive is missing: $archive " >&2
72+ if [[ ! -f " $archive " || ! -f " $archive_checksum " || ! -f " $importer_manifest " ]]; then
73+ echo " sticky importer archive, manifest, or checksum is missing under $sticky_root " >&2
74+ exit 1
75+ fi
76+ expected_archive_checksum=" $( sed -n ' 1p' " $archive_checksum " | tr -d ' [:space:]' ) "
77+ expected_manifest_checksum=" $( sed -n ' 2p' " $archive_checksum " | tr -d ' [:space:]' ) "
78+ actual_archive_checksum=" $( archive_sha256 " $archive " ) "
79+ actual_manifest_checksum=" $( archive_sha256 " $importer_manifest " ) "
80+ if [[ ! " $expected_archive_checksum " =~ ^[a-f0-9]{64}$ ]] || \
81+ [[ ! " $expected_manifest_checksum " =~ ^[a-f0-9]{64}$ ]] || \
82+ [[ " $actual_archive_checksum " != " $expected_archive_checksum " ]] || \
83+ [[ " $actual_manifest_checksum " != " $expected_manifest_checksum " ]]; then
84+ echo " sticky importer archive or manifest checksum mismatch" >&2
85+ exit 1
86+ fi
87+ # A restored archive is authoritative for checkout-local importer links.
88+ # Clear first so entries absent from the archive cannot survive from a
89+ # reused workspace, and clear again when validation rejects the restore.
90+ clear_importers
91+ if ! tar --extract --file " $archive " --directory " $workspace " ; then
92+ clear_importers
93+ exit 1
94+ fi
95+ if ! verify_importers " $importer_manifest " ; then
96+ clear_importers
3397 exit 1
3498 fi
35- tar --extract --file " $archive " --directory " $workspace "
3699 ;;
37100 * )
38101 echo " unsupported sticky importer mode: $mode " >&2
0 commit comments