webcake-landing-mcp 1.18.0 → 1.18.2
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/dist/auth/oauth-server.js +37 -22
- package/dist/auth/token-crypto.js +49 -0
- package/dist/changelog.json +14 -14
- package/dist/http.js +2 -1
- package/dist/persistence/postgres.js +40 -0
- package/dist/persistence/screenshot-playwright.js +15 -2
- package/dist/smoke.js +30 -0
- package/package.json +1 -1
|
@@ -20,6 +20,8 @@
|
|
|
20
20
|
* layer resolves a Bearer access token to its ljwt and injects it as the normal
|
|
21
21
|
* `x-webcake-jwt` header, so the rest of the server (persistence/config.ts) is
|
|
22
22
|
* UNCHANGED and the legacy `?jwt=` / `x-webcake-jwt` paths keep working untouched.
|
|
23
|
+
* At rest, codes/access/refresh tokens are keyed by their sha256 and Postgres keeps the
|
|
24
|
+
* ljwt/wsid AES-GCM encrypted when WEBCAKE_TOKEN_KEY is set (see ./token-crypto.ts).
|
|
23
25
|
*
|
|
24
26
|
* STORE: Postgres when DATABASE_URL is set (tokens survive a `serve` restart and
|
|
25
27
|
* are shared across instances behind a load balancer), else in-memory maps
|
|
@@ -31,6 +33,7 @@ import { randomBytes, createHash } from "node:crypto";
|
|
|
31
33
|
import { getPg, ensureOAuthSchema } from "../persistence/postgres.js";
|
|
32
34
|
import { jwtExpired } from "../persistence/config.js";
|
|
33
35
|
import { getRedis } from "../persistence/redis.js";
|
|
36
|
+
import { sha256hex, sealCred, openCred, hasTokenKey } from "./token-crypto.js";
|
|
34
37
|
// ---- Dead Webcake sessions -------------------------------------------------
|
|
35
38
|
// The OAuth tokens outlive the Webcake JWT they wrap. Once that JWT is expired (or the
|
|
36
39
|
// backend rejected it), refreshing would just re-issue the dead JWT; refusing it makes the
|
|
@@ -41,7 +44,6 @@ import { getRedis } from "../persistence/redis.js";
|
|
|
41
44
|
const DEAD_PREFIX = "wc:lp:deadjwt:";
|
|
42
45
|
const DEAD_MAX_TTL = 30 * 24 * 60 * 60 * 1000;
|
|
43
46
|
const deadLocal = new Map(); // hash → expiresAt (ms)
|
|
44
|
-
const jwtHash = (jwt) => createHash("sha256").update(jwt).digest("hex");
|
|
45
47
|
function deadTtl(jwt) {
|
|
46
48
|
try {
|
|
47
49
|
const exp = JSON.parse(Buffer.from(jwt.split(".")[1], "base64url").toString()).exp;
|
|
@@ -64,14 +66,14 @@ function rememberDeadLocally(hash, ttl) {
|
|
|
64
66
|
function isDeadLocally(jwt) {
|
|
65
67
|
if (jwtExpired(jwt))
|
|
66
68
|
return true;
|
|
67
|
-
const exp = deadLocal.get(
|
|
69
|
+
const exp = deadLocal.get(sha256hex(jwt));
|
|
68
70
|
return exp !== undefined && exp >= Date.now();
|
|
69
71
|
}
|
|
70
72
|
/** The backend rejected this JWT: make every instance treat it as dead. Never throws. */
|
|
71
73
|
export async function markJwtDead(jwt) {
|
|
72
74
|
if (!jwt)
|
|
73
75
|
return;
|
|
74
|
-
const hash =
|
|
76
|
+
const hash = sha256hex(jwt);
|
|
75
77
|
const ttl = deadTtl(jwt);
|
|
76
78
|
rememberDeadLocally(hash, ttl);
|
|
77
79
|
try {
|
|
@@ -91,7 +93,7 @@ export async function isDeadJwt(jwt) {
|
|
|
91
93
|
const redis = getRedis();
|
|
92
94
|
if (!redis)
|
|
93
95
|
return false;
|
|
94
|
-
const hash =
|
|
96
|
+
const hash = sha256hex(jwt);
|
|
95
97
|
const hit = await redis.get(DEAD_PREFIX + hash).catch(() => null);
|
|
96
98
|
if (hit)
|
|
97
99
|
rememberDeadLocally(hash, deadTtl(jwt));
|
|
@@ -235,7 +237,7 @@ class PgStore {
|
|
|
235
237
|
}
|
|
236
238
|
async putCode(code, c) {
|
|
237
239
|
await this.pool.query(`INSERT INTO oauth_codes (code, client_id, redirect_uri, code_challenge, scope, ljwt, wsid, expires_at)
|
|
238
|
-
VALUES ($1,$2,$3,$4,$5,$6,$7,$8)`, [code, c.client_id, c.redirect_uri, c.code_challenge, c.scope ?? null, c.ljwt, c.wsid, c.expiresAt]);
|
|
240
|
+
VALUES ($1,$2,$3,$4,$5,$6,$7,$8)`, [code, c.client_id, c.redirect_uri, c.code_challenge, c.scope ?? null, sealCred(c.ljwt), sealCred(c.wsid), c.expiresAt]);
|
|
239
241
|
}
|
|
240
242
|
async takeCode(code) {
|
|
241
243
|
const { rows } = await this.pool.query(`DELETE FROM oauth_codes WHERE code=$1 RETURNING client_id, redirect_uri, code_challenge, scope, ljwt, wsid, expires_at`, [code]);
|
|
@@ -247,13 +249,13 @@ class PgStore {
|
|
|
247
249
|
redirect_uri: r.redirect_uri,
|
|
248
250
|
code_challenge: r.code_challenge,
|
|
249
251
|
scope: r.scope ?? undefined,
|
|
250
|
-
ljwt: r.ljwt,
|
|
251
|
-
wsid: r.wsid ?? "",
|
|
252
|
+
ljwt: openCred(r.ljwt),
|
|
253
|
+
wsid: openCred(r.wsid ?? ""),
|
|
252
254
|
expiresAt: Number(r.expires_at),
|
|
253
255
|
};
|
|
254
256
|
}
|
|
255
257
|
async putAccess(t, a) {
|
|
256
|
-
await this.pool.query(`INSERT INTO oauth_access_tokens (token, ljwt, wsid, scope, expires_at) VALUES ($1,$2,$3,$4,$5)`, [t, a.ljwt, a.wsid, a.scope ?? null, a.expiresAt]);
|
|
258
|
+
await this.pool.query(`INSERT INTO oauth_access_tokens (token, ljwt, wsid, scope, expires_at, ljwt_sha) VALUES ($1,$2,$3,$4,$5,$6)`, [t, sealCred(a.ljwt), sealCred(a.wsid), a.scope ?? null, a.expiresAt, sha256hex(a.ljwt)]);
|
|
257
259
|
}
|
|
258
260
|
async getAccess(t) {
|
|
259
261
|
const { rows } = await this.pool.query(`SELECT ljwt, wsid, scope, expires_at FROM oauth_access_tokens WHERE token=$1`, [t]);
|
|
@@ -264,10 +266,10 @@ class PgStore {
|
|
|
264
266
|
await this.pool.query(`DELETE FROM oauth_access_tokens WHERE token=$1`, [t]);
|
|
265
267
|
return undefined;
|
|
266
268
|
}
|
|
267
|
-
return { ljwt: r.ljwt, wsid: r.wsid ?? "", scope: r.scope ?? undefined, expiresAt: Number(r.expires_at) };
|
|
269
|
+
return { ljwt: openCred(r.ljwt), wsid: openCred(r.wsid ?? ""), scope: r.scope ?? undefined, expiresAt: Number(r.expires_at) };
|
|
268
270
|
}
|
|
269
271
|
async putRefresh(t, r) {
|
|
270
|
-
await this.pool.query(`INSERT INTO oauth_refresh_tokens (token, ljwt, wsid, client_id, scope, expires_at) VALUES ($1,$2,$3,$4,$5,$6)`, [t, r.ljwt, r.wsid, r.client_id, r.scope ?? null, r.expiresAt]);
|
|
272
|
+
await this.pool.query(`INSERT INTO oauth_refresh_tokens (token, ljwt, wsid, client_id, scope, expires_at, ljwt_sha) VALUES ($1,$2,$3,$4,$5,$6,$7)`, [t, sealCred(r.ljwt), sealCred(r.wsid), r.client_id, r.scope ?? null, r.expiresAt, sha256hex(r.ljwt)]);
|
|
271
273
|
}
|
|
272
274
|
async takeRefresh(t, clientId) {
|
|
273
275
|
const { rows } = await this.pool.query(`DELETE FROM oauth_refresh_tokens WHERE token=$1 AND ($2::text IS NULL OR client_id=$2) RETURNING ljwt, wsid, client_id, scope, expires_at`, [t, clientId || null]);
|
|
@@ -275,8 +277,8 @@ class PgStore {
|
|
|
275
277
|
if (!r || Number(r.expires_at) < now())
|
|
276
278
|
return undefined;
|
|
277
279
|
return {
|
|
278
|
-
ljwt: r.ljwt,
|
|
279
|
-
wsid: r.wsid ?? "",
|
|
280
|
+
ljwt: openCred(r.ljwt),
|
|
281
|
+
wsid: openCred(r.wsid ?? ""),
|
|
280
282
|
client_id: r.client_id,
|
|
281
283
|
scope: r.scope ?? undefined,
|
|
282
284
|
expiresAt: Number(r.expires_at),
|
|
@@ -286,10 +288,12 @@ class PgStore {
|
|
|
286
288
|
await this.pool.query(`DELETE FROM oauth_access_tokens WHERE token=$1`, [t]);
|
|
287
289
|
await this.pool.query(`DELETE FROM oauth_refresh_tokens WHERE token=$1`, [t]);
|
|
288
290
|
}
|
|
289
|
-
//
|
|
291
|
+
// ljwt is encrypted with a random IV, so match its sha256; `ljwt=$2` catches a row an older
|
|
292
|
+
// instance wrote in plaintext. ponytail: no index — a seq scan, but this only runs when a JWT dies.
|
|
290
293
|
async revokeJwt(ljwt) {
|
|
291
|
-
|
|
292
|
-
await this.pool.query(`DELETE FROM
|
|
294
|
+
const args = [sha256hex(ljwt), ljwt];
|
|
295
|
+
await this.pool.query(`DELETE FROM oauth_access_tokens WHERE ljwt_sha=$1 OR ljwt=$2`, args);
|
|
296
|
+
await this.pool.query(`DELETE FROM oauth_refresh_tokens WHERE ljwt_sha=$1 OR ljwt=$2`, args);
|
|
293
297
|
}
|
|
294
298
|
}
|
|
295
299
|
// ---- Backend selection (memoized) -----------------------------------------
|
|
@@ -310,6 +314,16 @@ function getStore() {
|
|
|
310
314
|
});
|
|
311
315
|
return storePromise;
|
|
312
316
|
}
|
|
317
|
+
/**
|
|
318
|
+
* `serve` startup: fail on a malformed WEBCAKE_TOKEN_KEY, warn once when Postgres holds
|
|
319
|
+
* credentials without one, and run the schema + at-rest migration before the first request.
|
|
320
|
+
*/
|
|
321
|
+
export async function initOAuthStore() {
|
|
322
|
+
if (!hasTokenKey() && getPg()) {
|
|
323
|
+
console.error("[oauth] WARNING: WEBCAKE_TOKEN_KEY is not set — Webcake credentials are stored UNENCRYPTED in Postgres.");
|
|
324
|
+
}
|
|
325
|
+
await getStore();
|
|
326
|
+
}
|
|
313
327
|
// ---- PKCE -----------------------------------------------------------------
|
|
314
328
|
/** base64url( SHA256(verifier) ) — the S256 transform. */
|
|
315
329
|
export function s256(verifier) {
|
|
@@ -445,7 +459,8 @@ export async function completeAuthorize(internalState, ljwt, wsid) {
|
|
|
445
459
|
return { ok: false, error: "access_denied", error_description: "No Webcake token returned from login." };
|
|
446
460
|
}
|
|
447
461
|
const code = token(32);
|
|
448
|
-
|
|
462
|
+
// Bearer secrets are stored by sha256 only (both stores); the raw value goes to the client alone.
|
|
463
|
+
await store.putCode(sha256hex(code), {
|
|
449
464
|
client_id: p.client_id,
|
|
450
465
|
redirect_uri: p.redirect_uri,
|
|
451
466
|
code_challenge: p.code_challenge,
|
|
@@ -459,14 +474,14 @@ export async function completeAuthorize(internalState, ljwt, wsid) {
|
|
|
459
474
|
async function issueTokens(store, ljwt, wsid, client_id, scope) {
|
|
460
475
|
const access = token(32);
|
|
461
476
|
const refresh = token(32);
|
|
462
|
-
await store.putAccess(access, { ljwt, wsid, scope, expiresAt: now() + ACCESS_TTL });
|
|
463
|
-
await store.putRefresh(refresh, { ljwt, wsid, client_id, scope, expiresAt: now() + REFRESH_TTL });
|
|
477
|
+
await store.putAccess(sha256hex(access), { ljwt, wsid, scope, expiresAt: now() + ACCESS_TTL });
|
|
478
|
+
await store.putRefresh(sha256hex(refresh), { ljwt, wsid, client_id, scope, expiresAt: now() + REFRESH_TTL });
|
|
464
479
|
return { access_token: access, token_type: "Bearer", expires_in: Math.floor(ACCESS_TTL / 1000), refresh_token: refresh, scope };
|
|
465
480
|
}
|
|
466
481
|
export async function exchangeToken(p) {
|
|
467
482
|
const store = await getStore();
|
|
468
483
|
if (p.grant_type === "authorization_code") {
|
|
469
|
-
const c = p.code ? await store.takeCode(p.code) : undefined; // one-time use
|
|
484
|
+
const c = p.code ? await store.takeCode(sha256hex(p.code)) : undefined; // one-time use
|
|
470
485
|
if (!c) {
|
|
471
486
|
return { ok: false, status: 400, error: "invalid_grant", error_description: "Unknown or expired authorization code." };
|
|
472
487
|
}
|
|
@@ -484,7 +499,7 @@ export async function exchangeToken(p) {
|
|
|
484
499
|
if (p.grant_type === "refresh_token") {
|
|
485
500
|
// Rotate — but only for the client the token was issued to: another client_id finds nothing (and
|
|
486
501
|
// burns nothing); a request without client_id keeps the binding it already has.
|
|
487
|
-
const r = p.refresh_token ? await store.takeRefresh(p.refresh_token, p.client_id) : undefined;
|
|
502
|
+
const r = p.refresh_token ? await store.takeRefresh(sha256hex(p.refresh_token), p.client_id) : undefined;
|
|
488
503
|
if (!r) {
|
|
489
504
|
return { ok: false, status: 400, error: "invalid_grant", error_description: "Unknown or expired refresh token, or not issued to this client_id." };
|
|
490
505
|
}
|
|
@@ -500,14 +515,14 @@ export async function exchangeToken(p) {
|
|
|
500
515
|
export async function resolveAccessToken(accessToken) {
|
|
501
516
|
if (!accessToken)
|
|
502
517
|
return undefined;
|
|
503
|
-
const a = await (await getStore()).getAccess(accessToken);
|
|
518
|
+
const a = await (await getStore()).getAccess(sha256hex(accessToken));
|
|
504
519
|
return a && !isDeadLocally(a.ljwt) ? { ljwt: a.ljwt, wsid: a.wsid } : undefined;
|
|
505
520
|
}
|
|
506
521
|
/** Revoke an access or refresh token (best-effort; for /revoke). */
|
|
507
522
|
export async function revokeToken(t) {
|
|
508
523
|
if (!t)
|
|
509
524
|
return;
|
|
510
|
-
await (await getStore()).revoke(t);
|
|
525
|
+
await (await getStore()).revoke(sha256hex(t));
|
|
511
526
|
}
|
|
512
527
|
// ---- Metadata documents (RFC 8414 / RFC 9728) -----------------------------
|
|
513
528
|
/** /.well-known/oauth-authorization-server */
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* At-rest protection for the OAuth store (see ./oauth-server.ts, ../persistence/postgres.ts).
|
|
3
|
+
*
|
|
4
|
+
* - Bearer secrets (access/refresh tokens, authorization codes) are stored and looked up by
|
|
5
|
+
* `sha256hex(token)` only: a leaked table row can't be replayed.
|
|
6
|
+
* - The wrapped Webcake credential (ljwt, wsid) is AES-256-GCM encrypted under
|
|
7
|
+
* WEBCAKE_TOKEN_KEY (base64 of 32 bytes) as `enc:v1:` + base64(iv(12) | tag(16) | ciphertext).
|
|
8
|
+
* A value without the prefix is legacy plaintext and passes through unchanged; with no key
|
|
9
|
+
* set, new values are stored as plaintext too, so a deploy works before the secret exists.
|
|
10
|
+
*/
|
|
11
|
+
import { createCipheriv, createDecipheriv, createHash, randomBytes } from "node:crypto";
|
|
12
|
+
export const ENC_PREFIX = "enc:v1:";
|
|
13
|
+
export const sha256hex = (s) => createHash("sha256").update(s).digest("hex");
|
|
14
|
+
/** Decode WEBCAKE_TOKEN_KEY. Unset → null; anything but canonical base64 of 32 bytes throws. */
|
|
15
|
+
export function parseTokenKey(raw) {
|
|
16
|
+
const s = (raw ?? "").trim();
|
|
17
|
+
if (!s)
|
|
18
|
+
return null;
|
|
19
|
+
const k = Buffer.from(s, "base64");
|
|
20
|
+
// Buffer.from silently skips junk, so require a canonical round trip, not just the length.
|
|
21
|
+
if (k.length !== 32 || k.toString("base64") !== s) {
|
|
22
|
+
throw new Error("WEBCAKE_TOKEN_KEY must be base64 of exactly 32 bytes (openssl rand -base64 32)");
|
|
23
|
+
}
|
|
24
|
+
return k;
|
|
25
|
+
}
|
|
26
|
+
let key;
|
|
27
|
+
const tokenKey = () => (key === undefined ? (key = parseTokenKey(process.env.WEBCAKE_TOKEN_KEY)) : key);
|
|
28
|
+
/** Throws on a malformed WEBCAKE_TOKEN_KEY — call at startup so a bad secret fails loudly. */
|
|
29
|
+
export const hasTokenKey = () => !!tokenKey();
|
|
30
|
+
/** Encrypt a credential for storage; "" and no-key pass through as plaintext. */
|
|
31
|
+
export function sealCred(v, k = tokenKey()) {
|
|
32
|
+
if (!k || !v)
|
|
33
|
+
return v;
|
|
34
|
+
const iv = randomBytes(12);
|
|
35
|
+
const c = createCipheriv("aes-256-gcm", k, iv);
|
|
36
|
+
const ct = Buffer.concat([c.update(v, "utf8"), c.final()]);
|
|
37
|
+
return ENC_PREFIX + Buffer.concat([iv, c.getAuthTag(), ct]).toString("base64");
|
|
38
|
+
}
|
|
39
|
+
/** Decrypt a stored credential; a value without the prefix is legacy plaintext. */
|
|
40
|
+
export function openCred(v, k = tokenKey()) {
|
|
41
|
+
if (!v || !v.startsWith(ENC_PREFIX))
|
|
42
|
+
return v;
|
|
43
|
+
if (!k)
|
|
44
|
+
throw new Error("stored credential is encrypted but WEBCAKE_TOKEN_KEY is not set");
|
|
45
|
+
const b = Buffer.from(v.slice(ENC_PREFIX.length), "base64");
|
|
46
|
+
const d = createDecipheriv("aes-256-gcm", k, b.subarray(0, 12));
|
|
47
|
+
d.setAuthTag(b.subarray(12, 28));
|
|
48
|
+
return Buffer.concat([d.update(b.subarray(28)), d.final()]).toString("utf8");
|
|
49
|
+
}
|
package/dist/changelog.json
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
[
|
|
2
|
+
{
|
|
3
|
+
"v": "1.18.2",
|
|
4
|
+
"d": "01/12/2026",
|
|
5
|
+
"type": "Fixed",
|
|
6
|
+
"en": "render_preview's self-hosted Playwright screenshot route now launches Chromium with its sandbox enabled by default, instead of always passing…",
|
|
7
|
+
"vi": "Route screenshot Playwright tự host của render_preview nay mặc định bật sandbox cho Chromium thay vì luôn truyền --no-sandbox, nên một lỗ hổng…"
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"v": "1.18.1",
|
|
11
|
+
"d": "01/12/2026",
|
|
12
|
+
"type": "Fixed",
|
|
13
|
+
"en": "OAuth authorization codes and access/refresh tokens issued by the remote HTTP server (serve) are now looked up by their sha256 hash in both the…",
|
|
14
|
+
"vi": "Authorization code và access/refresh token OAuth do remote HTTP server (serve) cấp nay được tra cứu bằng sha256 hash của chúng ở cả in-memory store…"
|
|
15
|
+
},
|
|
2
16
|
{
|
|
3
17
|
"v": "1.18.0",
|
|
4
18
|
"d": "01/12/2026",
|
|
@@ -26,19 +40,5 @@
|
|
|
26
40
|
"type": "Fixed",
|
|
27
41
|
"en": "login and the OAuth connect flow now also capture the landing session id (wsid, the SPA's lwsid cookie) alongside the JWT and persistence calls send…",
|
|
28
42
|
"vi": "login và luồng OAuth connect nay cũng lấy thêm session id landing (wsid, tức cookie lwsid của SPA) cùng với JWT, và các lượt gọi persistence gửi nó…"
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
"v": "1.16.1",
|
|
32
|
-
"d": "25/11/2026",
|
|
33
|
-
"type": "Fixed",
|
|
34
|
-
"en": "validate_page's text height estimate now matches the renderer's actual line-height: 1.5 default (previously assumed 1.4), so a text-block with no…",
|
|
35
|
-
"vi": "Công thức ước lượng chiều cao chữ của validate_page nay khớp với giá trị line-height: 1.5 thật của renderer (trước đó giả định 1.4), nên một…"
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
"v": "1.16.0",
|
|
39
|
-
"d": "25/11/2026",
|
|
40
|
-
"type": "Added",
|
|
41
|
-
"en": "list_pages, find_pages, get_page, update_page, add_section, patch_page, and publish_page now accept an organization_id argument (an organization id,…",
|
|
42
|
-
"vi": "list_pages, find_pages, get_page, update_page, add_section, patch_page và publish_page nay nhận thêm tham số organization_id (một id tổ chức, hoặc…"
|
|
43
43
|
}
|
|
44
44
|
]
|
package/dist/http.js
CHANGED
|
@@ -25,7 +25,7 @@ import { captureWithPlaywright, captureTilesWithPlaywright, assertPublicUrl, set
|
|
|
25
25
|
import { resolveEnv, ENVIRONMENTS, stripTrailingSlash, readConfig, configFromHeaders } from "./persistence/config.js";
|
|
26
26
|
import { listOrganizations } from "./persistence/webcake-client.js";
|
|
27
27
|
import { buildConnectUrl } from "./auth/login.js";
|
|
28
|
-
import { registerClient, startAuthorize, completeAuthorize, exchangeToken, resolveAccessToken, isDeadJwt, markJwtDead, revokeToken, pendingCookieName, authServerMetadata, protectedResourceMetadata, } from "./auth/oauth-server.js";
|
|
28
|
+
import { registerClient, startAuthorize, completeAuthorize, exchangeToken, resolveAccessToken, isDeadJwt, markJwtDead, revokeToken, initOAuthStore, pendingCookieName, authServerMetadata, protectedResourceMetadata, } from "./auth/oauth-server.js";
|
|
29
29
|
const MCP_PATH = "/mcp";
|
|
30
30
|
const IMAGES_PATH = "/api/images/search";
|
|
31
31
|
const RENDER_SCREENSHOT_PATH = "/api/render/screenshot";
|
|
@@ -491,6 +491,7 @@ const MAX_SESSIONS = 500;
|
|
|
491
491
|
const MAX_SESSIONS_PER_CRED = 20;
|
|
492
492
|
export async function startHttpServer(port) {
|
|
493
493
|
setSsrfGuard(true); // hosted: caller-chosen URLs must not reach this host's private network
|
|
494
|
+
await initOAuthStore(); // a malformed WEBCAKE_TOKEN_KEY fails here; runs the at-rest migration
|
|
494
495
|
// mcp-session-id -> live session (each bound to its own McpServer instance), in least-recently-used
|
|
495
496
|
// order: a request re-inserts its session at the end, so the oldest idle ones sit first. `busy`
|
|
496
497
|
// counts open responses (incl. SSE streams); a busy session is never evicted.
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* e.g. postgres://user:pw@host:5432/webcake_mcp
|
|
16
16
|
*/
|
|
17
17
|
import { createRequire } from "node:module";
|
|
18
|
+
import { ENC_PREFIX, hasTokenKey, sealCred } from "../auth/token-crypto.js";
|
|
18
19
|
const require = createRequire(import.meta.url);
|
|
19
20
|
let cached; // undefined = not yet resolved
|
|
20
21
|
function redactUrl(u) {
|
|
@@ -111,7 +112,19 @@ export function ensureOAuthSchema(pool) {
|
|
|
111
112
|
ALTER TABLE oauth_codes ADD COLUMN IF NOT EXISTS wsid text;
|
|
112
113
|
ALTER TABLE oauth_access_tokens ADD COLUMN IF NOT EXISTS wsid text;
|
|
113
114
|
ALTER TABLE oauth_refresh_tokens ADD COLUMN IF NOT EXISTS wsid text;
|
|
115
|
+
ALTER TABLE oauth_access_tokens ADD COLUMN IF NOT EXISTS ljwt_sha text;
|
|
116
|
+
ALTER TABLE oauth_refresh_tokens ADD COLUMN IF NOT EXISTS ljwt_sha text;
|
|
117
|
+
-- At-rest migration (idempotent): raw tokens are base64url, never 64-hex, so these rows
|
|
118
|
+
-- are legacy keys; rewriting them to their sha256 keeps issued tokens working.
|
|
119
|
+
UPDATE oauth_codes SET code = encode(sha256(convert_to(code, 'UTF8')), 'hex') WHERE code !~ '^[0-9a-f]{64}$';
|
|
120
|
+
UPDATE oauth_access_tokens SET token = encode(sha256(convert_to(token, 'UTF8')), 'hex') WHERE token !~ '^[0-9a-f]{64}$';
|
|
121
|
+
UPDATE oauth_refresh_tokens SET token = encode(sha256(convert_to(token, 'UTF8')), 'hex') WHERE token !~ '^[0-9a-f]{64}$';
|
|
122
|
+
-- revokeJwt matches ljwt_sha once ljwt is encrypted; fill it while ljwt is still plaintext.
|
|
123
|
+
UPDATE oauth_access_tokens SET ljwt_sha = encode(sha256(convert_to(ljwt, 'UTF8')), 'hex') WHERE ljwt_sha IS NULL AND ljwt NOT LIKE '${ENC_PREFIX}%';
|
|
124
|
+
UPDATE oauth_refresh_tokens SET ljwt_sha = encode(sha256(convert_to(ljwt, 'UTF8')), 'hex') WHERE ljwt_sha IS NULL AND ljwt NOT LIKE '${ENC_PREFIX}%';
|
|
114
125
|
`);
|
|
126
|
+
if (hasTokenKey())
|
|
127
|
+
await encryptLegacyCreds(pool);
|
|
115
128
|
return true;
|
|
116
129
|
}
|
|
117
130
|
catch (e) {
|
|
@@ -121,3 +134,30 @@ export function ensureOAuthSchema(pool) {
|
|
|
121
134
|
})();
|
|
122
135
|
return schemaReady;
|
|
123
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Encrypt the plaintext ljwt/wsid rows left from before WEBCAKE_TOKEN_KEY, in batches. Idempotent
|
|
139
|
+
* (only unprefixed values match). A failure is logged, not fatal: openCred still reads plaintext.
|
|
140
|
+
*/
|
|
141
|
+
async function encryptLegacyCreds(pool) {
|
|
142
|
+
const seal = (v) => (!v || v.startsWith(ENC_PREFIX) ? v : sealCred(v));
|
|
143
|
+
let n = 0;
|
|
144
|
+
try {
|
|
145
|
+
for (const [table, col] of [["oauth_codes", "code"], ["oauth_access_tokens", "token"], ["oauth_refresh_tokens", "token"]]) {
|
|
146
|
+
for (;;) {
|
|
147
|
+
const { rows } = await pool.query(`SELECT ${col} AS k, ljwt, wsid FROM ${table}
|
|
148
|
+
WHERE ljwt NOT LIKE '${ENC_PREFIX}%' OR (wsid <> '' AND wsid NOT LIKE '${ENC_PREFIX}%') LIMIT 500`);
|
|
149
|
+
for (const r of rows) {
|
|
150
|
+
await pool.query(`UPDATE ${table} SET ljwt=$2, wsid=$3 WHERE ${col}=$1`, [r.k, seal(r.ljwt), seal(r.wsid)]);
|
|
151
|
+
}
|
|
152
|
+
n += rows.length;
|
|
153
|
+
if (rows.length < 500)
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (n)
|
|
158
|
+
console.error(`[pg] encrypted ${n} legacy OAuth credential row(s)`);
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
console.error("[pg] encrypting legacy OAuth credentials failed (plaintext rows still readable):", e?.message ?? e);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -18,6 +18,12 @@
|
|
|
18
18
|
*/
|
|
19
19
|
let pwModule = null;
|
|
20
20
|
let triedLoad = false;
|
|
21
|
+
// Chromium renders caller-chosen URLs, so its sandbox stays ON (needs unprivileged user namespaces,
|
|
22
|
+
// or seccomp=deploy/vps/chrome-seccomp.json in Docker). CHROMIUM_NO_SANDBOX=1 is the opt-out for a
|
|
23
|
+
// host that can't provide one.
|
|
24
|
+
const NO_SANDBOX = process.env.CHROMIUM_NO_SANDBOX === "1";
|
|
25
|
+
if (NO_SANDBOX)
|
|
26
|
+
console.error("[render] WARNING: CHROMIUM_NO_SANDBOX=1 — Chromium runs WITHOUT its sandbox; a renderer exploit reaches this process's user.");
|
|
21
27
|
let browserPromise = null;
|
|
22
28
|
/** Lazy-load `playwright` (then `playwright-core`); cache the result (incl. the not-installed case). */
|
|
23
29
|
async function loadPlaywright() {
|
|
@@ -47,13 +53,20 @@ async function getBrowser() {
|
|
|
47
53
|
if (!pw)
|
|
48
54
|
return null;
|
|
49
55
|
if (!browserPromise) {
|
|
50
|
-
|
|
56
|
+
// Playwright adds --no-sandbox itself unless chromiumSandbox is true.
|
|
57
|
+
const launchOpts = { headless: true, chromiumSandbox: !NO_SANDBOX, args: ["--disable-dev-shm-usage"] };
|
|
51
58
|
const exe = process.env.PLAYWRIGHT_CHROMIUM_PATH || process.env.CHROME_BIN;
|
|
52
59
|
if (exe)
|
|
53
60
|
launchOpts.executablePath = exe;
|
|
54
61
|
// No bundled Chromium and no CHROME_BIN (a dev machine): fall back to the installed Chrome.
|
|
55
|
-
|
|
62
|
+
let first;
|
|
63
|
+
browserPromise = pw.chromium.launch(launchOpts).catch((e) => {
|
|
64
|
+
first = e;
|
|
65
|
+
return exe ? Promise.reject(e) : pw.chromium.launch({ ...launchOpts, channel: "chrome" });
|
|
66
|
+
}).catch((e) => {
|
|
56
67
|
browserPromise = null; // allow a later retry
|
|
68
|
+
// The route only says "failed to launch"; the real cause (e.g. "No usable sandbox!") goes to the log.
|
|
69
|
+
console.error("[render] chromium launch failed:", String(first?.message ?? e).split("\n").slice(0, 4).join(" | "));
|
|
57
70
|
throw e;
|
|
58
71
|
});
|
|
59
72
|
}
|
package/dist/smoke.js
CHANGED
|
@@ -20,6 +20,8 @@ import { normalizePhoto, resolvePexelsKey, pexelsKeyFromHeaders, resolvePexelsPr
|
|
|
20
20
|
import { putDraft, getDraft, updateDraft, deleteDraft } from "./persistence/draft-cache.js";
|
|
21
21
|
import { buildConnectUrl, parseCallback } from "./auth/login.js";
|
|
22
22
|
import { redirectMatches, isDeadJwt, markJwtDead, isAllowedRedirect } from "./auth/oauth-server.js";
|
|
23
|
+
import { sha256hex, sealCred, openCred, parseTokenKey, ENC_PREFIX } from "./auth/token-crypto.js";
|
|
24
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
23
25
|
import { isUnauthorized, renewAuthOnExpiry } from "./auth/renew.js";
|
|
24
26
|
import { jwtExpired } from "./persistence/config.js";
|
|
25
27
|
import { isLocalPath, resolveLocalPath, localContentType } from "./tools/media.js";
|
|
@@ -133,6 +135,34 @@ check("oauth redirect: loopback any port (Claude Code)", redirectMatches("http:/
|
|
|
133
135
|
check("oauth redirect: rejects other path/host/https port drift", !redirectMatches("http://localhost/callback", "http://localhost:3118/evil") && !redirectMatches("http://localhost/callback", "http://127.0.0.1:3118/callback") && !redirectMatches("https://claude.ai/api/mcp/auth_callback", "https://claude.ai:8443/api/mcp/auth_callback") && !redirectMatches("http://evil.com/callback", "http://evil.com:1/callback"));
|
|
134
136
|
check("oauth DCR: allows known clients + loopback", isAllowedRedirect("https://claude.ai/api/mcp/auth_callback") && isAllowedRedirect("https://chatgpt.com/connector_platform_oauth_redirect") && isAllowedRedirect("http://localhost:3118/callback") && isAllowedRedirect("http://[::1]/cb"));
|
|
135
137
|
check("oauth DCR: rejects foreign hosts / http non-loopback / other schemes", !isAllowedRedirect("https://evil.tld/cb") && !isAllowedRedirect("https://claude.ai.evil.tld/cb") && !isAllowedRedirect("http://claude.ai/cb") && !isAllowedRedirect("javascript:alert(1)") && !isAllowedRedirect("nope"));
|
|
138
|
+
{
|
|
139
|
+
// At-rest: tokens keyed by sha256 hex, ljwt/wsid sealed with AES-256-GCM, legacy plaintext passes through.
|
|
140
|
+
const k = parseTokenKey(randomBytes(32).toString("base64"));
|
|
141
|
+
const jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4In0.sig";
|
|
142
|
+
const sealed = sealCred(jwt, k);
|
|
143
|
+
check("token-at-rest: encrypt/decrypt round trip", sealed.startsWith(ENC_PREFIX) && !sealed.includes(jwt) && openCred(sealed, k) === jwt && sealCred(jwt, k) !== sealed, sealed);
|
|
144
|
+
check("token-at-rest: legacy plaintext + no key pass through", openCred(jwt, k) === jwt && openCred(jwt, null) === jwt && sealCred(jwt, null) === jwt && sealCred("", k) === "");
|
|
145
|
+
let tampered = false;
|
|
146
|
+
try {
|
|
147
|
+
openCred(sealed.slice(0, -4) + (sealed.endsWith("AAAA") ? "BBBB" : "AAAA"), k);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
tampered = true;
|
|
151
|
+
}
|
|
152
|
+
check("token-at-rest: tampered ciphertext is rejected", tampered);
|
|
153
|
+
const tok = "q2Xv_8kA-Zr0bTn3yLmP4wEuH6sD1fGcJ9oKiV5aQxs";
|
|
154
|
+
check("token-at-rest: sha256 key is 64-hex and matches node createHash", sha256hex(tok) === createHash("sha256").update(tok).digest("hex") && /^[0-9a-f]{64}$/.test(sha256hex(tok)) && !/^[0-9a-f]{64}$/.test(tok));
|
|
155
|
+
let bad = 0;
|
|
156
|
+
for (const raw of ["short", randomBytes(16).toString("base64"), randomBytes(32).toString("hex")]) {
|
|
157
|
+
try {
|
|
158
|
+
parseTokenKey(raw);
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
bad++;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
check("token-at-rest: malformed WEBCAKE_TOKEN_KEY throws, unset is null", bad === 3 && parseTokenKey(undefined) === null && parseTokenKey("") === null);
|
|
165
|
+
}
|
|
136
166
|
check("good page has no dangling-target warnings", r1.warnings.length === 0, r1.warnings);
|
|
137
167
|
check("good page stats", r1.stats.sections === 2 && r1.stats.ids === 3, r1.stats);
|
|
138
168
|
console.log("== validate: catches problems ==");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webcake-landing-mcp",
|
|
3
|
-
"version": "1.18.
|
|
3
|
+
"version": "1.18.2",
|
|
4
4
|
"description": "MCP server exposing Webcake landing-page element schemas + AI usage hints, and persisting LLM-generated page sources to a Webcake backend.",
|
|
5
5
|
"mcpName": "io.github.vuluu2k/webcake-landing-mcp",
|
|
6
6
|
"type": "module",
|