Repository navigation
Expand file tree
/
Copy pathapp-auth.test.ts
More file actions
125 lines (115 loc) · 4.15 KB
/
Copy pathapp-auth.test.ts
File metadata and controls
125 lines (115 loc) · 4.15 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
import { afterEach, describe, expect, test } from "bun:test";
import { generateKeyPairSync } from "node:crypto";
import type { GithubAppConfig } from "../env";
import {
clearInstallationTokenCache,
getInstallationToken,
getInstallationTokenForId,
getRepositoryInstallationTokenForId,
getRepositoryPublicationTokenForId,
} from "./app-auth";
const originalFetch = globalThis.fetch;
afterEach(() => {
globalThis.fetch = originalFetch;
clearInstallationTokenCache();
});
describe("GitHub repository installation token permissions", () => {
test("both installation-wide read paths explicitly exclude publication writes", async () => {
const { privateKey } = generateKeyPairSync("rsa", { modulusLength: 2_048 });
const config: GithubAppConfig = {
appId: "4689651",
privateKey: privateKey.export({ type: "pkcs8", format: "pem" }).toString(),
org: "acme",
};
const requests: unknown[] = [];
globalThis.fetch = (async (input, init) => {
if (String(input).endsWith("/app/installations")) {
return Response.json([{ id: 123, account: { login: "acme" } }]);
}
requests.push(init?.body ? JSON.parse(String(init.body)) : null);
return Response.json({ token: "fixture-token", expires_at: "2099-01-01T00:00:00.000Z" });
}) as typeof fetch;
await getInstallationToken(config);
await getInstallationTokenForId(123, config);
expect(requests).toEqual([0, 1].map(() => ({ permissions: {
contents: "read", issues: "read", metadata: "read", pull_requests: "read",
} })));
});
test("caches read and publication credentials separately with least privilege", async () => {
const { privateKey } = generateKeyPairSync("rsa", { modulusLength: 2_048 });
const config: GithubAppConfig = {
appId: "4689651",
privateKey: privateKey
.export({ type: "pkcs8", format: "pem" })
.toString(),
org: null,
};
const requests: Array<Record<string, unknown>> = [];
globalThis.fetch = (async (_input, init) => {
requests.push(JSON.parse(String(init?.body)) as Record<string, unknown>);
return Response.json({
token: `installation-token-${requests.length}`,
expires_at: "2099-01-01T00:00:00.000Z",
});
}) as typeof fetch;
const read = await getRepositoryInstallationTokenForId(
"acme/widget",
123,
config,
);
const cachedRead = await getRepositoryInstallationTokenForId(
"acme/widget",
123,
config,
);
const publication = await getRepositoryPublicationTokenForId(
"acme/widget",
123,
config,
);
expect(read.token).toBe("installation-token-1");
expect(cachedRead.token).toBe(read.token);
expect(publication.token).toBe("installation-token-2");
expect(requests).toEqual([
{
repositories: ["widget"],
permissions: { contents: "read", metadata: "read" },
},
{
repositories: ["widget"],
permissions: {
contents: "write",
metadata: "read",
pull_requests: "write",
},
},
]);
});
});
describe("GitHub App token mint refusals", () => {
test("a 422 on a publication mint names the permissions the App lacks", async () => {
const { privateKey } = generateKeyPairSync("rsa", { modulusLength: 2_048 });
const config: GithubAppConfig = {
appId: "4689651",
privateKey: privateKey.export({ type: "pkcs8", format: "pem" }).toString(),
org: null,
};
globalThis.fetch = (async (_input, _init) =>
Response.json({ message: "Validation Failed" }, { status: 422 })) as typeof fetch;
await expect(getRepositoryPublicationTokenForId("acme/widget", 123, config)).rejects.toThrow(
/HTTP 422 \(Validation Failed\); this usually means the App or its installation lacks one of the requested permissions \(contents:write, metadata:read, pull_requests:write\)/,
);
});
test("other refusals keep the bare status", async () => {
const { privateKey } = generateKeyPairSync("rsa", { modulusLength: 2_048 });
const config: GithubAppConfig = {
appId: "4689651",
privateKey: privateKey.export({ type: "pkcs8", format: "pem" }).toString(),
org: null,
};
globalThis.fetch = (async (_input, _init) => new Response("nope", { status: 401 })) as typeof fetch;
await expect(getRepositoryInstallationTokenForId("acme/widget", 123, config)).rejects.toThrow(
/HTTP 401$/,
);
});
});