~james/core

ref: 452f17b75181d62d1d92016edecd38e1d70fecab core/web/src/lib/server/github/api.ts -rw-r--r-- 7.2 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
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
import type { GitHubAccount, GitHubKey, GitHubRepo } from "#lib/github.ts";
import { type GitHubEnv, hostOf } from "#lib/server/github/env.ts";
import type { Fetch } from "#lib/fetch.ts";

export class GitHubUpstreamError extends Error {
	constructor(public status: number) {
		super("github upstream");
	}
}

const malformed = () => new Error("github malformed response");

type Json = Record<string, unknown>;
const object = (value: unknown): Json | null =>
	value !== null && typeof value === "object" && !Array.isArray(value) ? (value as Json) : null;
const text = (o: Json, key: string) => (typeof o[key] === "string" ? o[key] : null);
const integer = (o: Json, key: string) =>
	typeof o[key] === "number" && Number.isInteger(o[key]) ? o[key] : null;

interface GitHubCaller {
	env: GitHubEnv;
	token: string;
	fetch: Fetch;
}

const send = async (call: GitHubCaller, url: string) => {
	const response = await call.fetch(url, {
		redirect: "error",
		headers: {
			accept: "application/vnd.github+json",
			authorization: `Bearer ${call.token}`,
			"user-agent": "tangled"
		}
	});
	if (!response.ok) throw new GitHubUpstreamError(response.status);
	return { body: (await response.json()) as unknown, link: response.headers.get("link") };
};

const one = async (call: GitHubCaller, path: string): Promise<Json> => {
	const { body } = await send(call, `${call.env.apiOrigin}${path}`);
	const value = object(body);
	if (!value) throw malformed();
	return value;
};

const NEXT = /<([^>]+)>;\s*rel="next"/;
// preserve configured origin to prevent pagination SSRF
const nextPage = (link: string | null, origin: string) => {
	const raw = link
		?.split(",")
		.map((part) => NEXT.exec(part.trim())?.[1])
		.find((href): href is string => href !== undefined);
	if (!raw) return null;
	const next = URL.parse(raw);
	if (!next || next.origin !== origin) throw malformed();
	return next.toString();
};

const PAGE_LIMIT = 100;
const all = async (
	call: GitHubCaller,
	path: string,
	rows: (body: unknown) => unknown = (body) => body
): Promise<Json[]> => {
	const out: Json[] = [];
	let url: string | null =
		`${call.env.apiOrigin}${path}${path.includes("?") ? "&" : "?"}per_page=100`;
	for (let page = 0; page < PAGE_LIMIT; page++) {
		const { body, link } = await send(call, url);
		const value = rows(body);
		if (!Array.isArray(value)) throw malformed();
		for (const row of value) {
			const item = object(row);
			if (!item) throw malformed();
			out.push(item);
		}
		url = nextPage(link, call.env.apiOrigin);
		if (!url) return out;
	}
	throw new Error("github pagination limit exceeded");
};

const inside = (key: string) => (body: unknown) => object(body)?.[key];

const sameHost = (value: string, host: string) => {
	const url = URL.parse(value);
	return (
		url !== null &&
		url.protocol === "https:" &&
		url.hostname === host &&
		!url.username &&
		!url.password
	);
};

const repo = (env: GitHubEnv, row: Json, installationId?: number): GitHubRepo | null => {
	const host = hostOf(env.publicOrigin);
	const id = integer(row, "id"),
		name = text(row, "name"),
		fullName = text(row, "full_name"),
		cloneUrl = text(row, "clone_url"),
		htmlUrl = text(row, "html_url"),
		visibility = text(row, "visibility"),
		privateFlag = row.private;
	if (
		id === null ||
		name === null ||
		fullName === null ||
		cloneUrl === null ||
		htmlUrl === null ||
		!sameHost(cloneUrl, host) ||
		!sameHost(htmlUrl, host) ||
		(privateFlag !== undefined && typeof privateFlag !== "boolean")
	)
		return null;
	const isPrivate = privateFlag === true || visibility === "private" || visibility === "internal";
	const language = text(row, "language"),
		stars = integer(row, "stargazers_count"),
		issues = integer(row, "open_issues_count"),
		forks = integer(row, "forks_count"),
		sizeKb = integer(row, "size"),
		updatedAt = text(row, "pushed_at") ?? text(row, "updated_at");
	return {
		id,
		name,
		fullName,
		description: text(row, "description"),
		cloneUrl,
		htmlUrl,
		defaultBranch: text(row, "default_branch") ?? "",
		visibility:
			visibility === "public" || visibility === "private" || visibility === "internal"
				? visibility
				: isPrivate
					? "private"
					: "public",
		private: isPrivate,
		installationId,
		language,
		stars,
		issues,
		forks,
		updatedAt,
		sizeKb
	};
};

