From dcb61bc4fd17606931f18110c16b35bd91687399 Mon Sep 17 00:00:00 2001 From: Daniel Salazar Date: Fri, 25 Sep 2026 17:47:03 -0700 Subject: [PATCH] fix: PUT-1824 + PUT-1884 + PUT-1952 + PUT-1827 (#3951) --- .../database/MySQLDatabaseClient.test.ts | 69 ++++++-- .../clients/database/MySQLDatabaseClient.ts | 33 ++-- src/backend/clients/database/SQLBatcher.js | 82 +++++++--- .../clients/database/SQLBatcher.test.ts | 61 +++++++- .../database/SqliteDatabaseClient.test.ts | 2 +- .../clients/database/SqliteDatabaseClient.ts | 1 + .../migrations/mysql/mysql_mig_43.sql | 44 ++++++ .../migrations/postgres/postgres_mig_32.sql | 24 +++ .../migrations/sqlite/0088_apps-index-url.sql | 22 +++ .../clients/database/retriableErrors.ts | 4 + .../apps/AppPermissionService.test.ts | 147 ++++++++++++++++++ .../services/apps/AppPermissionService.ts | 9 +- .../health/ServerHealthService.test.ts | 57 ++++++- .../services/health/ServerHealthService.ts | 50 +++++- .../services/permission/PermissionService.ts | 88 ++++++++--- .../services/permission/permissionUtil.ts | 7 + src/backend/stores/fs/S3ObjectStore.test.ts | 58 +++++++ src/backend/stores/fs/S3ObjectStore.ts | 5 +- .../stores/permission/PermissionStore.test.ts | 24 +++ .../stores/permission/PermissionStore.ts | 15 ++ src/mcp-connector/README.md | 7 +- src/mcp-connector/src/oauth.js | 73 ++++++++- src/mcp-connector/src/oauth.test.js | 144 +++++++++++++++++ src/mcp-connector/wrangler.toml | 4 +- 24 files changed, 936 insertions(+), 94 deletions(-) create mode 100644 src/backend/clients/database/migrations/mysql/mysql_mig_43.sql create mode 100644 src/backend/clients/database/migrations/postgres/postgres_mig_32.sql create mode 100644 src/backend/clients/database/migrations/sqlite/0088_apps-index-url.sql create mode 100644 src/mcp-connector/src/oauth.test.js diff --git a/src/backend/clients/database/MySQLDatabaseClient.test.ts b/src/backend/clients/database/MySQLDatabaseClient.test.ts index c0bd70a26..8b15c953c 100644 --- a/src/backend/clients/database/MySQLDatabaseClient.test.ts +++ b/src/backend/clients/database/MySQLDatabaseClient.test.ts @@ -184,7 +184,8 @@ describe('compareMigrationFilenames', () => { // // `read()` normally goes to the replica batcher; when the replica side is // degraded (batcher load-shed or a transient connection error) and a real -// replica is configured, the read retries once on the primary batcher. +// replica is configured, the read retries once on the primary read batcher. +// The write batcher is left unset, so a failover that reached it would throw. type Batcher = { execute: ReturnType }; @@ -200,7 +201,7 @@ const makeClient = (opts: { // inject the batchers directly. Configuration enum: SINGLE=0, REPLICA=1. Object.assign(client as unknown as Record, { dbReplica: opts.replica, - db: opts.primary, + dbPrimaryRead: opts.primary, configuration: opts.multiNode === false ? 0 : 1, }); return client; @@ -284,6 +285,10 @@ const startClient = async ( // must answer with one row-set per statement plus one for the SELECT 1. const rowSets = (...sets: unknown[][]) => [[...sets, [{ 1: 1 }]], undefined]; +// Writes go out one statement at a time, so the driver answers with just the +// result header. +const writeHeader = (header: Record) => [header, undefined]; + beforeEach(() => { createdPools.length = 0; createPoolMock.mockClear(); @@ -303,6 +308,9 @@ describe('MySQLDatabaseClient — pool construction', () => { expect(createdPools[0].poolConfig).toEqual({ maxPreparedStatements: 900, connectionLimit: 30, + maxIdle: 29, + idleTimeout: 60_000, + gracefulEnd: true, enableKeepAlive: true, host: 'db.internal', port: 3307, @@ -313,6 +321,32 @@ describe('MySQLDatabaseClient — pool construction', () => { }); }); + // mysql2 skips its idle reaper unless maxIdle < connectionLimit, and an + // unreaped connection is closed by the server under the pool's feet. + it('keeps the idle reaper on when a pool sets its own connection limit', async () => { + await startClient({ + replica: { host: 'replica.internal', connectionLimit: 10 }, + }); + + expect(createdPools[1].poolConfig).toMatchObject({ + connectionLimit: 10, + maxIdle: 9, + idleTimeout: 60_000, + }); + }); + + // mysql2 reads 0 as unlimited; a maxIdle of -1 would crash its reaper. + it('leaves maxIdle to mysql2 for an unlimited pool', async () => { + await startClient({ + replica: { host: 'replica.internal', connectionLimit: 0 }, + }); + + expect(createdPools[1].poolConfig).toMatchObject({ + connectionLimit: 0, + }); + expect(createdPools[1].poolConfig).not.toHaveProperty('maxIdle'); + }); + it('falls back to loopback defaults when the endpoint is unspecified', async () => { await startClient(); @@ -401,10 +435,30 @@ describe('MySQLDatabaseClient — query interface', () => { ]); }); + it('sends a write on its own, outside a transaction', async () => { + const client = await startClient(); + const pool = createdPools[0]; + pool.respond = () => writeHeader({ affectedRows: 1 }); + + await client.write('UPDATE `user` SET `username` = ? WHERE `id` = ?', [ + 'ada', + 1, + ]); + + expect(pool.calls).toEqual([ + { + sql: 'UPDATE `user` SET `username` = ? WHERE `id` = ?', + values: ['ada', 1], + }, + ]); + expect(pool.connections[0].beginTransaction).not.toHaveBeenCalled(); + expect(pool.connections[0].commit).not.toHaveBeenCalled(); + }); + it('maps the mysql result header onto the shared write result', async () => { const client = await startClient(); createdPools[0].respond = () => - rowSets({ insertId: 42, affectedRows: 2 } as unknown as unknown[]); + writeHeader({ insertId: 42, affectedRows: 2 }); await expect( client.write('UPDATE `user` SET `username` = ? WHERE `id` = ?', [ @@ -420,7 +474,7 @@ describe('MySQLDatabaseClient — query interface', () => { it('reports no rows affected when the header omits the counters', async () => { const client = await startClient(); - createdPools[0].respond = () => rowSets({} as unknown as unknown[]); + createdPools[0].respond = () => writeHeader({}); await expect(client.write('DELETE FROM `user`')).resolves.toEqual({ insertId: 0, @@ -432,17 +486,14 @@ describe('MySQLDatabaseClient — query interface', () => { it('builds an INSERT with quoted identifiers and positional params', async () => { const client = await startClient(); const pool = createdPools[0]; - pool.respond = () => - rowSets({ insertId: 7, affectedRows: 1 } as unknown as unknown[]); + pool.respond = () => writeHeader({ insertId: 7, affectedRows: 1 }); await expect( client.insert('user', { username: 'ada', email: null }), ).resolves.toMatchObject({ insertId: 7 }); expect(pool.calls[0]).toEqual({ - sql: - 'INSERT INTO `user` (`username`, `email`) VALUES (?, ?); ' + - 'SELECT 1', + sql: 'INSERT INTO `user` (`username`, `email`) VALUES (?, ?)', values: ['ada', null], }); }); diff --git a/src/backend/clients/database/MySQLDatabaseClient.ts b/src/backend/clients/database/MySQLDatabaseClient.ts index e5f791653..f5946bb8b 100644 --- a/src/backend/clients/database/MySQLDatabaseClient.ts +++ b/src/backend/clients/database/MySQLDatabaseClient.ts @@ -30,6 +30,8 @@ import { compareMigrationFilenames } from './migrationFilenames.js'; import type { IConfig } from '../../types'; const DEFAULT_SELECT_TIMEOUT_MS = 30_000; +const DEFAULT_CONNECTION_LIMIT = 30; +const IDLE_TIMEOUT_MS = 60_000; const replicaFailoverCounter = metrics .getMeter('puter-backend') @@ -54,7 +56,7 @@ export class MySQLDatabaseClient extends AbstractDatabaseClient { private replicaPool!: Pool; private db!: SQLBatcher; private dbReplica!: SQLBatcher; - /** Primary pool, SELECT-only: same rows as `db`, without the transaction. */ + /** Primary pool, SELECT-only: same rows as `db`, coalesced like the replica. */ private dbPrimaryRead!: SQLBatcher; private configuration = Configuration.SINGLE; private shutdownStarted = false; @@ -136,7 +138,7 @@ export class MySQLDatabaseClient extends AbstractDatabaseClient { throw error; } replicaFailoverCounter.add(1); - result = await this.db.execute(query, params); + result = await this.dbPrimaryRead.execute(query, params); } if (!result) return []; return (result[0] as Record[]) ?? []; @@ -300,9 +302,19 @@ export class MySQLDatabaseClient extends AbstractDatabaseClient { // ------------------------------------------------------------------ private createPool(poolConfig: PoolConfig): Pool { + const connectionLimit = + poolConfig.connectionLimit ?? DEFAULT_CONNECTION_LIMIT; const pool = createPool({ maxPreparedStatements: 900, - connectionLimit: 30, + connectionLimit, + // mysql2 only reaps idle connections when maxIdle < connectionLimit. + // Unreaped, they sit until the server's wait_timeout closes them, + // and the next statement sent on one fails. 0 means unlimited, + // and a negative maxIdle would crash the reaper. + ...(connectionLimit > 1 && { maxIdle: connectionLimit - 1 }), + idleTimeout: IDLE_TIMEOUT_MS, + // Reaped connections say goodbye instead of dropping the socket. + gracefulEnd: true, enableKeepAlive: true, ...poolConfig, multipleStatements: true, @@ -330,21 +342,20 @@ export class MySQLDatabaseClient extends AbstractDatabaseClient { return pool; } + /** + * One write per flush. Coalesced writes share a transaction, so unrelated + * callers hold each other's row locks until COMMIT and deadlock; a lone + * statement needs no BEGIN/COMMIT round trips. + */ private createPrimaryBatcher(pool: Pool): SQLBatcher { return new SQLBatcher(pool, { - maxTimeInQueue: 30, - maxBatchSize: 5, + maxBatchSize: 1, poolLabel: 'primary', acquireTimeoutMs: this.config.database?.acquireTimeoutMs, }); } - /** - * Reads that must see the primary still only read, so they skip the - * batcher's transaction wrapper. Worth a separate batcher because BEGIN and - * COMMIT are round trips: against a primary in another region they cost - * more than the query. - */ + /** Reads that must see the primary, still coalesced. */ private createPrimaryReadBatcher(pool: Pool): SQLBatcher { return new SQLBatcher(pool, { maxTimeInQueue: 30, diff --git a/src/backend/clients/database/SQLBatcher.js b/src/backend/clients/database/SQLBatcher.js index 7377deb62..b33e9c4d4 100644 --- a/src/backend/clients/database/SQLBatcher.js +++ b/src/backend/clients/database/SQLBatcher.js @@ -258,16 +258,11 @@ export class SQLBatcher { } if (batch.length === 0) return; - const query = `${batch.map((b) => b.sql.replace(/;+\s*$/, '')).join(';')}; SELECT 1`; // SELECT 1 forces mysql2 to return array - const values = batch.map((b) => b.values ?? []).flat(); - let connection; try { connection = await this.#acquireConnection(); } catch (error) { - this.#consecutiveFailures++; - this.#lastFailureAt = Date.now(); - flushFailureCounter.add(1, this.#metricAttrs); + this.#recordFlushFailure(); console.warn( 'SQLBatcher could not acquire connection for flush:', error, @@ -278,6 +273,14 @@ export class SQLBatcher { return; } + if (batch.length === 1 && !this.readOnly) { + await this.#flushLoneWrite(batch[0], connection); + return; + } + + const query = `${batch.map((b) => b.sql.replace(/;+\s*$/, '')).join(';')}; SELECT 1`; // SELECT 1 forces mysql2 to return array + const values = batch.map((b) => b.values ?? []).flat(); + // Run the coalesced multi-statement inside an explicit transaction so // a single bad statement (e.g. a duplicate-key INSERT) rolls back the // whole batch atomically, leaving us free to re-run each item @@ -333,7 +336,7 @@ export class SQLBatcher { async () => { while (cursor < batch.length) { const i = cursor++; - settled[i] = await this.#runFallbackItem(batch[i]); + settled[i] = await this.#runItem(batch[i]); } }, ); @@ -367,7 +370,34 @@ export class SQLBatcher { } } - // Run one fallback item, retrying transient failures with backoff. + // A single statement is atomic under autocommit, so it skips BEGIN/COMMIT + // and their round trips. With no transaction to roll back, a failure is + // not re-run wholesale; only the per-item retry policy applies. + async #flushLoneWrite(item, connection) { + const r = await this.#runItem(item, connection); + if (r.ok) { + this.#consecutiveFailures = 0; + item.resolve(r.value); + return; + } + // An error the server answered with (duplicate key, bad SQL) means it + // is up; only failing to reach it counts toward the breaker. + if (isRetriableError(r.error)) { + this.#recordFlushFailure(); + } else { + this.#consecutiveFailures = 0; + flushFailureCounter.add(1, this.#metricAttrs); + } + item.reject(r.error); + } + + #recordFlushFailure() { + this.#consecutiveFailures++; + this.#lastFailureAt = Date.now(); + flushFailureCounter.add(1, this.#metricAttrs); + } + + // Run one item on its own, retrying transient failures with backoff. // A read-only batcher may retry anything transient; a batcher that // carries writes only retries failures where the statement provably // did not apply — either it never reached the server, or the server @@ -378,33 +408,37 @@ export class SQLBatcher { // than surfacing: an item is a single statement, so a deadlock victim has // been fully undone, and the caller sees an unhandled 500 for what the // database is telling us to just run again. - async #runFallbackItem(b) { + async #runItem(b, connection) { let attempt = 0; while (true) { - let connection; - try { - connection = await this.#acquireConnection(); - } catch (error) { - return { ok: false, error }; + if (!connection) { + try { + connection = await this.#acquireConnection(); + } catch (error) { + return { ok: false, error }; + } } + let error; try { return { ok: true, value: await connection.query(b.sql, b.values ?? []), }; - } catch (error) { - const canRetry = this.readOnly - ? isRetriableError(error) || isRolledBackError(error) - : isNeverSentError(error) || isRolledBackError(error); - if (!canRetry || attempt >= ITEM_RETRY_ATTEMPTS) { - return { ok: false, error }; - } - attempt++; - fallbackItemRetriesCounter.add(1, this.#metricAttrs); - await sleep(RETRY_BASE_BACKOFF_MS * attempt); + } catch (e) { + error = e; } finally { connection.release(); + connection = undefined; } + const canRetry = this.readOnly + ? isRetriableError(error) || isRolledBackError(error) + : isNeverSentError(error) || isRolledBackError(error); + if (!canRetry || attempt >= ITEM_RETRY_ATTEMPTS) { + return { ok: false, error }; + } + attempt++; + fallbackItemRetriesCounter.add(1, this.#metricAttrs); + await sleep(RETRY_BASE_BACKOFF_MS * attempt); } } } diff --git a/src/backend/clients/database/SQLBatcher.test.ts b/src/backend/clients/database/SQLBatcher.test.ts index 85056d4ff..ad7ca94f4 100644 --- a/src/backend/clients/database/SQLBatcher.test.ts +++ b/src/backend/clients/database/SQLBatcher.test.ts @@ -126,7 +126,7 @@ describe('SQLBatcher', () => { }); it('drops the oldest item with reason queueOverflow at the high-water mark', async () => { - const conn = makeConnection(happyBatch); + const conn = makeConnection(() => [[{ n: 0 }], undefined]); const { pool } = makePool(conn); const batcher = new SQLBatcher(pool, { maxTimeInQueue: 5, @@ -277,6 +277,65 @@ describe('SQLBatcher', () => { ); }); + it('sends a lone write as-is, outside a transaction', async () => { + const conn = makeConnection((sql) => { + if (isBatchQuery(sql)) throw new Error('unexpected batch query'); + return [{ affectedRows: 1 }, undefined]; + }); + const { pool } = makePool(conn); + const batcher = new SQLBatcher(pool, { maxBatchSize: 1 }); + + const result = await batcher.query('UPDATE t SET x = ?', [1]); + expect(result[0]).toEqual({ affectedRows: 1 }); + expect(conn.query).toHaveBeenCalledTimes(1); + expect(conn.query).toHaveBeenCalledWith('UPDATE t SET x = ?', [1]); + expect(conn.beginTransaction).not.toHaveBeenCalled(); + expect(conn.commit).not.toHaveBeenCalled(); + expect(conn.release).toHaveBeenCalledTimes(1); + }); + + it('runs a lone write the server rejects once, without tripping the breaker', async () => { + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + const conn = makeConnection((sql) => { + if (sql === 'INSERT dup') throw makeError('ER_DUP_ENTRY'); + return [{ affectedRows: 1 }, undefined]; + }); + const { pool } = makePool(conn); + const batcher = new SQLBatcher(pool, { + maxBatchSize: 1, + failureThreshold: 1, + cooldownMs: 60_000, + }); + + await expect(batcher.query('INSERT dup', [])).rejects.toMatchObject({ + code: 'ER_DUP_ENTRY', + }); + expect(conn.query).toHaveBeenCalledTimes(1); + expect(conn.rollback).not.toHaveBeenCalled(); + expect(warnSpy).not.toHaveBeenCalled(); + // The server answered, so it is up: the next write goes through. + await expect(batcher.query('INSERT fine', [])).resolves.toBeTruthy(); + warnSpy.mockRestore(); + }); + + it('retries a lone write on a connection the server closed for inactivity', async () => { + let attempts = 0; + const conn = makeConnection(() => { + attempts++; + if (attempts === 1) { + throw makeError('ER_CLIENT_INTERACTION_TIMEOUT'); + } + return [{ affectedRows: 1 }, undefined]; + }); + const { pool, getConnection } = makePool(conn); + const batcher = new SQLBatcher(pool, { maxBatchSize: 1 }); + + const result = await batcher.query('INSERT x', []); + expect(result[0]).toEqual({ affectedRows: 1 }); + expect(attempts).toBe(2); + expect(getConnection).toHaveBeenCalledTimes(2); + }); + it('never retries deterministic row-level errors and does not escalate the breaker', async () => { let fallbackAttempts = 0; const conn = makeConnection((sql) => { diff --git a/src/backend/clients/database/SqliteDatabaseClient.test.ts b/src/backend/clients/database/SqliteDatabaseClient.test.ts index 7909bfe89..b20e91280 100644 --- a/src/backend/clients/database/SqliteDatabaseClient.test.ts +++ b/src/backend/clients/database/SqliteDatabaseClient.test.ts @@ -27,7 +27,7 @@ import { DatabaseClientFactory } from './index.js'; import { SqliteDatabaseClient } from './SqliteDatabaseClient.js'; /** Highest schema version the migration table can reach. */ -const CURRENT_SCHEMA_VERSION = 83; +const CURRENT_SCHEMA_VERSION = 84; /** * These suites migrate real files on disk. Idle they finish in well under a diff --git a/src/backend/clients/database/SqliteDatabaseClient.ts b/src/backend/clients/database/SqliteDatabaseClient.ts index 812f86f60..53afe5481 100644 --- a/src/backend/clients/database/SqliteDatabaseClient.ts +++ b/src/backend/clients/database/SqliteDatabaseClient.ts @@ -117,6 +117,7 @@ const AVAILABLE_MIGRATIONS: [number, string[]][] = [ [80, ['0085_event-subscriptions-include-value.sql']], [81, ['0086_subdomains-app-owner-cascade.sql']], [82, ['0087_feedback-attachments.sql']], + [83, ['0088_apps-index-url.sql']], ]; export class SqliteDatabaseClient extends AbstractDatabaseClient { diff --git a/src/backend/clients/database/migrations/mysql/mysql_mig_43.sql b/src/backend/clients/database/migrations/mysql/mysql_mig_43.sql new file mode 100644 index 000000000..78a3d9954 --- /dev/null +++ b/src/backend/clients/database/migrations/mysql/mysql_mig_43.sql @@ -0,0 +1,44 @@ +-- Copyright (C) 2024-present Puter Technologies Inc. +-- +-- This file is part of Puter. +-- +-- Puter is free software: you can redistribute it and/or modify +-- it under the terms of the GNU Affero General Public License as published +-- by the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Affero General Public License for more details. +-- +-- You should have received a copy of the GNU Affero General Public License +-- along with this program. If not, see . + +-- Mirrors SQLite migration 0088: index `apps.index_url` for origin lookups. +-- TEXT needs a prefix; 191 characters keeps the key under 767 bytes in +-- utf8mb4 whatever the table's row format, and URLs diverge well before it. +-- +-- Idempotent: the guarded procedure, as mig_36. The 1061 handler covers two +-- nodes booting at once, where the loser finds the index already built. + +DROP PROCEDURE IF EXISTS _puter_add_apps_index_url_index; +DELIMITER // +CREATE PROCEDURE _puter_add_apps_index_url_index() +BEGIN + DECLARE CONTINUE HANDLER FOR 1061 BEGIN END; + IF NOT EXISTS ( + SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'apps' + AND INDEX_NAME = 'idx_apps_index_url' + ) THEN + ALTER TABLE `apps` + ADD INDEX `idx_apps_index_url` (`index_url`(191)), + ALGORITHM=INPLACE, LOCK=NONE; + END IF; +END// +DELIMITER ; + +CALL _puter_add_apps_index_url_index(); +DROP PROCEDURE IF EXISTS _puter_add_apps_index_url_index; diff --git a/src/backend/clients/database/migrations/postgres/postgres_mig_32.sql b/src/backend/clients/database/migrations/postgres/postgres_mig_32.sql new file mode 100644 index 000000000..91cd611f0 --- /dev/null +++ b/src/backend/clients/database/migrations/postgres/postgres_mig_32.sql @@ -0,0 +1,24 @@ +-- Copyright (C) 2024-present Puter Technologies Inc. +-- +-- This file is part of Puter. +-- +-- Puter is free software: you can redistribute it and/or modify +-- it under the terms of the GNU Affero General Public License as published +-- by the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Affero General Public License for more details. +-- +-- You should have received a copy of the GNU Affero General Public License +-- along with this program. If not, see . + +-- Mirrors SQLite migration 0088: index `apps.index_url` for origin lookups. +-- Hash rather than btree: `index_url` allows 3000 characters, past btree's +-- ~2.7 KB row limit, and every lookup is equality (`=` / `IN`). +-- +-- Idempotent via IF NOT EXISTS. + +CREATE INDEX IF NOT EXISTS idx_apps_index_url ON apps USING hash (index_url); diff --git a/src/backend/clients/database/migrations/sqlite/0088_apps-index-url.sql b/src/backend/clients/database/migrations/sqlite/0088_apps-index-url.sql new file mode 100644 index 000000000..63100731f --- /dev/null +++ b/src/backend/clients/database/migrations/sqlite/0088_apps-index-url.sql @@ -0,0 +1,22 @@ +-- Copyright (C) 2024-present Puter Technologies Inc. +-- +-- This file is part of Puter. +-- +-- Puter is free software: you can redistribute it and/or modify +-- it under the terms of the GNU Affero General Public License as published +-- by the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Affero General Public License for more details. +-- +-- You should have received a copy of the GNU Affero General Public License +-- along with this program. If not, see . + +-- Resolving an origin to its app looks `apps` up by `index_url` on every +-- origin-based app token request (`AuthService#findCanonicalAppUidForOrigin`), +-- plus `AppStore.existsByIndexUrl`. With no index each lookup scans the table. + +CREATE INDEX IF NOT EXISTS `idx_apps_index_url` ON `apps` (`index_url`); diff --git a/src/backend/clients/database/retriableErrors.ts b/src/backend/clients/database/retriableErrors.ts index 12fee9ef6..5c5179d91 100644 --- a/src/backend/clients/database/retriableErrors.ts +++ b/src/backend/clients/database/retriableErrors.ts @@ -32,6 +32,7 @@ const RETRIABLE_ERROR_CODES = new Set([ 'ENETUNREACH', 'EAI_AGAIN', POOL_ACQUIRE_TIMEOUT, + 'ER_CLIENT_INTERACTION_TIMEOUT', ]); const RETRIABLE_ERROR_MESSAGES = [ @@ -52,6 +53,9 @@ const NEVER_SENT_ERROR_CODES = new Set([ 'ENETUNREACH', 'EAI_AGAIN', POOL_ACQUIRE_TIMEOUT, + // The server closed the pooled connection for inactivity before the + // statement arrived, so nothing ran. + 'ER_CLIENT_INTERACTION_TIMEOUT', ]); /** diff --git a/src/backend/services/apps/AppPermissionService.test.ts b/src/backend/services/apps/AppPermissionService.test.ts index 103915002..a9a19f6a1 100644 --- a/src/backend/services/apps/AppPermissionService.test.ts +++ b/src/backend/services/apps/AppPermissionService.test.ts @@ -395,6 +395,153 @@ describe('AppPermissionService — app-root-dir rewriter', () => { ).toBe(false); }); + /** Point `app` at a new hosted subdomain rooted at the owner's `dirName`. */ + const moveAppRoot = async ( + owner: Actor, + app: { id: number }, + dirName: string, + ) => { + const sub = `apx${Math.random().toString(36).slice(2, 10)}`; + const entry = await server.stores.fsEntry.getEntryByPath( + `/${owner.user.username}/${dirName}`, + ); + await server.stores.subdomain.create({ + userId: owner.user.id!, + subdomain: sub, + rootDirId: entry!.id as number, + }); + await server.stores.app.update(app.id, { + index_url: `https://${sub}.${HOSTING_DOMAIN}/index.html`, + }); + return entry!; + }; + + const storedRows = (owner: Actor, target: { id: number }) => + server.stores.permission.listUserAppPermsFromPrimary( + owner.user.id!, + target.id, + ); + + const grantAs = ( + owner: Actor, + target: { uid: string }, + permission: string, + extra?: Record, + ) => + runWithContext({ actor: owner }, () => + permissions.grantUserAppPermission( + owner, + target.uid, + permission, + extra, + ), + ); + + const revokeAs = ( + owner: Actor, + target: { uid: string }, + permission: string, + ) => + runWithContext({ actor: owner }, () => + permissions.revokeUserAppPermission(owner, target.uid, permission), + ); + + it('revoke withdraws the root the grant resolved after the app moves', async () => { + const owner = await makeUser(); + const { app, entry } = await makeHostedApp(owner); + const target = await makeApp(owner.user.id!); + const pseudo = `app-root-dir:${app.uid}:write`; + + await grantAs(owner, target, pseudo); + const newRoot = await moveAppRoot(owner, app, 'Documents'); + await revokeAs(owner, target, pseudo); + + const rows = await storedRows(owner, target); + expect(rows.map((r) => r.permission)).not.toContain( + `fs:${entry.uuid}:write`, + ); + expect(rows.map((r) => r.permission)).not.toContain( + `fs:${newRoot.uuid}:write`, + ); + }); + + it('revoke withdraws a recorded grant once the app has no root at all', async () => { + const owner = await makeUser(); + const { app, entry } = await makeHostedApp(owner); + const target = await makeApp(owner.user.id!); + const pseudo = `app-root-dir:${app.uid}:write`; + + await grantAs(owner, target, pseudo); + await server.stores.app.update(app.id, { + index_url: 'https://example.com/index.html', + }); + await revokeAs(owner, target, pseudo); + + expect( + (await storedRows(owner, target)).map((r) => r.permission), + ).not.toContain(`fs:${entry.uuid}:write`); + }); + + it('revoke falls back to the current root for a row with no recorded source', async () => { + const owner = await makeUser(); + const { app, entry } = await makeHostedApp(owner); + const target = await makeApp(owner.user.id!); + await server.stores.permission.upsertUserAppPerm( + owner.user.id!, + target.id, + `fs:${entry.uuid}:write`, + {}, + ); + + await revokeAs(owner, target, `app-root-dir:${app.uid}:write`); + + expect(await storedRows(owner, target)).toEqual([]); + }); + + it('re-granting over a row with no recorded source records it', async () => { + const owner = await makeUser(); + const { app, entry } = await makeHostedApp(owner); + const target = await makeApp(owner.user.id!); + const pseudo = `app-root-dir:${app.uid}:write`; + await server.stores.permission.upsertUserAppPerm( + owner.user.id!, + target.id, + `fs:${entry.uuid}:write`, + { note: 'kept' }, + ); + + await grantAs(owner, target, pseudo); + expect(await storedRows(owner, target)).toMatchObject([ + { + permission: `fs:${entry.uuid}:write`, + extra: { note: 'kept', grantedAs: pseudo }, + }, + ]); + + await moveAppRoot(owner, app, 'Documents'); + await revokeAs(owner, target, pseudo); + expect(await storedRows(owner, target)).toEqual([]); + }); + + it('does not let a caller plant a recorded source on a direct grant', async () => { + const owner = await makeUser(); + const { app } = await makeHostedApp(owner); + const target = await makeApp(owner.user.id!); + const pseudo = `app-root-dir:${app.uid}:write`; + const documents = await server.stores.fsEntry.getEntryByPath( + `/${owner.user.username}/Documents`, + ); + const direct = `fs:${documents!.uuid}:write`; + + await grantAs(owner, target, direct, { grantedAs: pseudo }); + expect((await storedRows(owner, target))[0].extra).toEqual({}); + + await revokeAs(owner, target, pseudo); + expect( + (await storedRows(owner, target)).map((r) => r.permission), + ).toEqual([direct]); + }); + it('refuses to resolve an app the actor does not own', async () => { const owner = await makeUser(); const stranger = await makeUser(); diff --git a/src/backend/services/apps/AppPermissionService.ts b/src/backend/services/apps/AppPermissionService.ts index c36f70119..2367a3073 100644 --- a/src/backend/services/apps/AppPermissionService.ts +++ b/src/backend/services/apps/AppPermissionService.ts @@ -152,13 +152,16 @@ export class AppPermissionService extends PuterService { // -- app-root-dir:: → fs:: ------- // Only rewrites while a user-app permission row is being written or - // removed — `grantUserAppPermission` / `revokeUserAppPermission`, which - // both set the context flag (see PermissionService) precisely so the - // revoke names the row the grant wrote. During scans we return + // removed — `grantUserAppPermission` / `revokeUserAppPermission` set + // the context flag (see PermissionService). During scans we return // PERMISSION_FOR_NOTHING_IN_PARTICULAR so `check(actor, 'app-root-dir:…')` // never accidentally matches through the fs-permission path. + // + // An app's root can move after the grant, so the row records the + // pseudo-permission and revoke finds it by that, not by the new root. permissions.registerRewriter({ id: 'app-root-dir-to-fs', + recordSource: true, matches: (permission: string) => permission.startsWith('app-root-dir:'), rewrite: async (permission: string): Promise => { diff --git a/src/backend/services/health/ServerHealthService.test.ts b/src/backend/services/health/ServerHealthService.test.ts index 15a980855..cb42fe1af 100644 --- a/src/backend/services/health/ServerHealthService.test.ts +++ b/src/backend/services/health/ServerHealthService.test.ts @@ -229,17 +229,70 @@ describe('ServerHealthService — default checks', () => { const { service, dbRead } = makeService({ server_health: { db_liveness_latency_fail_ms: 10 }, }); + // Answered on a timer, so the event loop keeps turning while it waits. + dbRead.mockImplementation( + () => + new Promise((resolve) => + setTimeout(() => resolve([{ ok: 1 }]), 50), + ), + ); + service.onServerStart(); + await runCycle(); + await vi.advanceTimersByTimeAsync(100); + expect(await uncachedStatus(service)).toEqual({ + ok: false, + failed: ['database-liveness'], + }); + service.onServerShutdown(); + }); + + it('passes a slow probe when the event loop was blocked for the difference', async () => { + const { service, dbRead } = makeService({ + server_health: { db_liveness_latency_fail_ms: 10 }, + }); + // A clock jump with no timer ticks in between is a blocked loop: 50ms + // gap, 40ms of it beyond the watch timer's own interval. dbRead.mockImplementation(async () => { - // Advance the fake clock so the measured latency crosses over. vi.setSystemTime(Date.now() + 50); return [{ ok: 1 }]; }); service.onServerStart(); await runCycle(); - expect(await service.getStatus()).toEqual({ + await vi.advanceTimersByTimeAsync(100); + + expect(await uncachedStatus(service)).toEqual({ ok: true }); + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining('event loop was blocked for 40ms'), + ); + service.onServerShutdown(); + }); + + it('still fails when the latency outlasts an event-loop stall', async () => { + const { service, dbRead } = makeService({ + server_health: { db_liveness_latency_fail_ms: 10 }, + }); + // 30ms of waiting with the loop turning, then a 30ms stall. + dbRead.mockImplementation(async () => { + await new Promise((resolve) => setTimeout(resolve, 30)); + vi.setSystemTime(Date.now() + 30); + return [{ ok: 1 }]; + }); + service.onServerStart(); + await runCycle(); + await vi.advanceTimersByTimeAsync(100); + + expect(await uncachedStatus(service)).toEqual({ ok: false, failed: ['database-liveness'], }); + expect(errorSpy).toHaveBeenCalledWith( + expect.stringContaining('database-liveness'), + expect.objectContaining({ + message: expect.stringContaining( + 'event loop blocked up to 20ms', + ), + }), + ); service.onServerShutdown(); }); diff --git a/src/backend/services/health/ServerHealthService.ts b/src/backend/services/health/ServerHealthService.ts index 28e268e9a..5a13be664 100644 --- a/src/backend/services/health/ServerHealthService.ts +++ b/src/backend/services/health/ServerHealthService.ts @@ -32,7 +32,9 @@ import { PUTER_KV_STORE_TABLE_NAME } from '../../stores/systemKv/tableDefinition * * - `database-liveness` — `SELECT 1 AS ok` through the normal read path (a * read-replica where one is configured), latency-gated against - * `config.server_health.db_liveness_latency_fail_ms` (default 1500ms). + * `config.server_health.db_liveness_latency_fail_ms` (default 1500ms). Time + * this process spent with its event loop blocked is not counted against the + * database; a probe slow only because of that passes with a warning. * - `socket-initialized` — socket.io must be attached. Only registered when * SocketService is present (skipped for API-only deployments). * @@ -78,6 +80,7 @@ const DEFAULT_DEPENDENCY_CHECK_INTERVAL_MS = 30 * SECOND; const DEFAULT_REDIS_LATENCY_FAIL_MS = 1 * SECOND; const DEFAULT_DYNAMO_LATENCY_FAIL_MS = 1500; const DEFAULT_S3_LATENCY_FAIL_MS = 2 * SECOND; +const LOOP_WATCH_INTERVAL_MS = 10; const STATUS_CACHE_TTL_SECONDS = 5; const STATUS_CACHE_KEY = 'server-health:status'; @@ -93,6 +96,31 @@ const DYNAMO_PROBE_KEY = { key: 'liveness-probe', }; +/** + * Times the longest gap between ticks of a short timer, for as long as a probe + * runs. The gap still open when `stop` is called counts too, so a stall that + * ends just before the reply is read isn't missed. Returns ms blocked beyond + * the timer's own interval. + */ +const watchLoopStalls = (): (() => number) => { + const startedAt = Date.now(); + let lastTick = startedAt; + let longest = 0; + const timer = setInterval(() => { + const now = Date.now(); + longest = Math.max(longest, now - lastTick); + lastTick = now; + // A probe that never settles must not leave this running. + if (now - startedAt > CHECK_TIMEOUT_MS) clearInterval(timer); + }, LOOP_WATCH_INTERVAL_MS); + timer.unref?.(); + return () => { + clearInterval(timer); + const gap = Math.max(longest, Date.now() - lastTick); + return Math.max(0, gap - LOOP_WATCH_INTERVAL_MS); + }; +}; + type CheckFn = () => Promise | unknown; type FailHandler = (err: unknown) => Promise | void; @@ -312,19 +340,31 @@ export class ServerHealthService extends PuterService { const db = this.clients.db; if (db && typeof db.read === 'function') { this.addCheck('database-liveness', async () => { + const stopWatch = watchLoopStalls(); const startedAt = Date.now(); - const rows = (await db.read('SELECT 1 AS ok')) as unknown[]; + let rows: unknown[]; + let blockedMs: number; + try { + rows = (await db.read('SELECT 1 AS ok')) as unknown[]; + } finally { + blockedMs = stopWatch(); + } const durationMs = Date.now() - startedAt; this.#stats.database_liveness_latency_ms = durationMs; if (!Array.isArray(rows) || rows.length === 0) { throw new Error('database liveness query returned no rows'); } - if (durationMs > latencyFailMs) { - throw new Error( - `database liveness latency ${durationMs}ms > threshold ${latencyFailMs}ms`, + if (durationMs <= latencyFailMs) return; + if (durationMs - blockedMs <= latencyFailMs) { + console.warn( + `[server-health] database-liveness took ${durationMs}ms, but the event loop was blocked for ${blockedMs}ms of it`, ); + return; } + throw new Error( + `database liveness latency ${durationMs}ms > threshold ${latencyFailMs}ms (event loop blocked up to ${blockedMs}ms)`, + ); }); } diff --git a/src/backend/services/permission/PermissionService.ts b/src/backend/services/permission/PermissionService.ts index 0d275c2d0..9cbccad29 100644 --- a/src/backend/services/permission/PermissionService.ts +++ b/src/backend/services/permission/PermissionService.ts @@ -1309,6 +1309,13 @@ export class PermissionService extends PuterService { } } + /** See `PermissionRewriter.recordSource`. */ + #recordsSource(permission: string): boolean { + return this.rewriters.some( + (rewriter) => rewriter.recordSource && rewriter.matches(permission), + ); + } + /** * What `grantUserAppPermission` checks before it writes: the rewrite that * decides what the row stores, and the width of the column it lands in. @@ -1334,6 +1341,7 @@ export class PermissionService extends PuterService { extra: Record = {}, meta: GrantMeta = {}, ): Promise { + const grantedAs = this.#recordsSource(permission) ? permission : null; permission = await this.#rewriteForUserAppWrite(permission); this.assertGrantableFsPermission(permission); // Checked after the rewrite, because the rewrite is what decides how @@ -1356,21 +1364,25 @@ export class PermissionService extends PuterService { legacyCode: 'forbidden', }); - // Skip redundant upserts (saves db roundtrip + cache invalidation) - if ( - await this.stores.permission.hasUserAppPerm( - actor.user.id, - app.id, - permission, - ) - ) + // Only this layer writes `grantedAs`, so revoke can trust it. + const { grantedAs: _, ...rowExtra } = extra; + const [existing] = await this.stores.permission.readUserAppPerms( + actor.user.id, + app.id, + [permission], + ); + // Skip redundant upserts (saves db roundtrip + cache invalidation), + // unless the row has yet to record what this grant was asked as. + if (existing && (!grantedAs || existing.extra?.grantedAs === grantedAs)) return; await this.stores.permission.upsertUserAppPerm( actor.user.id, app.id, permission, - extra, + grantedAs + ? { ...existing?.extra, ...rowExtra, grantedAs } + : rowExtra, ); this.stores.permission .auditUserAppPerm({ @@ -1399,8 +1411,13 @@ export class PermissionService extends PuterService { throw new HttpError(403, 'actor must be a user', { legacyCode: 'forbidden', }); + const recordsSource = this.#recordsSource(permission); // The same rewrite the grant used, so this names the row it wrote. - permission = await this.#rewriteForUserAppWrite(permission); + // A recorded source is looked up as asked instead, since it may now + // resolve somewhere other than where the grant did. + let rewritten = recordsSource + ? null + : await this.#rewriteForUserAppWrite(permission); const app = await this.stores.app.resolveApp(appIdentifier); if (!app) throw new HttpError(404, `entity_not_found: app:${appIdentifier}`, { @@ -1411,20 +1428,40 @@ export class PermissionService extends PuterService { legacyCode: 'forbidden', }); - await this.stores.permission.deleteUserAppPerm( - actor.user.id, - app.id, - permission, - ); - this.stores.permission - .auditUserAppPerm({ - user_id: actor.user.id, - app_id: app.id, - permission, - action: 'revoke', - reason: meta.reason ?? 'revoked via PermissionService', - }) - .catch(() => {}); + let removed: string[] = []; + if (recordsSource) { + const rows = + await this.stores.permission.listUserAppPermsFromPrimary( + actor.user.id, + app.id, + ); + removed = rows + .filter((row) => row.extra?.grantedAs === permission) + .map((row) => row.permission); + } + if (removed.length === 0) { + // Rows written before sources were recorded carry none, so they + // can only be found by what the permission resolves to now. + rewritten ??= await this.#rewriteForUserAppWrite(permission); + removed = [rewritten]; + } + + for (const removedPermission of removed) { + await this.stores.permission.deleteUserAppPerm( + actor.user.id, + app.id, + removedPermission, + ); + this.stores.permission + .auditUserAppPerm({ + user_id: actor.user.id, + app_id: app.id, + permission: removedPermission, + action: 'revoke', + reason: meta.reason ?? 'revoked via PermissionService', + }) + .catch(() => {}); + } if (actor.user.uuid) { await this.#bumpAppUnderUserCacheGeneration( @@ -1432,7 +1469,8 @@ export class PermissionService extends PuterService { app.uid, ); } - this.#announceRevoked(actor.user.id, app.uid, permission); + for (const removedPermission of removed) + this.#announceRevoked(actor.user.id, app.uid, removedPermission); } async revokeUserAppAll( diff --git a/src/backend/services/permission/permissionUtil.ts b/src/backend/services/permission/permissionUtil.ts index 4e1a3a5c8..8ac086361 100644 --- a/src/backend/services/permission/permissionUtil.ts +++ b/src/backend/services/permission/permissionUtil.ts @@ -198,6 +198,13 @@ export interface PermissionRewriter { id?: string; matches: (permission: string) => boolean; rewrite: (permission: string) => Promise | string; + /** + * The rewrite resolves through state that can change after a grant. A + * user-app row written through it records the permission as asked, and + * revoking that permission removes the rows recorded under it instead of + * whatever it resolves to now. + */ + recordSource?: boolean; } export interface ImplicatorCheckInput { diff --git a/src/backend/stores/fs/S3ObjectStore.test.ts b/src/backend/stores/fs/S3ObjectStore.test.ts index b413e92a1..37ce085fa 100644 --- a/src/backend/stores/fs/S3ObjectStore.test.ts +++ b/src/backend/stores/fs/S3ObjectStore.test.ts @@ -703,6 +703,64 @@ describe('S3ObjectStore upload sizing', () => { ]); }); + it('treats a sizeHint of 0 on a stream as unknown and uploads every byte', async () => { + const { store: fake, sent } = makeFakeStore({ + maxSingleUploadSize: 100, + partSize: 100, + respond: (command) => + command.name === 'CreateMultipartUploadCommand' + ? { UploadId: 'upload-1' } + : { ETag: 'etag-1' }, + }); + + await fake.uploadFromServer( + { + bucket, + objectKey: 'k', + contentType: 'text/plain', + body: Readable.from([Buffer.from('hello world')]), + sizeHint: 0, + }, + region, + ); + + expect(names(sent)).toEqual([ + 'CreateMultipartUploadCommand', + 'UploadPartCommand', + 'CompleteMultipartUploadCommand', + ]); + expect(sent[1]?.input.ContentLength).toBe(11); + }); + + it('stores an empty object for an empty stream with a sizeHint of 0', async () => { + const { store: fake, sent } = makeFakeStore({ + maxSingleUploadSize: 100, + partSize: 100, + respond: (command) => + command.name === 'CreateMultipartUploadCommand' + ? { UploadId: 'upload-1' } + : {}, + }); + + await fake.uploadFromServer( + { + bucket, + objectKey: 'k', + contentType: 'text/plain', + body: Readable.from([]), + sizeHint: 0, + }, + region, + ); + + expect(names(sent)).toEqual([ + 'CreateMultipartUploadCommand', + 'AbortMultipartUploadCommand', + 'PutObjectCommand', + ]); + expect(sent[2]?.input.ContentLength).toBe(0); + }); + it('splits a buffer body into parts of the resolved part size', async () => { const { store: fake, sent } = makeFakeStore({ maxSingleUploadSize: 4, diff --git a/src/backend/stores/fs/S3ObjectStore.ts b/src/backend/stores/fs/S3ObjectStore.ts index 1adb1ced1..beb35a006 100644 --- a/src/backend/stores/fs/S3ObjectStore.ts +++ b/src/backend/stores/fs/S3ObjectStore.ts @@ -577,7 +577,10 @@ export class S3ObjectStore extends PuterStore { ) { return Number(input.contentLength); } - if (Number.isFinite(input.sizeHint) && Number(input.sizeHint) >= 0) { + // A 0 hint is the "length unknown" placeholder streamed writes pass + // (legacy signed write, chunked WebDAV PUT); taking it literally + // declares an empty body. + if (Number.isFinite(input.sizeHint) && Number(input.sizeHint) > 0) { return Number(input.sizeHint); } if (Buffer.isBuffer(input.body) || input.body instanceof Uint8Array) { diff --git a/src/backend/stores/permission/PermissionStore.test.ts b/src/backend/stores/permission/PermissionStore.test.ts index 60269d50e..6093c3ae0 100644 --- a/src/backend/stores/permission/PermissionStore.test.ts +++ b/src/backend/stores/permission/PermissionStore.test.ts @@ -176,6 +176,30 @@ describe('PermissionStore', () => { ).toEqual([]); }); + it('lists every row for one user and app from the primary', async () => { + const user = await makeUser(); + const app = await makeApp(user.id); + const otherApp = await makeApp(user.id); + await store.upsertUserAppPerm(user.id, app.id, 'driver:a', { + v: 1, + }); + await store.upsertUserAppPerm(user.id, app.id, 'driver:b', {}); + await store.upsertUserAppPerm(user.id, otherApp.id, 'driver:c', {}); + + const rows = await store.listUserAppPermsFromPrimary( + user.id, + app.id, + ); + expect( + rows + .map((r) => ({ permission: r.permission, extra: r.extra })) + .sort((a, b) => a.permission.localeCompare(b.permission)), + ).toEqual([ + { permission: 'driver:a', extra: { v: 1 } }, + { permission: 'driver:b', extra: {} }, + ]); + }); + it('returns nothing for an empty permission list', async () => { const user = await makeUser(); const app = await makeApp(user.id); diff --git a/src/backend/stores/permission/PermissionStore.ts b/src/backend/stores/permission/PermissionStore.ts index 7729df6ea..4bec11e25 100644 --- a/src/backend/stores/permission/PermissionStore.ts +++ b/src/backend/stores/permission/PermissionStore.ts @@ -757,6 +757,21 @@ export class PermissionStore extends PuterStore { return all.some((row) => row.permission === permission); } + /** + * Every row this user granted this app, read from the primary past the row + * cache — for a write that must see a grant made moments before. + */ + async listUserAppPermsFromPrimary( + userId: number, + appId: number, + ): Promise { + const rows = await this.clients.db.pread( + 'SELECT * FROM `user_to_app_permissions` WHERE `user_id` = ? AND `app_id` = ?', + [userId, appId], + ); + return rows.map((row) => this.#decodeExtra(row)); + } + async upsertUserAppPerm( userId: number, appId: number, diff --git a/src/mcp-connector/README.md b/src/mcp-connector/README.md index 25fafb4a4..fedb7d9f5 100644 --- a/src/mcp-connector/README.md +++ b/src/mcp-connector/README.md @@ -216,6 +216,8 @@ If you use the OAuth flow (below), also set the sealing secret in production: wrangler secret put OAUTH_SECRET ``` +Rotating it invalidates every issued `client_id`, so clients have to register again. + ## Authentication Two ways, both running as the caller — the Worker holds no credentials of its own: @@ -226,8 +228,9 @@ Two ways, both running as the caller — the Worker holds no credentials of its the Worker hands the client your Puter token. See [`src/oauth.js`](src/oauth.js). Under the hood it redirects to Puter's `?action=authme` page and catches the returned token on its `/oauth/callback`; - the short-lived flow/code blobs are AES-GCM sealed with `OAUTH_SECRET`, so the - Worker stays stateless. + the `client_id` (carrying its registered redirect URIs) and the short-lived + flow/code blobs are AES-GCM sealed with `OAUTH_SECRET`, so the Worker stays + stateless. 2. **Bearer token** (copy/paste). Get it from a logged-in Puter browser tab's devtools console: `puter.authToken`. Treat it like a password. Pass it as an `Authorization: Bearer ` header (or the `.mcpb` token field). diff --git a/src/mcp-connector/src/oauth.js b/src/mcp-connector/src/oauth.js index cb98ac25b..7e72fabc7 100644 --- a/src/mcp-connector/src/oauth.js +++ b/src/mcp-connector/src/oauth.js @@ -10,9 +10,10 @@ // client → POST /token -> { access_token: } // client → MCP calls with Authorization: Bearer (the existing path) // -// Stateless: the short-lived `flow` (authorize→callback) and `code` -// (callback→token) blobs are AES-GCM sealed with a worker secret, so nothing is -// persisted. PKCE (S256) is enforced when the client provides a challenge. +// Stateless: the `client_id` (which carries its registered redirect URIs) and +// the short-lived `flow` (authorize→callback) and `code` (callback→token) blobs +// are AES-GCM sealed with a worker secret, so nothing is persisted. PKCE (S256) +// is enforced when the client provides a challenge. // // Discovery: serves RFC 8414 (authorization-server) and RFC 9728 // (protected-resource) metadata, plus RFC 7591 dynamic client registration. @@ -144,6 +145,34 @@ function protectedResourceMetadata(event) { }); } +// ---- redirect URIs --------------------------------------------------------- + +const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '[::1]']); + +// Registrable redirect URI: absolute, no fragment, plain http only on loopback. +function parseRedirectUri(value) { + if (typeof value !== 'string' || value.includes('#')) return null; + try { + const url = new URL(value); + return url.protocol === 'http:' && !LOOPBACK_HOSTS.has(url.hostname) ? null : url; + } catch { + return null; + } +} + +// Exact match, except a loopback redirect may use any port (RFC 8252 §7.3). +function isRegisteredRedirect(registered, redirectUri) { + if (registered.includes(redirectUri)) return true; + const wanted = parseRedirectUri(redirectUri); + if (!wanted || !LOOPBACK_HOSTS.has(wanted.hostname)) return false; + wanted.port = ''; + return registered.some((uri) => { + const url = new URL(uri); + url.port = ''; + return url.href === wanted.href; + }); +} + // ---- endpoints ------------------------------------------------------------- // GET /authorize — kick off the flow by sending the browser to Puter's authme. @@ -162,6 +191,23 @@ async function authorize(event) { }); } + // Answer these directly; an unmatched redirect_uri must never be redirected to. + let client; + try { + client = await unseal(p.get('client_id') || ''); + } catch { + return json(400, { + error: 'invalid_request', + error_description: 'unknown client_id; register the client again', + }); + } + if (!Array.isArray(client?.redirectUris) || !isRegisteredRedirect(client.redirectUris, redirectUri)) { + return json(400, { + error: 'invalid_request', + error_description: 'redirect_uri is not registered for this client_id', + }); + } + const flow = await seal({ redirectUri, clientState, @@ -248,13 +294,24 @@ async function token(event) { return json(200, { access_token: payload.token, token_type: 'Bearer', scope: 'puter' }); } -// POST /register — dynamic client registration (RFC 7591). We don't persist -// clients; security rests on PKCE + the redirect_uri sealed into the flow. +// POST /register — dynamic client registration (RFC 7591). Registration is +// open; the client_id is the sealed list of redirect URIs /authorize accepts. async function register(event) { - const body = await event.request.json().catch(() => ({})); - const redirectUris = Array.isArray(body.redirect_uris) ? body.redirect_uris : []; + let body; + try { + body = await event.request.json(); + } catch { + body = null; + } + const redirectUris = body?.redirect_uris; + if (!Array.isArray(redirectUris) || redirectUris.length === 0 || !redirectUris.every(parseRedirectUri)) { + return json(400, { + error: 'invalid_redirect_uri', + error_description: 'redirect_uris must be absolute URLs without a fragment; http is only allowed for loopback', + }); + } return json(201, { - client_id: `puter-mcp-${crypto.randomUUID()}`, + client_id: await seal({ redirectUris }), client_id_issued_at: Math.floor(Date.now() / 1000), redirect_uris: redirectUris, grant_types: ['authorization_code'], diff --git a/src/mcp-connector/src/oauth.test.js b/src/mcp-connector/src/oauth.test.js new file mode 100644 index 000000000..68a72240f --- /dev/null +++ b/src/mcp-connector/src/oauth.test.js @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2024-present Puter Technologies Inc. + * + * This file is part of Puter. + * + * Puter is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import { createHash } from 'node:crypto'; +import { describe, expect, it } from 'vitest'; +import registerOAuthRoutes from './oauth.js'; + +const routes = {}; +registerOAuthRoutes({ + get: (path, handler) => { + routes[`GET ${path}`] = handler; + }, + post: (path, handler) => { + routes[`POST ${path}`] = handler; + }, +}); + +const call = (method, pathAndQuery, init = {}) => + routes[`${method} ${pathAndQuery.split('?')[0]}`]({ + request: new Request(`https://mcp.example${pathAndQuery}`, { method, ...init }), + }); + +const registerClient = async (redirectUris) => { + const res = await call('POST', '/register', { + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ redirect_uris: redirectUris }), + }); + return { status: res.status, body: await res.json() }; +}; + +const authorize = (params) => + call('GET', `/authorize?${new URLSearchParams({ response_type: 'code', ...params })}`); + +describe('mcp oauth bridge', () => { + it('delivers the token to a registered redirect_uri', async () => { + const redirectUri = 'https://client.example/callback'; + const { body: client } = await registerClient([redirectUri]); + const verifier = 'a-verifier-long-enough-for-pkce-0123456789abcdef'; + + const res = await authorize({ + client_id: client.client_id, + redirect_uri: redirectUri, + state: 'client-state', + code_challenge: createHash('sha256').update(verifier).digest('base64url'), + code_challenge_method: 'S256', + }); + expect(res.status).toBe(302); + const authme = new URL(res.headers.get('Location')); + expect(authme.searchParams.get('action')).toBe('authme'); + + // What authme does once the user approves. + const callbackUrl = new URL(authme.searchParams.get('redirectURL')); + callbackUrl.searchParams.set('token', 'puter-token'); + const callback = await call('GET', `${callbackUrl.pathname}${callbackUrl.search}`); + expect(callback.status).toBe(302); + const delivered = new URL(callback.headers.get('Location')); + expect(`${delivered.origin}${delivered.pathname}`).toBe(redirectUri); + expect(delivered.searchParams.get('state')).toBe('client-state'); + + const tokenRes = await call('POST', '/token', { + headers: { 'content-type': 'application/x-www-form-urlencoded' }, + body: new URLSearchParams({ + grant_type: 'authorization_code', + code: delivered.searchParams.get('code'), + code_verifier: verifier, + }), + }); + expect((await tokenRes.json()).access_token).toBe('puter-token'); + }); + + it('refuses a redirect_uri the client did not register', async () => { + const { body: client } = await registerClient(['https://client.example/callback']); + + const res = await authorize({ + client_id: client.client_id, + redirect_uri: 'https://evil.example/callback', + }); + expect(res.status).toBe(400); + expect(res.headers.get('Location')).toBeNull(); + }); + + it('refuses a missing or unissued client_id', async () => { + const redirectUri = 'https://client.example/callback'; + + expect((await authorize({ redirect_uri: redirectUri })).status).toBe(400); + const res = await authorize({ + client_id: 'puter-mcp-00000000-0000-0000-0000-000000000000', + redirect_uri: redirectUri, + }); + expect(res.status).toBe(400); + expect(res.headers.get('Location')).toBeNull(); + }); + + it('lets a loopback redirect change port but not path', async () => { + const { body: client } = await registerClient(['http://127.0.0.1:33418/callback']); + + const otherPort = await authorize({ + client_id: client.client_id, + redirect_uri: 'http://127.0.0.1:50123/callback', + }); + expect(otherPort.status).toBe(302); + const otherPath = await authorize({ + client_id: client.client_id, + redirect_uri: 'http://127.0.0.1:50123/elsewhere', + }); + expect(otherPath.status).toBe(400); + }); + + it('validates redirect_uris at registration', async () => { + const accepted = await registerClient([ + 'http://localhost:8123/callback', + 'cursor://anysphere.cursor-mcp/oauth/callback', + ]); + expect(accepted.status).toBe(201); + + for (const redirectUris of [ + undefined, + [], + ['http://client.example/callback'], + ['https://client.example/callback#fragment'], + ['not a url'], + ]) { + const { status, body } = await registerClient(redirectUris); + expect(status).toBe(400); + expect(body.error).toBe('invalid_redirect_uri'); + } + }); +}); diff --git a/src/mcp-connector/wrangler.toml b/src/mcp-connector/wrangler.toml index 2835820cc..929bc14ad 100644 --- a/src/mcp-connector/wrangler.toml +++ b/src/mcp-connector/wrangler.toml @@ -21,7 +21,7 @@ no_bundle = true # puter_endpoint = "https://api.puter.com" # puter_gui_origin = "https://puter.com" -# OAuth bridge secret — used to seal the stateless authorization-flow / code -# blobs (AES-GCM). REQUIRED in production; set it as a secret, do NOT commit it: +# OAuth bridge secret — used to seal the stateless client_id / authorization-flow / +# code blobs (AES-GCM). REQUIRED in production; set it as a secret, do NOT commit it: # wrangler secret put OAUTH_SECRET # (Local `wrangler dev` falls back to an insecure default if unset.)