@goosetools/goose-tools 0.0.0-stage → 0.1.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.
Files changed (54) hide show
  1. package/README.md +98 -2
  2. package/bin/goose-tools.js +61 -0
  3. package/lib/check/src/agent.js +90 -0
  4. package/lib/check/src/analyze.js +311 -0
  5. package/lib/check/src/client.js +136 -0
  6. package/lib/check/src/copy.js +355 -0
  7. package/lib/check/src/fixes.js +153 -0
  8. package/lib/check/src/frame.js +92 -0
  9. package/lib/check/src/guard.js +441 -0
  10. package/lib/check/src/index.js +18 -0
  11. package/lib/check/src/mcp-http.js +169 -0
  12. package/lib/check/src/mcp.js +153 -0
  13. package/lib/check/src/scan.js +55 -0
  14. package/lib/check/src/stack.js +61 -0
  15. package/lib/check/src/tlds.js +3 -0
  16. package/lib/check/src/view.js +130 -0
  17. package/lib/check/src/worker.js +257 -0
  18. package/lib/engine/src/bots.js +146 -0
  19. package/lib/engine/src/directives.js +131 -0
  20. package/lib/engine/src/exposure.js +70 -0
  21. package/lib/engine/src/frontend.js +157 -0
  22. package/lib/engine/src/headers.js +76 -0
  23. package/lib/engine/src/hidden-text.js +425 -0
  24. package/lib/engine/src/html.js +794 -0
  25. package/lib/engine/src/index.js +20 -0
  26. package/lib/engine/src/php.js +316 -0
  27. package/lib/engine/src/policies.js +234 -0
  28. package/lib/engine/src/reach.js +199 -0
  29. package/lib/engine/src/robots.js +275 -0
  30. package/lib/engine/src/secrets.js +142 -0
  31. package/lib/engine/src/signals.js +144 -0
  32. package/lib/rules/data/bots.json.js +1 -0
  33. package/lib/rules/data/exposure.json.js +1 -0
  34. package/lib/rules/data/frontend.json.js +1 -0
  35. package/lib/rules/data/headers.json.js +1 -0
  36. package/lib/rules/data/secrets.json.js +1 -0
  37. package/lib/ui/LICENSE-Inter.txt +93 -0
  38. package/lib/ui/assets.js +5 -0
  39. package/package.json +37 -4
  40. package/src/local.js +61 -0
  41. package/src/mcp.js +61 -0
  42. package/src/setup.js +395 -0
  43. package/src/site/auth.js +287 -0
  44. package/src/site/beacon.js +462 -0
  45. package/src/site/collect.js +484 -0
  46. package/src/site/handler.js +405 -0
  47. package/src/site/index.js +88 -0
  48. package/src/site/limits.js +48 -0
  49. package/src/site/pages.js +75 -0
  50. package/src/site/qr.js +318 -0
  51. package/src/site/reports.js +1117 -0
  52. package/src/site/source.js +68 -0
  53. package/src/site/store-pg.js +225 -0
  54. package/src/site/store.js +249 -0
