@cortexkit/opencode-magic-context 0.49.84 → 0.49.86
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{announcement-p41dde4h.js → announcement-fnyxaahs.js} +3 -1
- package/dist/auto-search-worker.js +3 -3
- package/dist/chunk-embedding-write-worker.js +5 -4
- package/dist/{context-3sajdysh.js → context-xesm7rj3.js} +27 -26
- package/dist/{derive-budgets-0kcek5ex.js → derive-budgets-90869322.js} +3 -1
- package/dist/{embedding-backfill-k994xdas.js → embedding-backfill-v6sw58m8.js} +7 -5
- package/dist/embedding-worker.js +15 -27
- package/dist/features/magic-context/memory/embedding-worker-boundary.test-support.d.ts +24 -0
- package/dist/features/magic-context/memory/embedding-worker-boundary.test-support.d.ts.map +1 -0
- package/dist/features/magic-context/memory/embedding-worker-client.d.ts +35 -1
- package/dist/features/magic-context/memory/embedding-worker-client.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-worker-loop.d.ts +57 -0
- package/dist/features/magic-context/memory/embedding-worker-loop.d.ts.map +1 -0
- package/dist/features/magic-context/memory/verification-paths.d.ts.map +1 -1
- package/dist/features/magic-context/project-embedding-registry.d.ts +6 -1
- package/dist/features/magic-context/project-embedding-registry.d.ts.map +1 -1
- package/dist/features/magic-context/storage-db.d.ts.map +1 -1
- package/dist/hooks/magic-context/lkg-recovery-fence.d.ts.map +1 -1
- package/dist/{index-xjfan0za.js → index-022w6314.js} +37 -37
- package/dist/{index-tvbkemec.js → index-08jfpny9.js} +1 -1
- package/dist/{index-fgy6hnr3.js → index-1bqkqde0.js} +5 -4
- package/dist/{index-0wehkw71.js → index-1pf5qrx4.js} +4 -4
- package/dist/{index-tqnvn9pa.js → index-25nktq8n.js} +3 -3
- package/dist/{index-98qy103c.js → index-2kf73btc.js} +1 -1
- package/dist/{index-vpw46557.js → index-3dtzyred.js} +1 -1
- package/dist/{index-bd34njzg.js → index-4ntnt8ee.js} +1 -1
- package/dist/{index-w2wpx7t5.js → index-4w5rx7de.js} +1 -1
- package/dist/{index-xd0ww0ns.js → index-798s7c8s.js} +1 -1
- package/dist/{index-fpqdbb67.js → index-7svecf3m.js} +3 -3
- package/dist/{v2/server-fvnzcnww.js → index-c5tmzym6.js} +192 -11
- package/dist/{index-k0jnsjcp.js → index-d0rtbv0f.js} +6 -6
- package/dist/{index-qybp04ff.js → index-e32b3x4n.js} +3 -3
- package/dist/{index-cfy5k1pa.js → index-gxdc95cp.js} +5 -5
- package/dist/{index-w8dcjt7f.js → index-k0tb2z28.js} +1 -1
- package/dist/{index-zb4h9nrk.js → index-k50xhjw1.js} +202 -14
- package/dist/{index-szwrpyac.js → index-nny4gm06.js} +1 -1
- package/dist/{index-shpyv8gq.js → index-s8ks5nz4.js} +16 -9
- package/dist/{index-d028gmgp.js → index-ttk3trse.js} +14 -14
- package/dist/{index-skwwbcnv.js → index-w6v4dw5f.js} +24 -24
- package/dist/{index-5wjv4c41.js → index-xccywqdj.js} +15 -15
- package/dist/{index-kthfyf45.js → index-zmqx48vt.js} +3 -3
- package/dist/index.js +33 -33
- package/dist/migration-worker.js +4 -3
- package/dist/{project-embedding-registry-y3z36mjy.js → project-embedding-registry-q80scexh.js} +6 -4
- package/dist/raw-ordinal-warmup-worker.js +2 -2
- package/dist/{recomp-orchestrator-m2s3jpvj.js → recomp-orchestrator-7ecg8ryx.js} +16 -15
- package/dist/{render-p2j7rn9a.js → render-yqp7q79f.js} +8 -7
- package/dist/{search-archive-hz9xh7nh.js → search-archive-r4h96j2z.js} +2 -2
- package/dist/{search-ksx0f1qd.js → search-bw22d7zc.js} +12 -10
- package/dist/shared/storage-file-mutex.d.ts.map +1 -1
- package/dist/shared/storage-permissions.d.ts +5 -3
- package/dist/shared/storage-permissions.d.ts.map +1 -1
- package/dist/{store-x0swqb5c.js → store-n4yz3n66.js} +2 -2
- package/dist/{store-reader-tsv1w4vt.js → store-reader-nf8z86n9.js} +1 -1
- package/dist/{transform-postprocess-phase-1wb2rrr1.js → transform-postprocess-phase-t99yj9vt.js} +17 -16
- package/dist/v2/auto-search-worker.js +3 -3
- package/dist/v2/chunk-embedding-write-worker.js +5 -4
- package/dist/v2/{context-at9nje0j.js → context-k8sx1nyy.js} +39 -39
- package/dist/v2/{derive-budgets-xbwcap8k.js → derive-budgets-ecjy1aqh.js} +4 -2
- package/dist/v2/{embedding-backfill-hjvjxp7d.js → embedding-backfill-k8kqnwfq.js} +7 -5
- package/dist/v2/embedding-worker.js +15 -27
- package/dist/v2/migration-worker.js +4 -3
- package/dist/v2/{project-embedding-registry-mzsvyads.js → project-embedding-registry-2jp1antr.js} +6 -4
- package/dist/v2/raw-ordinal-warmup-worker.js +2 -2
- package/dist/v2/{recomp-orchestrator-nhef90gc.js → recomp-orchestrator-ssde9ft1.js} +17 -16
- package/dist/v2/{render-c9r2g2jv.js → render-fezgh7pa.js} +8 -7
- package/dist/v2/{search-archive-80ckv9kq.js → search-archive-zz1kza50.js} +2 -2
- package/dist/v2/{search-t06js5dc.js → search-m251amra.js} +11 -9
- package/dist/v2/{server-4z964n1c.js → server-3p6b0qya.js} +1 -1
- package/dist/v2/{server-3cndh146.js → server-4wwwm2ya.js} +24 -24
- package/dist/v2/{server-mr9rqn0y.js → server-97w51d8n.js} +4 -4
- package/dist/v2/{server-d706s5xn.js → server-b93f0bqh.js} +205 -17
- package/dist/v2/{server-dfesqfv1.js → server-brgandk4.js} +1 -1
- package/dist/v2/{server-w1jfaxas.js → server-cwtehwbb.js} +1 -1
- package/dist/v2/{server-y4favn4d.js → server-e2k9mt86.js} +1 -1
- package/dist/v2/{server-3hhakg5a.js → server-fsw8kbng.js} +13 -13
- package/dist/v2/{server-68q80y27.js → server-hn8b7sfe.js} +16 -9
- package/dist/v2/{server-pejjj0nv.js → server-mbkwcwgw.js} +1 -1
- package/dist/v2/{server-ry6rpspz.js → server-n5zr7190.js} +15 -15
- package/dist/v2/{server-210rc0z9.js → server-ps6rz88g.js} +1 -1
- package/dist/{index-fvnzcnww.js → v2/server-r2es6zt7.js} +192 -11
- package/dist/v2/{server-bt546axy.js → server-rz4snd3x.js} +3 -3
- package/dist/v2/{server-863bwkga.js → server-ssg29wjg.js} +6 -6
- package/dist/v2/{server-hhw6rzsn.js → server-w7mpvkpj.js} +1 -1
- package/dist/v2/{server-sy98vksf.js → server-xa8fefhw.js} +3 -3
- package/dist/v2/{server-gx1s06dx.js → server-y31y6gws.js} +5 -4
- package/dist/v2/{server-cce6m31c.js → server-ytqps2xa.js} +1 -1
- package/dist/v2/server.js +6 -4
- package/dist/v2/{store-reader-paamf0ac.js → store-reader-2vhx33n6.js} +1 -1
- package/dist/v2/{store-h3s95ywe.js → store-y56y034s.js} +2 -2
- package/dist/v2/{v2-archive-reader-1zzteaa5.js → v2-archive-reader-e2w39c3p.js} +1 -1
- package/dist/v2/{wrapup-orchestrator-9y98qcqz.js → wrapup-orchestrator-eg5b1p89.js} +16 -15
- package/dist/{v2-archive-reader-3yjxaj5z.js → v2-archive-reader-8dsngf2j.js} +1 -1
- package/dist/{wrapup-orchestrator-sa3a075a.js → wrapup-orchestrator-nmk6yf7f.js} +17 -16
- package/package.json +1 -1
- package/src/shared/storage-file-mutex-review.test.ts +125 -2
- package/src/shared/storage-file-mutex.ts +17 -8
- package/src/shared/storage-permissions-creators.test.ts +194 -0
- package/src/shared/storage-permissions-startup.test.ts +476 -0
- package/src/shared/storage-permissions.ts +227 -12
- package/src/tui/compaction-off.ts +2 -2
- package/src/tui/slots/sidebar-content-render.test.ts +309 -0
- package/src/tui/slots/sidebar-content.tsx +134 -99
- package/src/tui-compiled/compaction-off.ts +2 -2
- package/src/tui-compiled/slots/sidebar-content.tsx +321 -247
- /package/dist/{index-rdh63md0.js → index-1y4y98r1.js} +0 -0
- /package/dist/v2/{server-9nk5h6w6.js → server-qyazxy92.js} +0 -0
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, spyOn } from "bun:test";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { withStorageFileMutex } from "./storage-file-mutex";
|
|
5
|
+
import {
|
|
6
|
+
__resetStoragePrivatePermissionEnforcementForTests,
|
|
7
|
+
ensurePrivateStorageDirectorySync,
|
|
8
|
+
setStoragePrivatePermissionEnforcement,
|
|
9
|
+
tightenStorageTreeSync,
|
|
10
|
+
writeStorageFileDurableAtomicSync,
|
|
11
|
+
} from "./storage-permissions";
|
|
12
|
+
import { cleanupTestTempDir, createTestTempDir } from "./test-temp-dir";
|
|
13
|
+
|
|
14
|
+
const roots: string[] = [];
|
|
15
|
+
const markerName = ".storage-permissions-v1";
|
|
16
|
+
|
|
17
|
+
function fixture(): string {
|
|
18
|
+
const { dir: parent } = createTestTempDir("mc-storage-startup-");
|
|
19
|
+
roots.push(parent);
|
|
20
|
+
const root = join(parent, "storage");
|
|
21
|
+
fs.mkdirSync(root, { mode: 0o700 });
|
|
22
|
+
fs.mkdirSync(join(root, "lkg-recovery-fences"), { mode: 0o700 });
|
|
23
|
+
for (let i = 0; i < 2000; i++) {
|
|
24
|
+
fs.writeFileSync(join(root, "lkg-recovery-fences", `${i}.json`), "{}", { mode: 0o600 });
|
|
25
|
+
}
|
|
26
|
+
fs.writeFileSync(join(root, "context.db"), "", { mode: 0o600 });
|
|
27
|
+
return root;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
afterEach(() => {
|
|
31
|
+
__resetStoragePrivatePermissionEnforcementForTests();
|
|
32
|
+
for (const root of roots.splice(0)) cleanupTestTempDir(root);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe.skipIf(process.platform === "win32")("startup permission certificate", () => {
|
|
36
|
+
it("keeps fresh legacy fence temp files and removes only ones older than ten minutes", () => {
|
|
37
|
+
const root = fixture();
|
|
38
|
+
const fences = join(root, "lkg-recovery-fences");
|
|
39
|
+
const fresh = join(fences, "fresh.json.publisher.tmp");
|
|
40
|
+
const old = join(fences, "old.json.publisher.tmp");
|
|
41
|
+
const unrelated = join(fences, "unrelated.tmp");
|
|
42
|
+
const fixed = join(fences, "fixed.json.tmp");
|
|
43
|
+
const now = Date.now();
|
|
44
|
+
for (const path of [fresh, old, unrelated, fixed]) {
|
|
45
|
+
fs.writeFileSync(path, "keep", { mode: 0o600 });
|
|
46
|
+
const age = path === fresh ? 9 * 60_000 : 11 * 60_000;
|
|
47
|
+
fs.utimesSync(path, new Date(now - age), new Date(now - age));
|
|
48
|
+
}
|
|
49
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
50
|
+
expect(fs.existsSync(fresh)).toBe(true);
|
|
51
|
+
expect(fs.existsSync(old)).toBe(false);
|
|
52
|
+
expect(fs.readFileSync(unrelated, "utf8")).toBe("keep");
|
|
53
|
+
expect(fs.readFileSync(fixed, "utf8")).toBe("keep");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("does not unlink an old legacy temp before its live publisher renames it", () => {
|
|
57
|
+
const root = fixture();
|
|
58
|
+
const fence = join(root, "lkg-recovery-fences", "pending.json");
|
|
59
|
+
const temp = `${fence}.${process.pid}.${"a".repeat(24)}.tmp`;
|
|
60
|
+
const old = new Date(Date.now() - 11 * 60_000);
|
|
61
|
+
withStorageFileMutex(`${fence}.locks`, () => {
|
|
62
|
+
writeStorageFileDurableAtomicSync(
|
|
63
|
+
fence,
|
|
64
|
+
"published",
|
|
65
|
+
() => {
|
|
66
|
+
fs.utimesSync(temp, old, old);
|
|
67
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
68
|
+
},
|
|
69
|
+
temp,
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
expect(fs.readFileSync(fence, "utf8")).toBe("published");
|
|
73
|
+
expect(fs.existsSync(join(root, markerName))).toBe(false);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("sweeps only empty orphan fence lock directories during the full pass", () => {
|
|
77
|
+
const root = fixture();
|
|
78
|
+
const fences = join(root, "lkg-recovery-fences");
|
|
79
|
+
for (const name of ["orphan.json.locks", "held.json.locks", "0.json.locks", "other.locks"])
|
|
80
|
+
fs.mkdirSync(join(fences, name), { mode: 0o700 });
|
|
81
|
+
fs.writeFileSync(join(fences, "held.json.locks", "ticket"), "held", { mode: 0o600 });
|
|
82
|
+
fs.symlinkSync("orphan.json.locks", join(fences, "link.json.locks"));
|
|
83
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
84
|
+
expect(fs.existsSync(join(fences, "orphan.json.locks"))).toBe(false);
|
|
85
|
+
for (const name of ["held.json.locks", "0.json.locks", "other.locks"])
|
|
86
|
+
expect(fs.existsSync(join(fences, name))).toBe(true);
|
|
87
|
+
expect(fs.lstatSync(join(fences, "link.json.locks")).isSymbolicLink()).toBe(true);
|
|
88
|
+
fs.mkdirSync(join(fences, "later.json.locks"), { mode: 0o700 });
|
|
89
|
+
tightenStorageTreeSync(root);
|
|
90
|
+
expect(fs.existsSync(join(fences, "later.json.locks"))).toBe(true);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("checks only startup entries after a certified walk, not 2000 recovery fences", () => {
|
|
94
|
+
const root = fixture();
|
|
95
|
+
fs.writeFileSync(join(root, markerName), "1\n", { mode: 0o600 });
|
|
96
|
+
const lstat = spyOn(fs, "lstatSync");
|
|
97
|
+
try {
|
|
98
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
99
|
+
expect(lstat.mock.calls.length).toBeLessThanOrEqual(8);
|
|
100
|
+
console.log(`CERTIFIED_LSTAT calls=${lstat.mock.calls.length} fences=2000`);
|
|
101
|
+
} finally {
|
|
102
|
+
lstat.mockRestore();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("certifies the first full walk only after all entries succeed", () => {
|
|
107
|
+
const root = fixture();
|
|
108
|
+
const lstat = spyOn(fs, "lstatSync");
|
|
109
|
+
try {
|
|
110
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
111
|
+
expect(lstat.mock.calls.length).toBeGreaterThan(2000);
|
|
112
|
+
expect(fs.lstatSync(join(root, markerName)).mode & 0o777).toBe(0o600);
|
|
113
|
+
} finally {
|
|
114
|
+
lstat.mockRestore();
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("repairs the root and files when the marker is a directory", () => {
|
|
119
|
+
const root = fixture();
|
|
120
|
+
const marker = join(root, markerName);
|
|
121
|
+
const loose = join(root, "loose.json");
|
|
122
|
+
fs.mkdirSync(marker, { mode: 0o700 });
|
|
123
|
+
fs.writeFileSync(join(marker, "keep"), "keep", { mode: 0o600 });
|
|
124
|
+
fs.writeFileSync(loose, "{}");
|
|
125
|
+
fs.chmodSync(loose, 0o644);
|
|
126
|
+
fs.chmodSync(root, 0o755);
|
|
127
|
+
|
|
128
|
+
tightenStorageTreeSync(root);
|
|
129
|
+
|
|
130
|
+
expect(fs.lstatSync(root).mode & 0o777).toBe(0o700);
|
|
131
|
+
expect(fs.lstatSync(loose).mode & 0o777).toBe(0o600);
|
|
132
|
+
expect(fs.readFileSync(join(marker, "keep"), "utf8")).toBe("keep");
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const runningAsRoot = process.getuid?.() === 0;
|
|
136
|
+
if (runningAsRoot) console.warn("Skipping the non-searchable storage test as root.");
|
|
137
|
+
it.skipIf(runningAsRoot)("repairs a non-searchable root before walking its files", () => {
|
|
138
|
+
const root = fixture();
|
|
139
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
140
|
+
const loose = join(root, "loose.json");
|
|
141
|
+
fs.writeFileSync(loose, "{}");
|
|
142
|
+
fs.chmodSync(loose, 0o644);
|
|
143
|
+
fs.chmodSync(root, 0o600);
|
|
144
|
+
try {
|
|
145
|
+
tightenStorageTreeSync(root);
|
|
146
|
+
|
|
147
|
+
expect(fs.lstatSync(root).mode & 0o777).toBe(0o700);
|
|
148
|
+
expect(fs.lstatSync(loose).mode & 0o777).toBe(0o600);
|
|
149
|
+
} finally {
|
|
150
|
+
fs.chmodSync(root, 0o700);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("repairs new files outside the startup paths after 24 hours", () => {
|
|
155
|
+
const root = fixture();
|
|
156
|
+
const marker = join(root, markerName);
|
|
157
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
158
|
+
const loose = join(root, "lkg-recovery-fences", "later.json");
|
|
159
|
+
fs.writeFileSync(loose, "{}");
|
|
160
|
+
fs.chmodSync(loose, 0o644);
|
|
161
|
+
|
|
162
|
+
const fresh = new Date(Date.now() - 23 * 60 * 60_000);
|
|
163
|
+
fs.utimesSync(marker, fresh, fresh);
|
|
164
|
+
for (let i = 0; i < 3; i++) {
|
|
165
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
166
|
+
expect(fs.lstatSync(loose).mode & 0o777).toBe(0o644);
|
|
167
|
+
}
|
|
168
|
+
const expired = new Date(Date.now() - 25 * 60 * 60_000);
|
|
169
|
+
fs.utimesSync(marker, expired, expired);
|
|
170
|
+
|
|
171
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
172
|
+
expect(fs.lstatSync(loose).mode & 0o777).toBe(0o600);
|
|
173
|
+
expect(fs.lstatSync(marker).mtimeMs).toBeGreaterThan(fresh.getTime());
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("repairs a peer's loose git identity sidecar after the certificate expires", () => {
|
|
177
|
+
const root = fixture();
|
|
178
|
+
const directory = join(root, "project-identities");
|
|
179
|
+
fs.mkdirSync(directory, { mode: 0o700 });
|
|
180
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
181
|
+
|
|
182
|
+
const sidecar = join(directory, `${"a".repeat(64)}.json`);
|
|
183
|
+
fs.writeFileSync(sidecar, JSON.stringify({ directory: root, identity: "git:abcdef0" }));
|
|
184
|
+
fs.chmodSync(sidecar, 0o644);
|
|
185
|
+
const expired = new Date(Date.now() - 25 * 60 * 60_000);
|
|
186
|
+
fs.utimesSync(join(root, markerName), expired, expired);
|
|
187
|
+
|
|
188
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
189
|
+
expect(fs.lstatSync(sidecar).mode & 0o777).toBe(0o600);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("does not trust a certificate with a future modification time", () => {
|
|
193
|
+
const root = fixture();
|
|
194
|
+
const marker = join(root, markerName);
|
|
195
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
196
|
+
const loose = join(root, "lkg-recovery-fences", "later.json");
|
|
197
|
+
fs.writeFileSync(loose, "{}");
|
|
198
|
+
fs.chmodSync(loose, 0o644);
|
|
199
|
+
const future = new Date(Date.now() + 60_000);
|
|
200
|
+
fs.utimesSync(marker, future, future);
|
|
201
|
+
|
|
202
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
203
|
+
expect(fs.lstatSync(loose).mode & 0o777).toBe(0o600);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("does not certify a storage root that is a symbolic link", () => {
|
|
207
|
+
const root = fixture();
|
|
208
|
+
const alias = join(root, "alias");
|
|
209
|
+
fs.symlinkSync(root, alias);
|
|
210
|
+
expect(tightenStorageTreeSync(alias).failures).toBe(1);
|
|
211
|
+
expect(fs.existsSync(join(root, markerName))).toBe(false);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
for (const target of ["context.db", ".", "context.db-wal", "context.db-shm"]) {
|
|
215
|
+
it(`invalidates after repairing ${target}, then walks fully on the next open`, () => {
|
|
216
|
+
const root = fixture();
|
|
217
|
+
fs.writeFileSync(join(root, markerName), "1\n", { mode: 0o600 });
|
|
218
|
+
if (target.endsWith("-wal") || target.endsWith("-shm")) {
|
|
219
|
+
fs.writeFileSync(join(root, target), "");
|
|
220
|
+
}
|
|
221
|
+
fs.chmodSync(join(root, target), target === "." ? 0o755 : 0o644);
|
|
222
|
+
expect(tightenStorageTreeSync(root)).toMatchObject({ tightened: 1, failures: 0 });
|
|
223
|
+
expect(fs.lstatSync(join(root, target)).mode & 0o777).toBe(
|
|
224
|
+
target === "." ? 0o700 : 0o600,
|
|
225
|
+
);
|
|
226
|
+
expect(fs.existsSync(join(root, markerName))).toBe(false);
|
|
227
|
+
const lstat = spyOn(fs, "lstatSync");
|
|
228
|
+
try {
|
|
229
|
+
tightenStorageTreeSync(root);
|
|
230
|
+
expect(lstat.mock.calls.length).toBeGreaterThan(2000);
|
|
231
|
+
} finally {
|
|
232
|
+
lstat.mockRestore();
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
it("does not trust a symlink or a permissive certificate", () => {
|
|
238
|
+
for (const kind of ["symlink", "mode"]) {
|
|
239
|
+
const root = fixture();
|
|
240
|
+
const marker = join(root, markerName);
|
|
241
|
+
if (kind === "symlink") fs.symlinkSync("context.db", marker);
|
|
242
|
+
else fs.writeFileSync(marker, "1\n", { mode: 0o644 });
|
|
243
|
+
const lstat = spyOn(fs, "lstatSync");
|
|
244
|
+
try {
|
|
245
|
+
tightenStorageTreeSync(root);
|
|
246
|
+
expect(lstat.mock.calls.length).toBeGreaterThan(2000);
|
|
247
|
+
} finally {
|
|
248
|
+
lstat.mockRestore();
|
|
249
|
+
}
|
|
250
|
+
expect(fs.lstatSync(marker).isSymbolicLink()).toBe(false);
|
|
251
|
+
expect(fs.lstatSync(marker).mode & 0o777).toBe(0o600);
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("never certifies a failed walk or enforcement-off storage", () => {
|
|
256
|
+
const root = fixture();
|
|
257
|
+
const realLstat = fs.lstatSync;
|
|
258
|
+
const lstat = spyOn(fs, "lstatSync").mockImplementation((...args) => {
|
|
259
|
+
if (String(args[0]).endsWith("/12.json")) throw new Error("injected lstat failure");
|
|
260
|
+
return realLstat(...args);
|
|
261
|
+
});
|
|
262
|
+
try {
|
|
263
|
+
expect(tightenStorageTreeSync(root).failures).toBe(1);
|
|
264
|
+
expect(fs.existsSync(join(root, markerName))).toBe(false);
|
|
265
|
+
} finally {
|
|
266
|
+
lstat.mockRestore();
|
|
267
|
+
}
|
|
268
|
+
setStoragePrivatePermissionEnforcement(false);
|
|
269
|
+
expect(tightenStorageTreeSync(root)).toEqual({ tightened: 0, failures: 0 });
|
|
270
|
+
expect(fs.existsSync(join(root, markerName))).toBe(false);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("invalidates certification when a bounded entry cannot be checked", () => {
|
|
274
|
+
const root = fixture();
|
|
275
|
+
fs.writeFileSync(join(root, markerName), "1\n", { mode: 0o600 });
|
|
276
|
+
const realLstat = fs.lstatSync;
|
|
277
|
+
const lstat = spyOn(fs, "lstatSync").mockImplementation((...args) => {
|
|
278
|
+
if (String(args[0]) === join(root, "context.db")) {
|
|
279
|
+
throw Object.assign(new Error("injected permission failure"), { code: "EACCES" });
|
|
280
|
+
}
|
|
281
|
+
return realLstat(...args);
|
|
282
|
+
});
|
|
283
|
+
try {
|
|
284
|
+
expect(tightenStorageTreeSync(root)).toMatchObject({ failures: 1 });
|
|
285
|
+
expect(fs.existsSync(join(root, markerName))).toBe(false);
|
|
286
|
+
} finally {
|
|
287
|
+
lstat.mockRestore();
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
for (const subtree of ["models", "logs"]) {
|
|
292
|
+
it(`still tightens third-party ${subtree} entries after certification`, () => {
|
|
293
|
+
const root = fixture();
|
|
294
|
+
fs.writeFileSync(join(root, markerName), "1\n", { mode: 0o600 });
|
|
295
|
+
fs.mkdirSync(join(root, subtree), { mode: 0o755 });
|
|
296
|
+
fs.writeFileSync(join(root, subtree, "entry"), "fixture", { mode: 0o644 });
|
|
297
|
+
fs.chmodSync(join(root, subtree), 0o755);
|
|
298
|
+
fs.chmodSync(join(root, subtree, "entry"), 0o644);
|
|
299
|
+
expect(tightenStorageTreeSync(root)).toMatchObject({ tightened: 2, failures: 0 });
|
|
300
|
+
expect(fs.lstatSync(join(root, subtree)).mode & 0o777).toBe(0o700);
|
|
301
|
+
expect(fs.lstatSync(join(root, subtree, "entry")).mode & 0o777).toBe(0o600);
|
|
302
|
+
expect(fs.existsSync(join(root, markerName))).toBe(false);
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
it("invalidates when the existing private-directory check finds a wrong mode", () => {
|
|
307
|
+
const root = fixture();
|
|
308
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
309
|
+
expect(fs.existsSync(join(root, markerName))).toBe(true);
|
|
310
|
+
const directory = join(root, "lkg-recovery-fences");
|
|
311
|
+
fs.chmodSync(directory, 0o755);
|
|
312
|
+
ensurePrivateStorageDirectorySync(directory);
|
|
313
|
+
expect(fs.existsSync(join(root, markerName))).toBe(false);
|
|
314
|
+
expect(fs.lstatSync(directory).mode & 0o777).toBe(0o700);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("repairs a loose private directory when the marker is a directory", () => {
|
|
318
|
+
const { dir: root } = createTestTempDir("mc-marker-directory-");
|
|
319
|
+
roots.push(root);
|
|
320
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
321
|
+
const marker = join(root, markerName);
|
|
322
|
+
fs.unlinkSync(marker);
|
|
323
|
+
fs.mkdirSync(marker);
|
|
324
|
+
const sentinel = join(marker, "keep");
|
|
325
|
+
fs.writeFileSync(sentinel, "keep");
|
|
326
|
+
const directory = join(root, "some.json.locks");
|
|
327
|
+
fs.mkdirSync(directory);
|
|
328
|
+
fs.chmodSync(directory, 0o755);
|
|
329
|
+
|
|
330
|
+
expect(() => ensurePrivateStorageDirectorySync(directory)).not.toThrow();
|
|
331
|
+
expect(fs.lstatSync(directory).mode & 0o777).toBe(0o700);
|
|
332
|
+
expect(fs.readFileSync(sentinel, "utf8")).toBe("keep");
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it("repairs an unchecked directory when unlink fails on a read-only ancestor", () => {
|
|
336
|
+
const { dir: parent } = createTestTempDir("mc-read-only-ancestor-");
|
|
337
|
+
roots.push(parent);
|
|
338
|
+
const root = join(parent, "storage");
|
|
339
|
+
const directory = join(root, "lkg-recovery-fences");
|
|
340
|
+
const marker = join(root, markerName);
|
|
341
|
+
fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
342
|
+
fs.chmodSync(directory, 0o755);
|
|
343
|
+
fs.writeFileSync(marker, "1\n", { mode: 0o600 });
|
|
344
|
+
|
|
345
|
+
const rmSync = fs.rmSync;
|
|
346
|
+
const remove = spyOn(fs, "rmSync").mockImplementation((path, options) => {
|
|
347
|
+
if (path === join(parent, markerName)) {
|
|
348
|
+
throw Object.assign(new Error("injected read-only ancestor"), { code: "EROFS" });
|
|
349
|
+
}
|
|
350
|
+
return rmSync(path, options);
|
|
351
|
+
});
|
|
352
|
+
try {
|
|
353
|
+
ensurePrivateStorageDirectorySync(directory);
|
|
354
|
+
expect(fs.lstatSync(directory).mode & 0o777).toBe(0o700);
|
|
355
|
+
expect(fs.existsSync(marker)).toBe(false);
|
|
356
|
+
} finally {
|
|
357
|
+
remove.mockRestore();
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it("walks the full tree in the same call when a certified start finds a loose root", () => {
|
|
362
|
+
const root = fixture();
|
|
363
|
+
fs.writeFileSync(join(root, markerName), "1\n", { mode: 0o600 });
|
|
364
|
+
const deep = join(root, "lkg-recovery-fences", "5.json");
|
|
365
|
+
fs.chmodSync(deep, 0o644);
|
|
366
|
+
fs.chmodSync(root, 0o755);
|
|
367
|
+
expect(tightenStorageTreeSync(root)).toMatchObject({ tightened: 2, failures: 0 });
|
|
368
|
+
expect(fs.lstatSync(root).mode & 0o777).toBe(0o700);
|
|
369
|
+
expect(fs.lstatSync(deep).mode & 0o777).toBe(0o600);
|
|
370
|
+
expect(fs.existsSync(join(root, markerName))).toBe(false);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it("invalidates the certificate before a repair can hide the loose root", () => {
|
|
374
|
+
const root = fixture();
|
|
375
|
+
const marker = join(root, markerName);
|
|
376
|
+
fs.writeFileSync(marker, "1\n", { mode: 0o600 });
|
|
377
|
+
fs.chmodSync(root, 0o755);
|
|
378
|
+
const chmodSync = fs.chmodSync;
|
|
379
|
+
const markerDuringRepair: boolean[] = [];
|
|
380
|
+
const chmod = spyOn(fs, "chmodSync").mockImplementation((path, mode) => {
|
|
381
|
+
if (path === root) markerDuringRepair.push(fs.existsSync(marker));
|
|
382
|
+
return chmodSync(path, mode);
|
|
383
|
+
});
|
|
384
|
+
try {
|
|
385
|
+
expect(tightenStorageTreeSync(root)).toEqual({ tightened: 1, failures: 0 });
|
|
386
|
+
expect(markerDuringRepair).toEqual([false]);
|
|
387
|
+
} finally {
|
|
388
|
+
chmod.mockRestore();
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it("stops marker invalidation at the storage root", () => {
|
|
393
|
+
const root = fixture();
|
|
394
|
+
const parent = join(root, "..");
|
|
395
|
+
const decoy = join(parent, markerName);
|
|
396
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
397
|
+
expect(fs.existsSync(join(root, markerName))).toBe(true);
|
|
398
|
+
fs.writeFileSync(decoy, "1\n", { mode: 0o600 });
|
|
399
|
+
const directory = join(root, "lkg-recovery-fences");
|
|
400
|
+
fs.chmodSync(directory, 0o755);
|
|
401
|
+
ensurePrivateStorageDirectorySync(directory);
|
|
402
|
+
expect(fs.existsSync(join(root, markerName))).toBe(false);
|
|
403
|
+
expect(fs.existsSync(decoy)).toBe(true);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("removes certification while enforcement is off before a later private open", () => {
|
|
407
|
+
const root = fixture();
|
|
408
|
+
fs.writeFileSync(join(root, markerName), "1\n", { mode: 0o600 });
|
|
409
|
+
setStoragePrivatePermissionEnforcement(false);
|
|
410
|
+
expect(tightenStorageTreeSync(root)).toEqual({ tightened: 0, failures: 0 });
|
|
411
|
+
expect(fs.existsSync(join(root, markerName))).toBe(false);
|
|
412
|
+
setStoragePrivatePermissionEnforcement(true);
|
|
413
|
+
const lstat = spyOn(fs, "lstatSync");
|
|
414
|
+
try {
|
|
415
|
+
tightenStorageTreeSync(root);
|
|
416
|
+
expect(lstat.mock.calls.length).toBeGreaterThan(2000);
|
|
417
|
+
} finally {
|
|
418
|
+
lstat.mockRestore();
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it("clears the certificate of a checked root when enforcement turns off", () => {
|
|
423
|
+
const root = fixture();
|
|
424
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
425
|
+
expect(fs.existsSync(join(root, markerName))).toBe(true);
|
|
426
|
+
setStoragePrivatePermissionEnforcement(false);
|
|
427
|
+
expect(fs.existsSync(join(root, markerName))).toBe(false);
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it("does not trust a certificate with unexpected contents", () => {
|
|
431
|
+
for (const content of ["", "1", "2\n", "1\nextra\n"]) {
|
|
432
|
+
const root = fixture();
|
|
433
|
+
const marker = join(root, markerName);
|
|
434
|
+
fs.writeFileSync(marker, content, { mode: 0o600 });
|
|
435
|
+
const lstat = spyOn(fs, "lstatSync");
|
|
436
|
+
try {
|
|
437
|
+
tightenStorageTreeSync(root);
|
|
438
|
+
expect(lstat.mock.calls.length).toBeGreaterThan(2000);
|
|
439
|
+
} finally {
|
|
440
|
+
lstat.mockRestore();
|
|
441
|
+
}
|
|
442
|
+
expect(fs.readFileSync(marker, "utf8")).toBe("1\n");
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
it("rejects an oversized certificate without a whole-file read", () => {
|
|
447
|
+
const root = fixture();
|
|
448
|
+
const marker = join(root, markerName);
|
|
449
|
+
const deep = join(root, "lkg-recovery-fences", "5.json");
|
|
450
|
+
fs.writeFileSync(marker, "1\n", { mode: 0o600 });
|
|
451
|
+
fs.truncateSync(marker, 1024 * 1024);
|
|
452
|
+
fs.chmodSync(deep, 0o644);
|
|
453
|
+
const readFile = spyOn(fs, "readFileSync");
|
|
454
|
+
try {
|
|
455
|
+
expect(tightenStorageTreeSync(root)).toEqual({ tightened: 1, failures: 0 });
|
|
456
|
+
expect(readFile.mock.calls.some(([path]) => path === marker)).toBe(false);
|
|
457
|
+
} finally {
|
|
458
|
+
readFile.mockRestore();
|
|
459
|
+
}
|
|
460
|
+
expect(fs.readFileSync(marker, "utf8")).toBe("1\n");
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
it("does not certify a full pass that keeps a fresh legacy fence temp", () => {
|
|
464
|
+
const root = fixture();
|
|
465
|
+
const fresh = join(root, "lkg-recovery-fences", "fresh.json.publisher.tmp");
|
|
466
|
+
fs.writeFileSync(fresh, "keep", { mode: 0o600 });
|
|
467
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
468
|
+
expect(fs.existsSync(fresh)).toBe(true);
|
|
469
|
+
expect(fs.existsSync(join(root, markerName))).toBe(false);
|
|
470
|
+
const old = new Date(Date.now() - 11 * 60_000);
|
|
471
|
+
fs.utimesSync(fresh, old, old);
|
|
472
|
+
expect(tightenStorageTreeSync(root).failures).toBe(0);
|
|
473
|
+
expect(fs.existsSync(fresh)).toBe(false);
|
|
474
|
+
expect(fs.existsSync(join(root, markerName))).toBe(true);
|
|
475
|
+
});
|
|
476
|
+
});
|