-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathdirectory-scopes.test.ts
More file actions
235 lines (210 loc) · 8 KB
/
Copy pathdirectory-scopes.test.ts
File metadata and controls
235 lines (210 loc) · 8 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* GRIDA-SEC-004 — directory-reference authority pins.
*
* Descriptors expose a virtual read-only mount, never a host root. Pending
* grants are bounded/expiring, claim is all-or-nothing and session-exclusive,
* sensitive roots remain denied even without an OS sandbox, and a fork-like
* second session inherits no authority.
*/
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import {
DirectoryScopeError,
DirectoryScopeRegistry,
} from "./directory-scopes";
describe("DirectoryScopeRegistry", () => {
let root: string;
let userData: string;
let picked: string;
beforeEach(async () => {
root = await fs.mkdtemp(path.join(os.tmpdir(), "grida-dir-scope-"));
userData = path.join(root, "user-data");
picked = path.join(root, "references");
await fs.mkdir(userData);
await fs.mkdir(picked);
});
afterEach(async () => {
await fs.rm(root, { recursive: true, force: true });
});
it("mints an opaque descriptor and keeps the canonical root server-side", async () => {
const registry = new DirectoryScopeRegistry({ secrets_root: userData });
const descriptor = await registry.attach(picked);
expect(descriptor).toEqual({
kind: "scope",
id: expect.stringMatching(/^dir_[0-9a-f-]+$/),
name: "references",
path: `/__references__/${descriptor.id}`,
access: "read",
});
expect(descriptor).not.toHaveProperty("root");
const [grant] = registry.claim("ses_a", [descriptor]);
expect(grant.root).toBe(await fs.realpath(picked));
expect(registry.forSession("ses_a")).toEqual([grant]);
});
it.skipIf(process.platform === "win32")(
"records the exact realpath target without expanding to a parent",
async () => {
const target = path.join(root, "actual");
const link = path.join(root, "picked-link");
await fs.mkdir(target);
await fs.symlink(target, link);
const registry = new DirectoryScopeRegistry({ secrets_root: userData });
const descriptor = await registry.attach(link);
const [grant] = registry.claim("ses_a", [descriptor]);
expect(grant.root).toBe(await fs.realpath(target));
expect(grant.root).not.toBe(root);
}
);
it("rejects files and missing paths", async () => {
const file = path.join(root, "note.txt");
await fs.writeFile(file, "x");
const registry = new DirectoryScopeRegistry({ secrets_root: userData });
await expect(registry.attach(file)).rejects.toMatchObject({
code: "directory-scope-not-directory",
});
await expect(
registry.attach(path.join(root, "missing"))
).rejects.toMatchObject({ code: "directory-scope-invalid-path" });
});
it("rejects a protected root, its descendant, and any ancestor containing it", async () => {
const homeSecret = path.join(root, "home", ".ssh");
await fs.mkdir(homeSecret, { recursive: true });
const registry = new DirectoryScopeRegistry({
secrets_root: userData,
protected_roots: [homeSecret],
});
for (const unsafe of [
userData,
path.join(userData, "sessions"),
path.dirname(userData),
homeSecret,
path.dirname(homeSecret),
root,
]) {
await fs.mkdir(unsafe, { recursive: true });
await expect(registry.attach(unsafe)).rejects.toMatchObject({
code: "directory-scope-protected-root",
});
}
});
it.skipIf(process.platform === "win32")(
"re-resolves protected roots for every attachment gesture",
async () => {
const safe = path.join(root, "safe");
const protectedLink = path.join(root, "protected-link");
await fs.mkdir(safe);
const registry = new DirectoryScopeRegistry({
secrets_root: userData,
protected_roots: [protectedLink],
});
// The missing protected path initially falls back to its lexical path.
await registry.attach(safe);
// Its filesystem identity can change during the registry's lifetime.
await fs.symlink(picked, protectedLink);
await expect(registry.attach(picked)).rejects.toMatchObject({
code: "directory-scope-protected-root",
});
}
);
it("bounds pending grants and releases expired capacity", async () => {
let now = 1_000;
const registry = new DirectoryScopeRegistry({
secrets_root: userData,
pending_ttl_ms: 100,
max_pending: 1,
now: () => now,
});
await registry.attach(picked);
await expect(registry.attach(picked)).rejects.toMatchObject({
code: "directory-scope-pending-limit",
});
now += 101;
await expect(registry.attach(picked)).resolves.toMatchObject({
kind: "scope",
});
});
it("reserves pending capacity across concurrent validation and releases failures", async () => {
const registry = new DirectoryScopeRegistry({
secrets_root: userData,
max_pending: 1,
});
const first = registry.attach(picked);
await expect(registry.attach(picked)).rejects.toMatchObject({
code: "directory-scope-pending-limit",
});
registry.claim("ses_a", [await first]);
const invalid = registry.attach(path.join(root, "missing"));
const blocked = registry.attach(picked);
await Promise.all([
expect(invalid).rejects.toMatchObject({
code: "directory-scope-invalid-path",
}),
expect(blocked).rejects.toMatchObject({
code: "directory-scope-pending-limit",
}),
]);
await expect(registry.attach(picked)).resolves.toMatchObject({
kind: "scope",
});
});
it("claims atomically, idempotently, and exclusively to one session", async () => {
const other = path.join(root, "other");
await fs.mkdir(other);
const registry = new DirectoryScopeRegistry({ secrets_root: userData });
const a = await registry.attach(picked);
const b = await registry.attach(other);
expect(() =>
registry.claim("ses_a", [
a,
{
kind: "scope",
id: "dir_missing",
name: "missing",
path: "/__references__/dir_missing",
access: "read",
},
])
).toThrow(DirectoryScopeError);
// The failed set consumed nothing: both real candidates remain claimable.
const grants = registry.claim("ses_a", [a, b]);
expect(grants.map((g) => g.id)).toEqual([a.id, b.id]);
expect(registry.claim("ses_a", [a])).toEqual([grants[0]]);
expect(() => registry.claim("ses_b", [a])).toThrowError(
expect.objectContaining({
code: "directory-scope-owned-by-another-session",
})
);
// A fork-like session owns no scopes merely because transcript parts copy.
expect(registry.forSession("ses_b")).toEqual([]);
});
it("rejects a relabeled descriptor without consuming its valid grant", async () => {
const registry = new DirectoryScopeRegistry({ secrets_root: userData });
const descriptor = await registry.attach(picked);
expect(() =>
registry.claim("ses_a", [{ ...descriptor, name: "misleading" }])
).toThrowError(
expect.objectContaining({ code: "directory-scope-descriptor-mismatch" })
);
// Canonical facts still claim successfully after the rejected mutation.
expect(registry.claim("ses_a", [descriptor])).toHaveLength(1);
});
it("revokes a deleted session and clears all authority on dispose", async () => {
const registry = new DirectoryScopeRegistry({ secrets_root: userData });
const first = await registry.attach(picked);
registry.claim("ses_a", [first]);
registry.forgetSession("ses_a");
expect(registry.forSession("ses_a")).toEqual([]);
expect(() => registry.claim("ses_a", [first])).toThrowError(
expect.objectContaining({ code: "directory-scope-unavailable" })
);
const second = await registry.attach(picked);
registry.claim("ses_b", [second]);
registry.dispose();
expect(registry.forSession("ses_b")).toEqual([]);
expect(() => registry.claim("ses_b", [second])).toThrowError(
expect.objectContaining({ code: "directory-scope-unavailable" })
);
});
});