Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesPull RequestsActionsSecurityInsightsSettings
✨ AI
More
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepfix/audit-sweep-2026-07-26gatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
package-access.test.ts8.8 KB · 246 lines
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
/**
 * Regression guard: the package registry must not serve private packages.
 *
 * The bug this locks down: `GET /npm/...` resolved a package by name and
 * returned its packument and tarball with no authorization check whatsoever.
 * The platform-wide private-repo gate in app.tsx keys on the owner/repo path
 * shape, which the npm protocol routes do not have (they address packages by
 * package name alone) and which the JSON helper routes under the api prefix
 * also miss. So `npm install` returned the FULL SOURCE of a private
 * repository's package to an anonymous caller.
 *
 * Two layers are asserted here:
 *   1. The decision rule in lib/package-access.ts, driven through its
 *      injectable-deps seam so no database is required.
 *   2. That the read routes actually still CALL that rule — a rule nothing
 *      invokes is worth nothing, and the original bug was a missing call.
 *
 * Deliberately no `mock.module`: bun loads every test module before running
 * any test, and mock.module swaps the registry entry rather than rebinding
 * namespaces an importer already holds, so mocking `../db` here would leak
 * into unrelated files in a full run.
 */

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";

/**
 * Stand-in for the real resolver. Mirrors its contract: the owner and an
 * accepted collaborator get a level regardless of visibility; everyone else
 * falls back to "read" for public repos and "none" for private ones.
 */
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 () => {
    // visibility is stamped at publish time and never rewritten, so a
    // package marked private must stay private even in a public repo.
    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 () => {
    // The package row still says "public" because it was published while the
    // repo was public; the LIVE repo row is what must decide.
    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 () => {
    // Membership is not the same as readability — a public reader is not a
    // member, which is what decides visibility of private packages.
    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);
  });
});

// --- call sites stay wired -------------------------------------------------
//
// Strip ONLY line comments before asserting. A block-comment regex eats route
// paths (they contain a slash followed by a star), and these files are full
// of them.

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;
}

/** Slice by anchor, never by character count — handlers move. */
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(");
    // The tarball branch lives inside this handler, so gating the handler
    // gates the tarball too — assert the gate precedes the tarball read.
    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(");
    // The rendered rows must come from the filtered list, not the raw query.
    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(");
  });
});