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
236
237
238
239
240
241
242
243
244
245
246
|
import { describe, it, expect, afterAll, beforeEach } from "bun:test";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import type { Package } from "../db/schema";
import type { RepoAccessLevel } from "../middleware/repo-access";
import {
canReadPackage,
canReadRepoPackages,
isRepoMember,
__setPackageAccessDepsForTests,
} from "../lib/package-access";
const PUBLIC_REPO = { id: "repo-public", isPrivate: false };
const PRIVATE_REPO = { id: "repo-private", isPrivate: true };
const OWNER_ID = "user-owner";
const COLLAB_ID = "user-collab";
const STRANGER_ID = "user-stranger";
async function fakeResolve(args: {
repoId: string;
userId: string | null;
isPublic: boolean;
}): Promise<RepoAccessLevel> {
if (args.userId === OWNER_ID) return "owner";
if (args.userId === COLLAB_ID) return "read";
return args.isPublic ? "read" : "none";
}
const REPOS_BY_ID: Record<string, { id: string; isPrivate: boolean }> = {
[PUBLIC_REPO.id]: PUBLIC_REPO,
[PRIVATE_REPO.id]: PRIVATE_REPO,
};
beforeEach(() => {
__setPackageAccessDepsForTests({
resolveRepoAccess: fakeResolve as any,
loadRepoById: async (id: string) => REPOS_BY_ID[id] ?? null,
});
});
afterAll(() => {
__setPackageAccessDepsForTests(null);
});
function pkg(overrides: Partial<Package>): Package {
return {
id: "pkg-1",
repositoryId: PUBLIC_REPO.id,
ecosystem: "npm",
scope: null,
name: "widget",
visibility: "public",
...overrides,
} as Package;
}
describe("package read access — repository is private", () => {
it("denies an anonymous caller", async () => {
expect(await canReadRepoPackages(PRIVATE_REPO, null)).toBe(false);
});
it("denies a signed-in stranger", async () => {
expect(await canReadRepoPackages(PRIVATE_REPO, STRANGER_ID)).toBe(false);
});
it("allows the repository owner", async () => {
expect(await canReadRepoPackages(PRIVATE_REPO, OWNER_ID)).toBe(true);
});
it("allows an accepted collaborator", async () => {
expect(await canReadRepoPackages(PRIVATE_REPO, COLLAB_ID)).toBe(true);
});
});
describe("package read access — repository is public", () => {
it("allows an anonymous caller", async () => {
expect(await canReadRepoPackages(PUBLIC_REPO, null)).toBe(true);
});
it("still denies a package explicitly marked private", async () => {
expect(await canReadRepoPackages(PUBLIC_REPO, null, "private")).toBe(
false
);
expect(await canReadRepoPackages(PUBLIC_REPO, STRANGER_ID, "private")).toBe(
false
);
});
it("allows members to see an explicitly private package", async () => {
expect(await canReadRepoPackages(PUBLIC_REPO, OWNER_ID, "private")).toBe(
true
);
expect(await canReadRepoPackages(PUBLIC_REPO, COLLAB_ID, "private")).toBe(
true
);
});
});
describe("canReadPackage resolves the owning repository live", () => {
it("denies an anonymous caller a package in a private repo", async () => {
const stale = pkg({ repositoryId: PRIVATE_REPO.id, visibility: "public" });
expect(await canReadPackage(stale, null)).toBe(false);
});
it("allows the owner the same package", async () => {
const stale = pkg({ repositoryId: PRIVATE_REPO.id, visibility: "public" });
expect(await canReadPackage(stale, OWNER_ID)).toBe(true);
});
it("allows an anonymous caller a public package in a public repo", async () => {
expect(await canReadPackage(pkg({}), null)).toBe(true);
});
it("denies when the owning repository no longer exists", async () => {
const dangling = pkg({ repositoryId: "repo-deleted" });
expect(await canReadPackage(dangling, OWNER_ID)).toBe(false);
});
});
describe("isRepoMember", () => {
it("is false for anonymous even on a public repo", async () => {
expect(await isRepoMember(PUBLIC_REPO.id, null)).toBe(false);
});
it("is false for a signed-in stranger", async () => {
expect(await isRepoMember(PUBLIC_REPO.id, STRANGER_ID)).toBe(false);
});
it("is true for the owner and for a collaborator", async () => {
expect(await isRepoMember(PUBLIC_REPO.id, OWNER_ID)).toBe(true);
expect(await isRepoMember(PUBLIC_REPO.id, COLLAB_ID)).toBe(true);
});
});
function sourceWithoutLineComments(relPath: string): string {
const text = readFileSync(join(import.meta.dir, "..", relPath), "utf8");
const stripped = text
.split("\n")
.filter((l) => !l.trim().startsWith("//"))
.join("\n");
expect(stripped.length).toBeGreaterThan(0);
return stripped;
}
function handlerBody(src: string, startAnchor: string, endAnchor: string) {
const start = src.indexOf(startAnchor);
expect(start).toBeGreaterThan(-1);
const end = src.indexOf(endAnchor, start + startAnchor.length);
expect(end).toBeGreaterThan(start);
const body = src.slice(start, end);
expect(body.length).toBeGreaterThan(0);
return body;
}
describe("registry read routes invoke the access gate", () => {
it("GET /npm/ checks canReadPackage before serving", async () => {
const src = sourceWithoutLineComments("routes/packages-api.ts");
const body = handlerBody(src, `api.get("/npm/*"`, `api.put("/npm/*"`);
expect(body).toContain("canReadPackage");
const gateAt = body.indexOf("canReadPackage");
const tarballAt = body.indexOf("match.tarball");
expect(tarballAt).toBeGreaterThan(gateAt);
});
it("the JSON package routes check the gate", async () => {
const src = sourceWithoutLineComments("routes/packages-api.ts");
const listBody = handlerBody(
src,
`api.get("/api/packages/:owner/:repo"`,
`api.get("/api/packages/:owner/:repo/:pkgName`
);
expect(listBody).toContain("canReadRepoPackages");
expect(listBody).toContain("isRepoMember");
const detailBody = handlerBody(
src,
`api.get("/api/packages/:owner/:repo/:pkgName`,
`api.get("/npm/*"`
);
expect(detailBody).toContain("canReadRepoPackages");
});
it("the package UI routes filter private packages for non-members", async () => {
const src = sourceWithoutLineComments("routes/packages.tsx");
const listBody = handlerBody(
src,
`ui.get("/:owner/:repo/packages"`,
`ui.get("/:owner/:repo/packages/:pkgName`
);
expect(listBody).toContain("isRepoMember");
expect(listBody).toContain("visiblePkgs.map");
expect(listBody).not.toContain("rows = pkgs.map");
const detailBody = handlerBody(
src,
`ui.get("/:owner/:repo/packages/:pkgName`,
"export default"
);
expect(detailBody).toContain("isRepoMember");
});
});
|