bugxhunter 0.0.0-stage → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +41 -0
- package/LICENSE +21 -0
- package/README.md +347 -2
- package/SECURITY.md +121 -0
- package/bin/bugxhunter.mjs +154 -0
- package/bugxhunter-0.5.0.tgz +0 -0
- package/docker-compose.yml +118 -0
- package/package.json +37 -4
- package/scripts/vault-reset.mjs +30 -0
- package/server/dist/attachments.js +67 -0
- package/server/dist/audit.js +123 -0
- package/server/dist/auth.js +248 -0
- package/server/dist/env.js +15 -0
- package/server/dist/errors.js +3 -0
- package/server/dist/github.js +462 -0
- package/server/dist/index.js +783 -0
- package/server/dist/opencode.js +421 -0
- package/server/dist/providers.js +187 -0
- package/server/dist/rag.js +100 -0
- package/server/dist/scx.js +146 -0
- package/server/dist/vault.js +286 -0
- package/web/dist/assets/index-CqhtWJfv.css +1 -0
- package/web/dist/assets/index-DFfqRBl8.js +118 -0
- package/web/dist/claude-logo-ivory.svg +17 -0
- package/web/dist/example-tools.json +1 -0
- package/web/dist/favicon.png +0 -0
- package/web/dist/index.html +20 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sign-in with the vault's passkey. No passwords.
|
|
3
|
+
*
|
|
4
|
+
* Until a vault with a sign-in-capable passkey exists, the API is open: the
|
|
5
|
+
* server listens on localhost only and refuses foreign Host headers, so that
|
|
6
|
+
* is the same trust boundary as a local tool. Once a passkey is enrolled, every
|
|
7
|
+
* /api route except /api/auth/* and /api/health needs a session cookie, issued
|
|
8
|
+
* after one of:
|
|
9
|
+
*
|
|
10
|
+
* - a WebAuthn assertion verified here against the public key captured at
|
|
11
|
+
* enrolment (single-use challenge, allowed origin, rpId hash, user presence
|
|
12
|
+
* and verification flags, signature, monotonic counter when the
|
|
13
|
+
* authenticator keeps one); or
|
|
14
|
+
* - proof of the vault's data key (the recovery path: the browser unwrapped it
|
|
15
|
+
* with the recovery code), which also unseals the vault.
|
|
16
|
+
*
|
|
17
|
+
* The cookie is `<expiry>.<hmac>` under OPEN_RUNNER_SECRET, or a random
|
|
18
|
+
* per-process secret, in which case a restart signs everyone out.
|
|
19
|
+
*/
|
|
20
|
+
import crypto from "node:crypto";
|
|
21
|
+
import * as vault from "./vault.js";
|
|
22
|
+
import { audit } from "./audit.js";
|
|
23
|
+
const SECRET = process.env.OPEN_RUNNER_SECRET || crypto.randomBytes(32).toString("hex");
|
|
24
|
+
const COOKIE = "or_session";
|
|
25
|
+
const MAX_AGE_S = 7 * 24 * 3600;
|
|
26
|
+
const CHALLENGE_TTL_MS = 2 * 60_000;
|
|
27
|
+
export const authRequired = () => vault.loginCredentials().length > 0;
|
|
28
|
+
const sign = (payload) => crypto.createHmac("sha256", SECRET).update(payload).digest("base64url");
|
|
29
|
+
function safeEqual(a, b) {
|
|
30
|
+
const ha = crypto.createHash("sha256").update(a).digest();
|
|
31
|
+
const hb = crypto.createHash("sha256").update(b).digest();
|
|
32
|
+
return crypto.timingSafeEqual(ha, hb);
|
|
33
|
+
}
|
|
34
|
+
function readCookie(req, name) {
|
|
35
|
+
for (const part of (req.headers.cookie ?? "").split(";")) {
|
|
36
|
+
const [k, ...v] = part.trim().split("=");
|
|
37
|
+
if (k === name)
|
|
38
|
+
return decodeURIComponent(v.join("="));
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
export function isAuthenticated(req) {
|
|
43
|
+
if (!authRequired())
|
|
44
|
+
return true;
|
|
45
|
+
const token = readCookie(req, COOKIE);
|
|
46
|
+
if (!token)
|
|
47
|
+
return false;
|
|
48
|
+
const [exp, mac] = token.split(".");
|
|
49
|
+
if (!exp || !mac || Number(exp) < Date.now() / 1000)
|
|
50
|
+
return false;
|
|
51
|
+
return safeEqual(mac, sign(exp));
|
|
52
|
+
}
|
|
53
|
+
function setCookie(req, res, value, maxAge) {
|
|
54
|
+
const secure = req.secure || req.headers["x-forwarded-proto"] === "https" || process.env.COOKIE_SECURE === "1";
|
|
55
|
+
res.setHeader("Set-Cookie", `${COOKIE}=${encodeURIComponent(value)}; Path=/; HttpOnly; SameSite=Strict; Max-Age=${maxAge}${secure ? "; Secure" : ""}`);
|
|
56
|
+
}
|
|
57
|
+
/** Start a session for this browser (after a verified assertion, a recovery proof, or creating the vault). */
|
|
58
|
+
export function issueSession(req, res) {
|
|
59
|
+
const exp = String(Math.floor(Date.now() / 1000) + MAX_AGE_S);
|
|
60
|
+
setCookie(req, res, `${exp}.${sign(exp)}`, MAX_AGE_S);
|
|
61
|
+
}
|
|
62
|
+
// --- challenges: single use, short-lived, bounded -------------------------------
|
|
63
|
+
const challenges = new Map();
|
|
64
|
+
export function issueChallenge() {
|
|
65
|
+
const now = Date.now();
|
|
66
|
+
for (const [c, exp] of challenges)
|
|
67
|
+
if (exp < now)
|
|
68
|
+
challenges.delete(c);
|
|
69
|
+
if (challenges.size > 1000)
|
|
70
|
+
challenges.clear();
|
|
71
|
+
const c = crypto.randomBytes(32).toString("base64url");
|
|
72
|
+
challenges.set(c, now + CHALLENGE_TTL_MS);
|
|
73
|
+
return c;
|
|
74
|
+
}
|
|
75
|
+
function consumeChallenge(c) {
|
|
76
|
+
const exp = challenges.get(c);
|
|
77
|
+
challenges.delete(c);
|
|
78
|
+
return exp !== undefined && exp >= Date.now();
|
|
79
|
+
}
|
|
80
|
+
// --- WebAuthn assertion verification --------------------------------------------
|
|
81
|
+
const sha256 = (b) => crypto.createHash("sha256").update(b).digest();
|
|
82
|
+
/** Hostname of an origin, without an IPv6's brackets, lower-cased. */
|
|
83
|
+
function hostOf(origin) {
|
|
84
|
+
const u = new URL(origin);
|
|
85
|
+
return u.hostname.toLowerCase();
|
|
86
|
+
}
|
|
87
|
+
export class AssertionError extends Error {
|
|
88
|
+
constructor(message) { super(message); this.name = "AssertionError"; }
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Verify a WebAuthn assertion against the enrolled passkeys. Returns the
|
|
92
|
+
* method that signed. Throws AssertionError with a reason safe to show.
|
|
93
|
+
*/
|
|
94
|
+
export function verifyAssertion(a, allowedHosts) {
|
|
95
|
+
const cred = vault.loginCredentials().find((m) => m.credentialId === a.credentialId);
|
|
96
|
+
if (!cred)
|
|
97
|
+
throw new AssertionError("Unknown passkey.");
|
|
98
|
+
// clientDataJSON: type, our challenge, an origin we serve.
|
|
99
|
+
let cd;
|
|
100
|
+
const cdBytes = Buffer.from(a.clientDataJSON, "base64url");
|
|
101
|
+
try {
|
|
102
|
+
cd = JSON.parse(cdBytes.toString("utf8"));
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
throw new AssertionError("Malformed client data.");
|
|
106
|
+
}
|
|
107
|
+
if (cd.type !== "webauthn.get")
|
|
108
|
+
throw new AssertionError("Not a sign-in assertion.");
|
|
109
|
+
if (!cd.challenge || !consumeChallenge(cd.challenge))
|
|
110
|
+
throw new AssertionError("Challenge expired or already used. Try again.");
|
|
111
|
+
let host;
|
|
112
|
+
try {
|
|
113
|
+
host = hostOf(cd.origin ?? "");
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
throw new AssertionError("Malformed origin.");
|
|
117
|
+
}
|
|
118
|
+
if (!allowedHosts.has(host))
|
|
119
|
+
throw new AssertionError(`Origin ${cd.origin} is not allowed.`);
|
|
120
|
+
// authenticatorData: rpIdHash, flags (UP, UV), counter.
|
|
121
|
+
const authData = Buffer.from(a.authenticatorData, "base64url");
|
|
122
|
+
if (authData.length < 37)
|
|
123
|
+
throw new AssertionError("Malformed authenticator data.");
|
|
124
|
+
if (!crypto.timingSafeEqual(authData.subarray(0, 32), sha256(Buffer.from(host, "utf8")))) {
|
|
125
|
+
throw new AssertionError("The passkey was made for a different site name.");
|
|
126
|
+
}
|
|
127
|
+
const flags = authData[32];
|
|
128
|
+
if (!(flags & 0x01))
|
|
129
|
+
throw new AssertionError("User presence not confirmed.");
|
|
130
|
+
if (!(flags & 0x04))
|
|
131
|
+
throw new AssertionError("User verification (PIN, biometric) is required.");
|
|
132
|
+
const counter = authData.readUInt32BE(33);
|
|
133
|
+
if (counter > 0 && (cred.signCount ?? 0) > 0 && counter <= (cred.signCount ?? 0)) {
|
|
134
|
+
audit("auth.counter.regressed", { credentialId: a.credentialId, seen: cred.signCount, got: counter });
|
|
135
|
+
throw new AssertionError("Signature counter went backwards — a cloned authenticator? Sign in with another passkey or the recovery code.");
|
|
136
|
+
}
|
|
137
|
+
// signature over authenticatorData || SHA-256(clientDataJSON), with the enrolled key.
|
|
138
|
+
const signed = Buffer.concat([authData, sha256(cdBytes)]);
|
|
139
|
+
const sig = Buffer.from(a.signature, "base64url");
|
|
140
|
+
let key;
|
|
141
|
+
try {
|
|
142
|
+
key = crypto.createPublicKey({ key: Buffer.from(cred.publicKey, "base64"), format: "der", type: "spki" });
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
throw new AssertionError("The enrolled public key is unreadable.");
|
|
146
|
+
}
|
|
147
|
+
let ok = false;
|
|
148
|
+
try {
|
|
149
|
+
switch (cred.alg) {
|
|
150
|
+
case -7:
|
|
151
|
+
ok = crypto.verify("sha256", signed, { key, dsaEncoding: "der" }, sig);
|
|
152
|
+
break; // ES256
|
|
153
|
+
case -8:
|
|
154
|
+
ok = crypto.verify(null, signed, key, sig);
|
|
155
|
+
break; // Ed25519
|
|
156
|
+
case -257:
|
|
157
|
+
ok = crypto.verify("sha256", signed, key, sig);
|
|
158
|
+
break; // RS256
|
|
159
|
+
default: throw new AssertionError(`Unsupported algorithm ${cred.alg}.`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
catch (e) {
|
|
163
|
+
if (e instanceof AssertionError)
|
|
164
|
+
throw e;
|
|
165
|
+
ok = false;
|
|
166
|
+
}
|
|
167
|
+
if (!ok)
|
|
168
|
+
throw new AssertionError("Signature did not verify.");
|
|
169
|
+
if (counter > 0)
|
|
170
|
+
vault.recordSignCount(cred.id, counter);
|
|
171
|
+
return { methodId: cred.id, credentialId: cred.credentialId };
|
|
172
|
+
}
|
|
173
|
+
// --- routes ------------------------------------------------------------------------
|
|
174
|
+
export function mountAuth(app, allowedHosts) {
|
|
175
|
+
app.get("/api/auth/me", (req, res) => {
|
|
176
|
+
const me = { required: authRequired(), authenticated: isAuthenticated(req) };
|
|
177
|
+
res.json(me);
|
|
178
|
+
});
|
|
179
|
+
// The sign-in screen needs the public method parameters (to unlock the
|
|
180
|
+
// vault in the same touch, and for the recovery path). Public by design.
|
|
181
|
+
app.get("/api/auth/methods", (_req, res) => res.json({ methods: vault.publicMethods() }));
|
|
182
|
+
app.post("/api/auth/challenge", (req, res) => {
|
|
183
|
+
const creds = vault.loginCredentials();
|
|
184
|
+
if (!creds.length) {
|
|
185
|
+
issueSession(req, res);
|
|
186
|
+
return res.json({ open: true });
|
|
187
|
+
}
|
|
188
|
+
// The page's own host is the relying-party id. Prefer the Origin header
|
|
189
|
+
// (a dev proxy rewrites Host but not Origin); the browser re-derives it anyway.
|
|
190
|
+
let rpId = "localhost";
|
|
191
|
+
try {
|
|
192
|
+
rpId = hostOf(req.headers.origin || `http://${(req.headers.host ?? "localhost").toLowerCase()}`);
|
|
193
|
+
}
|
|
194
|
+
catch { /* keep default */ }
|
|
195
|
+
const body = {
|
|
196
|
+
challenge: issueChallenge(),
|
|
197
|
+
rpId,
|
|
198
|
+
allowCredentials: creds.map((c) => ({ id: c.credentialId, transports: c.transports })),
|
|
199
|
+
};
|
|
200
|
+
res.json(body);
|
|
201
|
+
});
|
|
202
|
+
app.post("/api/auth/passkey", (req, res) => {
|
|
203
|
+
const a = req.body;
|
|
204
|
+
if (!a?.credentialId || !a.clientDataJSON || !a.authenticatorData || !a.signature) {
|
|
205
|
+
return res.status(400).json({ error: { message: "assertion fields required" } });
|
|
206
|
+
}
|
|
207
|
+
try {
|
|
208
|
+
const { credentialId } = verifyAssertion(a, allowedHosts);
|
|
209
|
+
issueSession(req, res);
|
|
210
|
+
audit("auth.signin", { method: "passkey", credentialId });
|
|
211
|
+
res.json({ ok: true });
|
|
212
|
+
}
|
|
213
|
+
catch (e) {
|
|
214
|
+
audit("auth.signin.failed", { method: "passkey", reason: e.message });
|
|
215
|
+
res.status(401).json({ error: { message: e instanceof AssertionError ? e.message : "Sign-in failed." } });
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
// Lost passkey: proving possession of the vault key (unwrapped in the
|
|
219
|
+
// browser with the recovery code) signs in and unseals in one step.
|
|
220
|
+
app.post("/api/auth/recover", (req, res) => {
|
|
221
|
+
if (typeof req.body?.dek !== "string")
|
|
222
|
+
return res.status(400).json({ error: { message: "dek required" } });
|
|
223
|
+
try {
|
|
224
|
+
vault.unseal(req.body.dek); // throws 401 if it doesn't open the verifier
|
|
225
|
+
issueSession(req, res);
|
|
226
|
+
audit("auth.signin", { method: "recovery" });
|
|
227
|
+
res.json({ ok: true });
|
|
228
|
+
}
|
|
229
|
+
catch {
|
|
230
|
+
audit("auth.signin.failed", { method: "recovery" });
|
|
231
|
+
res.status(401).json({ error: { message: "That key does not open the vault." } });
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
app.post("/api/auth/logout", (req, res) => {
|
|
235
|
+
setCookie(req, res, "", 0);
|
|
236
|
+
res.json({ ok: true });
|
|
237
|
+
});
|
|
238
|
+
// Gate everything else under /api.
|
|
239
|
+
app.use("/api", (req, res, next) => {
|
|
240
|
+
if (req.path === "/health" || isAuthenticated(req))
|
|
241
|
+
return next();
|
|
242
|
+
// The header is how the browser tells our 401 apart from an upstream one
|
|
243
|
+
// (e.g. SCX rejecting a key), which must not bounce it to the sign-in screen.
|
|
244
|
+
res.setHeader("WWW-Authenticate", "BXH-Passkey");
|
|
245
|
+
res.status(401).json({ error: { message: "Not signed in.", code: "unauthenticated" } });
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
//# sourceMappingURL=auth.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loads the repo-root .env before any other module reads process.env.
|
|
3
|
+
*
|
|
4
|
+
* Must be the first import in index.ts: ES module imports are evaluated in
|
|
5
|
+
* order, and auth.ts, opencode.ts and audit.ts read their settings at import
|
|
6
|
+
* time. Loading dotenv later (as index.ts once did) meant a `.env` password or
|
|
7
|
+
* model never applied in local mode.
|
|
8
|
+
*/
|
|
9
|
+
import dotenv from "dotenv";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
// Both src/ (tsx) and dist/ (compiled) sit two levels below the repo root.
|
|
14
|
+
dotenv.config({ path: path.resolve(here, "../../.env") });
|
|
15
|
+
//# sourceMappingURL=env.js.map
|