const key = (row: Json): GitHubKey | null => {
	const id = integer(row, "id"),
		value = text(row, "key");
	return id === null || value === null
		? null
		: { id, key: value, title: text(row, "title") ?? "" };
};

const emailRow = (row: Json) => {
	const address = text(row, "email");
	return address === null
		? null
		: { address, primary: row.primary === true, verified: row.verified === true };
};

// noreply addresses cannot receive deliberi verification email
const NOREPLY = /@(?:users\.)?noreply\.github\.com$/i;
const primaryEmail = (rows: Json[]) => {
	const usable = rows.flatMap((row) => {
		const parsed = emailRow(row);
		return parsed === null || !parsed.verified || NOREPLY.test(parsed.address) ? [] : [parsed];
	});
	return (usable.find((e) => e.primary) ?? usable[0])?.address ?? null;
};

export const account = async (
	env: GitHubEnv,
	token: string,
	fetcher: Fetch = fetch
): Promise<GitHubAccount> => {
	const call: GitHubCaller = { env, token, fetch: fetcher };
	const [me, emails, keys] = await Promise.all([
		one(call, "/user"),
		all(call, "/user/emails"),
		all(call, "/user/keys")
	]);
	const login = text(me, "login");
	const [owned, installations] = await Promise.all([
		login === null ? [] : all(call, `/users/${encodeURIComponent(login)}/repos?type=owner`),
		all(call, "/user/installations", inside("installations"))
	]);
	const granted = await Promise.all(
		installations.flatMap((row) => {
			const id = integer(row, "id");
			return id === null
				? []
				: [
						all(
							call,
							`/user/installations/${id}/repositories`,
							inside("repositories")
						).then((rows) => rows.map((r) => repo(env, r, id)))
					];
		})
	);
	const unique = new Map(
		[...owned.map((row) => repo(env, row)), ...granted.flat()].flatMap((r) =>
			r === null ? [] : [[r.id, r] as const]
		)
	);
	return {
		login: login ?? "",
		avatarUrl: text(me, "avatar_url") ?? "",
		bio: text(me, "bio"),
		blog: text(me, "blog"),
		email: primaryEmail(emails),
		emails: emails.flatMap((row) => {
			const parsed = emailRow(row);
			return parsed === null ? [] : [parsed];
		}),
		keys: keys.flatMap((row) => {
			const parsed = key(row);
			return parsed === null ? [] : [parsed];
		}),
		repos: [...unique.values()].sort((a, b) => a.name.localeCompare(b.name)),
		publicOrigin: env.publicOrigin
	};
};

export const hasInstallations = async (env: GitHubEnv, token: string, fetcher: Fetch = fetch) =>
	(await all({ env, token, fetch: fetcher }, "/user/installations", inside("installations")))
		.length > 0;

// pinned to avatarsOrigin to prevent SSRF
export const avatarSource = async (
	env: GitHubEnv,
	token: string,
	fetcher: Fetch = fetch
): Promise<URL> => {
	const me = await one({ env, token, fetch: fetcher }, "/user");
	const raw = text(me, "avatar_url");
	const avatar = raw === null ? null : URL.parse(raw);
	if (!avatar || avatar.origin !== env.avatarsOrigin || avatar.username || avatar.password)
		throw malformed();
	// default github avatar size is ~40px without explicit size parameter
	avatar.searchParams.set("s", "256");
	return avatar;
};