|
| 1 | +// HTTPS CONNECT proxy that only admits github.invalid:443 and serves a minimal GitHub |
| 2 | +// Enterprise API there. Every request (and every refused CONNECT) is appended to a JSONL log. |
| 3 | +import * as NodeChildProcess from "node:child_process"; |
| 4 | +import * as NodeFS from "node:fs"; |
| 5 | +import * as NodeHttp from "node:http"; |
| 6 | +import * as NodeHttps from "node:https"; |
| 7 | +import * as NodePath from "node:path"; |
| 8 | + |
| 9 | +const HOST = "github.invalid"; |
| 10 | + |
| 11 | +export function ensureCertificates(caDir) { |
| 12 | + const leafKey = NodePath.join(caDir, "leaf.key"); |
| 13 | + if (NodeFS.existsSync(leafKey)) return; |
| 14 | + NodeFS.mkdirSync(caDir, { recursive: true }); |
| 15 | + const openssl = (...args) => NodeChildProcess.execFileSync("openssl", args, { stdio: "pipe" }); |
| 16 | + openssl( |
| 17 | + "req", |
| 18 | + "-x509", |
| 19 | + "-newkey", |
| 20 | + "rsa:2048", |
| 21 | + "-nodes", |
| 22 | + "-days", |
| 23 | + "2", |
| 24 | + "-subj", |
| 25 | + "/CN=pr-22943 throwaway CA", |
| 26 | + "-keyout", |
| 27 | + NodePath.join(caDir, "ca.key"), |
| 28 | + "-out", |
| 29 | + NodePath.join(caDir, "ca.pem"), |
| 30 | + ); |
| 31 | + NodeFS.writeFileSync(NodePath.join(caDir, "leaf.ext"), `subjectAltName=DNS:${HOST}\n`); |
| 32 | + openssl( |
| 33 | + "req", |
| 34 | + "-newkey", |
| 35 | + "rsa:2048", |
| 36 | + "-nodes", |
| 37 | + "-subj", |
| 38 | + `/CN=${HOST}`, |
| 39 | + "-keyout", |
| 40 | + leafKey, |
| 41 | + "-out", |
| 42 | + NodePath.join(caDir, "leaf.csr"), |
| 43 | + ); |
| 44 | + openssl( |
| 45 | + "x509", |
| 46 | + "-req", |
| 47 | + "-in", |
| 48 | + NodePath.join(caDir, "leaf.csr"), |
| 49 | + "-CA", |
| 50 | + NodePath.join(caDir, "ca.pem"), |
| 51 | + "-CAkey", |
| 52 | + NodePath.join(caDir, "ca.key"), |
| 53 | + "-CAcreateserial", |
| 54 | + "-days", |
| 55 | + "2", |
| 56 | + "-extfile", |
| 57 | + NodePath.join(caDir, "leaf.ext"), |
| 58 | + "-out", |
| 59 | + NodePath.join(caDir, "leaf.pem"), |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +/** @param {{ caDir: string, logFile: string, repoInfo: "ok" | "404" }} options */ |
| 64 | +export async function startForgeStub({ caDir, logFile, repoInfo }) { |
| 65 | + ensureCertificates(caDir); |
| 66 | + const log = (entry) => |
| 67 | + NodeFS.appendFileSync( |
| 68 | + logFile, |
| 69 | + JSON.stringify({ at: new Date().toISOString(), ...entry }) + "\n", |
| 70 | + ); |
| 71 | + let created = null; |
| 72 | + |
| 73 | + const api = NodeHttps.createServer( |
| 74 | + { |
| 75 | + key: NodeFS.readFileSync(NodePath.join(caDir, "leaf.key")), |
| 76 | + cert: NodeFS.readFileSync(NodePath.join(caDir, "leaf.pem")), |
| 77 | + }, |
| 78 | + (req, res) => { |
| 79 | + let raw = ""; |
| 80 | + req.on("data", (chunk) => (raw += chunk)); |
| 81 | + req.on("end", () => { |
| 82 | + let body = null; |
| 83 | + try { |
| 84 | + body = raw ? JSON.parse(raw) : null; |
| 85 | + } catch { |
| 86 | + body = raw; |
| 87 | + } |
| 88 | + const json = (status, value) => { |
| 89 | + log({ kind: "request", method: req.method, url: req.url, body, status, response: value }); |
| 90 | + res.writeHead(status, { "content-type": "application/json" }).end(JSON.stringify(value)); |
| 91 | + }; |
| 92 | + const path = (req.url ?? "").split("?")[0]; |
| 93 | + if (req.method === "GET" && path === "/api/v3/repos/acme/demo") { |
| 94 | + return repoInfo === "404" |
| 95 | + ? json(404, { message: "Not Found" }) |
| 96 | + : json(200, { |
| 97 | + id: 1, |
| 98 | + node_id: "R_1", |
| 99 | + name: "demo", |
| 100 | + full_name: "acme/demo", |
| 101 | + private: false, |
| 102 | + owner: { login: "acme", type: "Organization" }, |
| 103 | + html_url: `https://${HOST}/acme/demo`, |
| 104 | + clone_url: `https://${HOST}/acme/demo.git`, |
| 105 | + ssh_url: `git@${HOST}:acme/demo.git`, |
| 106 | + default_branch: "main", |
| 107 | + }); |
| 108 | + } |
| 109 | + if (req.method === "POST" && path === "/api/graphql") { |
| 110 | + const query = String(body?.query ?? ""); |
| 111 | + if (query.includes("pullRequests(")) { |
| 112 | + // Batched queries alias each field as `_<n>_repository` with `_<n>_head` variables. |
| 113 | + const prefixes = [ |
| 114 | + ...query.matchAll(/(_\d+_)?repository:repository\(|repository\(/g), |
| 115 | + ].map((match) => match[1] ?? ""); |
| 116 | + const data = {}; |
| 117 | + for (const prefix of new Set(prefixes)) { |
| 118 | + const head = body?.variables?.[`${prefix}head`]; |
| 119 | + const nodes = created && created.head === head ? [created.node] : []; |
| 120 | + data[`${prefix}repository`] = { |
| 121 | + pullRequests: { pageInfo: { hasNextPage: false, endCursor: null }, nodes }, |
| 122 | + }; |
| 123 | + } |
| 124 | + return json(200, { data }); |
| 125 | + } |
| 126 | + return json(200, { errors: [{ message: "stub: unsupported query" }] }); |
| 127 | + } |
| 128 | + if (req.method === "POST" && path === "/api/v3/repos/acme/demo/pulls") { |
| 129 | + const node = { |
| 130 | + number: 7, |
| 131 | + title: body.title, |
| 132 | + url: `https://${HOST}/acme/demo/pull/7`, |
| 133 | + baseRefName: body.base, |
| 134 | + headRefName: body.head, |
| 135 | + headRefOid: "0".repeat(40), |
| 136 | + state: "OPEN", |
| 137 | + isDraft: false, |
| 138 | + mergedAt: null, |
| 139 | + closedAt: null, |
| 140 | + updatedAt: new Date().toISOString(), |
| 141 | + isCrossRepository: false, |
| 142 | + headRepository: { name: "demo", nameWithOwner: "acme/demo" }, |
| 143 | + headRepositoryOwner: { __typename: "Organization", login: "acme" }, |
| 144 | + }; |
| 145 | + created = { head: body.head, node }; |
| 146 | + log({ kind: "forge-create-pr", body }); |
| 147 | + return json(201, { |
| 148 | + number: 7, |
| 149 | + html_url: node.url, |
| 150 | + base: { ref: body.base }, |
| 151 | + head: { ref: body.head }, |
| 152 | + title: body.title, |
| 153 | + }); |
| 154 | + } |
| 155 | + return json(404, { message: "stub: not handled" }); |
| 156 | + }); |
| 157 | + }, |
| 158 | + ); |
| 159 | + api.on("tlsClientError", (error) => log({ kind: "tls-error", message: error.message })); |
| 160 | + |
| 161 | + const proxy = NodeHttp.createServer((req, res) => { |
| 162 | + log({ kind: "plain-http-refused", method: req.method, url: req.url }); |
| 163 | + res.writeHead(403).end(); |
| 164 | + }); |
| 165 | + proxy.on("connect", (req, socket, head) => { |
| 166 | + if (req.url !== `${HOST}:443`) { |
| 167 | + log({ kind: "connect-refused", target: req.url }); |
| 168 | + socket.end("HTTP/1.1 403 Forbidden\r\n\r\n"); |
| 169 | + return; |
| 170 | + } |
| 171 | + log({ kind: "connect", target: req.url }); |
| 172 | + socket.write("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 173 | + if (head.length > 0) socket.unshift(head); |
| 174 | + api.emit("connection", socket); |
| 175 | + }); |
| 176 | + proxy.listen(0, "127.0.0.1"); |
| 177 | + await new Promise((done) => proxy.once("listening", done)); |
| 178 | + return { |
| 179 | + port: proxy.address().port, |
| 180 | + close: () => |
| 181 | + new Promise((done) => { |
| 182 | + proxy.closeAllConnections?.(); |
| 183 | + api.closeAllConnections?.(); |
| 184 | + proxy.close(() => done()); |
| 185 | + }), |
| 186 | + }; |
| 187 | +} |
0 commit comments