@@ -0,0 +1,287 @@
1
+ // Signing in to /_gt/ (spec section 4, "Trust answers"): a password stored only as a hash, authenticator-app codes
2
+ // (TOTP, RFC 6238) and 10 one-time recovery codes, all with the runtime's Web Crypto. No library, no outside service.
3
+ //
4
+ // First sign-in uses a setup code (owner, 2026-11-24): `npx goose-tools setup` prints a one-time code and puts only
5
+ // its SHA-256 in the route file. On the deployed site the owner enters the code, picks a password and adds the
6
+ // authenticator. The code then stops working. 100 random bits, so its hash in the site's repository gives nothing away.
7
+ //
8
+ // The checks run inside the route, not in middleware, so a middleware bypass (CVE-2025-29927) cannot skip them.
9
+
10
+ const enc = new TextEncoder();
11
+ const B32 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
12
+
13
+ /** PBKDF2-HMAC-SHA256 iterations (OWASP Password Storage Cheat Sheet: 600,000). */
14
+ export const PBKDF2_ITERATIONS = 600_000;
15
+ export const MIN_PASSWORD = 12;
16
+ export const MAX_PASSWORD = 128;
17
+ export const RECOVERY_CODES = 10;
18
+ export const SESSION_DAYS = 30;
19
+ /** TOTP: 30-second steps, 6 digits, HMAC-SHA1 (RFC 6238 defaults, what authenticator apps expect). */
20
+ const STEP = 30;
21
+ const DIGITS = 6;
22
+
23
+ /** @param {number} n */
24
+ export function randomBytes(n) {
25
+ return crypto.getRandomValues(new Uint8Array(n));
26
+ }
27
+
28
+ /** @param {Uint8Array} bytes */
29
+ export function base32(bytes) {
30
+ let bits = 0;
31
+ let value = 0;
32
+ let out = '';
33
+ for (const b of bytes) {
34
+ value = (value << 8) | b;
35
+ bits += 8;
36
+ while (bits >= 5) {
37
+ out += B32[(value >>> (bits - 5)) & 31];
38
+ bits -= 5;
39
+ }
40
+ }
41
+ if (bits > 0) out += B32[(value << (5 - bits)) & 31];
42
+ return out;
43
+ }
44
+
45
+ /** @param {string} s Base32, spaces and case ignored. */
46
+ export function fromBase32(s) {
47
+ const clean = s.toUpperCase().replace(/[\s=-]/g, '');
48
+ const out = [];
49
+ let bits = 0;
50
+ let value = 0;
51
+ for (const c of clean) {
52
+ const i = B32.indexOf(c);
53
+ if (i < 0) throw new Error('not base32');
54
+ value = (value << 5) | i;
55
+ bits += 5;
56
+ if (bits >= 8) {
57
+ out.push((value >>> (bits - 8)) & 255);
58
+ bits -= 8;
59
+ }
60
+ }
61
+ return new Uint8Array(out);
62
+ }
63
+
64
+ /** @param {Uint8Array | ArrayBuffer} b */
65
+ const b64 = (b) => btoa(String.fromCharCode(...new Uint8Array(b)));
66
+ /** @param {string} s */
67
+ const unb64 = (s) => Uint8Array.from(atob(s), (c) => c.charCodeAt(0));
68
+ /** @param {Uint8Array | ArrayBuffer} b */
69
+ const hex = (b) => [...new Uint8Array(b)].map((x) => x.toString(16).padStart(2, '0')).join('');
70
+
71
+ /** @param {string} s */
72
+ export async function sha256(s) {
73
+ return hex(await crypto.subtle.digest('SHA-256', enc.encode(s)));
74
+ }
75
+
76
+ /**
77
+ * Equal strings, in time that does not depend on where they differ.
78
+ * @param {string} a
79
+ * @param {string} b
80
+ */
81
+ export function same(a, b) {
82
+ if (a.length !== b.length) return false;
83
+ let d = 0;
84
+ for (let i = 0; i < a.length; i++) d |= a.charCodeAt(i) ^ b.charCodeAt(i);
85
+ return d === 0;
86
+ }
87
+
88
+ /* ------------------------------------------------------------------ Setup code */
89
+
90
+ /** A new setup code, "GT-XXXXX-XXXXX-XXXXX-XXXXX" (20 base32 characters, 100 bits). */
91
+ export function newSetupCode() {
92
+ const s = base32(randomBytes(13)).slice(0, 20);
93
+ return 'GT-' + s.match(/.{5}/g).join('-');
94
+ }
95
+
96
+ /** The form a setup code is hashed in: upper case, no spaces or dashes, no "GT" prefix. */
97
+ export function setupCodeKey(code) {
98
+ return String(code).toUpperCase().replace(/[\s-]/g, '').replace(/^GT/, '');
99
+ }
100
+
101
+ /** @param {string} code */
102
+ export function hashSetupCode(code) {
103
+ return sha256('goose-tools setup ' + setupCodeKey(code));
104
+ }
105
+
106
+ /* ------------------------------------------------------------------ Password */
107
+
108
+ /** @param {string} password */
109
+ export function passwordProblem(password) {
110
+ if (typeof password !== 'string' || password.length < MIN_PASSWORD) return 'too_short';
111
+ if (password.length > MAX_PASSWORD) return 'too_long';
112
+ return '';
113
+ }
114
+
115
+ /**
116
+ * @param {string} password
117
+ * @param {{iterations?: number, salt?: Uint8Array}} [o]
118
+ * @returns {Promise<string>} "pbkdf2-sha256$iterations$salt$hash", base64 parts.
119
+ */
120
+ export async function hashPassword(password, o = {}) {
121
+ const iterations = o.iterations ?? PBKDF2_ITERATIONS;
122
+ const salt = o.salt ?? randomBytes(16);
123
+ const bits = await derive(password, salt, iterations);
124
+ return `pbkdf2-sha256$${iterations}$${b64(salt)}$${b64(bits)}`;
125
+ }
126
+
127
+ /**
128
+ * @param {string} password
129
+ * @param {string} stored
130
+ */
131
+ export async function checkPassword(password, stored) {
132
+ const [kind, it, salt, hash] = String(stored).split('$');
133
+ if (kind !== 'pbkdf2-sha256' || !salt || !hash || typeof password !== 'string' || password.length > MAX_PASSWORD) return false;
134
+ const bits = await derive(password, unb64(salt), Number(it));
135
+ return same(b64(bits), hash);
136
+ }
137
+
138
+ /**
139
+ * @param {string} password
140
+ * @param {Uint8Array} salt
141
+ * @param {number} iterations
142
+ */
143
+ async function derive(password, salt, iterations) {
144
+ const key = await crypto.subtle.importKey('raw', enc.encode(password), 'PBKDF2', false, ['deriveBits']);
145
+ return crypto.subtle.deriveBits({ name: 'PBKDF2', hash: 'SHA-256', salt, iterations }, key, 256);
146
+ }
147
+
148
+ /* ------------------------------------------------------------------ Authenticator codes (TOTP) */
149
+
150
+ /** A new authenticator key: 20 random bytes as base32 (RFC 4226 recommends 160 bits). */
151
+ export function newTotpKey() {
152
+ return base32(randomBytes(20));
153
+ }
154
+
155
+ /**
156
+ * The otpauth:// address an authenticator app reads from the QR code.
157
+ * @param {string} key Base32.
158
+ * @param {string} host The site, shown in the app.
159
+ */
160
+ export function otpauthUri(key, host) {
161
+ const label = encodeURIComponent(`Goose Tools:${host}`);
162
+ return `otpauth://totp/${label}?secret=${key}&issuer=${encodeURIComponent('Goose Tools')}&algorithm=SHA1&digits=${DIGITS}&period=${STEP}`;
163
+ }
164
+
165
+ /**
166
+ * The code for one time step.
167
+ * @param {string} key Base32.
168
+ * @param {number} step
169
+ */
170
+ export async function totpAt(key, step) {
171
+ const msg = new Uint8Array(8);
172
+ new DataView(msg.buffer).setBigUint64(0, BigInt(step));
173
+ const k = await crypto.subtle.importKey('raw', fromBase32(key), { name: 'HMAC', hash: 'SHA-1' }, false, ['sign']);
174
+ const mac = new Uint8Array(await crypto.subtle.sign('HMAC', k, msg));
175
+ const off = mac[mac.length - 1] & 15;
176
+ const n = ((mac[off] & 0x7f) << 24) | (mac[off + 1] << 16) | (mac[off + 2] << 8) | mac[off + 3];
177
+ return String(n % 10 ** DIGITS).padStart(DIGITS, '0');
178
+ }
179
+
180
+ /**
181
+ * The time step a code matches, one step either side allowed for clock drift, or -1.
182
+ * @param {string} key
183
+ * @param {string} code
184
+ * @param {number} [nowMs]
185
+ */
186
+ export async function totpStep(key, code, nowMs = Date.now()) {
187
+ const c = String(code).replace(/\s/g, '');
188
+ if (!/^\d{6}$/.test(c)) return -1;
189
+ const now = Math.floor(nowMs / 1000 / STEP);
190
+ for (const step of [now, now - 1, now + 1]) if (same(await totpAt(key, step), c)) return step;
191
+ return -1;
192
+ }
193
+
194
+ /* ------------------------------------------------------------------ Recovery codes */
195
+
196
+ /** @returns {string[]} 10 codes, "xxxx-xxxx" as the approved Sign in screen shows them (40 bits each; one use, and
197
+ * failed tries wait). */
198
+ export function newRecoveryCodes() {
199
+ return Array.from({ length: RECOVERY_CODES }, () => base32(randomBytes(5)).toLowerCase().replace(/^(.{4})/, '$1-'));
200
+ }
201
+
202
+ /** @param {string} code */
203
+ export function hashRecoveryCode(code) {
204
+ return sha256('goose-tools recovery ' + String(code).toUpperCase().replace(/[\s-]/g, ''));
205
+ }
206
+
207
+ /* ------------------------------------------------------------------ Sessions */
208
+
209
+ /** A new session token for the cookie, and the hash the store keeps. */
210
+ export async function newSession() {
211
+ const token = b64(randomBytes(32)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
212
+ return { token, hash: await sha256('goose-tools session ' + token) };
213
+ }
214
+
215
+ /** @param {string} token */
216
+ export function sessionHash(token) {
217
+ return sha256('goose-tools session ' + token);
218
+ }
219
+
220
+ /**
221
+ * The cookie's name: __Host- on https (locked to this host, path / and Secure), plain on http://localhost.
222
+ * @param {URL} url
223
+ */
224
+ export function cookieName(url) {
225
+ return url.protocol === 'https:' ? '__Host-gt' : 'gt';
226
+ }
227
+
228
+ /**
229
+ * @param {URL} url
230
+ * @param {string} token '' clears it.
231
+ */
232
+ export function sessionCookie(url, token) {
233
+ const secure = url.protocol === 'https:' ? '; Secure' : '';
234
+ const age = token ? SESSION_DAYS * 86400 : 0;
235
+ return `${cookieName(url)}=${token}; Path=/; HttpOnly; SameSite=Strict; Max-Age=${age}${secure}`;
236
+ }
237
+
238
+ /**
239
+ * The session token in a request's cookies, or ''.
240
+ * @param {Request} request
241
+ */
242
+ export function readSessionToken(request) {
243
+ return readCookie(request, cookieName(new URL(request.url)));
244
+ }
245
+
246
+ /**
247
+ * A cookie's value, or ''.
248
+ * @param {Request} request
249
+ * @param {string} name
250
+ */
251
+ function readCookie(request, name) {
252
+ for (const part of (request.headers.get('cookie') || '').split(';')) {
253
+ const [k, ...v] = part.trim().split('=');
254
+ if (k === name) return v.join('=');
255
+ }
256
+ return '';
257
+ }
258
+
259
+ /* ------------------------------------------------------------------ Known browsers */
260
+
261
+ // A browser that signed in before carries a device cookie (OWASP, "Slow down online guessing attacks with device
262
+ // cookies"). Its failed tries count apart from everyone else's, so a stranger's wrong passwords never make the owner
263
+ // wait.
264
+
265
+ /** Known browsers kept, newest first. */
266
+ export const DEVICES = 10;
267
+
268
+ /** @param {URL} url */
269
+ const deviceName = (url) => (url.protocol === 'https:' ? '__Host-gtd' : 'gtd');
270
+
271
+ /**
272
+ * @param {URL} url
273
+ * @param {string} token
274
+ */
275
+ export function deviceCookie(url, token) {
276
+ return `${deviceName(url)}=${token}; Path=/; HttpOnly; SameSite=Strict; Max-Age=${400 * 86400}${url.protocol === 'https:' ? '; Secure' : ''}`;
277
+ }
278
+
279
+ /** @param {Request} request */
280
+ export function readDeviceToken(request) {
281
+ return readCookie(request, deviceName(new URL(request.url)));
282
+ }
283
+
284
+ /** @param {string} token */
285
+ export function deviceHash(token) {
286
+ return sha256('goose-tools device ' + token);
287
+ }