diff --git a/config.template.jsonc b/config.template.jsonc index f083f7423..4d07122d5 100644 --- a/config.template.jsonc +++ b/config.template.jsonc @@ -7,9 +7,8 @@ // Each setting lives at exactly one canonical key — there are no fallback // aliases. Values shown are illustrative, not production secrets. // - // Keys consumed only by closed-source / hosted-prod extensions (clickhouse, - // cacheUpdateHandler, pages, prodMeteringAndBilling, …) are intentionally - // omitted. + // Keys consumed only by closed-source / hosted-prod extensions are + // intentionally omitted. // ── Environment / identity ────────────────────────────────────────── "config_name": "template", diff --git a/extensions/whoami.test.ts b/extensions/whoami.test.ts index 3f42cf77e..3a4eae1f3 100644 --- a/extensions/whoami.test.ts +++ b/extensions/whoami.test.ts @@ -170,6 +170,81 @@ describe('whoami extension — handleWhoami', () => { expect(captured.body).toEqual({ error: 'User not found' }); }); + describe("an app's view of the email", () => { + const appActorFor = async (user: { uuid: string; id: unknown }) => { + const app = await ( + server.stores.app.create as unknown as ( + fields: Record, + opts: { ownerUserId: number }, + ) => Promise<{ uid: string; id: number }> + )( + { + name: `we-${uuidv4()}`, + title: 'Email reader', + index_url: `https://${uuidv4()}.example/`, + }, + { ownerUserId: user.id as number }, + ); + return { + app, + actor: makeActor({ + user: { uuid: user.uuid, id: user.id as number }, + app: { uid: app.uid, id: app.id }, + }), + }; + }; + + const whoamiAs = async (actor: Actor) => { + const { res, captured } = makeRes(); + await runWithContext({ actor }, () => + handleWhoami(makeReq(), res), + ); + return captured; + }; + + it('is withheld until the user grants it, then shown', async () => { + const user = await seedUser(); + const { app, actor } = await appActorFor(user); + + const before = (await whoamiAs(actor)).body as Record< + string, + unknown + >; + expect(before).not.toHaveProperty('email'); + expect(before).not.toHaveProperty('unconfirmed_email'); + + await server.services.permission.grantUserAppPermission( + makeActor({ user: { uuid: user.uuid, id: user.id as number } }), + app.uid, + `user:${user.uuid}:email:read`, + ); + const after = (await whoamiAs(actor)).body as Record< + string, + unknown + >; + expect(after.email).toBe(user.email); + expect(after.unconfirmed_email).toBe(user.email); + }); + + it('answers 404 for a missing account even when the check fails', async () => { + const check = vi + .spyOn(server.services.permission, 'check') + .mockRejectedValue(new Error('scan failed')); + try { + const captured = await whoamiAs( + makeActor({ + user: { uuid: 'ghost-uuid', id: 99_999_998 }, + app: { uid: 'app-ghost', id: 1 }, + }), + ); + expect(captured.statusCode).toBe(404); + expect(captured.body).toEqual({ error: 'User not found' }); + } finally { + check.mockRestore(); + } + }); + }); + it('returns full user details for a user actor', async () => { const user = await seedUser(); const { res, captured } = makeRes(); diff --git a/extensions/whoami.ts b/extensions/whoami.ts index 618f4bd0d..de413a24a 100644 --- a/extensions/whoami.ts +++ b/extensions/whoami.ts @@ -36,6 +36,18 @@ export const handleWhoami = async ( } const isUser = !actor.effectiveApp; + const canReadEmail = async (uuid: unknown) => { + try { + return await services.permission.check( + actor, + `user:${uuid}:email:read`, + ); + } catch { + return false; + } + }; + // Started beside the row read: the actor carries the row's uuid. + const emailCheck = isUser ? null : canReadEmail(actor.user.uuid); const user = await stores.user.getById(actor.user.id); if (!user) { res.status(404).json({ error: 'User not found' }); @@ -62,10 +74,10 @@ export const handleWhoami = async ( // Strip sensitive fields for app actors if (!isUser) { - const canReadEmail = await services.permission - .check(actor, `user:${user.uuid}:email:read`) - .catch(() => false); - if (!canReadEmail) { + const emailReadable = await (user.uuid === actor.user.uuid + ? emailCheck + : canReadEmail(user.uuid)); + if (!emailReadable) { delete details.email; delete details.unconfirmed_email; } @@ -109,7 +121,8 @@ export const handleWhoami = async ( } const subscription = details.subscription as - { offering?: Record } | undefined; + | { offering?: Record } + | undefined; if (subscription?.offering) { delete subscription.offering.group; delete subscription.offering.benefits; diff --git a/src/backend/clients/database/DatabaseClient.ts b/src/backend/clients/database/DatabaseClient.ts index 14b5b1de6..d74178258 100644 --- a/src/backend/clients/database/DatabaseClient.ts +++ b/src/backend/clients/database/DatabaseClient.ts @@ -28,6 +28,12 @@ export interface WriteResult { insertId: number | bigint; affectedRows: number; anyRowsAffected: boolean; + /** + * Warnings the statement raised; nonzero means a value may have been stored + * differently from how it was sent. Absent when the engine can't vouch that + * a clean insert stored exactly what was sent, on a fresh id. + */ + warningStatus?: number; } export interface BatchEntry { diff --git a/src/backend/clients/database/MySQLDatabaseClient.test.ts b/src/backend/clients/database/MySQLDatabaseClient.test.ts index ae2608121..6b20e04e0 100644 --- a/src/backend/clients/database/MySQLDatabaseClient.test.ts +++ b/src/backend/clients/database/MySQLDatabaseClient.test.ts @@ -435,6 +435,105 @@ describe('MySQLDatabaseClient — query interface', () => { ]); }); + it.each([ + { method: 'pread', poolIndex: 0, maxTimeInQueue: 30 }, + { method: 'read', poolIndex: 1, maxTimeInQueue: 10 }, + ] as const)( + '$method: flushes lone reads at once until 8 are in flight, then waits $maxTimeInQueue ms and coalesces', + async ({ method, poolIndex, maxTimeInQueue }) => { + const client = await startClient({ replica: { host: 'replica' } }); + const pool = createdPools[poolIndex]; + const held: Array<() => void> = []; + pool.respond = (sql) => + new Promise((resolve) => { + const sets = sql + .split(';') + .slice(0, -1) + .map((stmt) => [{ stmt }]); + held.push(() => resolve(rowSets(...sets))); + }); + vi.useFakeTimers({ + toFake: [ + 'setTimeout', + 'clearTimeout', + 'setImmediate', + 'clearImmediate', + ], + }); + try { + const reads: Promise[] = []; + for (let i = 0; i < 8; i++) { + reads.push(client[method](`SELECT ${i}`)); + await vi.advanceTimersByTimeAsync(0); + } + expect(pool.calls).toHaveLength(8); + + reads.push(client[method]('SELECT 8')); + await vi.advanceTimersByTimeAsync(maxTimeInQueue - 1); + reads.push(client[method]('SELECT 9')); + expect(pool.calls).toHaveLength(8); + await vi.advanceTimersByTimeAsync(1); + expect(pool.calls.map((c) => c.sql).at(-1)).toBe( + 'SELECT 8;SELECT 9; SELECT 1', + ); + + for (const release of held.splice(0)) release(); + const rows = await Promise.all(reads); + expect(rows.at(-2)).toEqual([{ stmt: 'SELECT 8' }]); + expect(rows.at(-1)).toEqual([{ stmt: 'SELECT 9' }]); + } finally { + vi.useRealTimers(); + } + }, + ); + + it.each([ + { setup: 'single node', replica: undefined, waits: true }, + { setup: 'replica', replica: { host: 'replica' }, waits: false }, + ])( + '$setup: 8 primary reads in flight hold back a lone read: $waits', + async ({ replica, waits }) => { + const client = await startClient(replica ? { replica } : {}); + const held: Array<() => void> = []; + for (const pool of createdPools) { + pool.respond = (sql) => + new Promise((resolve) => { + const sets = sql + .split(';') + .slice(0, -1) + .map((stmt) => [{ stmt }]); + held.push(() => resolve(rowSets(...sets))); + }); + } + const sent = () => createdPools.flatMap((pool) => pool.calls); + vi.useFakeTimers({ + toFake: [ + 'setTimeout', + 'clearTimeout', + 'setImmediate', + 'clearImmediate', + ], + }); + try { + const reads: Promise[] = []; + for (let i = 0; i < 8; i++) { + reads.push(client.pread(`SELECT ${i}`)); + await vi.advanceTimersByTimeAsync(0); + } + reads.push(client.read('SELECT 8')); + await vi.advanceTimersByTimeAsync(0); + expect(sent()).toHaveLength(waits ? 8 : 9); + await vi.advanceTimersByTimeAsync(10); + expect(sent()).toHaveLength(9); + + for (const release of held.splice(0)) release(); + await Promise.all(reads); + } finally { + vi.useRealTimers(); + } + }, + ); + it('sends a write on its own, outside a transaction', async () => { const client = await startClient(); const pool = createdPools[0]; @@ -472,6 +571,23 @@ describe('MySQLDatabaseClient — query interface', () => { }); }); + it('passes the warning count through, so callers can tell a clean write', async () => { + const client = await startClient(); + createdPools[0].respond = () => + writeHeader({ insertId: 9, affectedRows: 1, warningStatus: 1 }); + + await expect( + client.write('INSERT IGNORE INTO `user` (`username`) VALUES (?)', [ + 'ada', + ]), + ).resolves.toEqual({ + insertId: 9, + affectedRows: 1, + anyRowsAffected: true, + warningStatus: 1, + }); + }); + it('reports no rows affected when the header omits the counters', async () => { const client = await startClient(); createdPools[0].respond = () => writeHeader({}); diff --git a/src/backend/clients/database/MySQLDatabaseClient.ts b/src/backend/clients/database/MySQLDatabaseClient.ts index 6a5b9d934..ff95fe3ca 100644 --- a/src/backend/clients/database/MySQLDatabaseClient.ts +++ b/src/backend/clients/database/MySQLDatabaseClient.ts @@ -78,8 +78,13 @@ export class MySQLDatabaseClient extends AbstractDatabaseClient { }); console.log('[mysql] connected to primary'); + // Read batchers on one pool share the cap on immediate flushes. + const primaryReadFlushes = { count: 0 }; this.db = this.createPrimaryBatcher(this.primaryPool); - this.dbPrimaryRead = this.createPrimaryReadBatcher(this.primaryPool); + this.dbPrimaryRead = this.createPrimaryReadBatcher( + this.primaryPool, + primaryReadFlushes, + ); if (dbConf.replica) { this.replicaPool = this.createPool(dbConf.replica); @@ -90,7 +95,12 @@ export class MySQLDatabaseClient extends AbstractDatabaseClient { this.configuration = Configuration.SINGLE; } - this.dbReplica = this.createReplicaBatcher(this.replicaPool); + this.dbReplica = this.createReplicaBatcher( + this.replicaPool, + this.configuration === Configuration.SINGLE + ? primaryReadFlushes + : { count: 0 }, + ); await this.applyMigrationPaths( () => this.primaryPool.promise().getConnection(), @@ -164,12 +174,14 @@ export class MySQLDatabaseClient extends AbstractDatabaseClient { const header = result[0] as { insertId?: number; affectedRows?: number; + warningStatus?: number; }; const affectedRows = header.affectedRows ?? 0; return { insertId: header.insertId ?? 0, affectedRows, anyRowsAffected: affectedRows > 0, + warningStatus: header.warningStatus, }; } @@ -274,9 +286,14 @@ export class MySQLDatabaseClient extends AbstractDatabaseClient { } /** Reads that must see the primary, still coalesced. */ - private createPrimaryReadBatcher(pool: Pool): SQLBatcher { + private createPrimaryReadBatcher( + pool: Pool, + inFlight: { count: number }, + ): SQLBatcher { return new SQLBatcher(pool, { maxTimeInQueue: 30, + maxInFlightImmediate: 8, + inFlight, maxBatchSize: 5, poolLabel: 'primary', readOnly: true, @@ -284,9 +301,14 @@ export class MySQLDatabaseClient extends AbstractDatabaseClient { }); } - private createReplicaBatcher(pool: Pool): SQLBatcher { + private createReplicaBatcher( + pool: Pool, + inFlight: { count: number }, + ): SQLBatcher { return new SQLBatcher(pool, { maxTimeInQueue: 10, + maxInFlightImmediate: 8, + inFlight, maxBatchSize: 5, poolLabel: 'replica', readOnly: true, diff --git a/src/backend/clients/database/SQLBatcher.js b/src/backend/clients/database/SQLBatcher.js index b33e9c4d4..75c7cd2ba 100644 --- a/src/backend/clients/database/SQLBatcher.js +++ b/src/backend/clients/database/SQLBatcher.js @@ -76,6 +76,7 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); export class SQLBatcher { dbPool; maxTimeInQueue; + maxInFlightImmediate; maxBatchSize; maxQueueSize; failureThreshold; @@ -84,7 +85,9 @@ export class SQLBatcher { readOnly; acquireTimeoutMs; queue = []; - timeouts = []; + #flushTimer = null; + #flushTimerIsImmediate = false; + #inFlight; #consecutiveFailures = 0; #lastFailureAt = 0; #metricAttrs; @@ -93,6 +96,11 @@ export class SQLBatcher { * @param {object} dbPool Mysql2 pool * @param {object} [opts] * @param {number} [opts.maxTimeInQueue] Ms an item may wait before flush + * @param {number} [opts.maxInFlightImmediate] While fewer flushes than this + * are in flight, a lone item flushes on the next turn instead of waiting + * maxTimeInQueue; past it, items wait and coalesce to spare connections + * @param {{ count: number }} [opts.inFlight] In-flight flush counter the + * cap reads; batchers sharing a pool can share one * @param {number} [opts.maxBatchSize] Items coalesced per flush * @param {number} [opts.maxQueueSize] Drop-oldest high-water mark * @param {number} [opts.failureThreshold] Consecutive failures to open the @@ -111,6 +119,8 @@ export class SQLBatcher { dbPool, { maxTimeInQueue = 20, + maxInFlightImmediate = 0, + inFlight = { count: 0 }, maxBatchSize = 50, maxQueueSize = DEFAULT_MAX_QUEUE_SIZE, failureThreshold = DEFAULT_FAILURE_THRESHOLD, @@ -122,6 +132,8 @@ export class SQLBatcher { ) { this.dbPool = dbPool; this.maxTimeInQueue = maxTimeInQueue; + this.maxInFlightImmediate = maxInFlightImmediate; + this.#inFlight = inFlight; this.maxBatchSize = maxBatchSize; this.maxQueueSize = maxQueueSize; this.failureThreshold = failureThreshold; @@ -185,17 +197,34 @@ export class SQLBatcher { if (this.queue.length >= this.maxBatchSize) { this.flush(this.queue.splice(0, this.maxBatchSize)); - } else if (this.queue.length === 1) { - this.timeouts.push( - setTimeout(() => { - this.flush(this.queue.splice(0, this.queue.length)); - }, this.maxTimeInQueue), - ); + } else if (this.#flushTimer === null) { + this.#armFlushTimer(); } return promise; } + // A lone flush holds a pooled connection for a full round trip, so only + // the first maxInFlightImmediate flushes skip the wait. + #armFlushTimer() { + const run = () => { + this.#flushTimer = null; + this.flush(this.queue.splice(0, this.queue.length)); + }; + this.#flushTimerIsImmediate = + this.#inFlight.count < this.maxInFlightImmediate; + this.#flushTimer = this.#flushTimerIsImmediate + ? setImmediate(run) + : setTimeout(run, this.maxTimeInQueue); + } + + #clearFlushTimer() { + if (this.#flushTimer === null) return; + if (this.#flushTimerIsImmediate) clearImmediate(this.#flushTimer); + else clearTimeout(this.#flushTimer); + this.#flushTimer = null; + } + // Bounded wait for a pooled connection. Without a bound, a stalled // database turns every flush into an indefinite hang — nothing fails, // so neither the breaker nor callers' own timeouts ever engage. @@ -252,12 +281,18 @@ export class SQLBatcher { } async flush(batch) { - const timeout = this.timeouts.shift(); - if (timeout && !timeout._destroyed) { - clearTimeout(timeout); - } + this.#clearFlushTimer(); if (batch.length === 0) return; + this.#inFlight.count++; + try { + await this.#flushBatch(batch); + } finally { + this.#inFlight.count--; + } + } + + async #flushBatch(batch) { let connection; try { connection = await this.#acquireConnection(); diff --git a/src/backend/clients/database/SQLBatcher.test.ts b/src/backend/clients/database/SQLBatcher.test.ts index ad7ca94f4..874256201 100644 --- a/src/backend/clients/database/SQLBatcher.test.ts +++ b/src/backend/clients/database/SQLBatcher.test.ts @@ -69,6 +69,356 @@ const happyBatch = (sql: string) => { ]; }; +// Each query on this connection waits until the test releases it, so its +// flush stays in flight. +const makeHeldConnection = () => { + const held: Array<() => void> = []; + const conn = makeConnection( + (sql) => + new Promise((resolve) => { + held.push(() => resolve(happyBatch(sql))); + }), + ); + const releaseAll = () => { + for (const release of held.splice(0)) release(); + }; + const sent = () => conn.query.mock.calls.map(([sql]) => sql); + return { conn, releaseAll, sent }; +}; + +const fakeFlushTimers = () => + vi.useFakeTimers({ + toFake: [ + 'setTimeout', + 'clearTimeout', + 'setImmediate', + 'clearImmediate', + ], + }); + +describe('SQLBatcher flush timing', () => { + it('flushes a lone query on the next turn while under the in-flight cap', async () => { + const conn = makeConnection(happyBatch); + const { pool, getConnection } = makePool(conn); + const batcher = new SQLBatcher(pool, { + readOnly: true, + maxTimeInQueue: 60_000, + maxInFlightImmediate: 2, + }); + + const pending = batcher.query('SELECT a', []); + expect(getConnection).not.toHaveBeenCalled(); + await new Promise((resolve) => setImmediate(resolve)); + expect(getConnection).toHaveBeenCalledTimes(1); + expect((await pending)[0]).toEqual([{ n: 0 }]); + }); + + it('holds a lone query for maxTimeInQueue without an in-flight cap', async () => { + fakeFlushTimers(); + try { + const conn = makeConnection(happyBatch); + const { pool, getConnection } = makePool(conn); + const batcher = new SQLBatcher(pool, { + readOnly: true, + maxTimeInQueue: 30, + }); + + const pending = batcher.query('SELECT a', []); + await vi.advanceTimersByTimeAsync(29); + expect(getConnection).not.toHaveBeenCalled(); + await vi.advanceTimersByTimeAsync(1); + expect(getConnection).toHaveBeenCalledTimes(1); + expect((await pending)[0]).toEqual([{ n: 0 }]); + } finally { + vi.useRealTimers(); + } + }); + + it('batches queries from the same turn and sends later ones separately', async () => { + const conn = makeConnection(happyBatch); + const { pool, getConnection } = makePool(conn); + const batcher = new SQLBatcher(pool, { + readOnly: true, + maxInFlightImmediate: 8, + }); + + const sameTurn = [ + batcher.query('SELECT a', []), + batcher.query('SELECT b', []), + batcher.query('SELECT c', []), + ]; + await new Promise((resolve) => setImmediate(resolve)); + const nextTurn = batcher.query('SELECT d', []); + const results = await Promise.all([...sameTurn, nextTurn]); + + expect(results.map((r) => r[0])).toEqual([ + [{ n: 0 }], + [{ n: 1 }], + [{ n: 2 }], + [{ n: 0 }], + ]); + expect(getConnection).toHaveBeenCalledTimes(2); + expect(conn.query.mock.calls.map(([sql]) => sql)).toEqual([ + 'SELECT a;SELECT b;SELECT c; SELECT 1', + 'SELECT d; SELECT 1', + ]); + }); + + it('gives a query enqueued during an in-flight flush its own flush while under the cap', async () => { + const { conn, releaseAll, sent } = makeHeldConnection(); + const { pool } = makePool(conn); + const batcher = new SQLBatcher(pool, { + readOnly: true, + maxInFlightImmediate: 2, + }); + const turn = () => new Promise((resolve) => setImmediate(resolve)); + + const a = batcher.query('SELECT a', []); + await turn(); + const b = batcher.query('SELECT b', []); + await turn(); + expect(sent()).toEqual(['SELECT a; SELECT 1', 'SELECT b; SELECT 1']); + + releaseAll(); + expect((await a)[0]).toEqual([{ n: 0 }]); + expect((await b)[0]).toEqual([{ n: 0 }]); + }); + + it('holds a lone query for maxTimeInQueue at the in-flight cap and coalesces what follows', async () => { + fakeFlushTimers(); + try { + const { conn, releaseAll, sent } = makeHeldConnection(); + const { pool } = makePool(conn); + const batcher = new SQLBatcher(pool, { + readOnly: true, + maxTimeInQueue: 30, + maxInFlightImmediate: 1, + }); + + const a = batcher.query('SELECT a', []); + await vi.advanceTimersByTimeAsync(0); + expect(sent()).toEqual(['SELECT a; SELECT 1']); + + const b = batcher.query('SELECT b', []); + await vi.advanceTimersByTimeAsync(29); + const c = batcher.query('SELECT c', []); + expect(sent()).toHaveLength(1); + await vi.advanceTimersByTimeAsync(1); + expect(sent()).toEqual([ + 'SELECT a; SELECT 1', + 'SELECT b;SELECT c; SELECT 1', + ]); + + releaseAll(); + expect((await a)[0]).toEqual([{ n: 0 }]); + expect((await b)[0]).toEqual([{ n: 0 }]); + expect((await c)[0]).toEqual([{ n: 1 }]); + + // Settled flushes free the cap again. + const d = batcher.query('SELECT d', []); + await vi.advanceTimersByTimeAsync(0); + expect(sent()).toHaveLength(3); + releaseAll(); + expect((await d)[0]).toEqual([{ n: 0 }]); + } finally { + vi.useRealTimers(); + } + }); + + it('shares the in-flight cap between batchers given one counter', async () => { + fakeFlushTimers(); + try { + const { conn, releaseAll, sent } = makeHeldConnection(); + const { pool } = makePool(conn); + const inFlight = { count: 0 }; + const opts = { + readOnly: true, + maxTimeInQueue: 30, + maxInFlightImmediate: 1, + inFlight, + }; + const first = new SQLBatcher(pool, opts); + const second = new SQLBatcher(pool, opts); + const own = new SQLBatcher(pool, { ...opts, inFlight: undefined }); + + const a = first.query('SELECT a', []); + await vi.advanceTimersByTimeAsync(0); + const b = second.query('SELECT b', []); + const c = own.query('SELECT c', []); + await vi.advanceTimersByTimeAsync(0); + expect(sent()).toEqual([ + 'SELECT a; SELECT 1', + 'SELECT c; SELECT 1', + ]); + await vi.advanceTimersByTimeAsync(30); + expect(sent()).toHaveLength(3); + expect(sent()[2]).toBe('SELECT b; SELECT 1'); + + releaseAll(); + await Promise.all([a, b, c]); + expect(inFlight.count).toBe(0); + } finally { + vi.useRealTimers(); + } + }); + + it('counts a flush as in flight until its per-item fallback settles', async () => { + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + fakeFlushTimers(); + try { + const held: Array<() => void> = []; + const conn = makeConnection((sql) => { + if (isBatchQuery(sql)) throw makeError('ECONNRESET'); + return new Promise((resolve) => { + held.push(() => resolve([[{ ok: sql }], undefined])); + }); + }); + const { pool } = makePool(conn); + const sent = () => conn.query.mock.calls.map(([sql]) => sql); + const batcher = new SQLBatcher(pool, { + readOnly: true, + maxTimeInQueue: 30, + maxInFlightImmediate: 1, + acquireTimeoutMs: 0, + }); + + const a = batcher.query('SELECT a', []); + await vi.advanceTimersByTimeAsync(0); + // The batch failed; the per-item retry of `a` is still running. + expect(sent()).toEqual(['SELECT a; SELECT 1', 'SELECT a']); + + const b = batcher.query('SELECT b', []); + await vi.advanceTimersByTimeAsync(29); + expect(sent()).toHaveLength(2); + await vi.advanceTimersByTimeAsync(1); + expect(sent()).toEqual([ + 'SELECT a; SELECT 1', + 'SELECT a', + 'SELECT b; SELECT 1', + 'SELECT b', + ]); + + for (const release of held.splice(0)) release(); + expect((await a)[0]).toEqual([{ ok: 'SELECT a' }]); + expect((await b)[0]).toEqual([{ ok: 'SELECT b' }]); + } finally { + vi.useRealTimers(); + warnSpy.mockRestore(); + } + }); + + it('flushes a full batch at once past the cap and cancels the armed wait', async () => { + fakeFlushTimers(); + try { + const { conn, releaseAll, sent } = makeHeldConnection(); + const { pool, getConnection } = makePool(conn); + const batcher = new SQLBatcher(pool, { + readOnly: true, + maxBatchSize: 2, + maxTimeInQueue: 30, + maxInFlightImmediate: 1, + acquireTimeoutMs: 0, + }); + + const a = batcher.query('SELECT a', []); + await vi.advanceTimersByTimeAsync(0); + const b = batcher.query('SELECT b', []); + expect(vi.getTimerCount()).toBe(1); + const c = batcher.query('SELECT c', []); + expect(getConnection).toHaveBeenCalledTimes(2); + expect(vi.getTimerCount()).toBe(0); + + // A later lone query waits its own full maxTimeInQueue. + await vi.advanceTimersByTimeAsync(10); + const d = batcher.query('SELECT d', []); + await vi.advanceTimersByTimeAsync(29); + expect(getConnection).toHaveBeenCalledTimes(2); + await vi.advanceTimersByTimeAsync(1); + expect(getConnection).toHaveBeenCalledTimes(3); + expect(sent()).toEqual([ + 'SELECT a; SELECT 1', + 'SELECT b;SELECT c; SELECT 1', + 'SELECT d; SELECT 1', + ]); + + releaseAll(); + await Promise.all([a, b, c, d]); + } finally { + vi.useRealTimers(); + } + }); + + it('cancels the pending next-turn flush when a batch fills up', async () => { + fakeFlushTimers(); + try { + const conn = makeConnection(happyBatch); + const { pool, getConnection } = makePool(conn); + const batcher = new SQLBatcher(pool, { + maxBatchSize: 2, + readOnly: true, + maxInFlightImmediate: 8, + acquireTimeoutMs: 0, + }); + + const a = batcher.query('SELECT a', []); + expect(vi.getTimerCount()).toBe(1); + const b = batcher.query('SELECT b', []); + expect(vi.getTimerCount()).toBe(0); + + const c = batcher.query('SELECT c', []); + expect(vi.getTimerCount()).toBe(1); + await vi.runAllTimersAsync(); + await Promise.all([a, b, c]); + + expect(vi.getTimerCount()).toBe(0); + expect(getConnection).toHaveBeenCalledTimes(2); + } finally { + vi.useRealTimers(); + } + }); + + it('leaves no timer armed after a failed flush, a drop or a breaker rejection', async () => { + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + fakeFlushTimers(); + try { + const getConnection = vi.fn(async () => { + throw makeError('ECONNREFUSED'); + }); + const batcher = new SQLBatcher( + { promise: () => ({ getConnection }) }, + { + readOnly: true, + maxQueueSize: 1, + failureThreshold: 1, + cooldownMs: 60_000, + maxInFlightImmediate: 8, + }, + ); + + const dropped = expect( + batcher.query('SELECT a', []), + ).rejects.toMatchObject({ reason: 'queueOverflow' }); + const failed = expect( + batcher.query('SELECT b', []), + ).rejects.toMatchObject({ reason: 'connAcquire' }); + expect(vi.getTimerCount()).toBe(1); + await vi.runAllTimersAsync(); + await dropped; + await failed; + expect(vi.getTimerCount()).toBe(0); + + await expect(batcher.query('SELECT c', [])).rejects.toMatchObject({ + reason: 'breakerOpen', + }); + expect(vi.getTimerCount()).toBe(0); + expect(batcher.queue).toHaveLength(0); + } finally { + vi.useRealTimers(); + warnSpy.mockRestore(); + } + }); +}); + describe('SQLBatcher', () => { it('resolves each batched item with its own result', async () => { const conn = makeConnection(happyBatch); diff --git a/src/backend/clients/database/SqliteDatabaseClient.test.ts b/src/backend/clients/database/SqliteDatabaseClient.test.ts index 175889866..954413eea 100644 --- a/src/backend/clients/database/SqliteDatabaseClient.test.ts +++ b/src/backend/clients/database/SqliteDatabaseClient.test.ts @@ -930,6 +930,7 @@ describe('SqliteDatabaseClient — query interface', () => { affectedRows: 1, anyRowsAffected: true, }); + expect(inserted).not.toHaveProperty('warningStatus'); const missed = await client.write( 'UPDATE `widget` SET `name` = ? WHERE `id` = ?', diff --git a/src/backend/clients/database/SqliteDatabaseClient.ts b/src/backend/clients/database/SqliteDatabaseClient.ts index 21ec4c26a..8ee29cba0 100644 --- a/src/backend/clients/database/SqliteDatabaseClient.ts +++ b/src/backend/clients/database/SqliteDatabaseClient.ts @@ -207,6 +207,9 @@ export class SqliteDatabaseClient extends AbstractDatabaseClient { insertId: info.lastInsertRowid, affectedRows: info.changes, anyRowsAffected: info.changes > 0, + // No warningStatus: SQLite raises none, but a new row can reuse a + // deleted row's rowid that other tables still name, so a clean + // insert doesn't prove the row is all the caller sent. }; } diff --git a/src/backend/clients/event/EventClient.test.ts b/src/backend/clients/event/EventClient.test.ts index 81f6d619a..f6299810f 100644 --- a/src/backend/clients/event/EventClient.test.ts +++ b/src/backend/clients/event/EventClient.test.ts @@ -1,3 +1,9 @@ +import { trace } from '@opentelemetry/api'; +import { + BasicTracerProvider, + InMemorySpanExporter, + SimpleSpanProcessor, +} from '@opentelemetry/sdk-trace-base'; import { afterAll, afterEach, @@ -207,6 +213,122 @@ describe('EventClient', () => { await target.emitAndWait(`${key}.child`, {}, {}); expect(resolved).toBe(true); }); + + describe('tracing', () => { + const exporter = new InMemorySpanExporter(); + const provider = new BasicTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(exporter)], + }); + const spansFor = (eventKey: string) => + exporter + .getFinishedSpans() + .filter( + (s) => + s.name === 'event.emitAndWait' && + s.attributes['event.key'] === eventKey, + ); + + beforeAll(() => { + trace.setGlobalTracerProvider(provider); + }); + + afterAll(async () => { + await provider.shutdown(); + trace.disable(); + }); + + it('resolves without a span when nothing listens', async () => { + const meta: { listener_failed?: boolean } = {}; + await expect( + target.emitAndWait(`${key}.child`, {}, meta), + ).resolves.toBeUndefined(); + expect(meta).toEqual({}); + expect(spansFor(`${key}.child`)).toHaveLength(0); + }); + + it('spans the wait whenever a listener matches, wildcards included', async () => { + target.on(`${key}.*`, vi.fn()); + await expect( + target.emitAndWait(`${key}.child`, {}, {}), + ).resolves.toBeUndefined(); + expect(spansFor(`${key}.child`)).toHaveLength(1); + + extensionStore.events[`${key}.ext`] = [vi.fn()]; + try { + await target.emitAndWait(`${key}.ext`, {}, {}); + } finally { + delete extensionStore.events[`${key}.ext`]; + } + expect(spansFor(`${key}.ext`)).toHaveLength(1); + }); + }); + }); + + describe('emitAndWaitConcurrently', () => { + it('runs every listener at once, wildcards included, and awaits them all', async () => { + let inFlight = 0; + let peak = 0; + const contribute = + (share: number) => async (_k: string, data: unknown) => { + inFlight++; + peak = Math.max(peak, inFlight); + await new Promise((r) => setTimeout(r, 10)); + inFlight--; + (data as { total: number }).total += share; + }; + target.on(`${key}.*`, contribute(1)); + target.on(`${key}.child`, contribute(10)); + target.on(`${key}.child`, contribute(100)); + + const data = { total: 0 }; + await target.emitAndWaitConcurrently(`${key}.child`, data, {}); + + expect(peak).toBe(3); + expect(data.total).toBe(111); + }); + + it('flags a failed exact-key listener and still awaits the rest', async () => { + const errSpy = vi + .spyOn(console, 'error') + .mockImplementation(() => {}); + let finished = false; + target.on(key, () => { + throw new Error('boom'); + }); + target.on(key, async () => { + await new Promise((r) => setTimeout(r, 5)); + finished = true; + }); + const meta: Record = {}; + await target.emitAndWaitConcurrently(key, {}, meta); + expect(finished).toBe(true); + expect(meta.listener_failed).toBe(true); + expect(errSpy).toHaveBeenCalledWith( + 'Error in event listener for event', + key, + expect.any(Error), + ); + errSpy.mockRestore(); + }); + + it('leaves the flag alone when only a wildcard listener fails', async () => { + const errSpy = vi + .spyOn(console, 'error') + .mockImplementation(() => {}); + target.on(`${key}.*`, () => { + throw new Error('observer'); + }); + const meta: Record = {}; + await target.emitAndWaitConcurrently(`${key}.child`, {}, meta); + expect(meta.listener_failed).toBeUndefined(); + errSpy.mockRestore(); + }); + + it('resolves at once with nobody listening', async () => { + await expect( + target.emitAndWaitConcurrently(`${key}.nobody`, {}, {}), + ).resolves.toBeUndefined(); + }); }); describe('off', () => { diff --git a/src/backend/clients/event/EventClient.ts b/src/backend/clients/event/EventClient.ts index 1f71d5658..891d04012 100644 --- a/src/backend/clients/event/EventClient.ts +++ b/src/backend/clients/event/EventClient.ts @@ -94,6 +94,7 @@ export class EventClient extends PuterClient { data: EventMap[T], meta: EventMetadata, ) { + if (!this.hasListeners(key)) return; // Spanned because callers block on listeners — this is where time // spent in extension hooks (e.g. `ip.validate`) gets attributed. return withSpan('event.emitAndWait', { 'event.key': key }, async () => { @@ -118,6 +119,43 @@ export class EventClient extends PuterClient { }); } + /** + * Like `emitAndWait`, but every matched listener runs at once, so none sees + * another's mutation of `data` before all have finished. Only for events + * whose listeners each add an independent share, written after their own + * awaits (`const v = await x; data.n += v`). + */ + async emitAndWaitConcurrently( + key: T, + data: EventMap[T], + meta: EventMetadata, + ) { + const matched = this.#matchKeys(key).flatMap((matchKey) => + this.#listenersFor(matchKey).map((listener) => ({ + listener, + // A wildcard listener observes the event; it does not implement it. + isExact: matchKey === key, + })), + ); + if (matched.length === 0) return; + return withSpan('event.emitAndWait', { 'event.key': key }, async () => { + await Promise.all( + matched.map(async ({ listener, isExact }) => { + try { + await listener(key, data, meta); + } catch (e) { + if (isExact) meta.listener_failed = true; + console.error( + 'Error in event listener for event', + key, + e, + ); + } + }), + ); + }); + } + /** * Whether anything would run if `key` were emitted, wildcards included. * diff --git a/src/backend/clients/event/types.ts b/src/backend/clients/event/types.ts index 7304b54c1..e5bc7d8f4 100644 --- a/src/backend/clients/event/types.ts +++ b/src/backend/clients/event/types.ts @@ -459,7 +459,11 @@ export type EventMap = { [key: string]: unknown; }; }; - 'storage.quota.bonus': { userId: number; extra: number }; + /** + * Bytes to add to a user's storage allowance. Listeners run concurrently: + * add to `extra` after your own awaits, never `extra += await x`. + */ + 'storage.quota.bonus': { userId: number; userUuid?: string; extra: number }; // ---- Key-value ---- /** @@ -919,10 +923,11 @@ export type EventKey = keyof EventMap & string; // Generates a wildcard for every non-final dot-separated prefix of K. export type WildcardPrefixes = K extends `${infer Head}.${infer Tail}` - ? | `${Head}.*` - | (Tail extends `${string}.${string}` - ? `${Head}.${WildcardPrefixes}` - : never) + ? + | `${Head}.*` + | (Tail extends `${string}.${string}` + ? `${Head}.${WildcardPrefixes}` + : never) : never; export type ListenKey = EventKey | WildcardPrefixes; diff --git a/src/backend/controllers/auth/AuthController.http.test.ts b/src/backend/controllers/auth/AuthController.http.test.ts index 478de802d..1374fc679 100644 --- a/src/backend/controllers/auth/AuthController.http.test.ts +++ b/src/backend/controllers/auth/AuthController.http.test.ts @@ -18,7 +18,7 @@ */ import { TOTP } from 'otpauth'; -import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; import { makeActor } from '../../core/actor.js'; import { runWithContext } from '../../core/context.js'; import { createSecret as otpCreateSecret } from '../../services/auth/OTPUtil.js'; @@ -680,3 +680,89 @@ describe('temp-account reauth over HTTP', () => { expect(who.status).toBe(200); }); }); + +describe('session token helpers over HTTP', () => { + let env: PuterTestEnv; + + beforeAll(async () => { + env = await setupPuterTestEnv(); + }, 120_000); + + afterAll(async () => { + await env?.shutdown(); + }, 120_000); + + const signupTemp = async () => { + const res = await fetch(new URL('/signup', env.origin), { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ is_temp: true }), + }); + expect(res.status).toBe(200); + return (await res.json()) as { token: string; user: { uuid: string } }; + }; + + const get = (path: string, token: string) => + fetch(new URL(path, env.origin), { + headers: { authorization: `Bearer ${token}` }, + }); + + const claims = (token: string) => + env.server.services.token.verify('auth', token) as { + type: string; + user_uid: string; + session_uid: string; + }; + + it('mint tokens for the session the request authenticated with', async () => { + const { token, user } = await signupTemp(); + const session = claims(token); + const row = await env.server.stores.user.getByUuid(user.uuid); + const getById = vi.spyOn(env.server.stores.user, 'getById'); + try { + const gui = await get('/get-gui-token', token); + expect(gui.status).toBe(200); + const { token: guiToken } = (await gui.json()) as { + token: string; + }; + expect(claims(guiToken)).toMatchObject({ + type: 'gui', + user_uid: user.uuid, + session_uid: session.session_uid, + }); + + const sync = await get('/session/sync-cookie', token); + expect(sync.status).toBe(204); + const cookie = sync.headers.get('set-cookie') ?? ''; + const cookieToken = /puter_auth_token=([^;]+)/.exec(cookie)?.[1]; + expect(claims(cookieToken!)).toMatchObject({ + type: 'session', + user_uid: user.uuid, + session_uid: session.session_uid, + }); + + // The user is the row token auth already read. + expect(getById.mock.calls.filter(([id]) => id === row!.id)).toEqual( + [], + ); + } finally { + getById.mockRestore(); + } + }); + + it("refuse a deleted account's session before either handler runs", async () => { + const { token, user } = await signupTemp(); + const row = await env.server.stores.user.getByUuid(user.uuid); + await env.server.clients.db.write('DELETE FROM `user` WHERE `id` = ?', [ + row!.id, + ]); + await env.server.stores.user.markDeleted(row!); + + expect((await get('/get-gui-token', token)).status).toBe(401); + const sync = await get('/session/sync-cookie', token); + expect(sync.status).toBe(401); + expect(sync.headers.get('set-cookie') ?? '').not.toContain( + 'puter_auth_token=', + ); + }); +}); diff --git a/src/backend/controllers/auth/AuthController.test.ts b/src/backend/controllers/auth/AuthController.test.ts index 2bff18d93..8c81c6b59 100644 --- a/src/backend/controllers/auth/AuthController.test.ts +++ b/src/backend/controllers/auth/AuthController.test.ts @@ -3597,6 +3597,46 @@ describe('AuthController.handleGetUserAppToken + handleCheckApp', () => { ); }); + // The handler's own lookup is the row the grant and the token use. + it('reads the app row once per get-user-app-token and check-app', async () => { + const getByUid = vi.spyOn(server.stores.app, 'getByUid'); + const lookupsOf = () => + getByUid.mock.calls.filter(([uid]) => uid === app.uid).length; + try { + const tokenRes = makeRes(); + await inCtx(actor, () => + controller.handleGetUserAppToken( + makeReq({ app_uid: app.uid }, { actor }), + tokenRes, + ), + ); + expect(lookupsOf()).toBe(1); + const minted = tokenRes.body as { token: string }; + + getByUid.mockClear(); + const checkRes = makeRes(); + await inCtx(actor, () => + controller.handleCheckApp( + makeReq({ app_uid: app.uid }, { actor }), + checkRes, + ), + ); + expect(lookupsOf()).toBe(1); + const checked = checkRes.body as { token: string }; + + // Both tokens name the same app session. + const sessionOf = (token: string) => + ( + server.services.token.verify('auth', token) as { + session_uid: string; + } + ).session_uid; + expect(sessionOf(checked.token)).toBe(sessionOf(minted.token)); + } finally { + getByUid.mockRestore(); + } + }); + it('after get-user-app-token, check-app reports authenticated:true and returns a token', async () => { // Ensure the flag is granted (re-run is idempotent). await inCtx(actor, () => @@ -9403,56 +9443,6 @@ describe('AuthController.handleGetDevProfile additional branches', () => { }); }); -describe('AuthController.handleGetGuiToken / handleSessionSyncCookie additional branches', () => { - it('get-gui-token: 404 when actor has a session but the user row is gone', async () => { - const { user, actor } = await makeUserAndActor(); - const sessionRes = await server.services.auth.createSessionToken( - user, - {}, - ); - const sessionUid = (sessionRes.session as { uuid: string }).uuid; - const sessionedActor = { - ...actor, - session: { uid: sessionUid }, - } as Actor; - // Pull the user row out from under the session. - await server.clients.db.write('DELETE FROM `user` WHERE `id` = ?', [ - user.id, - ]); - await server.stores.user.invalidateById(user.id); - await expect( - controller.handleGetGuiToken( - makeReq({}, { actor: sessionedActor }), - makeRes(), - ), - ).rejects.toMatchObject({ statusCode: 404 }); - }); - - it('session/sync-cookie: 404 when actor has a session but the user row is gone', async () => { - const { user, actor } = await makeUserAndActor(); - const sessionRes = await server.services.auth.createSessionToken( - user, - {}, - ); - const sessionUid = (sessionRes.session as { uuid: string }).uuid; - const sessionedActor = { - ...actor, - session: { uid: sessionUid }, - } as Actor; - await server.clients.db.write('DELETE FROM `user` WHERE `id` = ?', [ - user.id, - ]); - await server.stores.user.invalidateById(user.id); - - const res = makeRes(); - await controller.handleSessionSyncCookie( - makeReq({}, { actor: sessionedActor, tokenSource: 'header' }), - res, - ); - expect(res.statusCode).toBe(404); - }); -}); - describe('AuthController.handleSendConfirmEmail additional branches', () => { it('throws 404 when the actor user row no longer exists', async () => { const { user, actor } = await makeUserAndActor(); diff --git a/src/backend/controllers/auth/AuthController.ts b/src/backend/controllers/auth/AuthController.ts index ea0bfaee3..7d623bfff 100644 --- a/src/backend/controllers/auth/AuthController.ts +++ b/src/backend/controllers/auth/AuthController.ts @@ -3750,6 +3750,7 @@ export class AuthController extends PuterController { APP_AUTHENTICATED_FLAG, {}, {}, + app, ); const tokenPromise: Promise<{ token: string; expiresAt?: number }> = @@ -3759,6 +3760,7 @@ export class AuthController extends PuterController { token: await this.services.auth.getUserAppToken( req.actor!, app_uid, + app, ), }))(); @@ -3861,6 +3863,7 @@ export class AuthController extends PuterController { result.token = await this.services.auth.getUserAppToken( req.actor!, app!.uid, + app!, ); } res.json(result); @@ -4240,13 +4243,10 @@ export class AuthController extends PuterController { throw new HttpError(400, 'No session bound to this actor', { legacyCode: 'session_required' as never, }); - const user = await this.stores.user.getById(req.actor.user.id!); - if (!user) - throw new HttpError(404, 'User not found', { - legacyCode: 'not_found', - }); + // Token auth built this actor from the user's row in this request, and + // a deleted user fails it. const guiToken = this.services.auth.createGuiToken( - user, + req.actor.user as UserRow, req.actor.session.uid, ); res.json({ token: guiToken }); @@ -4276,13 +4276,9 @@ export class AuthController extends PuterController { res.status(400).end(); return; } - const user = await this.stores.user.getById(req.actor.user.id!); - if (!user) { - res.status(404).end(); - return; - } + // As in `/get-gui-token`: the actor is the user's row as auth read it. const sessionToken = this.services.auth.createSessionTokenForSession( - user, + req.actor.user as UserRow, req.actor.session.uid, ); res.cookie(this.config.cookie_name ?? 'puter_token', sessionToken, { diff --git a/src/backend/controllers/drivers/DriverController.test.ts b/src/backend/controllers/drivers/DriverController.test.ts index 48498a080..7cc6382f9 100644 --- a/src/backend/controllers/drivers/DriverController.test.ts +++ b/src/backend/controllers/drivers/DriverController.test.ts @@ -823,9 +823,10 @@ describe('DriverController.#handleCall tracing', () => { 'driver.method': 'set', 'driver.name': 'puter-kvstore', }); - // The call path exercises the permission gate and the KV store's - // dynamo writes — both should have produced spans of their own. - expect(names).toContain('permission.scan'); + // The KV store's dynamo writes produce spans of their own. The + // permission gate doesn't: every user holds `service` by default, which + // `check` answers without a scan. + expect(names).not.toContain('permission.scan'); expect(names.some((n) => n.startsWith('ddb.'))).toBe(true); }); }); diff --git a/src/backend/controllers/fs/FSController.ts b/src/backend/controllers/fs/FSController.ts index 873a24a7d..5793e427a 100644 --- a/src/backend/controllers/fs/FSController.ts +++ b/src/backend/controllers/fs/FSController.ts @@ -20,12 +20,15 @@ import Busboy from 'busboy'; import type { Request, Response } from 'express'; import { posix as pathPosix } from 'node:path'; -import { pipeline } from 'node:stream/promises'; import type { Actor } from '../../core/actor.js'; import { Context } from '../../core/context.js'; import { HttpError } from '../../core/http/HttpError.js'; import { Controller, Get, Post } from '../../core/http/decorators.js'; -import { FS_ROUTE_REFUSAL } from '../../services/acl/ACLService.js'; +import { pipeToResponse } from '../../core/http/pipeToResponse.js'; +import { + FS_ROUTE_REFUSAL, + type ResourceDescriptor, +} from '../../services/acl/ACLService.js'; import { clientParentUid } from '../../services/fs/rootListing.js'; import { expandTildePath, @@ -42,7 +45,11 @@ import type { UploadedBatchWriteItem, UploadProgressTrackerLike, } from '../../services/fs/types.js'; -import type { FSEntry, FSEntryWriteInput } from '../../stores/fs/FSEntry.js'; +import type { + FSEntry, + FSEntryWriteInput, + PendingUploadSession, +} from '../../stores/fs/FSEntry.js'; import { runWithConcurrencyLimit, runWithConcurrencyLimitSettled, @@ -397,26 +404,26 @@ export class FSController extends PuterController { userId, requests, ); - const updatedResponse = await runWithConcurrencyLimit( - response, + const withSideEffects = await this.#applyWriteResponsesSideEffects( + userId, + response.map((writeResponse, index) => { + const write: WriteResponse = { + fsEntry: writeResponse.fsEntry, + wasOverwrite: writeResponse.wasOverwrite, + requestedThumbnail: writeResponse.requestedThumbnail, + contentHashSha256: null, + }; + return { + response: write, + guiMetadata: requests[index]?.guiMetadata, + }; + }), DEFAULT_BATCH_WRITE_SIDE_EFFECT_CONCURRENCY, - async (writeResponse, index) => { - const requestBody = requests[index]; - const withSideEffects = - await this.#applyWriteResponseSideEffects( - userId, - { - fsEntry: writeResponse.fsEntry, - wasOverwrite: writeResponse.wasOverwrite, - requestedThumbnail: - writeResponse.requestedThumbnail, - contentHashSha256: null, - }, - requestBody?.guiMetadata, - ); - return { ...writeResponse, fsEntry: withSideEffects.fsEntry }; - }, ); + const updatedResponse = response.map((writeResponse, index) => ({ + ...writeResponse, + fsEntry: withSideEffects[index]?.fsEntry ?? writeResponse.fsEntry, + })); res.json( updatedResponse.map((r) => this.#withRequiredClientFsEntry(r)), ); @@ -464,12 +471,15 @@ export class FSController extends PuterController { { legacyCode: 'bad_request' }, ); } - await this.#assertUploadSessionWriteAccess(req, userId, [ - req.body?.uploadId, - ]); + const sessions = await this.#assertUploadSessionWriteAccess( + req, + userId, + [req.body?.uploadId], + ); const response = await this.services.fs.signMultipartParts( userId, req.body, + sessions?.[0], ); res.json(this.#withoutStorageInternals(response)); } @@ -866,17 +876,13 @@ export class FSController extends PuterController { preparedBatch, uploadedItems, ); - const updatedResponses = await runWithConcurrencyLimit( - writeResponses, + const updatedResponses = await this.#applyWriteResponsesSideEffects( + userId, + writeResponses.map((writeResponse, index) => ({ + response: writeResponse, + guiMetadata: preparedBatch?.items[index]?.guiMetadata, + })), 32, - async (writeResponse, index) => { - const preparedItem = preparedBatch?.items[index]; - return this.#applyWriteResponseSideEffects( - userId, - writeResponse, - preparedItem?.guiMetadata, - ); - }, ); res.json( updatedResponses.map((r) => this.#withRequiredClientFsEntry(r)), @@ -989,17 +995,13 @@ export class FSController extends PuterController { preparedBatch, uploadedItems, ); - const updatedResponses = await runWithConcurrencyLimit( - writeResponses, + const updatedResponses = await this.#applyWriteResponsesSideEffects( + userId, + writeResponses.map((writeResponse, index) => ({ + response: writeResponse, + guiMetadata: filteredRequests[index]?.guiMetadata, + })), 32, - async (writeResponse, index) => { - const requestBody = filteredRequests[index]; - return this.#applyWriteResponseSideEffects( - userId, - writeResponse, - requestBody?.guiMetadata, - ); - }, ); res.json( updatedResponses.map((r) => this.#withRequiredClientFsEntry(r)), @@ -1022,7 +1024,9 @@ export class FSController extends PuterController { const actor = this.#requireActor(req); const body = this.#toObjectRecord(req.body); const entry = await this.#resolveEntryForRequest(body); - await this.#assertAccess(actor, entry.path, 'see'); + // One descriptor for both checks, so the chain is read at most once. + const descriptor = this.services.acl.fsDescriptor(entry.path); + await this.#assertAccess(actor, descriptor, 'see'); const wantsSize = this.#toBoolean(body.return_size); const wantsShares = this.#toBoolean(body.return_shares); @@ -1040,7 +1044,7 @@ export class FSController extends PuterController { const mayReadSize = entry.isDir && wantsSize && - (await this.#canAccess(actor, entry.path, 'list')); + (await this.#canAccess(actor, descriptor, 'list')); const [subtreeSize, suggestedApps, shareFlags, shares, parentUid] = await Promise.all([ @@ -1489,13 +1493,7 @@ export class FSController extends PuterController { ); res.status(range ? 206 : 200); - try { - await pipeline(download.body, res); - } catch { - // Client disconnect or upstream stream error — pipeline already - // tore down both ends. Response is partially sent; nothing to do. - return; - } + await pipeToResponse(download.body, res); } // -- Mutation routes ------------------------------------------------ @@ -1828,16 +1826,20 @@ export class FSController extends PuterController { /** As `#assertAccess`, but for what to include rather than what to serve. */ async #canAccess( actor: Actor, - path: string, + path: string | ResourceDescriptor, mode: 'see' | 'list' | 'read' | 'write', ): Promise { const acl = this.services.acl; - return acl.check(actor, acl.fsDescriptor(path), mode); + return acl.check( + actor, + typeof path === 'string' ? acl.fsDescriptor(path) : path, + mode, + ); } async #assertAccess( actor: Actor, - path: string, + path: string | ResourceDescriptor, mode: 'see' | 'list' | 'read' | 'write', ) { await this.services.acl.assertFsAccess( @@ -2472,19 +2474,24 @@ export class FSController extends PuterController { * `startWrite` checked has to be re-checked against the session's recorded * target every time the caller signs more parts or completes the upload — * otherwise a revoked sharee still lands bytes in the owner's tree. + * + * Returns the sessions it checked, in request order, for signing to reuse; + * undefined when any id was left to the service to read and reject. + * Completion reads its own, so a session settled during this check still + * gets a 409. */ async #assertUploadSessionWriteAccess( req: Request, userId: number, uploadIds: Array, - ): Promise { + ): Promise { // A malformed id is left to the service, which owns that error shape. const knownUploadIds = uploadIds.filter( (uploadId): uploadId is string => typeof uploadId === 'string' && uploadId.length > 0, ); if (knownUploadIds.length === 0) { - return; + return undefined; } const sessions = await this.services.fs.getUploadSessions( userId, @@ -2499,6 +2506,9 @@ export class FSController extends PuterController { })), { pathAlreadyNormalized: true }, ); + return knownUploadIds.length === uploadIds.length + ? sessions + : undefined; } #toEventGuiMetadata( @@ -2613,16 +2623,16 @@ export class FSController extends PuterController { return this.#estimateDataUrlSize(thumbnail) > MAX_THUMBNAIL_BYTES; } - async #applyThumbnailAfterWrite( - userId: number, + /** The thumbnail a write should store on `fsEntry`, or null for none. */ + async #thumbnailToStore( fsEntry: FSEntry, requestedThumbnail: string | null | undefined, - ): Promise { + ): Promise { if (!requestedThumbnail || this.#isAppDataPath(fsEntry.path)) { - return fsEntry; + return null; } if (this.#isOversizedThumbnailDataUrl(requestedThumbnail)) { - return fsEntry; + return null; } const thumbnailPayload = { @@ -2643,15 +2653,10 @@ export class FSController extends PuterController { ? thumbnailPayload.url : null; - if (finalThumbnail === fsEntry.thumbnail || finalThumbnail === null) { - return fsEntry; + if (finalThumbnail === fsEntry.thumbnail) { + return null; } - - return this.services.fs.updateEntryThumbnail( - userId, - fsEntry.uuid, - finalThumbnail, - ); + return finalThumbnail; } #toThumbnailPrepareItem( @@ -2881,36 +2886,130 @@ export class FSController extends PuterController { response: WriteResponse, guiMetadata: WriteGuiMetadata | undefined, ): Promise { - let fsEntry = response.fsEntry; + const [result] = await this.#applyWriteResponsesSideEffects( + userId, + [{ response, guiMetadata }], + 1, + ); + return result ?? response; + } - const hashEventPromise = this.#runNonCritical(async () => { - await this.#emitWriteHashEvent( - response.contentHashSha256, - fsEntry.uuid, - ); - }, 'emitWriteHashEvent'); + /** + * Each write's hash event, thumbnail and GUI event. Thumbnails are vetted + * `concurrency` writes at a time, then the caller's own go to the store in + * one update; a write into someone else's folder is updated alone. + */ + async #applyWriteResponsesSideEffects( + userId: number, + writes: Array<{ + response: WriteResponse; + guiMetadata: WriteGuiMetadata | undefined; + }>, + concurrency: number, + ): Promise { + type Write = (typeof writes)[number]; + const results: WriteResponse[] = new Array(writes.length); + const finish = async ( + index: number, + { response, guiMetadata }: Write, + fsEntry: FSEntry, + hashEventPromise: Promise, + ) => { + await this.#runNonCritical(async () => { + await this.#emitGuiWriteEvent( + response.wasOverwrite + ? 'outer.gui.item.updated' + : 'outer.gui.item.added', + fsEntry, + guiMetadata, + ); + }, 'emitGuiWriteEvent'); + await hashEventPromise; + results[index] = { ...response, fsEntry }; + }; - await this.#runNonCritical(async () => { - fsEntry = await this.#applyThumbnailAfterWrite( - userId, - response.fsEntry, - response.requestedThumbnail, - ); - }, 'applyThumbnailAfterWrite'); + const toStore: Array<{ + index: number; + write: Write; + thumbnail: string; + hashEventPromise: Promise; + }> = []; + await runWithConcurrencyLimit( + writes, + concurrency, + async (write, index) => { + const { fsEntry, contentHashSha256, requestedThumbnail } = + write.response; + const hashEventPromise = this.#runNonCritical(async () => { + await this.#emitWriteHashEvent( + contentHashSha256, + fsEntry.uuid, + ); + }, 'emitWriteHashEvent'); + let thumbnail = null as string | null; + await this.#runNonCritical(async () => { + thumbnail = await this.#thumbnailToStore( + fsEntry, + requestedThumbnail, + ); + }, 'applyThumbnailAfterWrite'); + if (thumbnail === null) { + await finish(index, write, fsEntry, hashEventPromise); + return; + } + toStore.push({ index, write, thumbnail, hashEventPromise }); + }, + ); + if (toStore.length === 0) return results; - await this.#runNonCritical(async () => { - await this.#emitGuiWriteEvent( - response.wasOverwrite - ? 'outer.gui.item.updated' - : 'outer.gui.item.added', - fsEntry, - guiMetadata, - ); - }, 'emitGuiWriteEvent'); + // The update is scoped to the caller's rows, so batching anyone else's + // would only fail the batch. + const own = toStore.filter( + ({ write }) => write.response.fsEntry.userId === userId, + ); + const groups = [ + own, + ...toStore + .filter(({ write }) => write.response.fsEntry.userId !== userId) + .map((item) => [item]), + ].filter((group) => group.length > 0); + const stored = new Map>(); + await runWithConcurrencyLimit(groups, concurrency, async (group) => { + let outcomes: PromiseSettledResult[]; + try { + outcomes = await this.services.fs.updateEntryThumbnails( + userId, + group.map(({ write, thumbnail }) => ({ + uuid: write.response.fsEntry.uuid, + thumbnail, + })), + ); + } catch (error) { + outcomes = group.map(() => ({ + status: 'rejected', + reason: error, + })); + } + group.forEach(({ index }, i) => { + const outcome = outcomes[i]; + if (outcome) stored.set(index, outcome); + }); + }); - await hashEventPromise; - - return { ...response, fsEntry }; + await runWithConcurrencyLimit( + toStore, + concurrency, + async ({ index, write, hashEventPromise }) => { + let fsEntry = write.response.fsEntry; + await this.#runNonCritical(async () => { + const outcome = stored.get(index); + if (outcome?.status === 'rejected') throw outcome.reason; + if (outcome) fsEntry = outcome.value; + }, 'applyThumbnailAfterWrite'); + await finish(index, write, fsEntry, hashEventPromise); + }, + ); + return results; } #shouldIgnoreUploadPath(targetPath: string): boolean { diff --git a/src/backend/controllers/fs/FSController.write.test.ts b/src/backend/controllers/fs/FSController.write.test.ts index 06dd3f11b..f1a736315 100644 --- a/src/backend/controllers/fs/FSController.write.test.ts +++ b/src/backend/controllers/fs/FSController.write.test.ts @@ -26,6 +26,7 @@ import type { Actor } from '../../core/actor.js'; import { runWithContext } from '../../core/context.js'; import { PuterServer } from '../../server.js'; import { setupTestServer } from '../../testUtil.js'; +import { toPendingUploadSessionKey } from '../../stores/fs/pendingUploadSessionHelpers.js'; import { generateDefaultFsentries } from '../../util/userProvisioning.js'; import type { FSController } from './FSController.js'; import { FS_BATCH_WRITE_MAX_ITEMS, FS_MULTIPART_MAX_PARTS } from './limits.js'; @@ -1256,8 +1257,271 @@ describe('FSController.completeWrite', () => { ), ).rejects.toMatchObject({ statusCode: 404 }); }); + + // One read for the access check and one for the status decision; the + // completion is recorded from the second without reading it back. + it('reads the session twice and records the completion from the second copy', async () => { + const { actor, username } = await makeUser(); + const target = `/${username}/Documents/complete-once.txt`; + const started = await startSignedWrite(actor, target, 4); + await fetch(started.url!, { method: 'PUT', body: 'wxyz' }); + const before = (await server.stores.fsEntry.getPendingEntryBySessionId( + started.sessionId, + ))!; + + const reads = countSessionReads(); + try { + await withActor(actor, () => + controller.completeWrite( + makeReq({ + body: { uploadId: started.sessionId }, + actor, + }), + makeRes().res, + ), + ); + expect(reads.of(started.sessionId)).toBe(2); + } finally { + reads.restore(); + } + + const after = (await server.stores.fsEntry.getPendingEntryBySessionId( + started.sessionId, + ))!; + expect(after).toEqual({ + ...before, + status: 'completed', + failureReason: null, + updatedAt: after.completedAt, + consumedAt: after.completedAt, + completedAt: expect.any(Number), + }); + }); + + it('reads each session of a batch completion twice', async () => { + const { actor, username } = await makeUser(); + const started = []; + for (const name of ['batch-once-a.txt', 'batch-once-b.txt']) { + const session = await startSignedWrite( + actor, + `/${username}/Documents/${name}`, + 2, + ); + await fetch(session.url!, { method: 'PUT', body: 'ab' }); + started.push(session); + } + + const reads = countSessionReads(); + try { + const { res, captured } = makeRes(); + await withActor(actor, () => + controller.completeBatchWrites( + makeReq({ + body: started.map((session) => ({ + uploadId: session.sessionId, + })), + actor, + }), + res, + ), + ); + expect( + (captured.body as Array<{ fsEntry: { path: string } }>).map( + (item) => item.fsEntry.path, + ), + ).toEqual([ + `/${username}/Documents/batch-once-a.txt`, + `/${username}/Documents/batch-once-b.txt`, + ]); + for (const session of started) { + expect(reads.of(session.sessionId)).toBe(2); + } + } finally { + reads.restore(); + } + for (const session of started) { + await expect( + server.stores.fsEntry.getPendingEntryBySessionId( + session.sessionId, + ), + ).resolves.toMatchObject({ status: 'completed' }); + } + }); + + it('leaves a malformed id in a batch to the service', async () => { + const { actor, username } = await makeUser(); + const started = await startSignedWrite( + actor, + `/${username}/Documents/batch-malformed.txt`, + 2, + ); + await expect( + withActor(actor, () => + controller.completeBatchWrites( + makeReq({ + body: [ + { uploadId: started.sessionId }, + { uploadId: '' }, + ], + actor, + }), + makeRes().res, + ), + ), + ).rejects.toMatchObject({ statusCode: 404 }); + }); + + // The access check reads the session before the status decision; another + // request that settles it in between must still turn this one away. + const settleDuringAccessCheck = (settle: () => Promise) => { + const fs = server.services.fs; + const read = fs.getUploadSessions.bind(fs); + return vi + .spyOn(fs, 'getUploadSessions') + .mockImplementationOnce(async (userId, uploadIds) => { + const sessions = await read(userId, uploadIds); + await settle(); + return sessions; + }); + }; + + it('turns away a completion whose session another one finished during the access check', async () => { + const { actor, userId, username } = await makeUser(); + const target = `/${username}/Documents/raced-complete.txt`; + const started = await startSignedWrite(actor, target, 4); + await fetch(started.url!, { method: 'PUT', body: 'wxyz' }); + + const spy = settleDuringAccessCheck(async () => { + await server.services.fs.completeUrlWrite(userId, { + uploadId: started.sessionId, + }); + }); + try { + await expect( + withActor(actor, () => + controller.completeWrite( + makeReq({ + body: { uploadId: started.sessionId }, + actor, + }), + makeRes().res, + ), + ), + ).rejects.toMatchObject({ + statusCode: 409, + message: expect.stringContaining('not pending'), + }); + } finally { + spy.mockRestore(); + } + + await expect( + server.stores.fsEntry.getPendingEntryBySessionId(started.sessionId), + ).resolves.toMatchObject({ status: 'completed' }); + // A completed session's object backs the live file; a late abort must + // leave it alone. + await server.services.fs.abortUrlWrite(userId, started.sessionId); + const entry = (await server.stores.fsEntry.getEntryByPath(target))!; + const { body } = await server.services.fs.readContent(entry); + expect(await readAll(body)).toBe('wxyz'); + }); + + it('turns away a completion whose session was aborted during the access check', async () => { + const { actor, userId, username } = await makeUser(); + const target = `/${username}/Documents/raced-abort.txt`; + const started = await startSignedWrite(actor, target, 4); + await fetch(started.url!, { method: 'PUT', body: 'wxyz' }); + + const spy = settleDuringAccessCheck(() => + server.services.fs.abortUrlWrite(userId, started.sessionId), + ); + try { + await expect( + withActor(actor, () => + controller.completeWrite( + makeReq({ + body: { uploadId: started.sessionId }, + actor, + }), + makeRes().res, + ), + ), + ).rejects.toMatchObject({ + statusCode: 409, + message: expect.stringContaining('not pending'), + }); + } finally { + spy.mockRestore(); + } + + await expect( + server.stores.fsEntry.getPendingEntryBySessionId(started.sessionId), + ).resolves.toMatchObject({ + status: 'aborted', + failureReason: 'Upload aborted by caller', + }); + await expect( + server.stores.fsEntry.getEntryByPath(target), + ).resolves.toBeNull(); + }); + + it('turns away a batch completion whose session another one finished during the access check', async () => { + const { actor, userId, username } = await makeUser(); + const target = `/${username}/Documents/raced-batch.txt`; + const started = await startSignedWrite(actor, target, 2); + await fetch(started.url!, { method: 'PUT', body: 'ab' }); + + const spy = settleDuringAccessCheck(async () => { + await server.services.fs.batchCompleteUrlWrite(userId, [ + { uploadId: started.sessionId }, + ]); + }); + try { + await expect( + withActor(actor, () => + controller.completeBatchWrites( + makeReq({ + body: [{ uploadId: started.sessionId }], + actor, + }), + makeRes().res, + ), + ), + ).rejects.toMatchObject({ + statusCode: 409, + message: expect.stringContaining('not pending'), + }); + } finally { + spy.mockRestore(); + } + + await expect( + server.stores.fsEntry.getPendingEntryBySessionId(started.sessionId), + ).resolves.toMatchObject({ status: 'completed' }); + }); }); +const readAll = async (body: Readable): Promise => { + const chunks: Buffer[] = []; + for await (const chunk of body) chunks.push(Buffer.from(chunk)); + return Buffer.concat(chunks).toString('utf8'); +}; + +/** Counts system KV reads of each upload session's key. */ +const countSessionReads = () => { + const get = vi.spyOn(server.stores.kv, 'get'); + return { + of: (sessionId: string) => + get.mock.calls + .flatMap(([params]) => + [(params as { key: unknown }).key].flat(), + ) + .filter((key) => key === toPendingUploadSessionKey(sessionId)) + .length, + restore: () => get.mockRestore(), + }; +}; + describe('FSController.abortWrite', () => { it('rejects a request with no uploadId', async () => { const { actor } = await makeUser(); @@ -1381,6 +1645,48 @@ describe('FSController.signMultipartParts', () => { ).rejects.toMatchObject({ statusCode: 404 }); }); + it('reads the session once to sign more parts', async () => { + const { actor, username } = await makeUser(); + const { res: startRes, captured: startCaptured } = makeRes(); + await withActor(actor, () => + controller.startWrite( + makeReq({ + body: { + fileMetadata: { + path: `/${username}/Documents/sign-once.bin`, + size: 8, + }, + uploadMode: 'multipart', + }, + actor, + }), + startRes, + ), + ); + const started = startCaptured.body as ClientSignedWriteResponse; + + const reads = countSessionReads(); + try { + const { res, captured } = makeRes(); + await withActor(actor, () => + controller.signMultipartParts( + makeReq({ + body: { uploadId: started.sessionId, partNumbers: [1] }, + actor, + }), + res, + ), + ); + expect(captured.body).toMatchObject({ + uploadId: started.sessionId, + multipartPartUrls: [expect.anything()], + }); + expect(reads.of(started.sessionId)).toBe(1); + } finally { + reads.restore(); + } + }); + // The session is unknown, so reaching the lookup would answer 404. it.each([ [FS_MULTIPART_MAX_PARTS + 1, 400], @@ -1447,6 +1753,66 @@ describe('FSController.batchWrites (json)', () => { } }); + it('stores a batch of thumbnails in one update and announces each with its thumbnail', async () => { + const { actor, username } = await makeUser(); + const names = ['th-a.png', 'th-b.png', 'th-c.png', 'th-none.png']; + const thumbnailFor = (name: string) => + name === 'th-none.png' + ? undefined + : `data:image/png;base64,${Buffer.from(name).toString('base64')}`; + const writeSpy = vi.spyOn(server.clients.db, 'write'); + const announced: Array<{ response: Record }> = []; + const listener = (_key: string, data: unknown) => { + announced.push(data as { response: Record }); + }; + server.clients.event.on('outer.gui.item.added', listener as never); + const { res, captured } = makeRes(); + let statements: string[]; + try { + await withActor(actor, () => + controller.batchWrites( + makeReq({ + body: names.map((name) => ({ + fileMetadata: { + path: `/${username}/Documents/${name}`, + size: 1, + }, + fileContent: 'x', + thumbnailData: thumbnailFor(name), + })) as WriteRequest[], + actor, + }), + res, + ), + ); + } finally { + statements = writeSpy.mock.calls.map(([sql]) => String(sql)); + writeSpy.mockRestore(); + server.clients.event.off('outer.gui.item.added', listener as never); + } + + expect( + statements.filter((sql) => sql.includes('SET thumbnail')), + ).toHaveLength(1); + const body = captured.body as Array<{ + fsEntry: { path: string; thumbnail?: string | null }; + }>; + expect(body.map((item) => item.fsEntry.path)).toEqual( + names.map((name) => `/${username}/Documents/${name}`), + ); + for (const name of names) { + const path = `/${username}/Documents/${name}`; + const expected = thumbnailFor(name) ?? null; + await expect( + server.stores.fsEntry.getEntryByUuidFromPrimary( + (await server.stores.fsEntry.getEntryByPath(path))!.uuid, + ), + ).resolves.toMatchObject({ thumbnail: expected }); + const event = announced.find((data) => data.response.path === path); + expect(event?.response.thumbnail ?? null).toBe(expected); + } + }); + it('returns [] for a non-array body', async () => { const { actor } = await makeUser(); const { res, captured } = makeRes(); diff --git a/src/backend/controllers/fs/LegacyFSController.http.test.ts b/src/backend/controllers/fs/LegacyFSController.http.test.ts new file mode 100644 index 000000000..02ba5e45a --- /dev/null +++ b/src/backend/controllers/fs/LegacyFSController.http.test.ts @@ -0,0 +1,304 @@ +/* + * 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 { GetObjectCommand } from '@aws-sdk/client-s3'; +import { randomBytes } from 'node:crypto'; +import { Readable } from 'node:stream'; +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; +import { setupPuterTestEnv, type PuterTestEnv } from '../../testUtil.js'; + +/** + * The v1 download routes over real HTTP: what reaches the client, and what + * happens to the storage stream when the client goes away mid-download. + */ +describe('legacy file downloads over HTTP', () => { + let env: PuterTestEnv; + + beforeAll(async () => { + env = await setupPuterTestEnv(); + }, 120_000); + + afterAll(async () => { + await env?.shutdown(); + }); + + const writeFile = async (name: string, content: Buffer) => { + const { username } = env.users.user; + const user = await env.server.stores.user.getByUsername(username); + const path = `/${username}/Documents/${name}`; + const { fsEntry } = await env.server.services.fs.write(user!.id, { + fileMetadata: { + path, + size: content.byteLength, + contentType: 'application/octet-stream', + }, + fileContent: content, + }); + return { path, uuid: fsEntry.uuid }; + }; + + const signedReadUrl = async (path: string): Promise => { + const signed = await fetch(new URL('/sign', env.apiOrigin), { + method: 'POST', + headers: { + Authorization: `Bearer ${env.users.user.token}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ items: [{ path, action: 'read' }] }), + }); + expect(signed.status).toBe(200); + const readUrl = new URL( + (await signed.json()).signatures[0].read_url as string, + ); + return new URL(`${readUrl.pathname}${readUrl.search}`, env.apiOrigin); + }; + + const accessTokenFor = async (uuid: string): Promise => { + const user = await env.server.stores.user.getByUsername( + env.users.user.username, + ); + return env.server.services.auth.createAccessToken( + { user } as never, + [[`fs:${uuid}:read`]], + { label: 'legacy-download-http' }, + ); + }; + + type Route = '/read' | '/token-read' | '/file' | '/down'; + const routes: Route[] = ['/read', '/token-read', '/file', '/down']; + + const requestFor = async ( + route: Route, + file: { path: string; uuid: string }, + init: RequestInit & { headers?: Record } = {}, + ): Promise => { + const { token, apiToken } = env.users.user; + let url: URL; + let base: RequestInit & { headers?: Record } = {}; + if (route === '/read') { + url = new URL('/read', env.apiOrigin); + url.searchParams.set('file', file.path); + base = { headers: { Authorization: `Bearer ${token}` } }; + } else if (route === '/token-read') { + url = new URL('/token-read', env.apiOrigin); + url.searchParams.set('uid', file.uuid); + url.searchParams.set('token', await accessTokenFor(file.uuid)); + } else if (route === '/file') { + url = await signedReadUrl(file.path); + } else { + url = new URL('/down', env.origin); + url.searchParams.set('path', file.path); + base = { + method: 'POST', + headers: { Authorization: `Bearer ${apiToken}` }, + }; + } + return fetch(url, { + ...base, + ...init, + headers: { ...base.headers, ...init.headers }, + }); + }; + + it.each(routes)( + 'GET %s returns the stored bytes and headers unchanged', + async (route) => { + const content = randomBytes(256 * 1024 + 7); + const name = `bytes-${route.replace(/\W/g, '')}.bin`; + const file = await writeFile(name, content); + + const raw = await requestFor(route, file, { + headers: { 'Accept-Encoding': 'identity' }, + }); + expect(raw.status).toBe(200); + expect(raw.headers.get('content-length')).toBe( + String(content.byteLength), + ); + expect(raw.headers.get('content-type')).toBe( + 'application/octet-stream', + ); + expect(raw.headers.get('content-disposition')).toBe( + `${route === '/down' ? 'attachment' : 'inline'}; filename="${name}"`, + ); + expect(raw.headers.get('etag')).toEqual(expect.any(String)); + expect(raw.headers.get('last-modified')).toEqual( + expect.any(String), + ); + expect(Buffer.from(await raw.arrayBuffer()).equals(content)).toBe( + true, + ); + + // Compressed on the wire by default; the bytes still match. + const compressed = await requestFor(route, file); + expect(compressed.headers.get('content-encoding')).toBe('gzip'); + expect( + Buffer.from(await compressed.arrayBuffer()).equals(content), + ).toBe(true); + }, + ); + + // Storage hands over one chunk and then goes quiet, as a large object + // does while the client is still reading. + const stallReadsOf = (objectKey: string) => { + const s3 = env.server.stores.s3Object; + const client = env.server.clients.s3.get(s3.resolveRegion(null)); + const send = client.send.bind(client); + const bodies: Readable[] = []; + const spy = vi.spyOn(client, 'send').mockImplementation((( + command: unknown, + ) => { + if ( + !(command instanceof GetObjectCommand) || + command.input.Key !== objectKey + ) { + return send(command as never); + } + let pushed = false; + const body = new Readable({ + read() { + if (!pushed) { + pushed = true; + this.push(Buffer.alloc(16 * 1024, 1)); + } + }, + }); + bodies.push(body); + return Promise.resolve({ + Body: body, + ContentLength: 1024 * 1024, + ContentType: 'application/octet-stream', + }); + }) as never); + return { spy, bodies }; + }; + + it.each(routes)( + 'GET %s releases the storage stream when the client aborts', + async (route) => { + const file = await writeFile( + `abort-${route.replace(/\W/g, '')}.bin`, + Buffer.alloc(1024 * 1024, 1), + ); + const { spy, bodies } = stallReadsOf(file.uuid); + try { + const abort = new AbortController(); + // Uncompressed, so the first chunk reaches the client as-is. + const response = await requestFor(route, file, { + signal: abort.signal, + headers: { 'Accept-Encoding': 'identity' }, + }); + expect(response.status).toBe(200); + const reader = response.body!.getReader(); + await reader.read(); + abort.abort(); + + expect(bodies).toHaveLength(1); + await vi.waitFor(() => expect(bodies[0]!.destroyed).toBe(true)); + } finally { + spy.mockRestore(); + } + + // The server keeps serving after the aborted transfer. + const healthy = await writeFile( + `after-abort-${route.replace(/\W/g, '')}.txt`, + Buffer.from('still fine'), + ); + const next = await requestFor(route, healthy); + expect(next.status).toBe(200); + expect(await next.text()).toBe('still fine'); + }, + ); + + // Storage fails, either before its first byte or after one chunk. + const failReadsOf = (objectKey: string, sendFirst: boolean) => { + const s3 = env.server.stores.s3Object; + const client = env.server.clients.s3.get(s3.resolveRegion(null)); + const send = client.send.bind(client); + return vi.spyOn(client, 'send').mockImplementation((( + command: unknown, + ) => { + if ( + !(command instanceof GetObjectCommand) || + command.input.Key !== objectKey + ) { + return send(command as never); + } + let reads = 0; + const body = new Readable({ + read() { + reads++; + if (sendFirst && reads === 1) { + this.push(Buffer.alloc(16 * 1024, 1)); + return; + } + setImmediate(() => + this.destroy(new Error('storage socket reset')), + ); + }, + }); + return Promise.resolve({ + Body: body, + ContentLength: 1024 * 1024, + ContentType: 'application/octet-stream', + }); + }) as never); + }; + + it.each( + routes.flatMap((route) => [ + [route, true] as const, + [route, false] as const, + ]), + )( + 'GET %s drops the connection when storage fails (bytes sent first: %s)', + async (route, sendFirst) => { + const file = await writeFile( + `fail-${sendFirst ? 'mid' : 'start'}-${route.replace(/\W/g, '')}.bin`, + Buffer.alloc(1024 * 1024, 1), + ); + const spy = failReadsOf(file.uuid, sendFirst); + try { + // No error body after a partial one: the client sees the + // transfer cut off, either at the headers or mid-body. + const outcome = await (async () => { + try { + const response = await requestFor(route, file, { + headers: { 'Accept-Encoding': 'identity' }, + }); + await response.arrayBuffer(); + return `completed ${response.status}`; + } catch { + return 'cut off'; + } + })(); + expect(outcome).toBe('cut off'); + } finally { + spy.mockRestore(); + } + + const healthy = await writeFile( + `after-fail-${sendFirst ? 'mid' : 'start'}-${route.replace(/\W/g, '')}.txt`, + Buffer.from('still fine'), + ); + const next = await requestFor(route, healthy); + expect(next.status).toBe(200); + expect(await next.text()).toBe('still fine'); + }, + ); +}); diff --git a/src/backend/controllers/fs/LegacyFSController.test.ts b/src/backend/controllers/fs/LegacyFSController.test.ts index fe37124a1..28c9b0cfa 100644 --- a/src/backend/controllers/fs/LegacyFSController.test.ts +++ b/src/backend/controllers/fs/LegacyFSController.test.ts @@ -467,6 +467,88 @@ describe('LegacyFSController.stat', () => { expect(body.size).toBe(0); }); + it('reports is_empty from the same children a listing returns', async () => { + const { actor, userId } = await makeUser(); + const username = actor.user!.username!; + const emptyPath = `/${username}/Documents/probe-empty`; + const fullPath = `/${username}/Documents/probe-full`; + for (const path of [emptyPath, fullPath]) { + await withActor(actor, () => + controller.mkdir( + makeReq({ body: { path }, actor }), + makeRes().res, + ), + ); + } + await server.services.fs.write(userId, { + fileMetadata: { + path: `${fullPath}/inner.txt`, + size: 5, + contentType: 'text/plain', + }, + fileContent: 'hello', + } as never); + + for (const path of [emptyPath, fullPath]) { + const entry = (await server.stores.fsEntry.getEntryByPath(path))!; + const listed = await server.stores.fsEntry.listChildren( + entry.uuid, + { limit: 1 }, + ); + const { res, captured } = makeRes(); + await withActor(actor, () => + controller.stat( + makeReq({ body: { path, return_size: true }, actor }), + res, + ), + ); + const body = captured.body as Record; + expect(body.is_empty).toBe(listed.length === 0); + expect(body.size).toBe(path === fullPath ? 5 : 0); + expect(body.owner).toEqual({ username }); + } + }); + + it('surfaces an earlier failure ahead of a failed subtree size, with nothing left unhandled', async () => { + const { actor } = await makeUser(); + const username = actor.user!.username!; + const path = `/${username}/Documents/size-order`; + await withActor(actor, () => + controller.mkdir(makeReq({ body: { path }, actor }), makeRes().res), + ); + const flagsFailure = new Error('share flags failed'); + const sizeFailure = new Error('subtree size failed'); + const flags = vi + .spyOn(server.services.share, 'shareFlags') + .mockRejectedValueOnce(flagsFailure); + const size = vi + .spyOn(server.services.fs, 'getSubtreeSize') + .mockRejectedValue(sizeFailure); + try { + await expect( + withActor(actor, () => + controller.stat( + makeReq({ body: { path, return_size: true }, actor }), + makeRes().res, + ), + ), + ).rejects.toBe(flagsFailure); + + // On its own, the size failure is what the caller sees. + await expect( + withActor(actor, () => + controller.stat( + makeReq({ body: { path, return_size: true }, actor }), + makeRes().res, + ), + ), + ).rejects.toBe(sizeFailure); + } finally { + flags.mockRestore(); + size.mockRestore(); + } + }); + it('reports the share flag, and the recipients when asked', async () => { const { actor } = await makeUser(); const recipient = await makeUser(); diff --git a/src/backend/controllers/fs/LegacyFSController.ts b/src/backend/controllers/fs/LegacyFSController.ts index d8148b7c5..56f95a2d7 100644 --- a/src/backend/controllers/fs/LegacyFSController.ts +++ b/src/backend/controllers/fs/LegacyFSController.ts @@ -30,6 +30,7 @@ import { import { Context } from '../../core/context.js'; import { HttpError } from '../../core/http/HttpError.js'; import { RouteOptions } from '../../core/http/index.js'; +import { pipeToResponse } from '../../core/http/pipeToResponse.js'; import { assertNotSuspended, assertTeam2fa, @@ -51,6 +52,7 @@ import { } from '../../util/fileSigning.js'; import { APP_ICON_SIZES } from '../../util/appIcon.js'; import { toAppSummary } from '../../util/appView.js'; +import { awaitLater } from '../../util/concurrency.js'; import { joinChildPath, normalizeAbsolutePath, @@ -494,6 +496,16 @@ export class LegacyFSController extends PuterController { 'see', ); + // Started early but awaited where it used to run, so its error still + // surfaces after the ones below. awaitLater covers the early exits + // that never reach that await. + const subtreeSize = + entry.isDir && getBoolean(body, 'return_size') + ? awaitLater( + this.services.fs.getSubtreeSize(userId, entry.path), + ) + : null; + const [suggestedApps, appsById, shareFlags, parentUid] = await Promise.all([ this.services.suggestedApps.getSuggestedApps(entry), @@ -521,12 +533,7 @@ export class LegacyFSController extends PuterController { }); // Optional hydrations: - if (entry.isDir && getBoolean(body, 'return_size')) { - shaped.size = await this.services.fs.getSubtreeSize( - userId, - entry.path, - ); - } + if (subtreeSize) shaped.size = await subtreeSize; // `return_versions` has no parity yet; the empty array keeps `forEach` // callers working. `return_owner` is a no-op — `owner` is always set. if (getBoolean(body, 'return_versions')) shaped.versions = []; @@ -914,7 +921,9 @@ export class LegacyFSController extends PuterController { // Trash, and `null`/`{}` when restoring. See // `src/gui/src/helpers.js` → `window.move_items`. newMetadata: (body.new_metadata ?? undefined) as - Record | null | undefined, + | Record + | null + | undefined, }); const overwrittenEntry = replacedByMove(overwriteTarget, moved, name); const oldPath = source.path; @@ -1305,10 +1314,7 @@ export class LegacyFSController extends PuterController { ); res.status(range ? 206 : 200); - download.body.on('error', (err) => { - res.destroy(err); - }); - download.body.pipe(res); + void pipeToResponse(download.body, res); }; tokenRead = async (req: Request, res: Response): Promise => { @@ -1391,7 +1397,8 @@ export class LegacyFSController extends PuterController { } type SignedOrEmpty = - (SignedFile & { path?: string }) | Record; + | (SignedFile & { path?: string }) + | Record; const result: { signatures: SignedOrEmpty[]; token?: string } = { signatures: [], }; @@ -1439,11 +1446,13 @@ export class LegacyFSController extends PuterController { } // ACL: always require read; downgrade write→read silently. + // One descriptor for both modes reads the chain at most once. + const descriptor = this.services.acl.fsDescriptor(entry.path); await assertAccess( this.services.acl, this.services.fs, actor, - entry.path, + descriptor, 'read', ); let finalAction: 'read' | 'write' = 'read'; @@ -1455,7 +1464,7 @@ export class LegacyFSController extends PuterController { // publishes the descriptor, so there is nothing to hide. const writeOk = await this.services.acl.check( actor, - this.services.acl.fsDescriptor(entry.path), + descriptor, 'write', ); finalAction = writeOk ? 'write' : 'read'; @@ -1819,10 +1828,7 @@ export class LegacyFSController extends PuterController { ); res.status(range ? 206 : 200); - download.body.on('error', (err) => { - res.destroy(err); - }); - download.body.pipe(res); + void pipeToResponse(download.body, res); }; /** GET|POST /df — user storage allowance. */ @@ -1986,7 +1992,10 @@ export class LegacyFSController extends PuterController { const subjectRef = body.subject; const appRef = body.app; const mode = (getString(body, 'mode') ?? 'read') as - 'see' | 'list' | 'read' | 'write'; + | 'see' + | 'list' + | 'read' + | 'write'; if (!subjectRef || !appRef) throw new HttpError(400, '`subject` and `app` are required', { legacyCode: 'bad_request', @@ -2092,10 +2101,7 @@ export class LegacyFSController extends PuterController { ); res.status(range ? 206 : 200); - download.body.on('error', (err) => { - res.destroy(err); - }); - download.body.pipe(res); + void pipeToResponse(download.body, res); }; async #emitGuiEvent( diff --git a/src/backend/controllers/fs/legacyFsHelpers.test.ts b/src/backend/controllers/fs/legacyFsHelpers.test.ts index 7625909d3..f371bb3e3 100644 --- a/src/backend/controllers/fs/legacyFsHelpers.test.ts +++ b/src/backend/controllers/fs/legacyFsHelpers.test.ts @@ -17,7 +17,9 @@ * along with this program. If not, see . */ -import { describe, expect, it } from 'vitest'; +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; +import type { PuterServer } from '../../server.js'; +import { createTestUser, setupTestServer } from '../../testUtil.js'; import { asRecord, fsEntryMimeType, @@ -360,7 +362,7 @@ describe('toLegacyEntry', () => { it('probes for children to set is_empty on a directory', async () => { const fsEntryStore = { - listChildren: async () => [{ uuid: 'child' }], + hasChildrenByParentUid: async () => true, } as never; const shaped = await toLegacyEntry( undefined, @@ -373,7 +375,7 @@ describe('toLegacyEntry', () => { it('treats a failed child probe as non-empty rather than throwing', async () => { const fsEntryStore = { - listChildren: async () => { + hasChildrenByParentUid: async () => { throw new Error('db down'); }, } as never; @@ -385,6 +387,61 @@ describe('toLegacyEntry', () => { expect(shaped.is_empty).toBe(false); }); + it('leaves is_empty unset for a directory with no store to ask', async () => { + const shaped = await toLegacyEntry( + undefined, + baseEntry({ isDir: true }), + ); + expect(shaped).not.toHaveProperty('is_empty'); + }); + + it('runs the child probe, owner and thumbnail lookups together, keeping key order', async () => { + const started: string[] = []; + let release!: () => void; + const gate = new Promise((resolve) => { + release = resolve; + }); + const fsEntryStore = { + hasChildrenByParentUid: async () => { + started.push('children'); + await gate; + return false; + }, + } as never; + const userStore = { + getById: async () => { + started.push('owner'); + await gate; + return { username: 'alice' }; + }, + }; + const eventClient = { + emitAndWait: async (_key: string, data: { thumbnail: string }) => { + started.push('thumbnail'); + await gate; + data.thumbnail = 'https://signed.example/t.png'; + }, + } as never; + + const pending = toLegacyEntry( + eventClient, + baseEntry({ isDir: true, thumbnail: 's3://bucket/t.png' }), + { fsEntryStore, userStore }, + ); + await vi.waitFor(() => expect(started).toHaveLength(3)); + release(); + const shaped = await pending; + + expect(shaped).toMatchObject({ + is_empty: true, + owner: { username: 'alice' }, + thumbnail: 'https://signed.example/t.png', + }); + const keys = Object.keys(shaped); + expect(keys.slice(-2)).toEqual(['is_empty', 'owner']); + expect(keys.indexOf('thumbnail')).toBe(keys.indexOf('is_public') + 1); + }); + it('hydrates the owner as a username-only object', async () => { const userStore = { getById: async () => ({ username: 'alice', email: 'a@b.test' }), @@ -522,3 +579,42 @@ describe('signEntry', () => { expect(inSeconds(signed.expires)).toBeLessThanOrEqual(30); }); }); + +describe('toLegacyEntry is_empty against stored children', () => { + let server: PuterServer; + + beforeAll(async () => { + server = await setupTestServer(); + }, 60_000); + + afterAll(async () => { + await server?.shutdown(); + }); + + it('reports an empty directory as empty, and one with a child as not', async () => { + const username = `lfh${Math.random().toString(36).slice(2, 10)}`; + await createTestUser(server, { username, password: 'lfh-password' }); + const fsEntryStore = server.stores.fsEntry; + const dir = await fsEntryStore.createNonFileEntry({ + parent: (await fsEntryStore.getEntryByPath( + `/${username}/Documents`, + ))!, + name: 'maybe-empty', + kind: 'directory', + }); + const shape = async () => + toLegacyEntry( + undefined, + (await fsEntryStore.getEntryByUuid(dir.uuid))!, + { fsEntryStore }, + ); + + expect((await shape()).is_empty).toBe(true); + await fsEntryStore.createNonFileEntry({ + parent: dir, + name: 'child', + kind: 'directory', + }); + expect((await shape()).is_empty).toBe(false); + }); +}); diff --git a/src/backend/controllers/fs/legacyFsHelpers.ts b/src/backend/controllers/fs/legacyFsHelpers.ts index 86c1f3768..92ec54ac8 100644 --- a/src/backend/controllers/fs/legacyFsHelpers.ts +++ b/src/backend/controllers/fs/legacyFsHelpers.ts @@ -28,6 +28,7 @@ import { type ACLService, type AclMode, type AclRefusalOptions, + type ResourceDescriptor, } from '../../services/acl/ACLService.js'; import type { Actor } from '../../core/actor.js'; import { Context } from '../../core/context.js'; @@ -229,7 +230,7 @@ export async function assertAccess( aclService: ACLService, _fsService: FSService, actor: Actor, - path: string, + path: string | ResourceDescriptor, mode: AclMode, ): Promise { await aclService.assertFsAccess(actor, path, mode, LEGACY_REFUSAL); @@ -474,39 +475,46 @@ export async function toLegacyEntry( ...(opts.isShared === undefined ? {} : { is_shared: opts.isShared }), }; - // `is_empty` — only meaningful for directories. Single-row probe so we - // don't pay for listing every child. - if (entry.isDir && opts.fsEntryStore) { - try { - const children = await opts.fsEntryStore.listChildren(entry.uuid, { - limit: 1, - }); - response.is_empty = children.length === 0; - } catch { - response.is_empty = false; - } - } else if (!entry.isDir) { - response.is_empty = false; - } + const { fsEntryStore, userStore } = opts; + const [isEmpty, ownerUsername, thumbnail] = await Promise.all([ + // `is_empty` — only meaningful for directories. + entry.isDir && fsEntryStore + ? (async () => { + try { + return !(await fsEntryStore.hasChildrenByParentUid( + entry.uuid, + )); + } catch { + return false; + } + })() + : undefined, + // `owner` — username-only. Matches the legacy safe-entry contract. + userStore + ? (async () => { + try { + const owner = await userStore.getById(entry.userId); + return owner && typeof owner.username === 'string' + ? owner.username + : undefined; + } catch { + return undefined; // best-effort + } + })() + : undefined, + // Let the thumbnail extension swap an s3:// key for a signed URL. + signEntryThumbnail( + eventClient, + entry.uuid, + response.thumbnail as string | null, + ), + ]); - // `owner` — username-only. Matches the legacy safe-entry contract. - if (opts.userStore) { - try { - const owner = await opts.userStore.getById(entry.userId); - if (owner && typeof owner.username === 'string') { - response.owner = { username: owner.username }; - } - } catch { - /* best-effort */ - } - } - - // Let the thumbnail extension swap an s3:// key for a signed URL. - response.thumbnail = await signEntryThumbnail( - eventClient, - entry.uuid, - response.thumbnail as string | null, - ); + if (isEmpty !== undefined) response.is_empty = isEmpty; + else if (!entry.isDir) response.is_empty = false; + if (ownerUsername !== undefined) + response.owner = { username: ownerUsername }; + response.thumbnail = thumbnail; return response; } diff --git a/src/backend/controllers/fs/sharedFolderWrites.http.test.ts b/src/backend/controllers/fs/sharedFolderWrites.http.test.ts index de4c1f037..01c94e622 100644 --- a/src/backend/controllers/fs/sharedFolderWrites.http.test.ts +++ b/src/backend/controllers/fs/sharedFolderWrites.http.test.ts @@ -195,6 +195,36 @@ describe('writes inside a folder shared with write', () => { ).toBe(`${s.maskedDir}/renamed.txt`); }, 180_000); + // The thumbnail update is scoped to the writer's own rows, so a file the + // owner ends up owning keeps none, while the writer's own file in the same + // batch gets its thumbnail. + it('batchWrite stores thumbnails on the writer’s own files only', async () => { + const s = await shareAFolder(); + const png = + 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=='; + const ownPath = `/${s.recipient.username}/Documents/own.png`; + const written = await post('/fs/batchWrite', s.recipient.token, [ + { + fileMetadata: { path: `${s.maskedDir}/shared.png`, size: 1 }, + fileContent: 'a', + thumbnailData: png, + }, + { + fileMetadata: { path: ownPath, size: 1 }, + fileContent: 'b', + thumbnailData: png, + }, + ]); + expect(written.status).toBe(200); + + const stored = async (path: string) => + env.server.stores.fsEntry.getEntryByUuidFromPrimary( + (await env.server.stores.fsEntry.getEntryByPath(path))!.uuid, + ); + expect((await stored(`${s.realDir}/shared.png`))?.thumbnail).toBeNull(); + expect((await stored(ownPath))?.thumbnail).toMatch(/^s3:\/\//); + }, 180_000); + // The recipient only knows the masked path, so refusals name entries the // way the recipient addressed them. it('keeps the owner’s real path out of error messages', async () => { diff --git a/src/backend/controllers/profile/ProfileController.test.ts b/src/backend/controllers/profile/ProfileController.test.ts new file mode 100644 index 000000000..9deb5cc07 --- /dev/null +++ b/src/backend/controllers/profile/ProfileController.test.ts @@ -0,0 +1,140 @@ +/* + * 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 type { Request, Response } from 'express'; +import { v4 as uuidv4 } from 'uuid'; +import { + afterAll, + afterEach, + beforeAll, + describe, + expect, + it, + vi, +} from 'vitest'; +import type { PuterServer } from '../../server.js'; +import type { UserRow } from '../../stores/user/UserStore.js'; +import { setupTestServer } from '../../testUtil.js'; +import type { ProfileController } from './ProfileController.js'; + +// GET /profile without a username: the caller's own profile, read alongside +// their account row. +describe('ProfileController own profile', () => { + let server: PuterServer; + let controller: ProfileController; + + beforeAll(async () => { + server = await setupTestServer(); + controller = server.controllers.profile as unknown as ProfileController; + }, 120_000); + + afterAll(async () => { + await server?.shutdown(); + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + const makeUser = async (): Promise => { + const username = `pc-${Math.random().toString(36).slice(2, 10)}`; + return server.stores.user.create({ + username, + uuid: uuidv4(), + password: null, + email: `${username}@test.local`, + requires_email_confirmation: false, + } as Parameters[0]); + }; + + const getOwn = async (uuid: string) => { + let body: unknown; + const res = { + json: (value: unknown) => { + body = value; + }, + } as unknown as Response; + await controller.get( + { query: {}, actor: { user: { uuid } } } as unknown as Request, + res, + ); + return body; + }; + + const isProfilePath = (path: unknown) => + String(path).startsWith('/system/profiles/'); + + it('starts reading the profile before the account lookup returns', async () => { + const user = await makeUser(); + await server.services.profile.updateProfile(user, { bio: 'hi' }); + + const order: string[] = []; + const getByUuid = server.stores.user.getByUuid.bind(server.stores.user); + vi.spyOn(server.stores.user, 'getByUuid').mockImplementation( + async (...args: Parameters) => { + await new Promise((resolve) => setTimeout(resolve, 10)); + order.push('account'); + return getByUuid(...args); + }, + ); + const getEntryByPath = server.stores.fsEntry.getEntryByPath.bind( + server.stores.fsEntry, + ); + vi.spyOn(server.stores.fsEntry, 'getEntryByPath').mockImplementation( + async (...args: Parameters) => { + if (isProfilePath(args[0])) order.push('profile'); + return getEntryByPath(...args); + }, + ); + + await expect(getOwn(user.uuid)).resolves.toEqual({ + picture: null, + displayName: null, + bio: 'hi', + }); + expect(order).toEqual(['profile', 'account']); + }); + + it('404s a caller with no account, whatever the profile read did', async () => { + vi.spyOn(server.stores.fsEntry, 'getEntryByPath').mockRejectedValue( + new Error('profile read failed'), + ); + await expect(getOwn(uuidv4())).rejects.toMatchObject({ + statusCode: 404, + }); + }); + + it('surfaces the account lookup failure before the profile read failure', async () => { + vi.spyOn(server.stores.user, 'getByUuid').mockRejectedValue( + new Error('account lookup failed'), + ); + vi.spyOn(server.stores.fsEntry, 'getEntryByPath').mockRejectedValue( + new Error('profile read failed'), + ); + await expect(getOwn(uuidv4())).rejects.toThrow('account lookup failed'); + }); + + it('surfaces a failed profile read for an existing account', async () => { + const user = await makeUser(); + vi.spyOn(server.stores.fsEntry, 'getEntryByPath').mockRejectedValue( + new Error('profile read failed'), + ); + await expect(getOwn(user.uuid)).rejects.toThrow('profile read failed'); + }); +}); diff --git a/src/backend/controllers/profile/ProfileController.ts b/src/backend/controllers/profile/ProfileController.ts index b86fd7a9e..42c293bcc 100644 --- a/src/backend/controllers/profile/ProfileController.ts +++ b/src/backend/controllers/profile/ProfileController.ts @@ -20,7 +20,10 @@ import type { Request, Response } from 'express'; import { Controller, Get, Post } from '../../core/http/decorators.js'; import { HttpError } from '../../core/http/HttpError.js'; -import type { ProfileService } from '../../services/profile/ProfileService.js'; +import type { + ProfileService, + UserProfile, +} from '../../services/profile/ProfileService.js'; import { PuterController } from '../types.js'; // Shape only; the store decides whether the account exists. @@ -56,13 +59,21 @@ export class ProfileController extends PuterController { const username = req.query.username; let target; + let ownProfile: PromiseSettledResult | undefined; if (username === undefined) { if (!caller?.uuid) { throw new HttpError(401, 'Sign in, or pass a username', { legacyCode: 'unauthorized', }); } - target = await this.stores.user.getByUuid(caller.uuid); + // Read alongside the account; discarded if there's no account. + const [found, profile] = await Promise.allSettled([ + this.stores.user.getByUuid(caller.uuid), + service.getProfile({ uuid: caller.uuid }), + ]); + if (found.status === 'rejected') throw found.reason; + target = found.value; + ownProfile = profile; } else { if ( typeof username !== 'string' || @@ -80,6 +91,11 @@ export class ProfileController extends PuterController { if (!isOwner && !(await service.isPubliclyVisible(target))) { throw notFound(); } + if (ownProfile && isOwner) { + if (ownProfile.status === 'rejected') throw ownProfile.reason; + res.json(ownProfile.value); + return; + } res.json(await service.getProfile(target)); } diff --git a/src/backend/controllers/puterai/PuterAIController.shapes.test.ts b/src/backend/controllers/puterai/PuterAIController.shapes.test.ts index e4b5123c2..77631e7f9 100644 --- a/src/backend/controllers/puterai/PuterAIController.shapes.test.ts +++ b/src/backend/controllers/puterai/PuterAIController.shapes.test.ts @@ -30,7 +30,8 @@ */ import crypto from 'node:crypto'; -import { Readable } from 'node:stream'; +import { Readable, Writable } from 'node:stream'; +import { finished } from 'node:stream/promises'; import type { Request, Response } from 'express'; import { afterAll, @@ -1530,25 +1531,34 @@ describe('PuterAIController videoProxy upstream handling', () => { } as unknown as Response); const handler = captureGet('/puterai/video/proxy'); - const { res, captured } = makeRes(); + // The body is piped, so the response has to be a real stream. const sink: Buffer[] = []; - (res as unknown as Record).write = ( - chunk: Buffer, - ) => { - sink.push(Buffer.from(chunk)); - return true; - }; - (res as unknown as Record).emit = () => true; + const headers: Record = {}; + const res = Object.assign( + new Writable({ + write(chunk: Buffer, _enc, cb) { + sink.push(Buffer.from(chunk)); + cb(); + }, + }), + { + setHeader: (k: string, v: string) => { + headers[k] = v; + }, + }, + ) as unknown as Response; await handler(makeReq({ query: signedQuery('vid1') }), res); - await settleStream(); + await finished(res as unknown as Writable); expect(fetchSpy).toHaveBeenCalledWith( expect.stringContaining( 'vid1:download?alt=media&key=gemini-test-key', ), + { signal: expect.any(AbortSignal) }, ); - expect(captured.headers['Content-Type']).toBe('video/mp4'); + expect(headers['Content-Type']).toBe('video/mp4'); + expect(Buffer.concat(sink).toString('utf8')).toBe('video-bytes'); }); }); diff --git a/src/backend/controllers/puterai/PuterAIController.ts b/src/backend/controllers/puterai/PuterAIController.ts index 7c4b6e1e1..f04927a12 100644 --- a/src/backend/controllers/puterai/PuterAIController.ts +++ b/src/backend/controllers/puterai/PuterAIController.ts @@ -24,6 +24,7 @@ import { Context } from '../../core/context.js'; import { abortOnDisconnect } from '../../core/http/abortOnDisconnect.js'; import { HttpError } from '../../core/http/HttpError.js'; import { RouteOptions } from '../../core/http/index.js'; +import { pipeToResponse } from '../../core/http/pipeToResponse.js'; import { computeNetworkFingerprint } from '../../core/http/middleware/rateLimit.js'; import type { PuterRouter } from '../../core/http/PuterRouter.js'; import { @@ -337,8 +338,10 @@ export class PuterAIController extends PuterController { return; } + // A visitor who leaves stops the download, not just the reply. const upstream = await fetch( `${GEMINI_DOWNLOAD_BASE}/${fileId}:download?alt=media&key=${apiKey}`, + { signal: abortOnDisconnect(res) }, ); if (!upstream.ok) { res.status(upstream.status).send('Failed to fetch video'); @@ -351,9 +354,12 @@ export class PuterAIController extends PuterController { res.status(500).send('Empty response body'); return; } - Readable.fromWeb( - upstream.body as unknown as import('node:stream/web').ReadableStream, - ).pipe(res); + void pipeToResponse( + Readable.fromWeb( + upstream.body as unknown as import('node:stream/web').ReadableStream, + ), + res, + ); }; #listModels(driverKey: 'aiChat' | 'aiImage' | 'aiVideo') { @@ -539,7 +545,8 @@ export class PuterAIController extends PuterController { } } else if (ev.type === 'usage') { usageDetails = ev.usageDetails as - UsageDetails | undefined; + | UsageDetails + | undefined; finishReason = ev.finish_reason as string | undefined; } else if (ev.type === 'error') { res.write( @@ -687,7 +694,8 @@ export class PuterAIController extends PuterController { sendChunk(ev.text); } else if (ev.type === 'usage') { usageDetails = ev.usageDetails as - UsageDetails | undefined; + | UsageDetails + | undefined; finishReason = ev.finish_reason as string | undefined; } else if (ev.type === 'error') { res.write( @@ -741,7 +749,8 @@ export class PuterAIController extends PuterController { text: extractTextContent( ( messageResult.message as - Record | undefined + | Record + | undefined )?.content, ), index: 0, diff --git a/src/backend/controllers/puterai/PuterAIController.videoProxy.http.test.ts b/src/backend/controllers/puterai/PuterAIController.videoProxy.http.test.ts new file mode 100644 index 000000000..7a5109d32 --- /dev/null +++ b/src/backend/controllers/puterai/PuterAIController.videoProxy.http.test.ts @@ -0,0 +1,249 @@ +/* + * 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 crypto from 'node:crypto'; +import http from 'node:http'; +import { + afterAll, + afterEach, + beforeAll, + describe, + expect, + it, + vi, +} from 'vitest'; +import type { PuterServer } from '../../server.js'; +import { allocateEphemeralPort, setupTestServer } from '../../testUtil.js'; +import type { IConfig } from '../../types.js'; + +/** + * The video proxy over a real socket: the provider download is the only thing + * stubbed, so what reaches the client and what a hang-up does upstream are the + * server's own behavior. + */ +describe('video proxy over HTTP', () => { + const DOWNLOAD_BASE = 'https://generativelanguage.googleapis.com/'; + const SECRET = 'video-proxy-test-secret'; + let server: PuterServer; + let port: number; + + beforeAll(async () => { + port = await allocateEphemeralPort(); + server = await setupTestServer( + { + port, + domain: 'puter.localhost', + origin: `http://puter.localhost:${port}`, + api_base_url: `http://api.puter.localhost:${port}`, + url_signature_secret: SECRET, + providers: { + 'gemini-video-generation': { apiKey: 'test-key' }, + }, + } as unknown as IConfig, + { listen: true }, + ); + }, 120_000); + + afterAll(async () => { + await server?.shutdown(); + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + const signedPath = (fileId: string) => { + const expires = String(Math.floor(Date.now() / 1000) + 60); + const signature = crypto + .createHash('sha256') + .update(`${fileId}/video-proxy/${SECRET}/${expires}`) + .digest('hex'); + return `/puterai/video/proxy?${new URLSearchParams({ + fileId, + expires, + signature, + provider: 'gemini', + })}`; + }; + + /** Serves `body` as the provider download and records what upstream sees. */ + const stubDownload = (body: ReadableStream) => { + const seen: { signal?: AbortSignal } = {}; + const realFetch = globalThis.fetch; + vi.spyOn(globalThis, 'fetch').mockImplementation( + async (input, init) => { + if (!String(input).startsWith(DOWNLOAD_BASE)) { + return realFetch(input, init); + } + seen.signal = init?.signal ?? undefined; + return new Response(body, { + status: 200, + headers: { 'content-type': 'video/mp4' }, + }); + }, + ); + return seen; + }; + + const get = (path: string) => + http.request({ + host: '127.0.0.1', + port, + path, + headers: { host: `api.puter.localhost:${port}` }, + }); + + const waitFor = async (check: () => boolean) => { + for (let i = 0; i < 100 && !check(); i++) { + await new Promise((resolve) => setTimeout(resolve, 10)); + } + return check(); + }; + + it('streams the provider download to the client byte for byte', async () => { + const parts = ['video', '-', 'bytes'].map((s) => + new TextEncoder().encode(s), + ); + stubDownload( + new ReadableStream({ + start(controller) { + for (const part of parts) controller.enqueue(part); + controller.close(); + }, + }), + ); + + const { status, contentType, body } = await new Promise<{ + status?: number; + contentType?: string; + body: string; + }>((resolve, reject) => { + const req = get(signedPath('vid-ok')); + req.on('response', (res) => { + const chunks: Buffer[] = []; + res.on('data', (chunk: Buffer) => chunks.push(chunk)); + res.on('end', () => + resolve({ + status: res.statusCode, + contentType: res.headers['content-type'], + body: Buffer.concat(chunks).toString('utf8'), + }), + ); + res.on('error', reject); + }); + req.on('error', reject); + req.end(); + }); + + expect(status).toBe(200); + expect(contentType).toBe('video/mp4'); + expect(body).toBe('video-bytes'); + }); + + it('streams without a listener-leak warning on the response', async () => { + stubDownload( + new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode('video')); + controller.close(); + }, + }), + ); + const warnings: string[] = []; + const onWarning = (warning: Error) => warnings.push(warning.name); + process.on('warning', onWarning); + try { + await new Promise((resolve, reject) => { + const req = get(signedPath('vid-listeners')); + req.on('response', (res) => { + res.on('data', () => {}); + res.on('end', resolve); + res.on('error', reject); + }); + req.on('error', reject); + req.end(); + }); + await new Promise((resolve) => setTimeout(resolve, 10)); + } finally { + process.off('warning', onWarning); + } + expect(warnings).not.toContain('MaxListenersExceededWarning'); + }); + + it('stops the provider download when the client hangs up', async () => { + let cancelled = false; + const seen = stubDownload( + new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode('first')); + }, + // Never finishes on its own: only a cancel ends it. + pull: () => new Promise(() => {}), + cancel() { + cancelled = true; + }, + }), + ); + + await new Promise((resolve, reject) => { + const req = get(signedPath('vid-leave')); + req.on('response', (res) => { + res.once('data', () => { + req.destroy(); + resolve(); + }); + }); + req.on('error', (err) => { + if (!req.destroyed) reject(err); + }); + req.end(); + }); + + expect(await waitFor(() => cancelled)).toBe(true); + expect(seen.signal?.aborted).toBe(true); + }); + + it('cuts the reply off when the provider download breaks mid-stream', async () => { + stubDownload( + new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode('partial')); + setTimeout( + () => controller.error(new Error('upstream broke')), + 10, + ); + }, + }), + ); + + const outcome = await new Promise((resolve) => { + const req = get(signedPath('vid-broken')); + req.on('response', (res) => { + res.on('data', () => {}); + res.on('end', () => resolve('ended')); + res.on('error', () => resolve('aborted')); + res.on('aborted', () => resolve('aborted')); + }); + req.on('error', () => resolve('aborted')); + req.end(); + }); + + expect(outcome).toBe('aborted'); + }); +}); diff --git a/src/backend/controllers/webdav/WebDAVController.ts b/src/backend/controllers/webdav/WebDAVController.ts index 559d3e37d..a79eb1b66 100644 --- a/src/backend/controllers/webdav/WebDAVController.ts +++ b/src/backend/controllers/webdav/WebDAVController.ts @@ -44,7 +44,7 @@ import { Put, Unlock, } from '../../core/http/decorators.js'; -import type { RouteOptions } from '../../core/http/types.js'; +import { PREFLIGHT_MAX_AGE, type RouteOptions } from '../../core/http/types.js'; import { verify as verifyOtp } from '../../services/auth/OTPUtil.js'; import { expandTildePath } from '../../services/fs/resolveNode.js'; import { rootShowsHome } from '../../services/fs/rootListing.js'; @@ -117,9 +117,6 @@ const DAV_ROUTE_OPTIONS: RouteOptions = { concurrent: DAV_CONCURRENT, }; -/** How long a browser may cache a DAV preflight. */ -const PREFLIGHT_MAX_AGE = '86400'; - /** How many shared items the virtual share collections list. */ const SHARE_LISTING_CAP = 200; diff --git a/src/backend/core/http/expressAugmentation.ts b/src/backend/core/http/expressAugmentation.ts index ec55b5a37..25b9b0ce9 100644 --- a/src/backend/core/http/expressAugmentation.ts +++ b/src/backend/core/http/expressAugmentation.ts @@ -69,7 +69,7 @@ declare global { rawBody?: Buffer; - /** Parsed user-agent, populated by the global UA-parsing middleware. */ + /** User-agent, parsed from the header on first read. */ ua?: { browser: { name?: string; version?: string; major?: string }; os: { name?: string; version?: string }; diff --git a/src/backend/core/http/middleware/privateAppGate.test.ts b/src/backend/core/http/middleware/privateAppGate.test.ts index d2cd757cc..7ac0bb6a7 100644 --- a/src/backend/core/http/middleware/privateAppGate.test.ts +++ b/src/backend/core/http/middleware/privateAppGate.test.ts @@ -669,7 +669,7 @@ describe('resolveOwnedAppForHostedSite', () => { const out = await resolveOwnedAppForHostedSite({ req: reqOf('beans.site.puter.localhost'), site: { user_id: owner.id }, - db: server.clients.db, + appStore: server.stores.app, config: baseConfig(), }); expect(out?.uid).toBe(app.uid); @@ -684,7 +684,7 @@ describe('resolveOwnedAppForHostedSite', () => { const out = await resolveOwnedAppForHostedSite({ req: reqOf('beans.app.puter.localhost'), site: { user_id: owner.id }, - db: server.clients.db, + appStore: server.stores.app, config: baseConfig(), }); expect(out?.uid).toBe(app.uid); @@ -700,7 +700,7 @@ describe('resolveOwnedAppForHostedSite', () => { const out = await resolveOwnedAppForHostedSite({ req: reqOf('beans.site.puter.localhost'), site: { user_id: a.id }, - db: server.clients.db, + appStore: server.stores.app, config: baseConfig(), }); expect(out).toBeNull(); @@ -716,7 +716,7 @@ describe('resolveOwnedAppForHostedSite', () => { const out = await resolveOwnedAppForHostedSite({ req: reqOf('beans.site.puter.localhost'), site: { user_id: owner.id }, - db: server.clients.db, + appStore: server.stores.app, config: baseConfig(), }); expect(out?.uid).toBe(app.uid); @@ -732,7 +732,7 @@ describe('resolveOwnedAppForHostedSite', () => { const out = await resolveOwnedAppForHostedSite({ req: reqOf('beans.site.puter.localhost'), site: { user_id: owner.id }, - db: server.clients.db, + appStore: server.stores.app, config: baseConfig(), requirePrivate: true, }); @@ -753,18 +753,54 @@ describe('resolveOwnedAppForHostedSite', () => { const out = await resolveOwnedAppForHostedSite({ req: reqOf('beans.app.puter.localhost'), site: { user_id: owner.id }, - db: server.clients.db, + appStore: server.stores.app, config: baseConfig(), }); expect(out?.uid).toBe(app.uid); expect(Boolean(out?.is_private)).toBe(true); }); + it('leaves columns the gate never reads, such as an inline icon, out of the match', async () => { + const owner = await makeUser(); + const app = await createPrivateApp( + owner.id, + 'http://beans.site.puter.localhost/', + ); + await server.clients.db.write( + 'UPDATE `apps` SET `icon` = ?, `description` = ? WHERE `uid` = ?', + [`data:image/png;base64,${'A'.repeat(512)}`, 'about', app.uid], + ); + const out = await resolveOwnedAppForHostedSite({ + req: reqOf('beans.site.puter.localhost'), + site: { user_id: owner.id }, + appStore: server.stores.app, + config: baseConfig(), + }); + expect(Object.keys(out!).sort()).toEqual([ + 'id', + 'index_url', + 'is_private', + 'name', + 'owner_user_id', + 'title', + 'uid', + ]); + expect(out).toMatchObject({ + id: app.id, + uid: app.uid, + name: `private-${app.uid}`, + title: `private-${app.uid}`, + owner_user_id: owner.id, + index_url: 'http://beans.site.puter.localhost/', + }); + expect(Boolean(out!.is_private)).toBe(true); + }); + it('returns null when the site has no owner', async () => { const out = await resolveOwnedAppForHostedSite({ req: reqOf('beans.site.puter.localhost'), site: { user_id: null }, - db: server.clients.db, + appStore: server.stores.app, config: baseConfig(), }); expect(out).toBeNull(); @@ -774,7 +810,7 @@ describe('resolveOwnedAppForHostedSite', () => { const out = await resolveOwnedAppForHostedSite({ req: reqOf(''), site: { user_id: 1 }, - db: server.clients.db, + appStore: server.stores.app, config: baseConfig(), }); expect(out).toBeNull(); diff --git a/src/backend/core/http/middleware/privateAppGate.ts b/src/backend/core/http/middleware/privateAppGate.ts index 6d04708ac..7af6ce925 100644 --- a/src/backend/core/http/middleware/privateAppGate.ts +++ b/src/backend/core/http/middleware/privateAppGate.ts @@ -18,12 +18,12 @@ * [https://www.gnu.org/licenses/](https://www.gnu.org/licenses/). */ -import type { AbstractDatabaseClient } from '@heyputer/backend/src/clients/database/DatabaseClient'; import type { Request } from 'express'; import { HOSTED_ASSET_COOKIES, type AuthService, } from '../../../services/auth/AuthService'; +import type { AppStore } from '../../../stores/app/AppStore.js'; import type { IConfig } from '../../../types'; import type { Actor } from '../../actor'; @@ -193,7 +193,7 @@ export function subdomainFromHost( export async function resolveOwnedAppForHostedSite(opts: { req: Request; site: SubdomainLike; - db: AbstractDatabaseClient; + appStore: Pick; config: PrivateHostingConfig; requirePrivate?: boolean; }): Promise { @@ -247,15 +247,11 @@ export async function resolveOwnedAppForHostedSite(opts: { const uniqueCandidates = [...new Set(urlCandidates)]; if (uniqueCandidates.length === 0) return null; - const privateFilter = opts.requirePrivate - ? `AND \`is_private\` = ${opts.db.booleanLiteral(true)} ` - : ''; - const placeholders = uniqueCandidates.map(() => '?').join(', '); // Ambiguity fails closed: a private row wins; `id` keeps it deterministic. - const orderClause = `ORDER BY CASE WHEN \`is_private\` = ${opts.db.booleanLiteral(true)} THEN 0 ELSE 1 END, \`id\``; - const rows = await opts.db.read( - `SELECT * FROM apps WHERE owner_user_id = ? ${privateFilter}AND index_url IN (${placeholders}) ${orderClause} LIMIT 2`, - [opts.site.user_id, ...uniqueCandidates], + const rows = await opts.appStore.listOwnedByIndexUrlCandidates( + opts.site.user_id, + uniqueCandidates, + { requirePrivate: opts.requirePrivate }, ); if (rows.length === 0) return null; if (rows.length > 1) { diff --git a/src/backend/core/http/middleware/puterSite.test.ts b/src/backend/core/http/middleware/puterSite.test.ts index cd8e19f1d..a0ecd37f3 100644 --- a/src/backend/core/http/middleware/puterSite.test.ts +++ b/src/backend/core/http/middleware/puterSite.test.ts @@ -1278,6 +1278,50 @@ describe('createPuterSiteMiddleware — .puter_site_config', () => { expect(piped!.equals(body)).toBe(true); }); + it('reads the config only for a path that 404s', async () => { + const { owner, homePath, sub } = await setupSiteWithConfig({ + errors: { '404': { file: '/index.html', status: 200 } }, + }); + const body = Buffer.from('spa-shell'); + await writeFile(owner.id, `${homePath}/index.html`, body, 'text/html'); + const root = await server.stores.fsEntry.getEntryByPath(homePath); + // The loader caches whatever it reads, so an empty slot means it + // never ran. + const cached = () => + server.clients.redis.get(`puter-site-config:${root!.id}`); + const settle = () => new Promise((resolve) => setTimeout(resolve, 10)); + + const mw = buildMiddleware(); + const served = makeRes(); + await mw( + makeReq({ + hostname: `${sub}.site.puter.localhost`, + path: '/index.html', + }), + served.res, + vi.fn(), + ); + await settle(); + expect(served.out.statusCode).toBe(200); + expect(await cached()).toBeNull(); + + const missing = makeRes(); + await mw( + makeReq({ + hostname: `${sub}.site.puter.localhost`, + path: '/client-route', + }), + missing.res, + vi.fn(), + ); + await settle(); + expect(missing.out.statusCode).toBe(200); + expect((missing.out.body as Buffer | undefined)?.equals(body)).toBe( + true, + ); + expect(await cached()).not.toBeNull(); + }); + it('serves the cached config on a second request, even when the on-disk file is removed (Redis cache holds within TTL)', async () => { // First request seeds the cache with the parsed config. We // then delete the underlying file and fire a second request: @@ -1965,3 +2009,463 @@ describe('createPuterSiteMiddleware — private app sign-in token', () => { } }); }); + +// ── Owner, app and root lookups ───────────────────────────────────── + +describe('createPuterSiteMiddleware — site lookups', () => { + const failure = (what: string) => new Error(`${what} lookup failed`); + const isAppQuery = (sql: unknown) => /FROM `?apps`?\b/.test(String(sql)); + + /** Unhandled rejections raised while `fn` runs, and shortly after. */ + const unhandledDuring = async (fn: () => Promise) => { + const seen: unknown[] = []; + const onUnhandled = (reason: unknown) => seen.push(reason); + process.on('unhandledRejection', onUnhandled); + try { + await fn(); + await new Promise((resolve) => setTimeout(resolve, 20)); + } finally { + process.off('unhandledRejection', onUnhandled); + } + return seen; + }; + + const makeSite = async (opts: { suspended?: boolean } = {}) => { + const owner = await makeUserWithHome(); + if (opts.suspended) { + await server.stores.user.update(owner.id, { suspended: 1 }); + } + const homePath = `/${owner.username}`; + const homeEntry = await server.stores.fsEntry.getEntryByPath(homePath); + const sub = `look-${Math.random().toString(36).slice(2, 8)}`; + await server.stores.subdomain.create({ + userId: owner.id, + subdomain: sub, + rootDirId: homeEntry!.id, + }); + await writeFile( + owner.id, + `${homePath}/index.html`, + Buffer.from('hi'), + 'text/html', + ); + return { owner, sub, rootDirId: homeEntry!.id }; + }; + + const failRootLookup = () => + vi + .spyOn(server.stores.fsEntry, 'getEntryById') + .mockRejectedValueOnce(failure('root')); + + const failAppLookup = () => { + const db = server.clients.db; + const realRead = db.read.bind(db); + return vi + .spyOn(db, 'read') + .mockImplementation((sql, params) => + isAppQuery(sql) + ? Promise.reject(failure('app')) + : realRead(sql, params), + ); + }; + + /** Spies on app queries and root lookups, failing both. */ + const failAndCountLookups = () => { + const readSpy = failAppLookup(); + const rootSpy = vi + .spyOn(server.stores.fsEntry, 'getEntryById') + .mockRejectedValue(failure('root')); + return { + appQueries: () => + readSpy.mock.calls.filter(([sql]) => isAppQuery(sql)).length, + rootLookups: () => rootSpy.mock.calls.length, + restore: () => { + readSpy.mockRestore(); + rootSpy.mockRestore(); + }, + }; + }; + + it('starts the app and root lookups together once the owner is known', async () => { + const { owner, sub, rootDirId } = await makeSite(); + const ownerRow = await server.stores.user.getById(owner.id); + let releaseOwner!: () => void; + const userSpy = vi + .spyOn(server.stores.user, 'getById') + .mockImplementationOnce( + () => + new Promise((resolve) => { + releaseOwner = () => resolve(ownerRow); + }), + ); + const rootSpy = vi.spyOn(server.stores.fsEntry, 'getEntryById'); + const db = server.clients.db; + const realRead = db.read.bind(db); + const heldAppQueries: Array<() => void> = []; + const readSpy = vi + .spyOn(db, 'read') + .mockImplementation(async (sql, params) => { + if (isAppQuery(sql)) { + await new Promise((resolve) => { + heldAppQueries.push(resolve); + }); + } + return realRead(sql, params); + }); + const appQueried = () => + readSpy.mock.calls.some(([sql]) => isAppQuery(sql)); + try { + const mw = buildMiddleware(); + const { res, out } = makeRes(); + const done = mw( + makeReq({ + hostname: `${sub}.site.puter.localhost`, + path: '/index.html', + }), + res, + vi.fn(), + ); + await new Promise((resolve) => setImmediate(resolve)); + expect(appQueried()).toBe(false); + expect(rootSpy).not.toHaveBeenCalled(); + + releaseOwner(); + await vi.waitFor(() => expect(appQueried()).toBe(true)); + expect(rootSpy).toHaveBeenCalledWith(rootDirId); + + for (const release of heldAppQueries.splice(0)) release(); + await done; + await new Promise((resolve) => setImmediate(resolve)); + expect(out.statusCode).toBe(200); + } finally { + for (const release of heldAppQueries.splice(0)) release(); + userSpy.mockRestore(); + rootSpy.mockRestore(); + readSpy.mockRestore(); + } + }); + + it.each([ + ['suspended', { suspended: true }], + ['missing', {}], + ] as const)( + '404s a %s owner without querying the app or the root', + async (kind, opts) => { + const { sub } = await makeSite(opts); + const userSpy = + kind === 'missing' + ? vi + .spyOn(server.stores.user, 'getById') + .mockResolvedValueOnce(null) + : null; + const lookups = failAndCountLookups(); + try { + let out: CapturedRes | undefined; + const unhandled = await unhandledDuring(async () => { + ({ out } = await runMiddleware( + buildMiddleware(), + makeReq({ hostname: `${sub}.site.puter.localhost` }), + )); + }); + expect(out?.statusCode).toBe(404); + expect(out?.body).toBe('Subdomain not found'); + expect(lookups.appQueries()).toBe(0); + expect(lookups.rootLookups()).toBe(0); + expect(unhandled).toEqual([]); + } finally { + userSpy?.mockRestore(); + lookups.restore(); + } + }, + ); + + it("surfaces the owner lookup's failure before the others start", async () => { + const { sub } = await makeSite(); + const userSpy = vi + .spyOn(server.stores.user, 'getById') + .mockRejectedValueOnce(failure('owner')); + const lookups = failAndCountLookups(); + try { + const unhandled = await unhandledDuring(async () => { + await expect( + runMiddleware( + buildMiddleware(), + makeReq({ hostname: `${sub}.site.puter.localhost` }), + ), + ).rejects.toThrow('owner lookup failed'); + }); + expect(lookups.appQueries()).toBe(0); + expect(lookups.rootLookups()).toBe(0); + expect(unhandled).toEqual([]); + } finally { + userSpy.mockRestore(); + lookups.restore(); + } + }); + + it("surfaces the app lookup's failure ahead of the root's", async () => { + const { sub } = await makeSite(); + const readSpy = failAppLookup(); + const rootSpy = failRootLookup(); + try { + const unhandled = await unhandledDuring(async () => { + await expect( + runMiddleware( + buildMiddleware(), + makeReq({ hostname: `${sub}.site.puter.localhost` }), + ), + ).rejects.toThrow('app lookup failed'); + }); + expect(unhandled).toEqual([]); + } finally { + readSpy.mockRestore(); + rootSpy.mockRestore(); + } + }); + + it('surfaces a failed root lookup once the gate lets the request through', async () => { + const { sub } = await makeSite(); + const rootSpy = failRootLookup(); + try { + const unhandled = await unhandledDuring(async () => { + await expect( + runMiddleware( + buildMiddleware(), + makeReq({ hostname: `${sub}.site.puter.localhost` }), + ), + ).rejects.toThrow('root lookup failed'); + }); + expect(unhandled).toEqual([]); + } finally { + rootSpy.mockRestore(); + } + }); + + it('redirects off the private host and drops the failed root lookup', async () => { + const { sub } = await makeSite(); + const rootSpy = failRootLookup(); + try { + let out: CapturedRes | undefined; + const unhandled = await unhandledDuring(async () => { + ({ out } = await runMiddleware( + buildMiddleware(), + makeReq({ hostname: `${sub}.app.puter.localhost` }), + )); + }); + expect(out?.redirected).toEqual({ + status: 302, + url: `http://${sub}.site.puter.localhost/`, + }); + expect(unhandled).toEqual([]); + } finally { + rootSpy.mockRestore(); + } + }); + + it('asks a private-app visitor to sign in and drops the failed root lookup', async () => { + const { owner, sub } = await makeSite(); + await server.clients.db.write( + 'INSERT INTO `apps` (`uid`, `name`, `title`, `index_url`, `owner_user_id`, `is_private`) VALUES (?, ?, ?, ?, ?, ?)', + [ + `app-${uuidv4()}`, + sub, + `Title ${sub}`, + `http://${sub}.app.puter.localhost/`, + owner.id, + 1, + ], + ); + const rootSpy = failRootLookup(); + try { + let out: CapturedRes | undefined; + const unhandled = await unhandledDuring(async () => { + ({ out } = await runMiddleware( + buildMiddleware(), + makeReq({ hostname: `${sub}.app.puter.localhost` }), + )); + }); + expect(out?.statusCode).toBe(200); + expect(String(out?.body)).toContain(`Title ${sub}`); + expect(String(out?.body)).toContain('Sign in required'); + expect(unhandled).toEqual([]); + } finally { + rootSpy.mockRestore(); + } + }); +}); + +// ── Private-app gate decisions ────────────────────────────────────── + +describe('createPuterSiteMiddleware — private-app gate decisions', () => { + let entitled = false; + const listener = (_key: string, data: { result: { allowed: boolean } }) => { + if (entitled) data.result.allowed = true; + }; + let visitor: Awaited>; + let token: string; + + beforeAll(async () => { + visitor = await makeUser(); + ({ token } = await server.services.auth.createSessionToken( + visitor as never, + )); + server.clients.event.on('app.privateAccess.check', listener as never); + }); + + afterAll(() => { + server.clients.event.off('app.privateAccess.check', listener as never); + }); + + const makePrivateSite = async (opts: { + suspended?: boolean; + root?: 'home' | 'none' | 'foreign'; + }) => { + const owner = await makeUserWithHome(); + const sub = `gate-${Math.random().toString(36).slice(2, 8)}`; + let rootDirId: number | undefined; + if (opts.root === 'home') { + rootDirId = (await server.stores.fsEntry.getEntryByPath( + `/${owner.username}`, + ))!.id; + await writeFile( + owner.id, + `/${owner.username}/index.html`, + Buffer.from('gated'), + 'text/html', + ); + } else if (opts.root === 'foreign') { + const other = await makeUserWithHome(); + rootDirId = (await server.stores.fsEntry.getEntryByPath( + `/${other.username}`, + ))!.id; + } + await server.stores.subdomain.create({ + userId: owner.id, + subdomain: sub, + ...(rootDirId !== undefined ? { rootDirId } : {}), + }); + await server.clients.db.write( + 'INSERT INTO `apps` (`uid`, `name`, `title`, `index_url`, `owner_user_id`, `is_private`) VALUES (?, ?, ?, ?, ?, ?)', + [ + `app-${uuidv4()}`, + sub, + sub, + `http://${sub}.app.puter.localhost/`, + owner.id, + 1, + ], + ); + if (opts.suspended) { + await server.stores.user.update(owner.id, { suspended: 1 }); + } + return sub; + }; + + const visit = async ( + sub: string, + host: 'site' | 'app', + identity: 'none' | 'cookie', + ) => { + const mw = buildMiddleware({ cookie_name: 'puter_session' }); + const { res, out } = makeRes(); + await mw( + makeReq({ + hostname: `${sub}.${host}.puter.localhost`, + path: '/index.html', + cookies: identity === 'cookie' ? { puter_session: token } : {}, + }), + res, + vi.fn(), + ); + await new Promise((resolve) => setImmediate(resolve)); + return out; + }; + + const decide = (out: CapturedRes, sub: string) => { + if (out.redirected) { + return `302 ${out.redirected.url.replaceAll(sub, 'SUB')}`; + } + const body = Buffer.isBuffer(out.body) + ? out.body.toString() + : String(out.body); + if (body.includes('Sign in required')) + return `${out.statusCode} sign-in`; + if (body.includes('Subdomain or site is not pointing')) { + return `${out.statusCode} site-404`; + } + return `${out.statusCode} ${body}`; + }; + + it.each([ + // A suspended owner hides the app entirely, whoever asks. + [ + { suspended: true, root: 'home' }, + 'app', + 'cookie', + true, + '404 Subdomain not found', + ], + [ + { suspended: true, root: 'home' }, + 'app', + 'none', + false, + '404 Subdomain not found', + ], + [ + { suspended: true, root: 'home' }, + 'site', + 'cookie', + true, + '404 Subdomain not found', + ], + // The public host always bounces to the private one first. + [ + { root: 'home' }, + 'site', + 'cookie', + true, + '302 http://SUB.app.puter.localhost/index.html', + ], + [ + { root: 'home' }, + 'site', + 'none', + false, + '302 http://SUB.app.puter.localhost/index.html', + ], + [{ root: 'home' }, 'app', 'none', true, '200 sign-in'], + [ + { root: 'home' }, + 'app', + 'cookie', + false, + '302 https://puter.localhost/app/app-center/?item=SUB', + ], + [{ root: 'home' }, 'app', 'cookie', true, '200 gated'], + // Root checks run only after entitlement passes. + [ + { root: 'none' }, + 'app', + 'cookie', + false, + '302 https://puter.localhost/app/app-center/?item=SUB', + ], + [{ root: 'none' }, 'app', 'cookie', true, '404 site-404'], + [{ root: 'foreign' }, 'app', 'none', false, '200 sign-in'], + [{ root: 'foreign' }, 'app', 'cookie', true, '404 site-404'], + ] as const)( + '%o on %s host, identity %s, entitled %s → %s', + async (site, host, identity, allow, expected) => { + const sub = await makePrivateSite(site); + entitled = allow; + try { + expect(decide(await visit(sub, host, identity), sub)).toBe( + expected, + ); + } finally { + entitled = false; + } + }, + ); +}); diff --git a/src/backend/core/http/middleware/puterSite.ts b/src/backend/core/http/middleware/puterSite.ts index 4c1c082ee..4c3c1d33e 100644 --- a/src/backend/core/http/middleware/puterSite.ts +++ b/src/backend/core/http/middleware/puterSite.ts @@ -26,6 +26,7 @@ import { HOSTED_ASSET_COOKIES } from '../../../services/auth/AuthService'; import { MANAGE_PERM_PREFIX } from '../../../services/permission/consts'; import type { puterStores } from '../../../stores'; import type { IConfig, LayerInstances } from '../../../types'; +import { awaitLater } from '../../../util/concurrency'; import { makeActor } from '../../actor'; import { buildAppCenterFallback, @@ -269,18 +270,32 @@ export const createPuterSiteMiddleware = ( return; } + // The hosted app and the site root depend only on the site row, so + // both lookups run together. Each is awaited where it is read, so an + // early return drops the other and errors surface in the same order. + // // The subdomain row's `associated_app_id` column is intentionally // not consulted — it was previously user-writable without an // ownership check (so any value there is either legacy or planted). // Resolve "what app does this subdomain host?" directly from // `apps.owner_user_id = site.user_id` + `index_url` match against // the request host. - const associatedApp = (await resolveOwnedAppForHostedSite({ - req, - site: { user_id: site.user_id }, - db: layers.clients.db, - config: hostingCfg, - })) as AppRow | null; + const appLookup = awaitLater( + resolveOwnedAppForHostedSite({ + req, + site: { user_id: site.user_id }, + appStore: layers.stores.app, + config: hostingCfg, + }), + ); + const rootLookup = + site.root_dir_id === null || site.root_dir_id === undefined + ? null + : awaitLater( + layers.stores.fsEntry.getEntryById(site.root_dir_id), + ); + + const associatedApp = (await appLookup) as AppRow | null; const isPrivateApp = Boolean(associatedApp?.is_private); const privateApp = isPrivateApp ? associatedApp : null; @@ -509,9 +524,7 @@ export const createPuterSiteMiddleware = ( return; } - const rootEntry = await layers.stores.fsEntry.getEntryById( - site.root_dir_id, - ); + const rootEntry = await rootLookup; if (!rootEntry) { res.status(404) .type('text/html; charset=UTF-8') @@ -593,24 +606,6 @@ export const createPuterSiteMiddleware = ( } const filePath = rootPath + resolvedUrlPath; - // Best-effort site config load. A missing / malformed config - // never blocks the request — `loadSiteConfig` swallows all - // errors and returns null on any failure. The Redis cache - // keyed on `rootDirId` keeps the hot path off S3 for the - // common case where the same subdomain gets repeat visits. - let siteConfig: SiteConfig | null = null; - try { - siteConfig = await loadSiteConfig({ - rootPath, - rootDirId: rootEntry.id, - fsEntryStore: layers.stores.fsEntry, - fsService: layers.services.fs, - cache: layers.clients.redis, - }); - } catch (e) { - console.warn('[puter-site] loadSiteConfig threw', e); - } - // Hide the config file from public serving — visitors should // see the same 404 as for any other missing path so the // deployment shape isn't leaked. @@ -646,6 +641,21 @@ export const createPuterSiteMiddleware = ( // default 404 page rather than looping. let statusOverride: number | undefined; if (!entry || entry.isDir) { + // Best-effort site config load, only needed on a 404. A missing / + // malformed config never blocks the request — `loadSiteConfig` + // returns null on any failure, and caches by `rootDirId`. + let siteConfig: SiteConfig | null = null; + try { + siteConfig = await loadSiteConfig({ + rootPath, + rootDirId: rootEntry.id, + fsEntryStore: layers.stores.fsEntry, + fsService: layers.services.fs, + cache: layers.clients.redis, + }); + } catch (e) { + console.warn('[puter-site] loadSiteConfig threw', e); + } const errorTarget = resolveErrorTarget(siteConfig, 404, rootPath); // A config pointing `errors.404.file` into `__workers/` would // publish worker source through the error page — refuse it the diff --git a/src/backend/core/http/pipeToResponse.test.ts b/src/backend/core/http/pipeToResponse.test.ts new file mode 100644 index 000000000..2a9d971a6 --- /dev/null +++ b/src/backend/core/http/pipeToResponse.test.ts @@ -0,0 +1,135 @@ +/* + * 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 type { Response } from 'express'; +import http from 'node:http'; +import { Socket, type AddressInfo } from 'node:net'; +import { PassThrough, Readable } from 'node:stream'; +import { afterEach, describe, expect, it } from 'vitest'; +import { pipeToResponse } from './pipeToResponse.js'; + +interface Served { + res: http.ServerResponse; + piped: Promise; + closeListeners: { before: number; after: number }; +} + +/** One request against a server that pipes `makeBody()` into its response. */ +const serve = (makeBody: () => Readable) => { + let onServed!: (served: Served) => void; + const served = new Promise((resolve) => { + onServed = resolve; + }); + const server = http.createServer((_req, res) => { + const before = res.listenerCount('close'); + const piped = pipeToResponse(makeBody(), res as unknown as Response); + onServed({ + res, + piped, + closeListeners: { before, after: res.listenerCount('close') }, + }); + }); + servers.push(server); + const listening = new Promise((resolve) => { + server.listen(0, '127.0.0.1', () => + resolve((server.address() as AddressInfo).port), + ); + }); + return { served, listening }; +}; + +const servers: http.Server[] = []; +afterEach(async () => { + for (const server of servers.splice(0)) { + server.closeAllConnections(); + await new Promise((resolve) => server.close(resolve)); + } +}); + +describe('pipeToResponse', () => { + it('streams the body byte for byte and resolves once the response is done', async () => { + const { served, listening } = serve(() => + Readable.from([Buffer.from('a'), Buffer.from('bc')]), + ); + const response = await fetch(`http://127.0.0.1:${await listening}/`); + expect(await response.text()).toBe('abc'); + const { res, piped } = await served; + await piped; + expect(res.writableFinished).toBe(true); + }); + + it("adds no 'close' listener beyond the one pipe adds", async () => { + const { served, listening } = serve(() => Readable.from(['x'])); + const response = await fetch(`http://127.0.0.1:${await listening}/`); + await response.text(); + const { closeListeners } = await served; + expect(closeListeners.after - closeListeners.before).toBe(1); + }); + + it('releases the body when the client hangs up mid-stream', async () => { + const body = new PassThrough(); + const { served, listening } = serve(() => body); + const abort = new AbortController(); + const pending = fetch(`http://127.0.0.1:${await listening}/`, { + signal: abort.signal, + }); + body.write('first'); + const response = await pending; + await response.body!.getReader().read(); + abort.abort(); + + const { piped } = await served; + await piped; + expect(body.destroyed).toBe(true); + }); + + it('cuts the response off when the body fails mid-stream', async () => { + const body = new PassThrough(); + const { served, listening } = serve(() => body); + const pending = fetch(`http://127.0.0.1:${await listening}/`); + body.write('partial'); + const response = await pending; + const reader = response.body!.getReader(); + await reader.read(); + body.destroy(new Error('upstream broke')); + + const outcome = await (async () => { + try { + while (!(await reader.read()).done); + return 'ended'; + } catch { + return 'cut off'; + } + })(); + expect(outcome).toBe('cut off'); + const { res, piped } = await served; + await piped; + expect(res.destroyed).toBe(true); + }); + + it('releases the body at once when the response already closed', async () => { + const res = new http.ServerResponse( + new http.IncomingMessage(new Socket()), + ); + res.destroy(); + const body = new PassThrough(); + await pipeToResponse(body, res as unknown as Response); + expect(body.destroyed).toBe(true); + }); +}); diff --git a/src/backend/core/http/pipeToResponse.ts b/src/backend/core/http/pipeToResponse.ts new file mode 100644 index 000000000..637185b78 --- /dev/null +++ b/src/backend/core/http/pipeToResponse.ts @@ -0,0 +1,47 @@ +/* + * 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 type { Response } from 'express'; +import type { Readable } from 'node:stream'; + +/** + * Streams `body` into the response. A body that fails mid-stream cuts the + * response off, and a response that closes first releases the body. Resolves + * once the response is done with the body; never rejects. + */ +export const pipeToResponse = ( + body: Readable, + res: Response, +): Promise => { + const { promise, resolve } = Promise.withResolvers(); + body.on('error', (err) => res.destroy(err)); + if (res.destroyed) { + body.destroy(); + resolve(); + return promise; + } + // `pipe` unpipes once the response finishes, closes or fails, so this + // needs no 'close' listener of its own on top of the one `pipe` adds. + res.once('unpipe', () => { + if (!body.readableEnded) body.destroy(); + resolve(); + }); + body.pipe(res); + return promise; +}; diff --git a/src/backend/core/http/types.ts b/src/backend/core/http/types.ts index c66b86f77..26707cfa6 100644 --- a/src/backend/core/http/types.ts +++ b/src/backend/core/http/types.ts @@ -28,7 +28,12 @@ import type { Actor } from '../actor'; * it instead of accepting any token that authenticates. */ export type TokenSource = - 'body' | 'header' | 'x-api-key' | 'cookie' | 'query' | 'handshake'; + | 'body' + | 'header' + | 'x-api-key' + | 'cookie' + | 'query' + | 'handshake'; /** Express router methods plus the WebDAV verbs some endpoints use. */ export type RouteMethod = @@ -334,6 +339,9 @@ export const ROUTES_METADATA_KEY = '__puterRoutes' as const; /** Internal: the property name used to stash a controller's path prefix. */ export const PREFIX_METADATA_KEY = '__puterControllerPrefix' as const; +/** How long a browser may cache a CORS preflight reply, in seconds. */ +export const PREFLIGHT_MAX_AGE = '86400'; + // -- Type narrowing helpers ------------------------------------------ // // When a route declares a gate option (requireAuth, requireUserActor, @@ -359,7 +367,8 @@ type AuthImplyingOptions = | { requireCardVerified: true } | { requireAnyVerified: - readonly VerificationFactor[] | VerificationFactor[]; + | readonly VerificationFactor[] + | VerificationFactor[]; } | { requireSubscription: true | readonly string[] | string[] } | { requireReputation: string }; diff --git a/src/backend/drivers/ai-chat/providers/claude/ClaudeProvider.test.ts b/src/backend/drivers/ai-chat/providers/claude/ClaudeProvider.test.ts index f4250da70..2c82aa0cb 100644 --- a/src/backend/drivers/ai-chat/providers/claude/ClaudeProvider.test.ts +++ b/src/backend/drivers/ai-chat/providers/claude/ClaudeProvider.test.ts @@ -1725,6 +1725,98 @@ describe('ClaudeProvider.complete streaming', () => { expect(part).toEqual({ puter_path: path }); }); + it('answers without waiting for the uploaded files to be deleted', async () => { + const { provider } = makeProvider(); + const { actor, path } = await makeUserWithFile(); + filesUploadMock.mockResolvedValue({ id: 'file_reply_1' }); + // A delete that never settles must not hold the reply. + filesDeleteMock.mockReturnValue(new Promise(() => {})); + messagesCreateMock.mockResolvedValueOnce({ + content: [{ type: 'text', text: 'hi' }], + usage: { input_tokens: 1, output_tokens: 1 }, + }); + + const result = await withTestActor( + () => + provider.complete({ + model: 'claude-haiku-4-5-20251001', + messages: [ + { role: 'user', content: [{ puter_path: path }] }, + ], + }), + actor, + ); + + expect(result).toMatchObject({ finish_reason: 'stop' }); + expect(filesDeleteMock).toHaveBeenCalledWith('file_reply_1', { + betas: [FILES_API_BETA], + }); + }); + + it('hands a refused stream to the fallback without waiting for the deletes', async () => { + const { provider } = makeProvider(); + const { actor, path } = await makeUserWithFile(); + filesUploadMock.mockResolvedValue({ id: 'file_stream_2' }); + filesDeleteMock.mockReturnValue(new Promise(() => {})); + const refused = Object.assign(new Error('Overloaded'), { + status: 529, + }); + messagesStreamMock.mockReturnValueOnce({ + ...makeStreamLike([]), + withResponse: () => Promise.reject(refused), + }); + + const part: Record = { puter_path: path }; + await expect( + withTestActor( + () => + provider.complete({ + model: 'claude-haiku-4-5-20251001', + messages: [{ role: 'user', content: [part] }], + stream: true, + }), + actor, + ), + ).rejects.toBe(refused); + expect(filesDeleteMock).toHaveBeenCalledWith('file_stream_2', { + betas: [FILES_API_BETA], + }); + expect(part).toEqual({ puter_path: path }); + }); + + it('leaves nothing unhandled when deleting an uploaded file fails', async () => { + const { provider } = makeProvider(); + const { actor, path } = await makeUserWithFile(); + filesUploadMock.mockResolvedValue({ id: 'file_gone_1' }); + filesDeleteMock.mockRejectedValue(new Error('already gone')); + messagesCreateMock.mockResolvedValueOnce({ + content: [{ type: 'text', text: 'hi' }], + usage: { input_tokens: 1, output_tokens: 1 }, + }); + const unhandled: unknown[] = []; + const onUnhandled = (reason: unknown) => unhandled.push(reason); + process.on('unhandledRejection', onUnhandled); + try { + await withTestActor( + () => + provider.complete({ + model: 'claude-haiku-4-5-20251001', + messages: [ + { role: 'user', content: [{ puter_path: path }] }, + ], + }), + actor, + ); + await new Promise((resolve) => setTimeout(resolve, 20)); + } finally { + process.off('unhandledRejection', onUnhandled); + } + expect(filesDeleteMock).toHaveBeenCalledWith('file_gone_1', { + betas: [FILES_API_BETA], + }); + expect(unhandled).toEqual([]); + }); + it('surfaces a failure the event iterator swallowed instead of ending the stream clean', async () => { const { provider } = makeProvider(); const dropped = Object.assign(new Error('Overloaded'), { diff --git a/src/backend/drivers/ai-chat/providers/claude/ClaudeProvider.ts b/src/backend/drivers/ai-chat/providers/claude/ClaudeProvider.ts index bd5550185..645bf8753 100644 --- a/src/backend/drivers/ai-chat/providers/claude/ClaudeProvider.ts +++ b/src/backend/drivers/ai-chat/providers/claude/ClaudeProvider.ts @@ -403,7 +403,8 @@ export class ClaudeProvider implements IChatProvider { try { await completion.withResponse(); } catch (e) { - await cleanupUploads(); + // Best-effort and never rejects, so the fallback need not wait. + void cleanupUploads(); restoreUploads(); throw e; } @@ -512,7 +513,8 @@ export class ClaudeProvider implements IChatProvider { restoreUploads(); throw e; } finally { - await cleanupUploads(); + // Best-effort and never rejects, so the response need not wait. + void cleanupUploads(); } } diff --git a/src/backend/drivers/subdomain/SubdomainDriver.test.ts b/src/backend/drivers/subdomain/SubdomainDriver.test.ts index c5f8c7afd..777bd2517 100644 --- a/src/backend/drivers/subdomain/SubdomainDriver.test.ts +++ b/src/backend/drivers/subdomain/SubdomainDriver.test.ts @@ -1129,6 +1129,102 @@ describe('SubdomainDriver associated_app derivation', () => { }); }); +// -- Row hydration ordering -- + +describe('SubdomainDriver row hydration', () => { + const DERIVE_SQL = '`index_url` IN'; + + const seedRow = async () => { + const { actor } = await makeUser(); + const created = (await withActor(actor, () => + driver.create({ + object: { + subdomain: uniqueSubdomain('hydrate'), + root_dir: `/${actor.user!.username}/Public`, + }, + }), + )) as Record; + return { actor, uid: created.uid }; + }; + + it('loads owners and root dirs while the app derive is still running', async () => { + const { actor, uid } = await seedRow(); + const db = server.clients.db; + const realRead = db.read.bind(db); + let release!: () => void; + const gate = new Promise((resolve) => { + release = resolve; + }); + let deriveStarted = false; + const read = vi + .spyOn(db, 'read') + .mockImplementation(async (sql, ...rest) => { + if (String(sql).includes(DERIVE_SQL)) { + deriveStarted = true; + await gate; + } + return realRead(sql, ...rest); + }); + const users = vi.spyOn(server.stores.user, 'getByIds'); + const entries = vi.spyOn(server.stores.fsEntry, 'getEntriesByIds'); + try { + const pending = withActor(actor, () => driver.read({ uid })); + await vi.waitFor(() => expect(deriveStarted).toBe(true)); + expect(users).toHaveBeenCalled(); + expect(entries).toHaveBeenCalled(); + release(); + const row = (await pending) as Record; + expect((row.owner as { username: string }).username).toBe( + actor.user!.username, + ); + expect((row.root_dir as { path: string }).path).toBe( + `/${actor.user!.username}/Public`, + ); + } finally { + release(); + read.mockRestore(); + users.mockRestore(); + entries.mockRestore(); + } + }); + + it('surfaces a failed derive over failed owner and root dir lookups, with no unhandled rejection', async () => { + const { actor, uid } = await seedRow(); + const db = server.clients.db; + const realRead = db.read.bind(db); + const unhandled = vi.fn(); + process.on('unhandledRejection', unhandled); + const read = vi + .spyOn(db, 'read') + .mockImplementation(async (sql, ...rest) => { + if (String(sql).includes(DERIVE_SQL)) { + // Settles after the lookups below have already failed. + await new Promise((resolve) => setTimeout(resolve, 10)); + throw new Error('derive failed'); + } + return realRead(sql, ...rest); + }); + const users = vi + .spyOn(server.stores.user, 'getByIds') + .mockRejectedValue(new Error('users failed')); + const entries = vi + .spyOn(server.stores.fsEntry, 'getEntriesByIds') + .mockRejectedValue(new Error('entries failed')); + try { + await expect( + withActor(actor, () => driver.read({ uid })), + ).rejects.toThrow('derive failed'); + await new Promise((resolve) => setTimeout(resolve, 20)); + expect(unhandled).not.toHaveBeenCalled(); + } finally { + process.off('unhandledRejection', unhandled); + read.mockRestore(); + users.mockRestore(); + entries.mockRestore(); + } + }); +}); + // -- Embedded apps get the launch gates a direct app read applies -- describe('SubdomainDriver embedded app launch gates', () => { diff --git a/src/backend/drivers/subdomain/SubdomainDriver.ts b/src/backend/drivers/subdomain/SubdomainDriver.ts index 7ba973fea..9dafced69 100644 --- a/src/backend/drivers/subdomain/SubdomainDriver.ts +++ b/src/backend/drivers/subdomain/SubdomainDriver.ts @@ -41,6 +41,7 @@ import { PROFILES_SUBDOMAIN, } from '../../util/systemSite.js'; import { expandTildePath } from '../../services/fs/resolveNode.js'; +import { awaitLater } from '../../util/concurrency.js'; import { isUniqueViolation } from '../../util/dbError.js'; import { buildHostedSubdomainIndexUrlCandidates } from '../../util/hostedAppBacking.js'; import { WORKER_SUBDOMAIN_PREFIX } from '../../stores/subdomain/SubdomainStore.js'; @@ -670,6 +671,14 @@ export class SubdomainDriver extends PuterDriver { const rootDirIds = collectIds('root_dir_id'); const appOwnerIds = collectIds('app_owner'); + // Owners and root dirs don't depend on the derived apps, so they load + // alongside the derive. Handled now so a derive error, which still + // wins, can't leave them rejecting unobserved. + const pendingUsers = awaitLater(this.stores.user.getByIds(userIds)); + const pendingEntries = awaitLater( + this.stores.fsEntry.getEntriesByIds(rootDirIds), + ); + // `associated_app` is derived: match apps owned by `subdomain.user_id` // whose `index_url` resolves to one of the subdomain's host // variants. The row's stored `associated_app_id` is ignored @@ -686,8 +695,8 @@ export class SubdomainDriver extends PuterDriver { // so it doesn't need to wait for the app rows. const [usersById, entriesById, appsById, filetypesByAppId] = await Promise.all([ - this.stores.user.getByIds(userIds), - this.stores.fsEntry.getEntriesByIds(rootDirIds), + pendingUsers, + pendingEntries, this.stores.app.getByIds(allAppIds), this.stores.app.getFiletypeAssociationsByIds(allAppIds), ]); diff --git a/src/backend/drivers/workers/WorkerDriver.test.ts b/src/backend/drivers/workers/WorkerDriver.test.ts index 0744b46ef..722ed4484 100644 --- a/src/backend/drivers/workers/WorkerDriver.test.ts +++ b/src/backend/drivers/workers/WorkerDriver.test.ts @@ -8,12 +8,14 @@ import { describe, expect, it, + vi, } from 'vitest'; import { Actor, makeActor as resolveActor } from '../../core/actor.js'; import { runWithContext } from '../../core/context.js'; import { PuterServer } from '../../server.js'; import { setupTestServer } from '../../testUtil.js'; import { decodeCursor } from '../../util/pagination.js'; +import { generateDefaultFsentries } from '../../util/userProvisioning.js'; import { INTERNAL_ADMISSION_BYPASS } from './WorkerDriver.js'; import type { WorkerDriver } from './WorkerDriver.js'; @@ -531,6 +533,125 @@ describe('WorkerDriver', () => { }); }); + describe('getFilePaths lookups', () => { + const seedWorker = async (tag: string) => { + const owner = await makeUser(tag); + await generateDefaultFsentries( + server.clients.db, + server.stores.user, + owner, + ); + const dir = (await server.stores.fsEntry.getEntryByPath( + `/${owner.username}/Public`, + ))!; + const app = await server.stores.app.create( + { + name: `app-${tag}`, + title: `app-${tag}`, + index_url: `https://app-${tag}.example.com/`, + }, + { ownerUserId: owner.id }, + ); + await server.stores.subdomain.create({ + userId: owner.id, + subdomain: `workers.puter.${tag}`, + rootDirId: dir.id, + associatedAppId: null, + appOwner: app.id, + }); + return { owner, dir, app }; + }; + const newTag = (label: string) => + `${label}${Math.random().toString(36).slice(2, 8)}`; + + it('loads root dirs and apps together and shapes each row from them', async () => { + const tag = newTag('wkboth'); + const { owner, dir, app } = await seedWorker(tag); + const realEntries = server.stores.fsEntry.getEntriesByIds.bind( + server.stores.fsEntry, + ); + let release!: () => void; + const gate = new Promise((resolve) => { + release = resolve; + }); + const entries = vi + .spyOn(server.stores.fsEntry, 'getEntriesByIds') + .mockImplementation(async (ids) => { + await gate; + return realEntries(ids); + }); + const apps = vi.spyOn(server.stores.app, 'getByIds'); + try { + const pending = inCtx( + () => target.getFilePaths({}), + actorFor(owner), + ); + await vi.waitFor(() => expect(entries).toHaveBeenCalled()); + expect(apps).toHaveBeenCalledWith([app.id]); + release(); + expect(await pending).toEqual([ + { + name: tag, + url: `https://${tag}.puter.work`, + file_path: dir.path, + file_uid: dir.uuid, + app_uid: app.uid, + created_at: expect.any(String), + }, + ]); + } finally { + release(); + entries.mockRestore(); + apps.mockRestore(); + } + }); + + it("doesn't look up the root dir of another user's worker", async () => { + const tag = newTag('wkforeign'); + const { dir } = await seedWorker(tag); + const intruder = await makeUser(newTag('wkintruder')); + const entries = vi.spyOn(server.stores.fsEntry, 'getEntriesByIds'); + try { + const res = await inCtx( + () => target.getFilePaths({ workerName: tag }), + actorFor(intruder), + ); + expect(res).toEqual([]); + for (const [ids] of entries.mock.calls) + expect(ids).not.toContain(dir.id); + } finally { + entries.mockRestore(); + } + }); + + it('surfaces a root dir lookup failure over an app lookup failure', async () => { + const tag = newTag('wkfail'); + const { owner } = await seedWorker(tag); + const unhandled = vi.fn(); + process.on('unhandledRejection', unhandled); + const entries = vi + .spyOn(server.stores.fsEntry, 'getEntriesByIds') + .mockImplementation(async () => { + await new Promise((resolve) => setTimeout(resolve, 10)); + throw new Error('entries failed'); + }); + const apps = vi + .spyOn(server.stores.app, 'getByIds') + .mockRejectedValue(new Error('apps failed')); + try { + await expect( + inCtx(() => target.getFilePaths({}), actorFor(owner)), + ).rejects.toThrow('entries failed'); + await new Promise((resolve) => setTimeout(resolve, 20)); + expect(unhandled).not.toHaveBeenCalled(); + } finally { + process.off('unhandledRejection', unhandled); + entries.mockRestore(); + apps.mockRestore(); + } + }); + }); + describe('getFilePaths pagination', () => { let nextUserId = 90_000; const seedWorkers = async (count: number) => { diff --git a/src/backend/drivers/workers/WorkerDriver.ts b/src/backend/drivers/workers/WorkerDriver.ts index eaca67413..a0339b743 100644 --- a/src/backend/drivers/workers/WorkerDriver.ts +++ b/src/backend/drivers/workers/WorkerDriver.ts @@ -40,6 +40,7 @@ import { } from '../../services/metering/consts.js'; import type { DriverConcurrentConfig, DriverRateLimitConfig } from '../meta.js'; import { PuterDriver } from '../types.js'; +import { awaitLater } from '../../util/concurrency.js'; import { isUniqueViolation } from '../../util/dbError.js'; import { loadFileInput } from '../util/fileInput.js'; import { @@ -666,12 +667,6 @@ export class WorkerDriver extends PuterDriver { } } - const rootDirIds = rows - .map((r) => r.root_dir_id) - .filter((id): id is number => typeof id === 'number'); - const entriesById = - await this.stores.fsEntry.getEntriesByIds(rootDirIds); - // Make sure the user only sees their own workers rows = rows.filter((r) => { return r.user_id === actor.user.id; @@ -686,10 +681,17 @@ export class WorkerDriver extends PuterDriver { }); } + const rootDirIds = rows + .map((r) => r.root_dir_id) + .filter((id): id is number => typeof id === 'number'); const appOwnerIds = rows .map((r) => r.app_owner) .filter((id): id is number => typeof id === 'number'); - const appsById = await this.stores.app.getByIds(appOwnerIds); + // Together, but awaited in order so an entries failure still wins. + const pendingApps = awaitLater(this.stores.app.getByIds(appOwnerIds)); + const entriesById = + await this.stores.fsEntry.getEntriesByIds(rootDirIds); + const appsById = await pendingApps; const items = rows.map((r) => { const name = diff --git a/src/backend/server.test.ts b/src/backend/server.test.ts index 26a1e6ccf..e30838248 100644 --- a/src/backend/server.test.ts +++ b/src/backend/server.test.ts @@ -21,6 +21,7 @@ import http from 'node:http'; import net from 'node:net'; import type { Request, RequestHandler, Response } from 'express'; +import uaParser from 'ua-parser-js'; import { afterAll, afterEach, @@ -295,6 +296,26 @@ describe('PuterServer host header validation', () => { expect(res.headers['dav']).toBeUndefined(); }); + it('lets a browser cache a preflight off the dav subdomain', async () => { + const res = await request( + '/drivers/call', + { + host: `api.puter.localhost:${port}`, + origin: 'https://third-party.example', + 'access-control-request-method': 'POST', + 'access-control-request-headers': 'authorization,content-type', + }, + 'OPTIONS', + ); + expect(res.status).toBe(200); + expect(res.body).toBe('OK'); + expect(res.headers['access-control-max-age']).toBe('86400'); + expect(res.headers['access-control-allow-origin']).toBe( + 'https://third-party.example', + ); + expect(res.headers['access-control-allow-credentials']).toBe('true'); + }); + it('pins X-Frame-Options on the main domain only', async () => { const main = await request('/healthcheck', { host: 'puter.localhost', @@ -322,6 +343,28 @@ describe('PuterServer host header validation', () => { server.clients.event.off('ip.validate', handler as never); } }); + + it('never marks a vetoed preflight as cacheable', async () => { + const handler = (_key: unknown, data: unknown) => { + (data as { allow: boolean }).allow = false; + }; + server.clients.event.on('ip.validate', handler as never); + try { + const res = await request( + '/drivers/call', + { + host: `api.puter.localhost:${port}`, + origin: 'https://third-party.example', + 'access-control-request-method': 'POST', + }, + 'OPTIONS', + ); + expect(res.status).toBe(403); + expect(res.headers['access-control-max-age']).toBeUndefined(); + } finally { + server.clients.event.off('ip.validate', handler as never); + } + }); }); /** @@ -422,6 +465,95 @@ describe('PuterServer host header validation — permissive modes', () => { }); }); +describe('PuterServer request parsing', () => { + let server: PuterServer; + let port: number; + const routeCount = { before: 0 }; + + beforeAll(async () => { + routeCount.before = extensionStore.routeHandlers.length; + extension.get('/ua-probe', (req: Request, res: Response) => { + const first = req.ua; + res.json({ ua: first ?? null, memoized: req.ua === first }); + }); + extension.get('/ua-assign', (req: Request, res: Response) => { + req.ua = { browser: { name: 'set' }, os: {}, device: {} }; + res.json({ ua: req.ua }); + }); + extension.post('/raw-probe', (req: Request, res: Response) => { + res.json({ + isBuffer: Buffer.isBuffer(req.rawBody), + raw: req.rawBody?.toString('base64') ?? null, + body: req.body, + }); + }); + port = await allocateEphemeralPort(); + server = await setupTestServer( + { + port, + domain: 'puter.localhost', + origin: `http://puter.localhost:${port}`, + } as unknown as IConfig, + { listen: true }, + ); + }); + + afterAll(async () => { + extensionStore.routeHandlers.length = routeCount.before; + await server?.shutdown(); + }); + + const host = () => `puter.localhost:${port}`; + + it('parses the user agent the first time a handler reads it', async () => { + const agent = + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36'; + const res = await rawRequest(port, '/ua-probe', { + host: host(), + 'user-agent': agent, + }); + const body = JSON.parse(res.body); + expect(body.ua).toEqual(JSON.parse(JSON.stringify(uaParser(agent)))); + expect(body.ua.browser.name).toBe('Chrome'); + expect(body.memoized).toBe(true); + }); + + it('leaves it unset without a User-Agent header', async () => { + const res = await rawRequest(port, '/ua-probe', { host: host() }); + expect(JSON.parse(res.body)).toEqual({ ua: null, memoized: true }); + }); + + it('lets a handler overwrite it', async () => { + const res = await rawRequest(port, '/ua-assign', { + host: host(), + 'user-agent': 'curl/8.0', + }); + expect(JSON.parse(res.body).ua.browser).toEqual({ name: 'set' }); + }); + + it('keeps the exact JSON bytes next to the parsed body', async () => { + const payload = '{ "a" : "\u00e9\u2713",\n "n": [1, 2] }'; + for (const contentType of [ + 'application/json', + 'text/plain;actually=json', + ]) { + const res = await rawRequest( + port, + '/raw-probe', + { host: host(), 'content-type': contentType }, + 'POST', + payload, + ); + const body = JSON.parse(res.body); + expect(body.isBuffer, contentType).toBe(true); + expect(Buffer.from(body.raw, 'base64').toString('utf8')).toBe( + payload, + ); + expect(body.body).toEqual({ a: '\u00e9\u2713', n: [1, 2] }); + } + }); +}); + /** * A claimed subdomain answers where an unclaimed one 404s, ahead of everything * after that point in the global stack, and only past its `authorize`. Driven diff --git a/src/backend/server.ts b/src/backend/server.ts index 724c2f0c2..7d157bce4 100644 --- a/src/backend/server.ts +++ b/src/backend/server.ts @@ -21,7 +21,7 @@ import compression from 'compression'; import cookieParser from 'cookie-parser'; import express from 'express'; -import type { IRouter, RequestHandler, Router } from 'express'; +import type { IRouter, Request, RequestHandler, Router } from 'express'; import helmet from 'helmet'; import uaParser from 'ua-parser-js'; import { existsSync, readdirSync, readFileSync } from 'node:fs'; @@ -93,6 +93,7 @@ import { } from './core/http/routeLifecycle'; import { PREFIX_METADATA_KEY, + PREFLIGHT_MAX_AGE, type RouteDescriptor, type RouteOptions, } from './core/http/types'; @@ -417,12 +418,26 @@ export class PuterServer { }); // -- UA parsing ---------------------------------------------- - this.#app.use((req, _res, next) => { - const header = req.headers['user-agent']; - if (header) { - req.ua = uaParser(header); - } - next(); + // Parsed on first read and kept on the request: most requests never + // look at it. + const ownUa = (req: Request, value: Request['ua']) => + Object.defineProperty(req, 'ua', { + value, + writable: true, + enumerable: true, + configurable: true, + }); + Object.defineProperty(this.#app.request, 'ua', { + configurable: true, + get(this: Request) { + const header = this.headers['user-agent']; + const ua = header ? uaParser(header) : undefined; + ownUa(this, ua); + return ua; + }, + set(this: Request, value: Request['ua']) { + ownUa(this, value); + }, }); // -- Host header validation ---------------------------------- @@ -467,6 +482,9 @@ export class PuterServer { next(); return; } + // The reply depends only on Origin and host, both part of the + // browser's preflight cache key. + res.setHeader('Access-Control-Max-Age', PREFLIGHT_MAX_AGE); res.sendStatus(200); }); @@ -486,7 +504,8 @@ export class PuterServer { const captureRawBody: NonNullable< Parameters[0] >['verify'] = (req, _res, buf) => { - (req as { rawBody?: Buffer }).rawBody = Buffer.from(buf); + // body-parser hands over a fresh buffer and never writes to it. + (req as { rawBody?: Buffer }).rawBody = buf; }; this.#app.use(express.json({ limit: '50mb', verify: captureRawBody })); this.#app.use( diff --git a/src/backend/services/acl/ACLService.lazyChain.test.ts b/src/backend/services/acl/ACLService.lazyChain.test.ts new file mode 100644 index 000000000..023dcefd0 --- /dev/null +++ b/src/backend/services/acl/ACLService.lazyChain.test.ts @@ -0,0 +1,242 @@ +/* + * 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 . + */ + +// `check` reads a resource's ancestor chain only once a decision needs it. +// These tests hold it to a check that read the chain first, for every kind of +// actor and path, and pin which checks read it at all. + +import { v4 as uuidv4 } from 'uuid'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { + isSystemActor, + makeActor, + SYSTEM_ACTOR, + type Actor, +} from '../../core/actor.js'; +import type { PuterServer } from '../../server.js'; +import { createTestUser, setupTestServer } from '../../testUtil.js'; +import { MANAGE_PERM_PREFIX } from '../permission/consts.js'; +import type { ACLService, AclMode } from './ACLService.js'; + +const MODES: AclMode[] = ['see', 'list', 'read', 'write', MANAGE_PERM_PREFIX]; + +describe('ACL checks read the ancestor chain lazily', () => { + let server: PuterServer; + let acl: ACLService; + let actors: Record; + let paths: string[]; + let owner: Actor; + + const makeUser = async (): Promise => { + const username = `acz${Math.random().toString(36).slice(2, 10)}`; + await createTestUser(server, { username, password: 'acz-password' }); + const row = (await server.stores.user.getByUsername(username))!; + return makeActor({ user: row }); + }; + + const mkdir = (actor: Actor, path: string) => + server.services.fs.mkdir(actor.user.id!, { + path, + createMissingParents: true, + } as never); + + /** A check that read the chain before anything else, as `check` once did. */ + const chainFirstCheck = async ( + actor: Actor, + path: string, + mode: AclMode, + ) => { + if (isSystemActor(actor)) return true; + if (path === '/') return ['read', 'list', 'see'].includes(mode); + const chain = await server.stores.fsEntry.getAncestorChain(path); + return acl.check( + actor, + { path, resolveAncestors: async () => chain }, + mode, + ); + }; + + const outcome = async (run: () => Promise) => { + try { + return { granted: await run() }; + } catch (error) { + const e = error as { statusCode?: number; message: string }; + return { status: e.statusCode, message: e.message }; + } + }; + + beforeAll(async () => { + server = await setupTestServer(); + acl = server.services.acl as unknown as ACLService; + + owner = await makeUser(); + const other = await makeUser(); + const recipient = await makeUser(); + const a = owner.user.username; + const b = other.user.username; + + const shared = `/${a}/Documents/shared-${uuidv4()}`; + await mkdir(owner, `${shared}/deep`); + await acl.setUserUser( + owner, + recipient, + acl.fsDescriptor(shared), + 'read', + ); + const app = await ( + server.stores.app.create as unknown as ( + f: Record, + o: { ownerUserId: number }, + ) => Promise<{ uid: string; id: number }> + )( + { + name: `acz-${uuidv4()}`, + title: 'Lazy chain app', + index_url: `https://acz-${uuidv4()}.test/`, + }, + { ownerUserId: owner.user.id! }, + ); + await mkdir(owner, `/${a}/AppData/${app.uid}/data`); + await mkdir(owner, `/${a}/Public/page`); + + const token = (issuer: Actor, fullAccess: boolean) => + makeActor({ + user: issuer.user, + accessToken: { + uid: `tok-${uuidv4()}`, + issuer, + authorized: null, + fullAccess, + }, + }); + const appActor = (user: Actor) => + makeActor({ user: user.user, app: { uid: app.uid, id: app.id } }); + + actors = { + owner, + other, + recipient, + ownerApp: appActor(owner), + recipientApp: appActor(recipient), + ownerFullToken: token(owner, true), + ownerScopedToken: token(owner, false), + recipientFullToken: token(recipient, true), + system: SYSTEM_ACTOR, + }; + paths = [ + `/${a}`, + `/${a}/Documents`, + shared, + `${shared}/deep`, + `${shared}/deep/missing`, + `/${a}/AppData/${app.uid}/data`, + `/${a}/AppData`, + `/${a}/Public/page`, + `/${a}/missing/thing`, + `/${b}/Documents`, + '/', + // Paths the chain read refuses. + `/${a}/../${b}/Documents`, + `/${a}//Documents`, + `/${a}/Documents/./x`, + '', + ' ', + // A legacy row whose path never healed. + null as unknown as string, + undefined as unknown as string, + ]; + }, 60_000); + + afterAll(async () => { + await server?.shutdown(); + }, 60_000); + + it('decides as a chain-first check does, for every actor, path and mode', async () => { + const seen = new Set(); + for (const [name, actor] of Object.entries(actors)) { + for (const path of paths) { + for (const mode of MODES) { + const expected = await outcome(() => + chainFirstCheck(actor, path, mode), + ); + const actual = await outcome(() => + acl.check(actor, acl.fsDescriptor(path), mode), + ); + expect( + actual, + `${name} ${JSON.stringify(path)} ${mode}`, + ).toEqual(expected); + seen.add( + 'granted' in expected + ? String(expected.granted) + : String(expected.status), + ); + } + } + } + // Grants, refusals, the 400 for a refused path and the failure for a + // missing one all came up. + expect([...seen].sort()).toEqual(['400', 'false', 'true', 'undefined']); + }); + + it("answers an owner's own path without reading the chain", async () => { + /** `fsDescriptor`, counting the chain reads `check` asks for. */ + const counted = (path: string) => { + const descriptor = acl.fsDescriptor(path); + const read = descriptor.resolveAncestors; + const reads = { count: 0 }; + descriptor.resolveAncestors = () => { + reads.count++; + return read(); + }; + return { descriptor, reads }; + }; + + const ownPaths: Array<[Actor, string]> = [ + [actors.owner, `/${owner.user.username}/Documents`], + [actors.ownerFullToken, `/${owner.user.username}/Documents`], + [actors.ownerApp, paths[5]], + ]; + for (const [actor, path] of ownPaths) { + const { descriptor, reads } = counted(path); + expect(await acl.check(actor, descriptor, 'write')).toBe(true); + expect(reads.count).toBe(0); + } + + // Anyone else needs it. + const { descriptor, reads } = counted(`${paths[2]}/deep`); + expect(await acl.check(actors.recipient, descriptor, 'read')).toBe( + true, + ); + expect(reads.count).toBe(1); + }); + + it('still refuses a path the chain read would refuse, before the owner shortcut', async () => { + const a = owner.user.username; + for (const path of [`/${a}/x/../../other`, `/${a}//x`]) { + await expect( + acl.check(actors.owner, acl.fsDescriptor(path), 'read'), + ).rejects.toMatchObject({ statusCode: 400 }); + } + // The system actor never needed the chain, before or now. + expect( + await acl.check(SYSTEM_ACTOR, acl.fsDescriptor(`/${a}//x`), 'read'), + ).toBe(true); + }); +}); diff --git a/src/backend/services/acl/ACLService.test.ts b/src/backend/services/acl/ACLService.test.ts index 506837035..4697d550c 100644 --- a/src/backend/services/acl/ACLService.test.ts +++ b/src/backend/services/acl/ACLService.test.ts @@ -672,6 +672,165 @@ describe('ACLService.check — stronger modes imply weaker ones', () => { }); }); +describe('ACLService.check — ancestor scans', () => { + const path = '/other/a/b/file.txt'; + + /** + * Scan answers by the ancestor they ask about: each after its own delay, a + * hit, a miss, or a failure. + */ + const answering = ( + services: ReturnType['services'], + answers: Record, + events: string[] = [], + ) => + services.permission.scan.mockImplementation( + async (_actor: unknown, permissions: string[]) => { + const ancestor = Object.keys(answers).find((p) => + permissions.includes(`fs:uid\\C${p}:read`), + )!; + const answer = answers[ancestor]; + events.push(`${ancestor}:start`); + await new Promise((resolve) => setTimeout(resolve, answer.ms)); + events.push(`${ancestor}:end`); + if (answer.fail) throw new Error(`${ancestor} scan failed`); + return answer.hit ? [{ $: 'option', key: 'k' }] : []; + }, + ); + + const outcome = async (run: () => Promise) => { + try { + return { granted: await run() }; + } catch (error) { + return { error: (error as Error).message }; + } + }; + + it('answers a grant on the node itself with one scan', async () => { + const { service, services } = makeService(); + const events: string[] = []; + answering( + services, + { + [path]: { ms: 5, hit: true }, + '/other/a/b': { ms: 5 }, + '/other/a': { ms: 5 }, + '/other': { ms: 5 }, + }, + events, + ); + expect(await service.check(issuerActor, resource(path), 'read')).toBe( + true, + ); + expect(events).toEqual([`${path}:start`, `${path}:end`]); + }); + + it('after a miss on the node, starts the scans above it before the first answers', async () => { + const { service, services } = makeService(); + const events: string[] = []; + answering( + services, + { + [path]: { ms: 5 }, + '/other/a/b': { ms: 5 }, + '/other/a': { ms: 5 }, + '/other': { ms: 5 }, + }, + events, + ); + expect(await service.check(issuerActor, resource(path), 'read')).toBe( + false, + ); + expect(events.slice(0, 2)).toEqual([`${path}:start`, `${path}:end`]); + expect(events.slice(2, 5).every((e) => e.endsWith(':start'))).toBe( + true, + ); + expect(services.permission.scan).toHaveBeenCalledTimes(4); + }); + + it('lets the deepest hit win over a failed scan above it', async () => { + const { service, services } = makeService(); + answering(services, { + [path]: { ms: 0 }, + '/other/a/b': { ms: 15, hit: true }, + '/other/a': { ms: 0, fail: true }, + '/other': { ms: 0, fail: true }, + }); + expect( + await outcome(() => + service.check(issuerActor, resource(path), 'read'), + ), + ).toEqual({ granted: true }); + }); + + it('fails with the first failed scan below any hit, however fast the hit', async () => { + const { service, services } = makeService(); + answering(services, { + [path]: { ms: 0 }, + '/other/a/b': { ms: 15, fail: true }, + '/other/a': { ms: 0, hit: true }, + '/other': { ms: 0, fail: true }, + }); + expect( + await outcome(() => + service.check(issuerActor, resource(path), 'read'), + ), + ).toEqual({ error: '/other/a/b scan failed' }); + }); + + it('fails with the deepest failure when nothing hits', async () => { + const { service, services } = makeService(); + answering(services, { + [path]: { ms: 0 }, + '/other/a/b': { ms: 15, fail: true }, + '/other/a': { ms: 0 }, + '/other': { ms: 0, fail: true }, + }); + expect( + await outcome(() => + service.check(issuerActor, resource(path), 'read'), + ), + ).toEqual({ error: '/other/a/b scan failed' }); + }); + + it('fails on a failed node scan without scanning above it, even where a grant sits', async () => { + const { service, services } = makeService(); + const events: string[] = []; + answering( + services, + { + [path]: { ms: 0, fail: true }, + '/other/a/b': { ms: 0, hit: true }, + '/other/a': { ms: 0 }, + '/other': { ms: 0 }, + }, + events, + ); + expect( + await outcome(() => + service.check(issuerActor, resource(path), 'read'), + ), + ).toEqual({ error: `${path} scan failed` }); + expect(events).toEqual([`${path}:start`, `${path}:end`]); + }); + + it('lets a grant on the node win before any scan above it could fail', async () => { + const { service, services } = makeService(); + answering(services, { + [path]: { ms: 0, hit: true }, + '/other/a/b': { ms: 0, fail: true }, + '/other/a': { ms: 0, fail: true }, + '/other': { ms: 0, fail: true }, + }); + expect( + await outcome(() => + service.check(issuerActor, resource(path), 'read'), + ), + ).toEqual({ granted: true }); + expect(services.permission.scan).toHaveBeenCalledTimes(1); + }); +}); + // -- Scoped access tokens, manage mode --------------------------------- describe('ACLService.check — scoped tokens and manage', () => { diff --git a/src/backend/services/acl/ACLService.ts b/src/backend/services/acl/ACLService.ts index e30de7a8d..00519890c 100644 --- a/src/backend/services/acl/ACLService.ts +++ b/src/backend/services/acl/ACLService.ts @@ -33,7 +33,9 @@ import { MANAGE_PERM_PREFIX } from '../permission/consts'; import { closedAppDataOwners } from '../permission/appDataScopes'; import { HttpError } from '../../core/http/HttpError.js'; import { actorHasSubscription } from '../metering/enforcement'; +import { runWithConcurrencyLimitSettled } from '../../util/concurrency.js'; import { withRedisLock } from '../../util/redisLock.js'; +import { normalizeAbsolutePath } from '../fs/resolveNode.js'; // -- Types ------------------------------------------------------------ @@ -47,6 +49,11 @@ export interface ResourceDescriptor { resolveAncestors: () => Promise< ReadonlyArray<{ uid: string; path: string }> >; + /** + * Throws what `resolveAncestors` would throw for a path it refuses, without + * reading the chain. `check` runs it before deciding from the path alone. + */ + validatePath?: () => void; } export type AclMode = @@ -136,6 +143,9 @@ const PUBLIC_READ_MODES: ReadonlyArray = Object.freeze([ 'see', ]); +/** Ancestor scans one check runs at once. */ +const ANCESTOR_SCAN_CONCURRENCY = 8; + /** Lock bounds for `setUserUser`; past the retry budget a 409 beats waiting. */ const SET_USER_LOCK_TTL_SECONDS = 5; const SET_USER_LOCK_RETRY_MS = 40; @@ -177,7 +187,12 @@ export class ACLService extends PuterService { if (resource.path === '/') { return (PUBLIC_READ_MODES as AclMode[]).includes(mode); } - const ancestors = await resource.resolveAncestors(); + // The chain is read on first use: the owner's own paths, the bulk of + // checks, are answered from the path alone. + resource.validatePath?.(); + let chain: ReturnType | null = + null; + const ancestors = () => (chain ??= resource.resolveAncestors()); const components = resource.path.slice(1).split('/'); @@ -213,7 +228,7 @@ export class ACLService extends PuterService { // asked path still names the folder. const closed = await closedAppDataOwners( this.stores.app, - [resource.path, ...ancestors.map((a) => a.path)], + [resource.path, ...(await ancestors()).map((a) => a.path)], appUid, ); if (closed.size > 0) return false; @@ -227,6 +242,8 @@ export class ACLService extends PuterService { components.length > 1 && components[1] === 'Public' ) { + // Read ahead of the owner, so a failing chain read still fails first. + await ancestors(); const ownerUsername = components[0]; const owner = await this.stores.user.getByUsername(ownerUsername); if (owner) { @@ -253,7 +270,7 @@ export class ACLService extends PuterService { return this.stores.permission.hasAnyAccessTokenPerm( actor.accessToken.uid, - ancestors.flatMap((ancestor) => + (await ancestors()).flatMap((ancestor) => this.permissionsFor(ancestor.uid, mode), ), ); @@ -279,18 +296,32 @@ export class ACLService extends PuterService { // Fall back to the permission scan: walk ancestors, any hit wins. // Widen the scan to all "higher" modes (`write` covers `read`/`list`/ // `see`, etc.) so granting a stronger mode implies the weaker ones. - for (const ancestor of ancestors) { + // The node, where grants usually sit, is scanned alone first. On a + // miss the rest are scanned together and decided deepest first, so a + // failed scan above the first hit doesn't count, as when they ran one + // at a time. + const ancestry = await ancestors(); + const holds = async (ancestor: { uid: string }) => { const reading = await this.services.permission.scan( actor, this.permissionsFor(ancestor.uid, mode), ); - const options = PermissionUtil.readingToOptions(reading); - if (options.length > 0) return true; + return PermissionUtil.readingToOptions(reading).length > 0; + }; + if (ancestry.length > 0 && (await holds(ancestry[0]))) return true; + const scans = await runWithConcurrencyLimitSettled( + ancestry.slice(1), + ANCESTOR_SCAN_CONCURRENCY, + holds, + ); + for (const scan of scans) { + if (scan.status === 'rejected') throw scan.reason; + if (scan.value) return true; } // Last: a link share on the node or a folder above it. Nothing in the // permission tables stands behind one, so it is read on its own. - return this.#anyoneWithLinkAllows(actor, ancestors, mode); + return this.#anyoneWithLinkAllows(actor, ancestry, mode); } /** @@ -381,7 +412,14 @@ export class ACLService extends PuterService { /** Descriptor for a filesystem path, its chain read from the store. */ fsDescriptor(path: string): ResourceDescriptor { const fsEntry = this.stores.fsEntry; - return memoizedDescriptor(path, () => fsEntry.getAncestorChain(path)); + return { + ...memoizedDescriptor(path, () => fsEntry.getAncestorChain(path)), + // The store reads the chain only for a normalized path. `trim` + // first, so a missing path fails here as it does in the store. + validatePath: () => { + normalizeAbsolutePath(path.trim()); + }, + }; } /** `check`, throwing the safe refusal (see `refusal`) when it fails. */ diff --git a/src/backend/services/appIcon/AppIconService.ts b/src/backend/services/appIcon/AppIconService.ts index 5b148302c..4ca822008 100644 --- a/src/backend/services/appIcon/AppIconService.ts +++ b/src/backend/services/appIcon/AppIconService.ts @@ -20,6 +20,7 @@ import { Readable } from 'node:stream'; import type { LayerInstances } from '../../types'; import { APP_ICON_SIZES, getAppIconsBaseUrl } from '../../util/appIcon.js'; +import { awaitLater } from '../../util/concurrency.js'; import { APP_ICONS_SUBDOMAIN, ensureSystemSite, @@ -106,7 +107,8 @@ export class AppIconService extends PuterService { if (d.action === 'icon-migrated') return; const app = await this.stores.app.getByUid(String(d.app_uid)); const icon = (app as Record | null)?.icon as - string | undefined; + | string + | undefined; if (icon?.startsWith('data:')) { await this.#scheduleIcon( { app_uid: d.app_uid, data_url: icon }, @@ -176,13 +178,19 @@ export class AppIconService extends PuterService { if (!base) return null; const normalized = appUid.startsWith('app-') ? appUid : `app-${appUid}`; const sizedPath = `${APP_ICONS_PATH_PREFIX}/${SIZED_ICON_FILENAME(normalized, size)}`; - const sizedExists = await this.stores.fsEntry.getEntryByPath(sizedPath); - if (sizedExists) - return `${base}/${SIZED_ICON_FILENAME(normalized, size)}`; const originalPath = `${APP_ICONS_PATH_PREFIX}/${ORIGINAL_ICON_FILENAME(normalized)}`; - const originalExists = - await this.stores.fsEntry.getEntryByPath(originalPath); - if (originalExists) + // Both probed at once; the sized one still decides first. Icons are + // only ever written with a path, so a missing original skips the + // legacy lineage walk. + const sizedLookup = this.stores.fsEntry.getEntryByPath(sizedPath); + const originalLookup = awaitLater( + this.stores.fsEntry.getEntryByPath(originalPath, { + lineage: false, + }), + ); + if (await sizedLookup) + return `${base}/${SIZED_ICON_FILENAME(normalized, size)}`; + if (await originalLookup) return `${base}/${ORIGINAL_ICON_FILENAME(normalized)}`; return null; } diff --git a/src/backend/services/appIcon/AppIconServiceLocal.test.ts b/src/backend/services/appIcon/AppIconServiceLocal.test.ts index 313495469..2d238f8a5 100644 --- a/src/backend/services/appIcon/AppIconServiceLocal.test.ts +++ b/src/backend/services/appIcon/AppIconServiceLocal.test.ts @@ -191,6 +191,148 @@ describe('AppIconService', () => { `http://puter-app-icons.site.puter.localhost/${app.uid}.png`, ); }); + it('probes a missing original by path alone, without the legacy lineage walk', async () => { + await appIcon.ensureIconsDirectory(); + const systemUser = + await server.stores.user.getByUuid(SYSTEM_ACTOR_UUID); + const uid = `app-${uuidv4()}`; + const png = Buffer.from(PNG_2X2_BASE64, 'base64'); + await server.services.fs.write(systemUser!.id, { + fileMetadata: { + path: `${ICONS_PATH}/${uid}-64.png`, + size: png.byteLength, + contentType: 'image/png', + }, + fileContent: png, + }); + + const read = vi.spyOn(server.clients.db, 'read'); + try { + expect(await appIcon.resolveIconRedirectUrl(uid, 64)).toBe( + `http://puter-app-icons.site.puter.localhost/${uid}-64.png`, + ); + // The original probe may still be running after the answer. + await new Promise((resolve) => setTimeout(resolve, 50)); + expect( + read.mock.calls.filter(([sql]) => + String(sql).includes('WITH RECURSIVE'), + ), + ).toEqual([]); + } finally { + read.mockRestore(); + } + }); + + describe('when the probes fail', () => { + const store = () => server.stores.fsEntry; + + /** + * Answer each probe after its own delay, failing the listed ones. + * Patched by hand: a `vi.spyOn` spy subscribes to the promise it + * returns, which would hide a rejection nothing else awaits. + */ + const probeWith = async ( + answers: { + sized: { ms: number; fail?: boolean; found?: boolean }; + original: { ms: number; fail?: boolean; found?: boolean }; + }, + run: () => Promise, + ) => { + const unhandled: unknown[] = []; + const onUnhandled = (reason: unknown) => unhandled.push(reason); + process.on('unhandledRejection', onUnhandled); + const target = store() as unknown as Record; + target.getEntryByPath = async (path: string) => { + const answer = /-\d+\.png$/.test(path) + ? answers.sized + : answers.original; + await new Promise((resolve) => + setTimeout(resolve, answer.ms), + ); + if (answer.fail) throw new Error(`${path} probe failed`); + return answer.found ? { path } : null; + }; + try { + let outcome: unknown; + try { + outcome = { url: await run() }; + } catch (error) { + outcome = { error: (error as Error).message }; + } + await new Promise((resolve) => setTimeout(resolve, 30)); + expect(unhandled).toEqual([]); + return outcome; + } finally { + delete target.getEntryByPath; + process.off('unhandledRejection', onUnhandled); + } + }; + + const uid = 'app-probe'; + const sizedError = `/system/app_icons/${uid}-64.png probe failed`; + + it("rethrows the sized probe's failure ahead of a faster original", async () => { + for (const original of [ + { ms: 0, fail: true }, + { ms: 0, found: true }, + ]) { + expect( + await probeWith( + { sized: { ms: 15, fail: true }, original }, + () => appIcon.resolveIconRedirectUrl(uid, 64), + ), + ).toEqual({ error: sizedError }); + } + }); + + it('starts both probes before either answers', async () => { + const events: string[] = []; + const target = store() as unknown as Record; + target.getEntryByPath = async (path: string) => { + const label = /-\d+\.png$/.test(path) + ? 'sized' + : 'original'; + events.push(`${label}:start`); + await new Promise((resolve) => setTimeout(resolve, 5)); + events.push(`${label}:end`); + return null; + }; + try { + await appIcon.resolveIconRedirectUrl(uid, 64); + } finally { + delete target.getEntryByPath; + } + expect(events.slice(0, 2)).toEqual([ + 'sized:start', + 'original:start', + ]); + }); + + it('answers with the sized icon whatever the original probe does', async () => { + expect( + await probeWith( + { + sized: { ms: 15, found: true }, + original: { ms: 0, fail: true }, + }, + () => appIcon.resolveIconRedirectUrl(uid, 64), + ), + ).toEqual({ + url: `http://puter-app-icons.site.puter.localhost/${uid}-64.png`, + }); + }); + + it("rethrows the original probe's failure when there is no sized icon", async () => { + expect( + await probeWith( + { sized: { ms: 0 }, original: { ms: 15, fail: true } }, + () => appIcon.resolveIconRedirectUrl(uid, 64), + ), + ).toEqual({ + error: `/system/app_icons/${uid}.png probe failed`, + }); + }); + }); }); describe('icon pipeline', () => { diff --git a/src/backend/services/auth/AuthService.test.ts b/src/backend/services/auth/AuthService.test.ts index 16484ea0d..c151ebdab 100644 --- a/src/backend/services/auth/AuthService.test.ts +++ b/src/backend/services/auth/AuthService.test.ts @@ -783,6 +783,87 @@ describe('AuthService (integration)', () => { expect(app.icon).toBe('data:image/png;base64,ICON'); }); + describe('listSessions app lookups', () => { + const insertApp = async (userId: number, title: string) => { + const uid = `app-${uuidv4()}`; + await server.clients.db.write( + 'INSERT INTO `apps` (`uid`, `name`, `title`, `icon`, `description`, `index_url`, `owner_user_id`) VALUES (?, ?, ?, ?, ?, ?, ?)', + [ + uid, + `n-${uid}`, + title, + `data:image/png;base64,${title}`, + '', + `https://${uuidv4()}.example`, + userId, + ], + ); + return uid; + }; + + /** A user with sessions under two live apps and one that is gone. */ + const seed = async () => { + const user = await makeUser(); + const liveA = await insertApp(user.id, 'Alpha'); + const liveB = await insertApp(user.id, 'Beta'); + const gone = `app-${uuidv4()}`; + for (const appUid of [liveA, liveB, gone]) { + await server.stores.session.create(user.id, { + kind: 'app', + app_uid: appUid, + }); + } + const actor = { + user: { id: user.id, uuid: user.uuid }, + } as unknown as Actor; + return { actor, liveA, liveB, gone }; + }; + + const appsByUid = (rows: Array>) => + Object.fromEntries(rows.map((r) => [r.app_uid, r.app])); + + it('reads the apps in one batch and looks up only what it missed', async () => { + const { actor, liveA, liveB, gone } = await seed(); + const getByUids = vi.spyOn(server.stores.app, 'getByUids'); + const getByUid = vi.spyOn(server.stores.app, 'getByUid'); + let rows: Array>; + try { + rows = await authService.listSessions(actor); + expect(getByUids).toHaveBeenCalledTimes(1); + expect(getByUid.mock.calls).toEqual([[gone]]); + } finally { + getByUids.mockRestore(); + getByUid.mockRestore(); + } + expect(appsByUid(rows)).toEqual({ + [liveA]: expect.objectContaining({ + uid: liveA, + title: 'Alpha', + }), + [liveB]: expect.objectContaining({ + uid: liveB, + title: 'Beta', + }), + [gone]: null, + }); + }); + + it('lists the same apps when the batch read fails', async () => { + const { actor } = await seed(); + const expected = await authService.listSessions(actor); + const getByUids = vi + .spyOn(server.stores.app, 'getByUids') + .mockRejectedValue(new Error('batch failed')); + try { + expect(await authService.listSessions(actor)).toEqual( + expected, + ); + } finally { + getByUids.mockRestore(); + } + }); + }); + it('listSessions sorts the actor’s current session first, then by last_activity desc', async () => { // Manage-sessions GUI anchors "you are here" at the top of the // list; downstream rendering doesn't re-sort, so the backend diff --git a/src/backend/services/auth/AuthService.tokenLookups.test.ts b/src/backend/services/auth/AuthService.tokenLookups.test.ts new file mode 100644 index 000000000..8cad689b3 --- /dev/null +++ b/src/backend/services/auth/AuthService.tokenLookups.test.ts @@ -0,0 +1,730 @@ +/* + * 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 . + */ + +// Token auth starts its identity lookups together. These tests hold it to a +// one-lookup-at-a-time reference: the same result for every token and row +// state, and the same first error when lookups fail, whatever order they fail +// in. + +import { v4 as uuidv4 } from 'uuid'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { makeActor, type Actor } from '../../core/actor.js'; +import { PuterServer } from '../../server.js'; +import { WORKER_SUBDOMAIN_PREFIX } from '../../stores/subdomain/SubdomainStore.js'; +import type { UserRow } from '../../stores/user/UserStore.js'; +import { setupTestServer } from '../../testUtil.js'; +import { isGodmodeApp } from '../../util/godmodeApps.js'; +import { EVENTS_WORKER_SESSION_NAME } from '../events/workerRuntime.js'; +import type { AuthResult, AuthService } from './AuthService.js'; +import type { SessionRow } from './types.js'; + +type Decoded = Record & { type: string }; + +const nowSeconds = () => Math.floor(Date.now() / 1000); + +/** Token auth as it read before its lookups overlapped: one at a time. */ +const sequentialAuthenticate = async ( + server: PuterServer, + decoded: Decoded, +): Promise => { + const { stores, services } = server; + const d = decoded as Record; + + const actorUser = (user: UserRow) => { + const { password: _password, ...rest } = user; + return { + ...rest, + email: user.email ?? null, + suspended: user.suspended ?? false, + email_confirmed: user.email_confirmed ?? false, + requires_email_confirmation: + user.requires_email_confirmation ?? false, + phone: user.phone ?? null, + requires_phone_verification: + user.requires_phone_verification ?? false, + requires_card_verification: + user.requires_card_verification ?? false, + }; + }; + const sessionOf = (session: SessionRow | null) => + session ? { uid: session.uuid, kind: session.kind ?? null } : null; + const ended = ( + row: SessionRow | null, + authId?: string, + ): AuthResult | null => { + let reason: 'session_revoked' | 'session_expired'; + if (row?.revoked_at != null) reason = 'session_revoked'; + else if (row?.expires_at != null && row.expires_at <= nowSeconds()) + reason = 'session_expired'; + else return null; + return { + reauth: + authId === undefined ? { reason } : { reason, auth_id: authId }, + }; + }; + const workerStillDeployed = async (userId: number) => { + if (!d.worker) return true; + if (d.worker_name === EVENTS_WORKER_SESSION_NAME) return true; + if (!d.worker_name) return false; + const row = await stores.subdomain.getBySubdomain( + `${WORKER_SUBDOMAIN_PREFIX}${d.worker_name}`, + ); + if (!row || Number(row.user_id) !== Number(userId)) return false; + return !d.worker_uid || row.uuid === d.worker_uid; + }; + const originBlock = async (app: { index_url?: unknown }) => { + const indexUrl = app.index_url; + if (typeof indexUrl !== 'string' || !indexUrl) return null; + const block = + await services.appOriginBlocklist.isOriginBlocked(indexUrl); + return block.blocked ? { reason: block.reason } : null; + }; + const notBeforeOf = (app: { created_epoch?: unknown }) => { + const created = Number(app?.created_epoch); + if (!Number.isFinite(created) || created <= 0) return null; + return created - 60; + }; + const createdBefore = (row: SessionRow | null, nb: number | null) => { + if (nb === null || !row) return false; + const createdAt = Number(row.created_at); + return createdAt > 0 && createdAt < nb; + }; + const applyHandlerDepth = (actor: Actor) => { + const handlerDepth = d.handler_depth as number | undefined; + if (!Number.isSafeInteger(handlerDepth) || handlerDepth! <= 0) return; + actor.handlerDepth = handlerDepth; + if (Number.isSafeInteger(d.exp)) actor.handlerExpiresAt = d.exp; + }; + + if (d.type === 'session' || d.type === 'gui') { + const user = await stores.user.getByUuid(d.user_uid); + if (!user) return { invalid: true }; + const sessionUuid = d.session_uid ?? d.uuid; + const rawRow = sessionUuid + ? ((await stores.session.getByUuidAny( + sessionUuid, + )) as SessionRow | null) + : null; + const authId = + d.worker || (rawRow?.kind ?? 'web') !== 'web' + ? undefined + : user.uuid; + const end = ended(rawRow, authId); + if (end) return end; + if (!rawRow) return { invalid: true }; + if (!(await workerStillDeployed(user.id))) return { invalid: true }; + return { + actor: makeActor({ + user: actorUser(user), + session: sessionOf(rawRow), + }), + }; + } + + if (d.type === 'app-under-user') { + const user = await stores.user.getByUuid(d.user_uid); + if (!user) return { invalid: true }; + const app = await stores.app.getByUid(d.app_uid); + if (!app) return { invalid: true }; + const blocked = await originBlock(app); + if (blocked) return { blocked }; + let rawRow: SessionRow | null = null; + if (d.session_uid) { + rawRow = (await stores.session.getByUuidAny( + d.session_uid, + )) as SessionRow | null; + } + const end = ended(rawRow); + if (end) return end; + if (!rawRow) return { invalid: true }; + const nb = notBeforeOf(app); + const createdAt = Number(rawRow.created_at); + if (nb !== null && createdAt > 0 && createdAt < nb) { + return { reauth: { reason: 'session_revoked' } }; + } + if (!(await workerStillDeployed(user.id))) return { invalid: true }; + const actor = makeActor({ + user: actorUser(user), + app: { uid: app.uid, id: app.id }, + session: sessionOf(rawRow), + }); + applyHandlerDepth(actor); + return { actor }; + } + + if (d.type === 'access-token') { + if (!d.token_uid || !d.user_uid) return { invalid: true }; + const user = await stores.user.getByUuid(d.user_uid); + if (!user) return { invalid: true }; + let session: SessionRow | null = null; + let parentSession: SessionRow | null = null; + if (d.session_uid) { + const rawRow = (await stores.session.getByUuidAny( + d.session_uid, + )) as SessionRow | null; + const end = ended(rawRow); + if (end) return end; + if (!rawRow) return { invalid: true }; + if (rawRow.parent_session_id) { + const parent = (await stores.session.getByUuidAny( + rawRow.parent_session_id, + )) as SessionRow | null; + if (!parent) return { reauth: { reason: 'session_revoked' } }; + const parentEnded = ended(parent); + if (parentEnded) return parentEnded; + parentSession = parent; + } + session = rawRow; + } + let authorizer: Actor; + if (d.app_uid) { + const app = await stores.app.getByUid(d.app_uid); + if (!app) return { invalid: true }; + const blocked = await originBlock(app); + if (blocked) return { blocked }; + const nb = notBeforeOf(app); + if (createdBefore(session, nb) || createdBefore(parentSession, nb)) + return { reauth: { reason: 'session_revoked' } }; + authorizer = makeActor({ + user: actorUser(user), + app: { uid: app.uid, id: app.id }, + session: null, + }); + } else { + authorizer = makeActor({ user: actorUser(user), session: null }); + } + let godmodeApp: { uid: string; id: number } | null = null; + if (d.godmode_app_uid !== undefined) { + if (d.app_uid || d.full_access !== true) return { invalid: true }; + const app = await stores.app.getByUid(d.godmode_app_uid); + if (!app || !isGodmodeApp(app)) return { invalid: true }; + godmodeApp = { uid: app.uid, id: app.id }; + } + const actor = makeActor({ + user: actorUser(user), + accessToken: { + uid: d.token_uid, + issuer: authorizer, + authorized: null, + fullAccess: !d.app_uid && d.full_access === true, + ...(godmodeApp ? { godmodeApp } : {}), + }, + }); + applyHandlerDepth(actor); + return { actor }; + } + + return { invalid: true }; +}; + +describe('token auth lookups match a sequential read', () => { + let server: PuterServer; + let authService: AuthService; + + // Row fixtures every matrix below draws from. + let user: UserRow; + const missing = () => uuidv4(); + const sessions: Record = {}; + const apps: Record = {}; + let workerName: string; + let workerUid: string; + + const signAndVerify = (payload: Record) => { + const token = server.services.token.sign('auth', payload); + return { + token, + decoded: server.services.token.verify('auth', token), + }; + }; + + const insertApp = async ( + label: string, + columns: { index_url?: string; godmode?: number; timestamp?: string }, + ) => { + const uid = `app-${uuidv4()}`; + const names = ['uid', 'name', 'title', 'index_url', 'owner_user_id']; + const values: unknown[] = [ + uid, + `n-${uid}`, + `t-${uid}`, + columns.index_url ?? `https://${label}-${uuidv4()}.example.com/`, + user.id, + ]; + if (columns.godmode !== undefined) { + names.push('godmode'); + values.push(columns.godmode); + } + if (columns.timestamp !== undefined) { + names.push('timestamp'); + values.push(columns.timestamp); + } + await server.clients.db.write( + `INSERT INTO \`apps\` (${names.map((n) => `\`${n}\``).join(', ')}) VALUES (${names.map(() => '?').join(', ')})`, + values, + ); + apps[label] = uid; + }; + + beforeAll(async () => { + server = await setupTestServer(); + authService = server.services.auth as unknown as AuthService; + + const username = `tl-${Math.random().toString(36).slice(2, 10)}`; + user = await server.stores.user.create({ + username, + uuid: uuidv4(), + password: null, + email: `${username}@test.local`, + free_storage: 100 * 1024 * 1024, + requires_email_confirmation: false, + }); + + const create = async ( + label: string, + opts: Record = {}, + ) => { + const row = (await server.stores.session.create( + user.id, + opts, + )) as SessionRow; + sessions[label] = row.uuid; + return row; + }; + const past = nowSeconds() - 60; + const future = nowSeconds() + 3600; + await create('web', { kind: 'web', expires_at: future }); + await create('webRevoked', { kind: 'web', expires_at: future }); + await server.stores.session.revokeCascade(sessions.webRevoked); + await create('webExpired', { kind: 'web', expires_at: past }); + await create('app', { kind: 'app', expires_at: future }); + await create('child', { + kind: 'access_token', + parent_session_id: sessions.web, + }); + await create('childOfMissing', { + kind: 'access_token', + parent_session_id: missing(), + }); + await create('childOfRevoked', { + kind: 'access_token', + parent_session_id: sessions.webRevoked, + }); + await create('childOfExpired', { + kind: 'access_token', + parent_session_id: sessions.webExpired, + }); + sessions.missing = missing(); + + await insertApp('plain', {}); + await insertApp('godmode', { godmode: 1 }); + // Created "after" every session above, so its sessions predate it. + await insertApp('recreated', { timestamp: '2099-01-01 00:00:00' }); + const blockedHost = `blocked-${uuidv4()}.example.com`; + await server.clients.db.write( + 'INSERT INTO `blocked_app_origins` (`domain`, `include_subdomains`) VALUES (?, ?)', + [blockedHost, 0], + ); + ( + server.services.appOriginBlocklist as { invalidate: () => void } + ).invalidate(); + await insertApp('blocked', { index_url: `https://${blockedHost}/` }); + apps.missing = `app-${uuidv4()}`; + + workerName = `wk-${Math.random().toString(36).slice(2, 8)}`; + const workerRow = await server.stores.subdomain.create({ + userId: user.id, + subdomain: `${WORKER_SUBDOMAIN_PREFIX}${workerName}`, + appOwner: null, + }); + workerUid = workerRow.uuid; + }); + + afterAll(async () => { + await server?.shutdown(); + }); + + const workerVariants = (): Array> => [ + {}, + { worker: true, worker_name: workerName, worker_uid: workerUid }, + { worker: true, worker_name: workerName, worker_uid: uuidv4() }, + { worker: true, worker_name: workerName }, + { worker: true, worker_name: `gone-${workerName}` }, + { worker: true }, + { worker: true, worker_name: EVENTS_WORKER_SESSION_NAME }, + ]; + + const sessionTokens = (): Array> => { + const out: Array> = []; + for (const type of ['session', 'gui']) { + for (const userUid of [user.uuid, missing()]) { + for (const label of [ + 'web', + 'webRevoked', + 'webExpired', + 'app', + 'missing', + null, + ]) { + const sessionFields = + label === null + ? [{}] + : [ + { session_uid: sessions[label] }, + { uuid: sessions[label] }, + ]; + for (const sessionField of sessionFields) { + for (const worker of workerVariants()) { + out.push({ + type, + version: '2', + user_uid: userUid, + ...sessionField, + ...worker, + }); + } + } + } + } + } + return out; + }; + + const appTokens = (): Array> => { + const out: Array> = []; + for (const userUid of [user.uuid, missing()]) { + for (const appLabel of [ + 'plain', + 'godmode', + 'recreated', + 'blocked', + 'missing', + ]) { + for (const sessionLabel of [ + 'app', + 'webRevoked', + 'webExpired', + 'missing', + null, + ]) { + for (const worker of workerVariants()) { + for (const depth of [{}, { handler_depth: 2 }]) { + out.push({ + type: 'app-under-user', + version: '2', + user_uid: userUid, + app_uid: apps[appLabel], + ...(sessionLabel + ? { session_uid: sessions[sessionLabel] } + : {}), + ...worker, + ...depth, + }); + } + } + } + } + } + return out; + }; + + const accessTokens = (): Array> => { + const out: Array> = [ + { type: 'access-token', version: '2', user_uid: user.uuid }, + { type: 'access-token', version: '2', token_uid: uuidv4() }, + ]; + for (const userUid of [user.uuid, missing()]) { + for (const sessionLabel of [ + null, + 'app', + 'child', + 'childOfMissing', + 'childOfRevoked', + 'childOfExpired', + 'webRevoked', + 'webExpired', + 'missing', + ]) { + for (const appLabel of [ + null, + 'plain', + 'recreated', + 'blocked', + 'missing', + ]) { + for (const fullAccess of [undefined, true, false]) { + for (const godmodeLabel of [ + undefined, + 'godmode', + 'plain', + 'missing', + ]) { + out.push({ + type: 'access-token', + version: '2', + token_uid: uuidv4(), + user_uid: userUid, + ...(sessionLabel + ? { session_uid: sessions[sessionLabel] } + : {}), + ...(appLabel + ? { app_uid: apps[appLabel] } + : {}), + ...(fullAccess === undefined + ? {} + : { full_access: fullAccess }), + ...(godmodeLabel === undefined + ? {} + : { godmode_app_uid: apps[godmodeLabel] }), + }); + } + } + } + } + } + return out; + }; + + const expectParity = async (payloads: Array>) => { + const outcomes = new Set(); + for (const payload of payloads) { + const { token, decoded } = signAndVerify(payload); + const expected = await sequentialAuthenticate(server, decoded); + const actual = await authService.authenticate(token, { + touch: false, + }); + expect(actual, JSON.stringify(payload)).toEqual(expected); + outcomes.add(Object.keys(expected).join(',')); + } + return outcomes; + }; + + it('session and GUI tokens', async () => { + const outcomes = await expectParity(sessionTokens()); + // The matrix reaches every kind of answer. + expect([...outcomes].sort()).toEqual(['actor', 'invalid', 'reauth']); + }); + + it('app-under-user tokens', async () => { + const outcomes = await expectParity(appTokens()); + expect([...outcomes].sort()).toEqual([ + 'actor', + 'blocked', + 'invalid', + 'reauth', + ]); + }); + + it('access tokens', async () => { + const outcomes = await expectParity(accessTokens()); + expect([...outcomes].sort()).toEqual([ + 'actor', + 'blocked', + 'invalid', + 'reauth', + ]); + }); + + // -- Failing lookups --------------------------------------------- + + type LookupName = + | 'user' + | 'session' + | 'parent' + | 'app' + | 'godmodeApp' + | 'worker'; + + const restorers: Array<() => void> = []; + const restoreLookups = () => { + for (const restore of restorers.splice(0).reverse()) restore(); + }; + + /** + * Patched by hand rather than with `vi.spyOn`: a spy subscribes to the + * promise it returns, which would hide a rejection nothing else awaits. + */ + const patch = ( + target: T, + key: K, + impl: T[K], + ) => { + const own = Object.getOwnPropertyDescriptor(target, key); + (target as Record)[key] = impl; + restorers.push(() => { + if (own) Object.defineProperty(target, key, own); + else delete (target as Record)[key]; + }); + }; + + /** + * Fail the named lookups, each after its own delay, so failures land in an + * order unrelated to the order they are checked in. + */ + const failLookups = ( + failing: Set, + ids: { parent?: string; godmodeApp?: string }, + delays: Record, + ) => { + const settle = async ( + name: LookupName, + real: () => Promise, + ): Promise => { + await new Promise((resolve) => setTimeout(resolve, delays[name])); + if (failing.has(name)) throw new Error(`${name} lookup failed`); + return real(); + }; + const { user: userStore, session, app, subdomain } = server.stores; + const getByUuid = userStore.getByUuid.bind(userStore); + const getByUuidAny = session.getByUuidAny.bind(session); + const getByUid = app.getByUid.bind(app); + const getBySubdomain = subdomain.getBySubdomain.bind(subdomain); + patch(userStore, 'getByUuid', (uuid: string) => + settle('user', () => getByUuid(uuid)), + ); + patch(session, 'getByUuidAny', (uuid: string) => + settle(uuid === ids.parent ? 'parent' : 'session', () => + getByUuidAny(uuid), + ), + ); + patch(app, 'getByUid', (uid: string) => + settle(uid === ids.godmodeApp ? 'godmodeApp' : 'app', () => + getByUid(uid), + ), + ); + patch(subdomain, 'getBySubdomain', (name: string) => + settle('worker', () => getBySubdomain(name)), + ); + }; + + const outcomeOf = async (run: () => Promise) => { + try { + return { result: await run() }; + } catch (error) { + return { error: (error as Error).message }; + } + }; + + const subsets = (items: T[]): T[][] => + items.reduce( + (acc, item) => [...acc, ...acc.map((s) => [...s, item])], + [[]], + ); + + const expectSameFirstError = async ( + payload: Record, + names: LookupName[], + ids: { parent?: string; godmodeApp?: string }, + ) => { + const unhandled: unknown[] = []; + const onUnhandled = (reason: unknown) => unhandled.push(reason); + process.on('unhandledRejection', onUnhandled); + try { + const { token, decoded } = signAndVerify(payload); + const forward = Object.fromEntries( + names.map((name, i) => [name, i * 3]), + ) as Record; + const backward = Object.fromEntries( + names.map((name, i) => [name, (names.length - i) * 3]), + ) as Record; + for (const failing of subsets(names)) { + for (const delays of [forward, backward]) { + failLookups(new Set(failing), ids, delays); + const expected = await outcomeOf(() => + sequentialAuthenticate(server, decoded), + ); + const actual = await outcomeOf(() => + authService.authenticate(token, { touch: false }), + ); + restoreLookups(); + expect( + actual, + `${JSON.stringify(payload)} failing=${failing}`, + ).toEqual(expected); + } + } + // Let lookups nobody awaited finish failing. + await new Promise((resolve) => setTimeout(resolve, 40)); + expect(unhandled).toEqual([]); + } finally { + restoreLookups(); + process.off('unhandledRejection', onUnhandled); + } + }; + + it('surfaces the same first error for a session token', async () => { + await expectSameFirstError( + { + type: 'session', + version: '2', + user_uid: user.uuid, + session_uid: sessions.web, + worker: true, + worker_name: workerName, + worker_uid: workerUid, + }, + ['user', 'session', 'worker'], + {}, + ); + }); + + it('surfaces the same first error for an app-under-user token', async () => { + await expectSameFirstError( + { + type: 'app-under-user', + version: '2', + user_uid: user.uuid, + app_uid: apps.plain, + session_uid: sessions.app, + worker: true, + worker_name: workerName, + worker_uid: workerUid, + }, + ['user', 'app', 'session', 'worker'], + {}, + ); + }); + + it('surfaces the same first error for an access token', async () => { + await expectSameFirstError( + { + type: 'access-token', + version: '2', + token_uid: uuidv4(), + user_uid: user.uuid, + session_uid: sessions.child, + app_uid: apps.plain, + }, + ['user', 'session', 'parent', 'app'], + { parent: sessions.web }, + ); + await expectSameFirstError( + { + type: 'access-token', + version: '2', + token_uid: uuidv4(), + user_uid: user.uuid, + session_uid: sessions.child, + full_access: true, + godmode_app_uid: apps.godmode, + }, + ['user', 'session', 'parent', 'godmodeApp'], + { parent: sessions.web, godmodeApp: apps.godmode }, + ); + }); +}); diff --git a/src/backend/services/auth/AuthService.ts b/src/backend/services/auth/AuthService.ts index 7a1d944bd..ddd728fda 100644 --- a/src/backend/services/auth/AuthService.ts +++ b/src/backend/services/auth/AuthService.ts @@ -27,6 +27,7 @@ import { } from '../../core/actor'; import { HttpError } from '../../core/http/HttpError.js'; import { WEB_AND_EXTENSION_PROTOCOLS } from '../../util/validation.js'; +import { awaitLater } from '../../util/concurrency.js'; import { ASSET_WINDOW_SECONDS, GODMODE_TOKEN_MAX_AGE_SECONDS, @@ -759,9 +760,22 @@ export class AuthService extends PuterService { ), ), ]; + // One batched read; a uid it doesn't answer exactly (gone, differently + // cased, or the batch failed) gets its own lookup, as before. + let batched = new Map>(); + try { + batched = await this.stores.app.getByUids(appUids); + } catch { + // Every uid falls back to its own lookup. + } const apps = new Map>(); await Promise.all( appUids.map(async (uid) => { + const hit = batched.get(uid); + if (hit) { + apps.set(uid, hit); + return; + } try { const app = await this.stores.app.getByUid(uid); if (app) apps.set(uid, app); @@ -1684,7 +1698,12 @@ export class AuthService extends PuterService { return result; } - async getUserAppToken(actor: Actor, appUid: string): Promise { + /** `app` is `appUid`'s row when the caller already read it. */ + async getUserAppToken( + actor: Actor, + appUid: string, + app?: { created_epoch?: unknown }, + ): Promise { if (!actor.user) throw new HttpError(403, 'Actor must be a user', { legacyCode: 'forbidden', @@ -1695,7 +1714,7 @@ export class AuthService extends PuterService { // the app row's `last_ip` / `last_user_agent` start NULL and get // populated later via `SessionStore.touch` on the first verified // request that carries those headers. - const app = await this.stores.app.getByUid(appUid); + app ??= await this.stores.app.getByUid(appUid); const appSession = await this.stores.session.getOrCreateApp( actor.user.id, appUid, @@ -2492,19 +2511,28 @@ export class AuthService extends PuterService { decoded: SessionTokenPayload, ctx: AuthenticateContext = {}, ): Promise { - const user = await this.stores.user.getByUuid(decoded.user_uid); - if (!user) return { invalid: true }; - // v2 tokens prefer `session_uid`; v1 only carries `uuid`. Both // store the web-session uuid. const sessionUuid = decoded.session_uid ?? decoded.uuid; - const rawRow = sessionUuid - ? ((await this.stores.session.getByUuidAny( - sessionUuid, - )) as SessionRow | null) + // Read together, awaited in the order the checks below need them. + const userLookup = this.stores.user.getByUuid(decoded.user_uid); + const sessionLookup = sessionUuid + ? awaitLater( + this.stores.session.getByUuidAny( + sessionUuid, + ) as Promise, + ) : null; + const user = await userLookup; + if (!user) return { invalid: true }; + const deployed = awaitLater( + this.#workerStillDeployed(decoded, user.id as number), + ); + + const rawRow = sessionLookup ? await sessionLookup : null; + // Only a browser session gets a reauth token back. A worker credential // rides the same token type, so check both the claim and the row. const auth_id = @@ -2518,9 +2546,7 @@ export class AuthService extends PuterService { const session: SessionRow | null = rawRow; if (!session) return { invalid: true }; - if (!(await this.#workerStillDeployed(decoded, user.id as number))) { - return { invalid: true }; - } + if (!(await deployed)) return { invalid: true }; void this.#touchSession(session.uuid, user.id, ctx); @@ -2531,21 +2557,30 @@ export class AuthService extends PuterService { decoded: AppUnderUserTokenPayload, ctx: AuthenticateContext = {}, ): Promise { - const user = await this.stores.user.getByUuid(decoded.user_uid); - if (!user) return { invalid: true }; + // Read together, awaited in the order the checks below need them. + const userLookup = this.stores.user.getByUuid(decoded.user_uid); + const appLookup = awaitLater(this.stores.app.getByUid(decoded.app_uid)); + const sessionLookup = decoded.session_uid + ? awaitLater( + this.stores.session.getByUuidAny( + decoded.session_uid, + ) as Promise, + ) + : null; - const app = await this.stores.app.getByUid(decoded.app_uid); + const user = await userLookup; + if (!user) return { invalid: true }; + const deployed = awaitLater( + this.#workerStillDeployed(decoded, user.id as number), + ); + + const app = await appLookup; if (!app) return { invalid: true }; const blocked = await this.#appOriginBlock(app); if (blocked) return { blocked }; - let rawRow: SessionRow | null = null; - if (decoded.session_uid) { - rawRow = (await this.stores.session.getByUuidAny( - decoded.session_uid, - )) as SessionRow | null; - } + const rawRow = sessionLookup ? await sessionLookup : null; // An app token never carries `auth_id` on its reauth result: only a // user's own session/GUI token can be reattached via a reauth token. @@ -2565,9 +2600,7 @@ export class AuthService extends PuterService { if (notBefore !== null && createdAt > 0 && createdAt < notBefore) { return { reauth: { reason: 'session_revoked' } }; } - if (!(await this.#workerStillDeployed(decoded, user.id as number))) { - return { invalid: true }; - } + if (!(await deployed)) return { invalid: true }; void this.#touchSession(session.uuid, user.id, ctx); @@ -2616,15 +2649,44 @@ export class AuthService extends PuterService { ): Promise { if (!decoded.token_uid || !decoded.user_uid) return { invalid: true }; - const user = await this.stores.user.getByUuid(decoded.user_uid); + // Read together, awaited in the order the checks below need them. The + // parent is read once its child row names it. + const userLookup = this.stores.user.getByUuid(decoded.user_uid); + const sessionLookup = decoded.session_uid + ? awaitLater( + this.stores.session.getByUuidAny( + decoded.session_uid, + ) as Promise, + ) + : null; + const parentLookup = sessionLookup + ? awaitLater( + (async () => { + const row = await sessionLookup; + if (!row?.parent_session_id) return null; + return (await this.stores.session.getByUuidAny( + row.parent_session_id, + )) as SessionRow | null; + })(), + ) + : null; + const appLookup = decoded.app_uid + ? awaitLater(this.stores.app.getByUid(decoded.app_uid)) + : null; + const godmodeAppLookup = + decoded.godmode_app_uid !== undefined && + !decoded.app_uid && + decoded.full_access === true + ? awaitLater(this.stores.app.getByUid(decoded.godmode_app_uid)) + : null; + + const user = await userLookup; if (!user) return { invalid: true }; let session: SessionRow | null = null; let parentSession: SessionRow | null = null; - if (decoded.session_uid) { - const rawRow = (await this.stores.session.getByUuidAny( - decoded.session_uid, - )) as SessionRow | null; + if (sessionLookup && parentLookup) { + const rawRow = await sessionLookup; // No `auth_id` on an access token's reauth result: a reauth token // is only ever minted for the user's own session/GUI token. const ended = this.#endedSession(rawRow); @@ -2633,9 +2695,7 @@ export class AuthService extends PuterService { // A token minted under another session (an app's, a godmode // token's, the desktop's) lives no longer than that session does. if (rawRow.parent_session_id) { - const parent = (await this.stores.session.getByUuidAny( - rawRow.parent_session_id, - )) as SessionRow | null; + const parent = await parentLookup; if (!parent) return { reauth: { reason: 'session_revoked' } }; const parentEnded = this.#endedSession(parent); if (parentEnded) return parentEnded; @@ -2645,8 +2705,8 @@ export class AuthService extends PuterService { } let authorizer: Actor; - if (decoded.app_uid) { - const app = await this.stores.app.getByUid(decoded.app_uid); + if (appLookup) { + const app = await appLookup; if (!app) return { invalid: true }; const blocked = await this.#appOriginBlock(app); if (blocked) return { blocked }; @@ -2667,10 +2727,8 @@ export class AuthService extends PuterService { // A godmode token stops working once its app is gone or demoted. let godmodeApp: { uid: string; id: number } | null = null; if (decoded.godmode_app_uid !== undefined) { - if (decoded.app_uid || decoded.full_access !== true) { - return { invalid: true }; - } - const app = await this.stores.app.getByUid(decoded.godmode_app_uid); + if (!godmodeAppLookup) return { invalid: true }; + const app = await godmodeAppLookup; if (!app || !isGodmodeApp(app)) return { invalid: true }; godmodeApp = { uid: app.uid, id: app.id }; } diff --git a/src/backend/services/auth/OIDCService.test.ts b/src/backend/services/auth/OIDCService.test.ts index 7ce4272a5..4ca601e9c 100644 --- a/src/backend/services/auth/OIDCService.test.ts +++ b/src/backend/services/auth/OIDCService.test.ts @@ -58,6 +58,10 @@ let jwk: Record; const fetchedUrls: string[] = []; /** Per-test fetch responses, consulted before the built-in discovery stubs. */ const stubbedResponses = new Map Partial>(); +/** Per-test delays before a URL answers, in ms. */ +const fetchDelays = new Map(); +/** Per-test gates a URL waits on before it answers. */ +const fetchGates = new Map>(); beforeAll(async () => { const pair = crypto.generateKeyPairSync('rsa', { modulusLength: 2048 }); @@ -117,6 +121,10 @@ beforeAll(async () => { vi.stubGlobal('fetch', (async (input: unknown) => { const url = String(input); fetchedUrls.push(url); + const delay = fetchDelays.get(url); + if (delay) await new Promise((resolve) => setTimeout(resolve, delay)); + const gate = fetchGates.get(url); + if (gate) await gate; const stubbed = stubbedResponses.get(url); if (stubbed) return stubbed() as Response; if (url === MS_DISCOVERY_URL) { @@ -609,6 +617,198 @@ describe('OIDCService.getProviderConfig', () => { }); }); +describe('OIDCService.getEnabledProviderIds', () => { + const appleKeyPem = () => + crypto + .generateKeyPairSync('ec', { namedCurve: 'P-256' }) + .privateKey.export({ format: 'pem', type: 'pkcs8' }) + .toString(); + + // A cold instance, so discovery and the provider list start uncached. + const fresh = (providers: Record>) => { + const svc = new OIDCServiceClass( + ...([ + { origin: TEST_ORIGIN, oidc: { providers } }, + {}, + {}, + {}, + ] as unknown as ConstructorParameters), + ); + svc.onServerStart(); + return svc; + }; + + const fetchesOf = (from: number, url: string) => + fetchedUrls.slice(from).filter((u) => u === url).length; + + it('shares one fetch per discovery document across concurrent calls, in config order', async () => { + const svc = fresh({ + microsoft: { client_id: 'm', client_secret: 's' }, + google: { client_id: 'g', client_secret: 's' }, + custom: { + client_id: 'c', + client_secret: 's', + authorization_endpoint: 'https://idp.example/authorize', + token_endpoint: CUSTOM_TOKEN, + userinfo_endpoint: CUSTOM_USERINFO, + }, + }); + const before = fetchedUrls.length; + // Microsoft answers last. + fetchDelays.set(MS_DISCOVERY_URL, 30); + fetchDelays.set(GOOGLE_DISCOVERY_URL, 5); + try { + const [first, second] = await Promise.all([ + svc.getEnabledProviderIds(), + svc.getEnabledProviderIds(), + ]); + expect(first).toEqual(['microsoft', 'google', 'custom']); + expect(second).toEqual(first); + } finally { + fetchDelays.clear(); + } + expect(fetchesOf(before, MS_DISCOVERY_URL)).toBe(1); + expect(fetchesOf(before, GOOGLE_DISCOVERY_URL)).toBe(1); + }); + + it('answers from memory once every discovery has succeeded', async () => { + const svc = fresh({ + google: { client_id: 'g', client_secret: 's' }, + apple: { + client_id: 'a', + team_id: 'T', + key_id: 'K', + private_key: appleKeyPem(), + }, + nameless: { client_secret: 'x' }, + }); + expect(await svc.getEnabledProviderIds()).toEqual(['google', 'apple']); + + const before = fetchedUrls.length; + const sign = vi.spyOn(crypto, 'sign'); + try { + const again = await svc.getEnabledProviderIds(); + expect(again).toEqual(['google', 'apple']); + // A caller editing its copy leaves the next answer alone. + again.pop(); + expect(await svc.getEnabledProviderIds()).toEqual([ + 'google', + 'apple', + ]); + expect(sign).not.toHaveBeenCalled(); + } finally { + sign.mockRestore(); + } + expect(fetchedUrls.length).toBe(before); + }); + + it('asks again for a discovery document that failed', async () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + const svc = fresh({ + google: { client_id: 'g', client_secret: 's' }, + custom: { + client_id: 'c', + client_secret: 's', + authorization_endpoint: 'https://idp.example/authorize', + token_endpoint: CUSTOM_TOKEN, + userinfo_endpoint: CUSTOM_USERINFO, + }, + }); + try { + stubbedResponses.set(GOOGLE_DISCOVERY_URL, () => ({ ok: false })); + expect(await svc.getEnabledProviderIds()).toEqual(['custom']); + stubbedResponses.set(GOOGLE_DISCOVERY_URL, () => { + throw new Error('network down'); + }); + expect(await svc.getEnabledProviderIds()).toEqual(['custom']); + stubbedResponses.delete(GOOGLE_DISCOVERY_URL); + expect(await svc.getEnabledProviderIds()).toEqual([ + 'google', + 'custom', + ]); + } finally { + stubbedResponses.delete(GOOGLE_DISCOVERY_URL); + warn.mockRestore(); + } + }); + + it('keeps no list from a call that joined a discovery fetch which then failed', async () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + const providers = { + google: { client_id: 'g', client_secret: 's' }, + custom: { + client_id: 'c', + client_secret: 's', + authorization_endpoint: 'https://idp.example/authorize', + token_endpoint: CUSTOM_TOKEN, + userinfo_endpoint: CUSTOM_USERINFO, + }, + }; + try { + // A second call lands a few microtasks after the failing fetch + // answers; every landing point has to leave the list unkept. + for (let ticks = 0; ticks < 8; ticks++) { + const svc = fresh(providers); + let open!: () => void; + const gate = new Promise((resolve) => { + open = resolve; + }); + fetchGates.set(GOOGLE_DISCOVERY_URL, gate); + stubbedResponses.set(GOOGLE_DISCOVERY_URL, () => ({ + ok: false, + })); + const first = svc.getEnabledProviderIds(); + const second = (async () => { + await gate; + for (let i = 0; i < ticks; i++) await null; + return svc.getEnabledProviderIds(); + })(); + open(); + expect(await first).toEqual(['custom']); + expect(await second).toEqual(['custom']); + + fetchGates.delete(GOOGLE_DISCOVERY_URL); + stubbedResponses.delete(GOOGLE_DISCOVERY_URL); + expect( + await svc.getEnabledProviderIds(), + `ticks=${ticks}`, + ).toEqual(['google', 'custom']); + } + } finally { + fetchGates.delete(GOOGLE_DISCOVERY_URL); + stubbedResponses.delete(GOOGLE_DISCOVERY_URL); + warn.mockRestore(); + } + }); + + it('keeps failing for an Apple key that does not parse', async () => { + const svc = fresh({ + google: { client_id: 'g', client_secret: 's' }, + apple: { + client_id: 'a', + team_id: 'T', + key_id: 'K', + private_key: 'not a key', + }, + }); + const parse = vi.spyOn(crypto, 'createPrivateKey'); + try { + let first: unknown; + try { + await svc.getEnabledProviderIds(); + } catch (error) { + first = error; + } + expect(first).toBeInstanceOf(Error); + await expect(svc.getEnabledProviderIds()).rejects.toBe(first); + await expect(svc.getProviderConfig('apple')).rejects.toBe(first); + expect(parse).toHaveBeenCalledTimes(1); + } finally { + parse.mockRestore(); + } + }); +}); + describe('OIDCService — authorization URLs', () => { it('builds the callback URL for each supported flow and rejects others', () => { for (const flow of ['login', 'signup', 'revalidate']) { diff --git a/src/backend/services/auth/OIDCService.ts b/src/backend/services/auth/OIDCService.ts index c991d1921..8ab479f35 100644 --- a/src/backend/services/auth/OIDCService.ts +++ b/src/backend/services/auth/OIDCService.ts @@ -91,6 +91,14 @@ export class OIDCService extends PuterService { declare protected services: LayerInstances; #discoveryCache: Map> = new Map(); + #discoveryInFlight: Map | null>> = + new Map(); + #discoveryMisses = 0; + #enabledProviderIds: string[] | null = null; + #applePrivateKey: + | { pem: string; key: crypto.KeyObject } + | { pem: string; error: unknown } + | null = null; #jwksCache: Map = new Map(); #providers: Record> = {}; @@ -199,12 +207,23 @@ export class OIDCService extends PuterService { } async getEnabledProviderIds(): Promise { - const ids: string[] = []; - for (const id of Object.keys(this.#providers)) { - const cfg = await this.getProviderConfig(id); - if (cfg) ids.push(id); + if (this.#enabledProviderIds) return [...this.#enabledProviderIds]; + const ids = Object.keys(this.#providers); + const missesBefore = this.#discoveryMisses; + const configs = await Promise.allSettled( + ids.map((id) => this.getProviderConfig(id)), + ); + const enabled: string[] = []; + for (const [i, result] of configs.entries()) { + if (result.status === 'rejected') throw result.reason; + if (result.value) enabled.push(ids[i]); } - return ids; + // Provider config is fixed after start, so only a failed discovery + // fetch (retried next time) can change the answer. + if (this.#discoveryMisses === missesBefore) { + this.#enabledProviderIds = enabled; + } + return [...enabled]; } // -- Auth URL ---------------------------------------------------- @@ -674,12 +693,33 @@ export class OIDCService extends PuterService { // -- Internals --------------------------------------------------- + /** Cached once fetched; concurrent callers share one fetch. */ async #fetchDiscovery( url: string, label: string, ): Promise | null> { const cached = this.#discoveryCache.get(url); if (cached) return cached; + const pending = this.#discoveryInFlight.get(url); + if (pending) return pending; + const load = this.#loadDiscovery(url, label); + this.#discoveryInFlight.set(url, load); + let data: Record | null = null; + try { + data = await load; + return data; + } finally { + // In one step: a caller that can no longer join this fetch has to + // see its miss. + this.#discoveryInFlight.delete(url); + if (!data) this.#discoveryMisses++; + } + } + + async #loadDiscovery( + url: string, + label: string, + ): Promise | null> { try { const res = await fetch(url); if (!res.ok) return null; @@ -692,6 +732,24 @@ export class OIDCService extends PuterService { } } + /** The configured key, parsed once; a key that fails to parse keeps failing. */ + #appleKey(privateKey: string): crypto.KeyObject { + if (this.#applePrivateKey?.pem !== privateKey) { + try { + this.#applePrivateKey = { + pem: privateKey, + key: crypto.createPrivateKey(privateKey), + }; + } catch (error) { + this.#applePrivateKey = { pem: privateKey, error }; + } + } + if ('error' in this.#applePrivateKey) { + throw this.#applePrivateKey.error; + } + return this.#applePrivateKey.key; + } + #generateAppleClientSecret( teamId: string, clientId: string, @@ -716,7 +774,7 @@ export class OIDCService extends PuterService { ); const signingInput = `${headerB64}.${payloadB64}`; - const key = crypto.createPrivateKey(privateKey); + const key = this.#appleKey(privateKey); const signature = crypto.sign('sha256', Buffer.from(signingInput), { key, dsaEncoding: 'ieee-p1363', diff --git a/src/backend/services/broadcast/BroadcastService.test.ts b/src/backend/services/broadcast/BroadcastService.test.ts index 4e6c9df91..1fd53dfa4 100644 --- a/src/backend/services/broadcast/BroadcastService.test.ts +++ b/src/backend/services/broadcast/BroadcastService.test.ts @@ -646,18 +646,26 @@ describe('BroadcastService outbound body budget', () => { ids: string[]; bytes: number; delivered: boolean; + nonce: number; } let server: PuterServer; let warn: ReturnType; let downPeerAnswers = true; + /** While set, the down peer holds each request until it settles, then fails. */ + let downPeerHang: Promise | null = null; const posts: Post[] = []; - const answerAsPeer = async (request: { url: string; data: string }) => { + const answerAsPeer = async (request: { + url: string; + data: string; + headers: Record; + }) => { const peer: PeerName = request.url.includes('peer-down') ? 'down' : 'up'; const bytes = Buffer.byteLength(request.data); + const nonce = Number(request.headers['X-Broadcast-Nonce']); const ids = ( JSON.parse(request.data) as { events: { key: string; data: { id: string } }[]; @@ -665,12 +673,17 @@ describe('BroadcastService outbound body budget', () => { ).events .filter((event) => event.key === EVENT_KEY) .map((event) => event.data.id); + if (peer === 'down' && downPeerHang) { + await downPeerHang; + posts.push({ peer, ids, bytes, delivered: false, nonce }); + throw new Error('timeout of 15000ms exceeded'); + } if (peer === 'down' && !downPeerAnswers) { - posts.push({ peer, ids, bytes, delivered: false }); + posts.push({ peer, ids, bytes, delivered: false, nonce }); throw new Error('connect ECONNREFUSED'); } const delivered = bytes <= PEER_BODY_LIMIT; - posts.push({ peer, ids, bytes, delivered }); + posts.push({ peer, ids, bytes, delivered, nonce }); return delivered ? { status: 200, statusText: 'OK', data: 'ok' } : { status: 413, statusText: 'Payload Too Large', data: '' }; @@ -723,6 +736,8 @@ describe('BroadcastService outbound body budget', () => { outbound_flush_ms: 25, outboundMaxBodyBytes: MAX_BODY_BYTES, }, + // Peers are reached over https, which is where the agent applies. + protocol: 'https', } as never); }); @@ -734,6 +749,7 @@ describe('BroadcastService outbound body budget', () => { beforeEach(() => { posts.length = 0; downPeerAnswers = true; + downPeerHang = null; axiosRequestMock.mockReset(); axiosRequestMock.mockImplementation(answerAsPeer); }); @@ -780,6 +796,64 @@ describe('BroadcastService outbound body budget', () => { expect(backlogBytes).toBeLessThanOrEqual(4 * MAX_BODY_BYTES); }); + it('keeps delivering to healthy peers while one hangs, then catches it up in order', async () => { + let release!: () => void; + downPeerHang = new Promise((resolve) => { + release = resolve; + }); + try { + const first = emit(['hang-0', 'hang-1']); + // Answered while the hung peer still holds its request. + await waitForFlush(() => deliveredIds('up').includes('hang-1')); + expect(attempted('down', 'hang-0')).toBe(false); + + // Queued behind the flush the hung peer is holding open. + const second = emit(['hang-2']); + downPeerHang = null; + release(); + await waitForFlush(() => deliveredIds('down').includes('hang-2')); + + const all = [...first, ...second]; + expect(deliveredIds('down')).toEqual(all); + expect(deliveredIds('up')).toEqual(all); + // Each peer draws on its own nonce counter, in its own send order. + for (const peer of ['down', 'up'] as const) { + const nonces = posts + .filter((post) => post.peer === peer) + .map((post) => post.nonce); + expect(nonces).toEqual([...nonces].sort((a, b) => a - b)); + expect(new Set(nonces).size).toBe(nonces.length); + } + } finally { + release(); + } + }); + + it('sends every webhook over its own keep-alive agent, certs unchecked as before', async () => { + emit(['agent-check']); + await waitForFlush(() => deliveredIds('up').includes('agent-check')); + + const requests = axiosRequestMock.mock.calls.map( + (call) => + call[0] as { + url: string; + httpsAgent?: { + keepAlive?: boolean; + maxSockets?: number; + options?: { rejectUnauthorized?: boolean }; + }; + }, + ); + expect(requests.length).toBeGreaterThan(0); + for (const request of requests) { + expect(request.url.startsWith('https:')).toBe(true); + expect(request.httpsAgent?.keepAlive).toBe(true); + expect(request.httpsAgent?.options?.rejectUnauthorized).toBe(false); + // Not the addressed pool: a flush never waits behind its cap. + expect(request.httpsAgent?.maxSockets).toBe(Infinity); + } + }); + it('drops an event too large for any body and delivers the rest', async () => { server.clients.event.emit( EVENT_KEY as never, diff --git a/src/backend/services/broadcast/BroadcastService.ts b/src/backend/services/broadcast/BroadcastService.ts index 57f2095aa..c0cb5a318 100644 --- a/src/backend/services/broadcast/BroadcastService.ts +++ b/src/backend/services/broadcast/BroadcastService.ts @@ -65,7 +65,7 @@ interface IncomingHeaders { /** Connections one peer may hold open, and how long an idle one survives. */ const ADDRESSED_MAX_SOCKETS_PER_PEER = 16; const ADDRESSED_MAX_FREE_SOCKETS_PER_PEER = 4; -const ADDRESSED_SOCKET_IDLE_MS = 30_000; +const PEER_SOCKET_IDLE_MS = 30_000; const ADDRESSED_REQUEST_TIMEOUT_MS = 5_000; /** What a deployment with no configured identity calls itself. */ @@ -132,8 +132,16 @@ export class BroadcastService extends PuterService { OUTBOUND_BACKLOG_BODIES * DEFAULT_OUTBOUND_MAX_BODY_BYTES; #webhookProtocol: 'http' | 'https' = 'https'; #webhookHostHeader: string | null = null; - /** Self-signed certs are common between Puter nodes — accept them. */ - #webhookHttpsAgent = new HttpsAgent({ rejectUnauthorized: false }); + /** + * Self-signed certs are common between Puter nodes — accept them. Kept + * alive across flushes, and a pool of its own so a flush never waits for a + * socket behind addressed sends at their cap. + */ + #webhookHttpsAgent = new HttpsAgent({ + rejectUnauthorized: false, + keepAlive: true, + timeout: PEER_SOCKET_IDLE_MS, + }); /** * The agent addressed sends use. Separate because they run at event rates, * where a fresh handshake per request outweighs the payload; the pool is @@ -144,7 +152,7 @@ export class BroadcastService extends PuterService { keepAlive: true, maxSockets: ADDRESSED_MAX_SOCKETS_PER_PEER, maxFreeSockets: ADDRESSED_MAX_FREE_SOCKETS_PER_PEER, - timeout: ADDRESSED_SOCKET_IDLE_MS, + timeout: PEER_SOCKET_IDLE_MS, }); #redisSub: ReturnType | null = null; @@ -467,27 +475,34 @@ export class BroadcastService extends PuterService { const fresh = this.#outboundQueue; this.#outboundQueue = new Set(); - for (const peer of this.#webhookPeers) { - const backlog = this.#outboundBacklogs.get(peer) ?? []; - // A fresh copy of a backlogged event keeps its later place. - const pending = [ - ...backlog.filter((event) => !fresh.has(event)), - ...fresh, - ]; - const undelivered = await this.#sendWebhookToPeer( - peer, - pending, - ); - // A lost flat-perm invalidation has no TTL to heal it. - if (undelivered.length > 0) { - this.#outboundBacklogs.set( + // Peers at once, each still in order: a slow or hung peer must not + // hold up the others' invalidations. Settled, so the flush lock + // stays held until every peer is done. + const flushed = await Promise.allSettled( + this.#webhookPeers.map(async (peer) => { + const backlog = this.#outboundBacklogs.get(peer) ?? []; + // A fresh copy of a backlogged event keeps its later place. + const pending = [ + ...backlog.filter((event) => !fresh.has(event)), + ...fresh, + ]; + const undelivered = await this.#sendWebhookToPeer( peer, - this.#boundBacklog(peer, undelivered), + pending, ); - } else { - this.#outboundBacklogs.delete(peer); - } - } + // A lost flat-perm invalidation has no TTL to heal it. + if (undelivered.length > 0) { + this.#outboundBacklogs.set( + peer, + this.#boundBacklog(peer, undelivered), + ); + } else { + this.#outboundBacklogs.delete(peer); + } + }), + ); + for (const result of flushed) + if (result.status === 'rejected') throw result.reason; } finally { this.#outboundIsFlushing = false; // Anything that arrived during flush, or is still owed to a peer, diff --git a/src/backend/services/events/EventsService.test.ts b/src/backend/services/events/EventsService.test.ts index 51ac70e57..70627657a 100644 --- a/src/backend/services/events/EventsService.test.ts +++ b/src/backend/services/events/EventsService.test.ts @@ -1089,6 +1089,76 @@ describe('cold-region rebuild concurrency', () => { }); }); +describe('a cold process cache', () => { + /** A watcher made on another process, so this one's cache stays cold. */ + const addRemoteWatcher = (documents: FSEntry) => + store.add({ + subId: `remote-sub-${seq}`, + socketId: 'remote-socket', + holderUserId: userId, + ownerUserId: userId, + subject: `fs:${documents.uid}`, + token: `f#${documents.uid}`, + anchorUid: documents.uid, + anchorPath: documents.path, + match: null, + op: null, + appUid: null, + permission: 'list', + }); + + it('answers a warm region in one pipeline, without consulting the table', async () => { + const { file } = seedTree(); + await store.markRegionWarm(userId); + const warm = vi.spyOn(durableSubscriptionStore, 'warmRegion'); + commands = []; + try { + await expect(dispatch(file)).resolves.toBe(false); + expect(commands).toEqual(['pipeline']); + expect(warm).not.toHaveBeenCalled(); + } finally { + warm.mockRestore(); + } + }); + + it('sees a watcher in that same pipeline', async () => { + vi.useFakeTimers(); + const { documents, file } = seedTree(); + await addRemoteWatcher(documents); + await store.markRegionWarm(userId); + const warm = vi.spyOn(durableSubscriptionStore, 'warmRegion'); + try { + await dispatch(file); + await vi.advanceTimersByTimeAsync(EVENTS_COALESCE_WINDOW_MS + 1); + expect(sent).toHaveLength(1); + expect(warm).not.toHaveBeenCalled(); + } finally { + warm.mockRestore(); + } + }); + + it('rebuilds a cold region, then answers from what the rebuild found', async () => { + vi.useFakeTimers(); + const { documents, file } = seedTree(); + const warm = vi + .spyOn(durableSubscriptionStore, 'warmRegion') + .mockImplementation(async (ownerUserId: number) => { + // A rebuild that finds rows the first look did not. + await addRemoteWatcher(documents); + await store.markRegionWarm(ownerUserId); + return true; + }); + try { + await dispatch(file); + await vi.advanceTimersByTimeAsync(EVENTS_COALESCE_WINDOW_MS + 1); + expect(warm).toHaveBeenCalledTimes(1); + expect(sent).toHaveLength(1); + } finally { + warm.mockRestore(); + } + }); +}); + // -- Matching -------------------------------------------------------- describe('matching', () => { diff --git a/src/backend/services/events/EventsService.ts b/src/backend/services/events/EventsService.ts index b37eb1085..6975519f1 100644 --- a/src/backend/services/events/EventsService.ts +++ b/src/backend/services/events/EventsService.ts @@ -102,6 +102,7 @@ import { KV_GLOBAL_APP_KEY, parseKvNamespace, } from '../../stores/systemKv/SystemKVStore.js'; +import { awaitLater } from '../../util/concurrency.js'; import { openCursor, sealCursor, @@ -2197,9 +2198,9 @@ export class EventsService extends PuterService { ): Promise { await this.#spendHandlerBudget(actor); const appUid = await this.#handlerApp(actor, request?.appUid); - const before = await this.stores.eventHandler.countForApp(appUid); - const result = await this.#publishOne(appUid, request); - await this.#maybeAnnounceWorkerCreate(appUid, before); + const before = awaitLater(this.stores.eventHandler.countForApp(appUid)); + const result = await this.#publishOne(appUid, request, before); + await this.#maybeAnnounceWorkerCreate(appUid, await before); return result; } @@ -2227,19 +2228,24 @@ export class EventsService extends PuterService { 'bad_request', ); - const before = await this.stores.eventHandler.countForApp(appUid); + const before = awaitLater(this.stores.eventHandler.countForApp(appUid)); const published: PublishedHandlerView[] = []; try { for (const item of handlers) { - if (!item || typeof item !== 'object' || Array.isArray(item)) + if (!item || typeof item !== 'object' || Array.isArray(item)) { + // The count is read ahead of every item, so its failure + // is the one to surface. + await before; throw badRequest( 'each handler must be an object', 'bad_request', ); + } published.push( await this.#publishOne( appUid, item as PublishHandlerRequest, + before, ), ); } @@ -2247,7 +2253,7 @@ export class EventsService extends PuterService { // A refused item stops the pass but leaves the ones before it // published, and those are what stood the worker up. if (published.length > 0) - await this.#maybeAnnounceWorkerCreate(appUid, before); + await this.#maybeAnnounceWorkerCreate(appUid, await before); } return published; } @@ -3133,14 +3139,23 @@ export class EventsService extends PuterService { if (!ok) throw tooManyCalls(); } - /** One publish, and whatever it releases. */ + /** + * One publish, and whatever it releases. `before` is the request's handler + * count, read alongside this publish's own reads and settled before it + * writes. + */ async #publishOne( appUid: string, item: PublishHandlerRequest, + before: Promise, ): Promise { const name = String(item?.name ?? ''); const source = typeof item?.source === 'string' ? item.source : ''; - await this.#assertHandlerSetSize(appUid, name, source); + const sizeChecked = awaitLater( + this.#assertHandlerSetSize(appUid, name, source), + ); + await before; + await sizeChecked; const { handler, outcome } = await this.stores.eventHandler.publish({ appUid, @@ -6098,7 +6113,7 @@ export class EventsService extends PuterService { /** * Whether this user has anything subscribed at all. Warm, a `Map` read; - * cold, one `EXISTS` — behind, at most once per warm window, the table read + * cold, one pipeline — behind, at most once per warm window, the table read * that teaches this region about durable rows it has never seen. Without * that, an empty watched set is ambiguous: it means "nobody is listening" * in a region that has looked, and nothing at all in one that has not. @@ -6130,9 +6145,12 @@ export class EventsService extends PuterService { async #lookUpHasAny(userId: number, epoch: number): Promise { try { - await this.stores.durableSubscription.warmRegion(userId); - const hasAny = - await this.stores.eventSubscription.userHasAny(userId); + let hasAny = + await this.stores.eventSubscription.userHasAnyIfWarm(userId); + if (hasAny === null) { + await this.stores.durableSubscription.warmRegion(userId); + hasAny = await this.stores.eventSubscription.userHasAny(userId); + } this.#cache.write(userId, epoch, hasAny); return hasAny; } catch { diff --git a/src/backend/services/events/handlers.integration.test.ts b/src/backend/services/events/handlers.integration.test.ts index 8af56cef6..49adfbcc6 100644 --- a/src/backend/services/events/handlers.integration.test.ts +++ b/src/backend/services/events/handlers.integration.test.ts @@ -391,6 +391,233 @@ describe('publishing a set', () => { }); }); +describe('what a publish reads', () => { + const COUNT_SQL = 'SELECT COUNT(*) AS `total` FROM `event_handlers`'; + const BYTES_SQL = 'SUM(OCTET_LENGTH(`source`))'; + const BY_NAME_SQL = + 'FROM `event_handlers` WHERE `app_uid` = ? AND `name` = ?'; + const THIRD_SOURCE = 'async ({ event }) => { console.log(event.op); }'; + + const sleep = (ms: number) => + new Promise((resolve) => setTimeout(resolve, ms)); + + /** Every primary read goes through `on`, which decides when it runs. */ + const watchReads = ( + on: (sql: string, run: () => Promise) => Promise, + ) => { + const db = env.server.clients.db; + const realPread = db.pread.bind(db); + return vi + .spyOn(db, 'pread') + .mockImplementation( + async (sql, ...rest) => + (await on(String(sql), () => + realPread(sql, ...rest), + )) as never, + ); + }; + + it('counts alongside the size check', async () => { + await seedHandler(); + const seen: string[] = []; + let release!: () => void; + const gate = new Promise((resolve) => { + release = resolve; + }); + let gated = false; + const pread = watchReads(async (sql, run) => { + if (sql.includes(BY_NAME_SQL)) seen.push('byName'); + if (sql.includes(BYTES_SQL)) seen.push('bytes'); + if (sql.startsWith(COUNT_SQL) && !gated) { + gated = true; + seen.push('count'); + await gate; + } + return run(); + }); + try { + const pending = publish(appToken, { + name: 'ingestUpload', + source: NEXT_SOURCE, + ifHash: hashContent(SOURCE), + }); + await vi.waitFor(() => + expect(seen).toEqual( + expect.arrayContaining(['count', 'bytes', 'byName']), + ), + ); + release(); + const updated = await pending; + expect(updated.status).toBe(200); + expect(updated.body).toMatchObject({ + name: 'ingestUpload', + hash: hashContent(NEXT_SOURCE), + outcome: 'updated', + }); + } finally { + release(); + pread.mockRestore(); + } + }); + + // A concurrent publish of the same source is the idempotent republish two + // build steps make, and it reads as unchanged, not as a conflict. + it.each([ + { what: 'create', seeded: false, ifHash: undefined }, + { what: 'update', seeded: true, ifHash: hashContent(SOURCE) }, + ])( + 'reads an identical $what that lands after the size check as unchanged', + async ({ seeded, ifHash }) => { + if (seeded) await seedHandler(); + let landed = false; + const pread = watchReads(async (sql, run) => { + const rows = await run(); + if (sql.includes(BY_NAME_SQL) && !landed) { + landed = true; + await env.server.stores.eventHandler.publish({ + appUid, + name: 'ingestUpload', + source: NEXT_SOURCE, + replace: true, + }); + } + return rows; + }); + try { + const republished = await publish(appToken, { + name: 'ingestUpload', + source: NEXT_SOURCE, + ...(ifHash ? { ifHash } : {}), + }); + expect(republished.status).toBe(200); + expect(republished.body).toMatchObject({ + hash: hashContent(NEXT_SOURCE), + outcome: 'unchanged', + }); + } finally { + pread.mockRestore(); + } + }, + ); + + it('refuses an update whose base moved after it was read', async () => { + await seedHandler(); + let moved = false; + const pread = watchReads(async (sql, run) => { + const rows = await run(); + if (sql.includes(BY_NAME_SQL) && !moved) { + moved = true; + // Another build step lands between this read and the write. + await env.server.clients.db.write( + 'UPDATE `event_handlers` SET `source` = ?, `source_hash` = ? ' + + 'WHERE `app_uid` = ? AND `name` = ?', + [ + THIRD_SOURCE, + hashContent(THIRD_SOURCE), + appUid, + 'ingestUpload', + ], + ); + } + return rows; + }); + try { + const refused = await publish(appToken, { + name: 'ingestUpload', + source: NEXT_SOURCE, + ifHash: hashContent(SOURCE), + }); + expect(refused.status).toBe(409); + expect(refused.body.code).toBe('events_handler_conflict'); + } finally { + pread.mockRestore(); + } + const standing = await env.server.stores.eventHandler.getByName( + appUid, + 'ingestUpload', + ); + expect(standing?.sourceHash).toBe(hashContent(THIRD_SOURCE)); + }); + + it('refuses a create another publish beat to the name', async () => { + let beaten = false; + const pread = watchReads(async (sql, run) => { + const rows = await run(); + if (sql.includes(BY_NAME_SQL) && !beaten) { + beaten = true; + await env.server.stores.eventHandler.publish({ + appUid, + name: 'ingestUpload', + source: THIRD_SOURCE, + }); + } + return rows; + }); + try { + const refused = await publish(appToken, { + name: 'ingestUpload', + source: NEXT_SOURCE, + }); + expect(refused.status).toBe(409); + expect(refused.body.code).toBe('events_handler_conflict'); + } finally { + pread.mockRestore(); + } + }); + + it('surfaces a failed count over a failed size check, with no unhandled rejection', async () => { + const { actor } = await env.server.services.auth.authenticate(appToken); + const unhandled = vi.fn(); + process.on('unhandledRejection', unhandled); + const pread = watchReads(async (sql, run) => { + if (sql.startsWith(COUNT_SQL)) { + await sleep(10); + throw new Error('count failed'); + } + if (sql.includes(BYTES_SQL) || sql.includes(BY_NAME_SQL)) + throw new Error('size check failed'); + return run(); + }); + try { + await expect( + events().publishHandler(actor!, { + name: 'ingestUpload', + source: SOURCE, + }), + ).rejects.toThrow('count failed'); + await expect( + events().publishHandlers(actor!, { + handlers: [{ name: 'ingestUpload', source: SOURCE }], + }), + ).rejects.toThrow('count failed'); + await expect( + events().publishHandlers(actor!, { + handlers: ['not an object' as never], + }), + ).rejects.toThrow('count failed'); + await sleep(20); + expect(unhandled).not.toHaveBeenCalled(); + } finally { + process.off('unhandledRejection', unhandled); + pread.mockRestore(); + } + await expect( + env.server.stores.eventHandler.countForApp(appUid), + ).resolves.toBe(0); + }); + + it('still refuses a malformed item once the count has been read', async () => { + const refused = await call( + 'POST', + '/events/handlers/publishAll', + appToken, + { handlers: ['not an object'] }, + ); + expect(refused.status).toBe(400); + expect(refused.body.code).toBe('bad_request'); + }); +}); + describe('the total-source cap', () => { it( 'refuses a publish that would push the app past the size cap', diff --git a/src/backend/services/fs/FSService.test.ts b/src/backend/services/fs/FSService.test.ts index 79a31de08..def4cc747 100644 --- a/src/backend/services/fs/FSService.test.ts +++ b/src/backend/services/fs/FSService.test.ts @@ -3610,6 +3610,136 @@ describe('FSService mkdir, touch, rename and shortcuts', () => { } }); + describe('parent and target lookups', () => { + const store = () => server.stores.fsEntry; + const realGetEntryByPath = () => + (Object.getPrototypeOf(store()) as ReturnType) + .getEntryByPath; + + it('looks the target up while the parent lookup is still in flight', async () => { + const parentPath = `${user.home}/Documents`; + const path = `${parentPath}/alongside`; + const original = realGetEntryByPath(); + let release!: () => void; + const gate = new Promise((resolve) => { + release = resolve; + }); + const looked: string[] = []; + const spy = vi + .spyOn(store(), 'getEntryByPath') + .mockImplementation(async (candidate, options) => { + looked.push(candidate); + if (candidate === parentPath) await gate; + return original.call(store(), candidate, options); + }); + try { + const pending = fs.mkdir(user.userId, { path }); + await vi.waitFor(() => + expect(looked).toEqual( + expect.arrayContaining([parentPath, path]), + ), + ); + release(); + const created = await pending; + expect(created.path).toBe(path); + expect(created.isDir).toBe(true); + } finally { + release(); + spy.mockRestore(); + } + }); + + it('surfaces the parent error ahead of a failed target lookup, leaving nothing unhandled', async () => { + const path = `${user.home}/Documents/nowhere/target`; + const original = realGetEntryByPath(); + const spy = vi + .spyOn(store(), 'getEntryByPath') + .mockImplementation(async (candidate, options) => { + if (candidate === path) throw new Error('replica down'); + return original.call(store(), candidate, options); + }); + try { + const error = await caught(() => + fs.mkdir(user.userId, { path }), + ); + expect(error.statusCode).toBe(404); + expect(error.legacyCode).toBe('not_found'); + + // With the parent in place, the target's own failure surfaces. + await fs.mkdir(user.userId, { + path: `${user.home}/Documents/nowhere`, + }); + await expect(fs.mkdir(user.userId, { path })).rejects.toThrow( + 'replica down', + ); + } finally { + spy.mockRestore(); + } + }); + + it('writes and emits nothing when the parent is missing or a file', async () => { + await writeFile(user, `${user.home}/Documents/a-file`, 'x'); + const cases = [ + [`${user.home}/Documents/absent/target`, 404], + [`${user.home}/Documents/a-file/target`, 409], + ] as const; + for (const [path, status] of cases) { + const lookups = vi.spyOn(store(), 'getEntryByPath'); + const writes = vi.spyOn(server.clients.db, 'write'); + const emits = vi.spyOn(server.clients.event, 'emit'); + try { + const error = await caught(() => + fs.mkdir(user.userId, { path }), + ); + expect(error.statusCode).toBe(status); + // The target was looked up, and that lookup changed nothing. + expect(lookups).toHaveBeenCalledWith(path); + expect( + writes.mock.calls.filter(([sql]) => + /fsentries/i.test(String(sql)), + ), + ).toEqual([]); + expect( + emits.mock.calls.filter(([key]) => + String(key).startsWith('fs.'), + ), + ).toEqual([]); + } finally { + lookups.mockRestore(); + writes.mockRestore(); + emits.mockRestore(); + } + await expect(store().getEntryByPath(path)).resolves.toBeNull(); + } + }); + + it('refuses a root path before looking anything up', async () => { + const spy = vi.spyOn(store(), 'getEntryByPath'); + try { + const error = await caught(() => + fs.mkdir(user.userId, { path: '/toplevel' }), + ); + expect(error.statusCode).toBe(400); + expect(spy).not.toHaveBeenCalled(); + } finally { + spy.mockRestore(); + } + }); + + it('creates missing parents and then the target', async () => { + const path = `${user.home}/Documents/fresh-parent/fresh-child`; + const created = await fs.mkdir(user.userId, { + path, + createMissingParents: true, + }); + expect(created.path).toBe(path); + const parent = await store().getEntryByPath( + `${user.home}/Documents/fresh-parent`, + ); + expect(created.parentUid).toBe(parent!.uuid); + }); + }); + /** * Reproduce losing the mkdir race against a concurrent writer: the * existence probe finds nothing (it ran before the other writer's commit) @@ -3886,10 +4016,23 @@ describe('FSService mkdir, touch, rename and shortcuts', () => { }); it('rejects a thumbnail update without an entry identifier', async () => { - const error = await caught(() => - fs.updateEntryThumbnail(user.userId, '', 'data:image/png;base64,A'), + const entry = await writeFile( + user, + `${user.home}/Documents/thumb-blank.txt`, + 'x', ); - expect(error.statusCode).toBe(400); + const [blank, valid] = await fs.updateEntryThumbnails(user.userId, [ + { uuid: '', thumbnail: 'data:image/png;base64,A' }, + { uuid: entry.uuid, thumbnail: 'data:image/png;base64,B' }, + ]); + expect(blank).toMatchObject({ + status: 'rejected', + reason: { statusCode: 400 }, + }); + expect(valid).toMatchObject({ + status: 'fulfilled', + value: { thumbnail: 'data:image/png;base64,B' }, + }); }); it('updates a thumbnail on an owned entry', async () => { @@ -3898,12 +4041,13 @@ describe('FSService mkdir, touch, rename and shortcuts', () => { `${user.home}/Documents/thumb.txt`, 'x', ); - const updated = await fs.updateEntryThumbnail( - user.userId, - entry.uuid, - 'data:image/png;base64,AAA', - ); - expect(updated.thumbnail).toBe('data:image/png;base64,AAA'); + const [updated] = await fs.updateEntryThumbnails(user.userId, [ + { uuid: entry.uuid, thumbnail: 'data:image/png;base64,AAA' }, + ]); + expect(updated).toMatchObject({ + status: 'fulfilled', + value: { thumbnail: 'data:image/png;base64,AAA' }, + }); }); }); diff --git a/src/backend/services/fs/FSService.ts b/src/backend/services/fs/FSService.ts index 8a9d32e33..474042139 100644 --- a/src/backend/services/fs/FSService.ts +++ b/src/backend/services/fs/FSService.ts @@ -67,7 +67,10 @@ import type { import type { FSEntryStore } from '../../stores/fs/FSEntryStore.js'; import type { puterStores } from '../../stores/index.js'; import type { LayerInstances } from '../../types.js'; -import { runWithConcurrencyLimitSettled } from '../../util/concurrency.js'; +import { + awaitLater, + runWithConcurrencyLimitSettled, +} from '../../util/concurrency.js'; import { isUniqueViolation } from '../../util/dbError.js'; import { AclMode } from '../acl/ACLService.js'; import type { puterServices } from '../index.js'; @@ -2742,9 +2745,11 @@ export class FSService extends PuterService { }); } + /** `preloadedSession`: the caller's read of `request.uploadId`, reused. */ async signMultipartParts( userId: number, request: SignMultipartPartsRequest, + preloadedSession?: PendingUploadSession, ): Promise { if (!request?.uploadId) { throw new HttpError(400, 'Missing uploadId', { @@ -2774,9 +2779,11 @@ export class FSService extends PuterService { }); } - const session = await this.stores.fsEntry.getPendingEntryBySessionId( - request.uploadId, - ); + const session = + preloadedSession ?? + (await this.stores.fsEntry.getPendingEntryBySessionId( + request.uploadId, + )); if (!session) { throw new HttpError(404, 'Upload session was not found', { legacyCode: 'not_found', @@ -3006,6 +3013,8 @@ export class FSService extends PuterService { userId: number, completeWriteRequest: CompleteWriteRequest, ): Promise { + // Read here, not taken from the caller's access check: an abort or a + // second completion landing during that check must still get a 409. const session = await this.stores.fsEntry.getPendingEntryBySessionId( completeWriteRequest.uploadId, ); @@ -3118,7 +3127,7 @@ export class FSService extends PuterService { } const fsEntry = await this.stores.fsEntry.completePendingEntry( - session.sessionId, + session, createInput, ); await this.#settleSessionUploadReservations([session]); @@ -3167,6 +3176,7 @@ export class FSService extends PuterService { ); } + // Read fresh for the same reason as completeUrlWrite. const sessions = await this.stores.fsEntry.getPendingEntriesBySessionIds(uploadIds); const completionItems: Array<{ @@ -3367,7 +3377,7 @@ export class FSService extends PuterService { const completedEntries = await this.stores.fsEntry.batchCompletePendingEntries( completionItems.map((item) => ({ - sessionId: item.session.sessionId, + session: item.session, finalData: item.finalData, })), ); @@ -3640,23 +3650,34 @@ export class FSService extends PuterService { await this.#cleanupPreparedBatchUploads(preparedBatch, uploadedItems); } - async updateEntryThumbnail( + /** + * Set thumbnails on entries `userId` owns, each result in input order: the + * updated entry, or why that one failed. + */ + async updateEntryThumbnails( userId: number, - entryUuid: string, - thumbnail: string | null, - ): Promise { - if (typeof entryUuid !== 'string' || entryUuid.length === 0) { - throw new HttpError( - 400, - 'Invalid file entry identifier for thumbnail update', - { legacyCode: 'bad_request' }, + updates: Array<{ uuid: string; thumbnail: string | null }>, + ): Promise[]> { + const isValid = ({ uuid }: { uuid: string }) => + typeof uuid === 'string' && uuid.length > 0; + const stored = + await this.stores.fsEntry.updateEntryThumbnailsByUuidForUser( + userId, + updates.filter(isValid), ); - } - - return this.stores.fsEntry.updateEntryThumbnailByUuidForUser( - userId, - entryUuid, - thumbnail, + let next = 0; + return updates.map( + (update): PromiseSettledResult => + isValid(update) + ? stored[next++] + : { + status: 'rejected', + reason: new HttpError( + 400, + 'Invalid file entry identifier for thumbnail update', + { legacyCode: 'bad_request' }, + ), + }, ); } @@ -3951,10 +3972,10 @@ export class FSService extends PuterService { /** * Resolve or create a parent directory for a given target path. Returns the - * parent entry. Throws 400 if the path has no parent (root) or 404 when - * parents are missing and create is disabled. + * parent entry. Throws 400 synchronously if the path has no parent (root), + * and rejects with 404 when parents are missing and create is disabled. */ - async #resolveOrCreateParent( + #resolveOrCreateParent( userId: number, targetPath: string, createMissingParents: boolean, @@ -3996,14 +4017,22 @@ export class FSService extends PuterService { }, ): Promise { const targetPath = input.path.trim(); - const parent = await this.#resolveOrCreateParent( + const parentLookup = this.#resolveOrCreateParent( userId, targetPath, !!input.createMissingParents, ); + // Looked up alongside the parent, but awaited after it so the parent's + // error still comes first. A target whose parent is still being created + // reads as missing; a concurrent create lands in the unique-violation + // handler below. + const existingLookup = awaitLater( + this.stores.fsEntry.getEntryByPath(targetPath), + ); + const parent = await parentLookup; let name = pathPosix.basename(targetPath); - const existing = await this.stores.fsEntry.getEntryByPath(targetPath); + const existing = await existingLookup; if (existing) { if (existing.isDir) { if (input.dedupeName) { diff --git a/src/backend/services/metering/MeteringService.test.ts b/src/backend/services/metering/MeteringService.test.ts index 7b0314e75..fcf2e4f7a 100644 --- a/src/backend/services/metering/MeteringService.test.ts +++ b/src/backend/services/metering/MeteringService.test.ts @@ -314,6 +314,136 @@ describe('MeteringService', () => { expect(stub).toHaveBeenCalledWith(actor); }); + // Resolvers are asked concurrently, but the answer must be the one + // asking them one at a time, in order, would give. + describe('concurrent resolvers', () => { + const lookupFailedFor = (who: Actor) => + ( + target as unknown as { + subscriptionCache: Map< + string, + { lookupFailed: boolean } + >; + } + ).subscriptionCache.get(who.user!.uuid as string)?.lookupFailed; + const sleep = (ms: number) => + new Promise((resolve) => setTimeout(resolve, ms)); + + it('starts every resolver in both lists before any answers', async () => { + const started: string[] = []; + let release!: () => void; + const gate = new Promise((resolve) => { + release = () => resolve(null); + }); + target.registerDefaultSubscriptionResolver(async () => { + started.push('default'); + return gate; + }); + target.registerSubscriptionResolver(async () => { + started.push('first'); + return gate; + }); + target.registerSubscriptionResolver(async () => { + started.push('second'); + return null; + }); + + const pending = target.getActorSubscription(actor); + expect(started).toEqual(['default', 'first', 'second']); + release(); + expect((await pending).id).toBe(DEFAULT_FREE_SUBSCRIPTION); + }); + + it('keeps the earlier resolver`s answer when a later one settles first', async () => { + for (const id of ['slow-plan', 'fast-plan']) + target.registerPolicy({ + id, + monthUsageAllowance: toMicroCents(10), + monthlyStorageAllowance: 1024, + }); + target.registerSubscriptionResolver(async () => { + await sleep(20); + return 'slow-plan'; + }); + target.registerSubscriptionResolver(async () => 'fast-plan'); + + expect((await target.getActorSubscription(actor)).id).toBe( + 'slow-plan', + ); + expect(lookupFailedFor(actor)).toBe(false); + }); + + it('counts a failure ahead of the winner as a failed lookup', async () => { + target.registerPolicy({ + id: 'after-failure', + monthUsageAllowance: toMicroCents(10), + monthlyStorageAllowance: 1024, + }); + const warn = vi + .spyOn(console, 'warn') + .mockImplementation(() => {}); + try { + target.registerSubscriptionResolver(async () => { + await sleep(10); + throw new Error('store down'); + }); + target.registerSubscriptionResolver( + async () => 'after-failure', + ); + + expect((await target.getActorSubscription(actor)).id).toBe( + 'after-failure', + ); + expect(lookupFailedFor(actor)).toBe(true); + expect(warn).toHaveBeenCalledWith( + '[metering] subscription resolver failed', + expect.any(Error), + ); + } finally { + warn.mockRestore(); + } + }); + + it('ignores failures after the winner, with no warning or unhandled rejection', async () => { + target.registerPolicy({ + id: 'early-winner', + monthUsageAllowance: toMicroCents(10), + monthlyStorageAllowance: 1024, + }); + const unhandled = vi.fn(); + process.on('unhandledRejection', unhandled); + const warn = vi + .spyOn(console, 'warn') + .mockImplementation(() => {}); + try { + target.registerSubscriptionResolver(async () => { + await sleep(10); + return 'early-winner'; + }); + target.registerSubscriptionResolver(async () => { + throw new Error('later, async'); + }); + target.registerSubscriptionResolver(() => { + throw new Error('later, sync'); + }); + + expect((await target.getActorSubscription(actor)).id).toBe( + 'early-winner', + ); + expect(lookupFailedFor(actor)).toBe(false); + await sleep(20); + expect(warn).not.toHaveBeenCalledWith( + '[metering] subscription resolver failed', + expect.anything(), + ); + expect(unhandled).not.toHaveBeenCalled(); + } finally { + warn.mockRestore(); + process.off('unhandledRejection', unhandled); + } + }); + }); + it('falls through to the default resolver when the primary returns nothing', async () => { const customDefault = { id: 'custom-default', diff --git a/src/backend/services/metering/MeteringService.ts b/src/backend/services/metering/MeteringService.ts index a7b80ef89..3e52b2e7b 100644 --- a/src/backend/services/metering/MeteringService.ts +++ b/src/backend/services/metering/MeteringService.ts @@ -75,6 +75,8 @@ export type SubscriptionResolver = ( actor: Actor, ) => Promise | string | null | undefined; +type ResolverOutcome = { id: string | null | undefined } | { error: unknown }; + /** One usage type's amount, pre-aggregated across one call. */ type TypeAmounts = { units: number; cost: number; count: number }; @@ -499,7 +501,8 @@ export class MeteringService extends PuterService { /** * Register a resolver that maps an actor to a subscription id. The first - * resolver that returns a non-empty id wins; later resolvers are skipped. A + * resolver that returns a non-empty id wins; later answers are ignored. All + * resolvers are called concurrently, so each must be a pure read. A * resolver whose lookup fails should throw rather than return null, so the * fallback it causes isn't cached as the actor's plan. */ @@ -509,7 +512,8 @@ export class MeteringService extends PuterService { /** * Register a resolver that maps an actor to a _default_ subscription id, - * used when none is set. Lowest `priority` is asked first. + * used when none is set. Lowest `priority` is consulted first. Called + * concurrently with the others, so it must be a pure read. */ registerDefaultSubscriptionResolver( fn: SubscriptionResolver, @@ -2070,15 +2074,18 @@ export class MeteringService extends PuterService { ? DEFAULT_FREE_SUBSCRIPTION : DEFAULT_TEMP_SUBSCRIPTION; - const defaults = await this.firstResolver( + // Both lists start together; each is still read in its own order. + const pendingDefaults = this.#startResolvers( this.defaultSubscriptionResolvers.map((entry) => entry.fn), actor, ); - const resolvedDefault = defaults.id || fallbackDefault; - const user = await this.firstResolver( + const pendingUser = this.#startResolvers( this.subscriptionResolvers, actor, ); + const defaults = await this.firstResolver(pendingDefaults); + const resolvedDefault = defaults.id || fallbackDefault; + const user = await this.firstResolver(pendingUser); const resolvedUser = user.id || resolvedDefault; const resolverFailed = defaults.failed || user.failed; @@ -2480,20 +2487,38 @@ export class MeteringService extends PuterService { tracked.finally(() => this.pendingAuxPromises.delete(tracked)); } - /** The first non-empty answer, and whether any resolver threw on the way. */ - private async firstResolver( + /** Calls every resolver at once; each outcome settles, never rejects. */ + #startResolvers( resolvers: SubscriptionResolver[], actor: Actor, + ): Promise[] { + return resolvers.map(async (resolver): Promise => { + try { + return { id: await resolver(actor) }; + } catch (error) { + return { error }; + } + }); + } + + /** + * The first non-empty answer in registration order, and whether a resolver + * ahead of it threw. Outcomes after the winner are ignored, as if never + * asked. + */ + private async firstResolver( + pending: Promise[], ): Promise<{ id: string | null; failed: boolean }> { let failed = false; - for (const resolver of resolvers) { - try { - const result = await resolver(actor); - if (result) return { id: result, failed }; - } catch (e) { + for (const next of pending) { + const outcome = await next; + if ('error' in outcome) { failed = true; - console.warn('[metering] subscription resolver failed', e); - } + console.warn( + '[metering] subscription resolver failed', + outcome.error, + ); + } else if (outcome.id) return { id: outcome.id, failed }; } return { id: null, failed }; } diff --git a/src/backend/services/permission/PermissionService.defaultGrants.test.ts b/src/backend/services/permission/PermissionService.defaultGrants.test.ts new file mode 100644 index 000000000..8947f60dd --- /dev/null +++ b/src/backend/services/permission/PermissionService.defaultGrants.test.ts @@ -0,0 +1,271 @@ +/* + * 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 . + */ + +// `check` answers what fixed config grants without a scan. These tests hold +// it to the scan's own answer across actor kinds and permissions, granted by +// default or not. + +import { v4 as uuidv4 } from 'uuid'; +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; +import { makeActor, SYSTEM_ACTOR, type Actor } from '../../core/actor.js'; +import { runWithContext } from '../../core/context.js'; +import { PuterServer } from '../../server.js'; +import { createTestUser, setupTestServer } from '../../testUtil.js'; +import type { AuthService } from '../auth/AuthService.js'; +import { FULL_API_ACCESS } from './consts.js'; +import { PermissionService } from './PermissionService.js'; +import { PermissionUtil } from './permissionUtil.js'; + +describe('check on default grants', () => { + let server: PuterServer; + let permService: PermissionService; + let actors: Record; + let fileUid: string; + let userUuid: string; + + /** `check` as it read before it could answer without a scan. */ + const scanCheck = async ( + actor: Actor, + options: string | string[], + noCache: boolean, + ) => + PermissionUtil.readingToOptions( + await permService.scan(actor, options, undefined, { noCache }), + ).length > 0; + + beforeAll(async () => { + server = await setupTestServer(); + permService = server.services + .permission as unknown as PermissionService; + const auth = server.services.auth as unknown as AuthService; + + const username = `pdg${Math.random().toString(36).slice(2, 10)}`; + await createTestUser(server, { username, password: 'pdg-password' }); + const grouped = (await server.stores.user.getByUsername(username))!; + const loose = await server.stores.user.create({ + username: `pdl${Math.random().toString(36).slice(2, 10)}`, + uuid: uuidv4(), + password: null, + email: null, + requires_email_confirmation: false, + }); + userUuid = grouped.uuid; + const userActor = makeActor({ user: grouped }); + const app = await ( + server.stores.app.create as unknown as ( + f: Record, + o: { ownerUserId: number }, + ) => Promise<{ uid: string; id: number }> + )( + { + name: `pdg-${uuidv4()}`, + title: 'Default grants app', + index_url: `https://pdg-${uuidv4()}.test/`, + }, + { ownerUserId: grouped.id }, + ); + const appActor = makeActor({ + user: grouped, + app: { uid: app.uid, id: app.id }, + }); + // A file the user owns, for a permission a rule answers. + const entry = (await server.services.fs.mkdir(grouped.id, { + path: `/${grouped.username}/pdg-${uuidv4()}`, + createMissingParents: true, + } as never)) as { uuid: string }; + fileUid = entry.uuid; + + const authenticate = async (token: string) => + (await auth.authenticate(token, { touch: false })).actor!; + const mint = (issuer: Actor, perms: string[]) => + runWithContext({ actor: issuer }, () => + auth.createAccessToken( + issuer, + perms.map((p) => [p] as [string]), + ), + ); + + actors = { + user: userActor, + looseUser: makeActor({ user: loose }), + // Its own uuid: a scan reading is cached by uuid, and real actors + // sharing one also share an id. + userWithoutId: makeActor({ + user: { uuid: uuidv4(), username: grouped.username }, + }), + app: appActor, + builtinApp: makeActor({ + user: grouped, + app: { uid: 'app-0bef044f-918f-4cbf-a0c0-b4a17ee81085', id: 1 }, + }), + appWithoutUserId: makeActor({ + user: { uuid: uuidv4(), username: grouped.username }, + app: { uid: app.uid, id: app.id }, + }), + scopedToken: await authenticate( + await mint(userActor, [ + 'service:puter-kvstore:ii:puter-kvstore', + ]), + ), + fullToken: await authenticate( + await mint(userActor, [FULL_API_ACCESS]), + ), + appToken: await authenticate( + await mint(appActor, ['driver:puter-kvstore:get']), + ), + system: SYSTEM_ACTOR, + }; + }, 60_000); + + afterAll(async () => { + await server?.shutdown(); + }, 60_000); + + const permissions = (): Array => [ + 'driver', + 'service', + 'events:background', + 'events:background:extra', + 'events', + 'driver:puter-kvstore', + 'driver:puter-kvstore:get', + 'driver:puter-chat-completion:complete', + 'service:puter-kvstore:ii:puter-kvstore', + 'service:puter-chat-completion:ii:puter-chat-completion', + 'feature', + 'feature:something', + 'driver:helloworld:greet', + 'zztest:not-granted:ii:read', + 'constructor', + 'toString', + '__proto__', + 'hasOwnProperty', + `user:${userUuid}:email:read`, + `fs:${fileUid}:read`, + `fs:${fileUid}`, + 'app:uid#app-nope:read', + `apps-of-user:${userUuid}:read`, + 'site:somewhere:read', + 'kv-share:abc', + '', + [], + ['zztest:not-granted:ii:read', 'service:x:ii:y'], + ['feature', 'zztest:nope'], + ['feature', 'driver'], + [`fs:${fileUid}:read`, 'service:puter-kvstore:ii:puter-kvstore'], + ]; + + it('gives the answer a scan gives, for every actor and permission', async () => { + const answered = { granted: 0, refused: 0 }; + for (const [name, actor] of Object.entries(actors)) { + for (const options of permissions()) { + for (const noCache of [false, true]) { + const expected = await scanCheck(actor, options, noCache); + const actual = await permService.check(actor, options, { + noCache, + }); + expect( + actual, + `${name} ${JSON.stringify(options)} noCache=${noCache}`, + ).toBe(expected); + answered[expected ? 'granted' : 'refused']++; + } + } + } + expect(answered.granted).toBeGreaterThan(0); + expect(answered.refused).toBeGreaterThan(0); + }); + + it('answers a default grant without reading the scan cache', async () => { + const getScanCache = vi.spyOn(server.stores.permission, 'getScanCache'); + try { + for (const actor of [actors.user, actors.looseUser, actors.app]) { + expect( + await permService.check( + actor, + 'service:puter-chat-completion:ii:puter-chat-completion', + ), + ).toBe(true); + } + expect(getScanCache).not.toHaveBeenCalled(); + + // Anything else still scans. + for (const [actor, permission] of [ + [ + actors.scopedToken, + 'service:puter-chat-completion:ii:puter-chat-completion', + ], + [ + actors.fullToken, + 'service:puter-chat-completion:ii:puter-chat-completion', + ], + [actors.app, 'feature:something'], + [actors.user, `fs:${fileUid}:read`], + ] as const) { + getScanCache.mockClear(); + await permService.check(actor, permission); + expect(getScanCache).toHaveBeenCalled(); + } + } finally { + getScanCache.mockRestore(); + } + }); + + it('defers to a rule that matches a default-granted permission', async () => { + const target = `driver:pdg-rewritten-${uuidv4()}`; + const implied = `service:pdg-implied-${uuidv4()}`; + permService.registerRewriter({ + id: 'pdg-test-rewriter', + matches: (p) => p === target, + rewrite: () => 'zztest:pdg-rewritten:ii:read', + }); + permService.registerImplicator({ + id: 'pdg-test-implicator', + matches: (p) => p === implied, + check: () => { + throw new Error('implicator failed'); + }, + }); + const outcome = async (run: () => Promise) => { + try { + return { granted: await run() }; + } catch (error) { + return { error: (error as Error).message }; + } + }; + const failures = new Set(); + for (const actor of [actors.user, actors.app]) { + for (const permission of [target, implied]) { + const expected = await outcome(() => + scanCheck(actor, permission, true), + ); + expect( + await outcome(() => + permService.check(actor, permission, { + noCache: true, + }), + ), + ).toEqual(expected); + if ('error' in expected) failures.add(expected.error); + } + } + // The app's scan reaches the failing rule; a default answer would not. + expect([...failures]).toEqual(['implicator failed']); + }); +}); diff --git a/src/backend/services/permission/PermissionService.test.ts b/src/backend/services/permission/PermissionService.test.ts index 5bc0e17ba..f0e4abb3a 100644 --- a/src/backend/services/permission/PermissionService.test.ts +++ b/src/backend/services/permission/PermissionService.test.ts @@ -2203,6 +2203,39 @@ describe('PermissionService — scan paths', () => { expect(nonShortcutRan).toBe(false); }); + it('asks each rule about an option once, however many requested options explode into it', async () => { + const { actor } = await makeLooseUser(); + const root = `zzdup${uuidv4().replaceAll('-', '')}`; + const asked = { shortcut: [] as string[], plain: [] as string[] }; + permService.registerImplicator({ + id: 'test-dup-shortcut', + shortcut: true, + matches: (p) => p.startsWith(root), + check: async ({ permission }) => { + asked.shortcut.push(permission); + return undefined; + }, + }); + permService.registerImplicator({ + id: 'test-dup-plain', + matches: (p) => p.startsWith(root), + check: async ({ permission }) => { + asked.plain.push(permission); + return permission === `${root}:a` ? {} : undefined; + }, + }); + + // Both options share the parents `${root}:a` and `${root}`. + expect( + await permService.check(actor, [`${root}:a:b`, `${root}:a`], { + noCache: true, + }), + ).toBe(true); + const firstSeen = [`${root}:a:b`, `${root}:a`, root]; + expect(asked.shortcut).toEqual(firstSeen); + expect(asked.plain).toEqual(firstSeen); + }); + it('a non-shortcut implicator contributes an option alongside the scanners', async () => { const { actor } = await makeLooseUser(); const permission = `plain-${uuidv4()}:go`; diff --git a/src/backend/services/permission/PermissionService.ts b/src/backend/services/permission/PermissionService.ts index bb0ecd269..5f041c53a 100644 --- a/src/backend/services/permission/PermissionService.ts +++ b/src/backend/services/permission/PermissionService.ts @@ -239,6 +239,7 @@ export class PermissionService extends PuterService { permissionOptions: string | string[], scanOptions?: ScanOptions, ): Promise { + if (this.#heldByDefault(actor, permissionOptions)) return true; const reading = await this.scan( actor, permissionOptions, @@ -249,6 +250,53 @@ export class PermissionService extends PuterService { return options.length > 0; } + /** + * `scan`'s answer, without scanning, for what fixed config grants: a user's + * default permissions, and an app's default implicit grants that its user + * holds by default. Only while no rule matches any option or parent, since + * a rule is the only thing that could change what `scan` reads here. + */ + #heldByDefault( + actor: Actor, + permissionOptions: string | string[], + ): boolean { + if (!actor.user?.id) return false; + const plainUser = isPlainUserActor(actor); + if (!plainUser && !isAppActor(actor)) return false; + + // `getHigherPermissions`, minus exploders, which can't match here. + const exploded: string[] = []; + const requested = Array.isArray(permissionOptions) + ? permissionOptions + : [permissionOptions]; + for (const permission of requested) { + if (typeof permission !== 'string') return false; + for (const parent of this.getParentPermissions(permission)) { + if ( + this.rewriters.some((rule) => rule.matches(parent)) || + this.exploders.some((rule) => rule.matches(parent)) || + this.implicators.some((rule) => rule.matches(parent)) + ) { + return false; + } + if (!isBareFsPermission(parent)) exploded.push(parent); + } + } + + const held = (grants: Record, option: string) => + Object.prototype.hasOwnProperty.call(grants, option); + if (!exploded.some((option) => held(default_user_permissions, option))) + return false; + if (plainUser) return true; + const implied = default_implicit_user_app_permissions as Record< + string, + unknown + >; + return exploded.some( + (option) => held(implied, option) && !!implied[option], + ); + } + /** * Batch sibling of `check`. Returns a `Map` answering * "does `actor` hold each of these permissions?" with one Redis MGET for @@ -425,7 +473,9 @@ export class PermissionService extends PuterService { reading.push({ $: 'explode', from: perm, to: higher }); } } - options = exploded.flat(); + // Options explode into overlapping sets; each is evaluated once, in + // the order it first appears. + options = [...new Set(exploded.flat())]; // -- default user permissions -- // A group-independent floor every user actor holds, resolved in @@ -1410,12 +1460,14 @@ export class PermissionService extends PuterService { this.#assertFitsColumn(rewritten); } + /** `resolvedApp` is `appIdentifier`'s row when the caller already read it. */ async grantUserAppPermission( actor: Actor, appIdentifier: string, permission: string, extra: Record = {}, meta: GrantMeta = {}, + resolvedApp?: { id: number; uid: string }, ): Promise { const grantedAs = this.#recordsSource(permission) ? permission : null; const askedFor = permission; @@ -1423,7 +1475,7 @@ export class PermissionService extends PuterService { this.assertGrantableFsPermission(permission); await this.#assertReachableFsPath(actor, askedFor, permission); this.#assertFitsColumn(permission); - const app = await this.#requireApp(appIdentifier); + const app = resolvedApp ?? (await this.#requireApp(appIdentifier)); const userId = this.#requireUserId(actor); // Only this layer writes `grantedAs`, so revoke can trust it. diff --git a/src/backend/services/profile/ProfileService.test.ts b/src/backend/services/profile/ProfileService.test.ts index d958f2dce..154a1e777 100644 --- a/src/backend/services/profile/ProfileService.test.ts +++ b/src/backend/services/profile/ProfileService.test.ts @@ -138,6 +138,21 @@ describe('ProfileService', () => { }); }); + it('looks up a missing profile by path alone, without the legacy lineage walk', async () => { + const user = await makeUser(); + const read = vi.spyOn(server.clients.db, 'read'); + try { + await service.getProfile(user); + expect( + read.mock.calls.filter(([sql]) => + String(sql).includes('WITH RECURSIVE'), + ), + ).toEqual([]); + } finally { + read.mockRestore(); + } + }); + it('stores a patch as .profile in the system directory and merges later patches', async () => { const user = await makeUser(); expect( diff --git a/src/backend/services/profile/ProfileService.ts b/src/backend/services/profile/ProfileService.ts index a79478fe2..fd0b2aa84 100644 --- a/src/backend/services/profile/ProfileService.ts +++ b/src/backend/services/profile/ProfileService.ts @@ -288,8 +288,10 @@ export class ProfileService extends PuterService { async #readStored( userUuid: string, ): Promise | null> { + // Profiles are only written through `fs.write`, which stores a path. const entry = await this.stores.fsEntry.getEntryByPath( profilePath(userUuid), + { lineage: false }, ); if (!entry || entry.isDir) return null; const { body } = await this.services.fs.readContent(entry); diff --git a/src/backend/services/share/ShareService.test.ts b/src/backend/services/share/ShareService.test.ts index 967fe91e4..925104888 100644 --- a/src/backend/services/share/ShareService.test.ts +++ b/src/backend/services/share/ShareService.test.ts @@ -824,6 +824,85 @@ describe('ShareService', () => { ).toEqual(new Map([[file.uuid, true]])); }); + it('still badges an item with a holder too while the owner is off a plan', async () => { + const owner = await makeUser(); + paid.add(owner.user.uuid); + const recipient = await makeUser(); + const file = await makeFile(owner.user); + await share(owner.actor, { + uid: file.uuid, + recipient: anyone, + mode: 'read', + }); + await share(owner.actor, { + uid: file.uuid, + recipient: { email: recipient.email }, + mode: 'read', + }); + + paid.delete(owner.user.uuid); + server.services.metering.invalidateActorSubscription( + owner.user.uuid, + ); + expect( + await server.services.share.shareFlags(owner.actor, [file]), + ).toEqual(new Map([[file.uuid, true]])); + }); + + it('asks about the plan only when an item is shared by link alone', async () => { + const owner = await makeUser(); + paid.add(owner.user.uuid); + const recipient = await makeUser(); + const direct = await makeFile(owner.user); + const untouched = await makeFile(owner.user); + const linked = await makeFile(owner.user); + await share(owner.actor, { + uid: direct.uuid, + recipient: { email: recipient.email }, + mode: 'read', + }); + await share(owner.actor, { + uid: linked.uuid, + recipient: anyone, + mode: 'read', + }); + await new Promise((resolve) => setTimeout(resolve, 20)); + const asked = () => + subscriptionSpy.mock.calls.filter( + ([actor]) => + (actor as Actor).user?.uuid === owner.user.uuid, + ).length; + + const before = asked(); + expect( + await server.services.share.shareFlags(owner.actor, [ + direct, + untouched, + ]), + ).toEqual( + new Map([ + [direct.uuid, true], + [untouched.uuid, false], + ]), + ); + expect(asked()).toBe(before); + + expect( + await server.services.share.shareFlags(owner.actor, [ + direct, + untouched, + linked, + ]), + ).toEqual( + new Map([ + [direct.uuid, true], + [untouched.uuid, false], + [linked.uuid, true], + ]), + ); + expect(asked()).toBe(before + 1); + }); + it('hands out access, never authority', async () => { const owner = await makeUser(); paid.add(owner.user.uuid); @@ -1025,6 +1104,163 @@ describe('ShareService', () => { expect(await canWrite(stranger.actor, file.path)).toBe(false); }); + it('goes with a file that changes owner, with its other grants, and nothing else is read', async () => { + const owner = await makeUser(); + paid.add(owner.user.uuid); + const newOwner = await makeUser(); + const stranger = await makeUser(); + const recipient = await makeUser(); + const member = await makeUser(); + const file = await makeFile(owner.user); + + const team = await server.stores.team.create({ + ownerUserId: owner.user.id, + name: `t-${Math.random().toString(36).slice(2, 9)}`, + }); + for (const user of [owner.user, member.user]) { + await server.stores.team.addMember(team.uid, user.id, { + orgOwned: false, + }); + } + for (const recipientSpec of [ + anyone, + { email: recipient.email }, + { team: team.uid }, + ]) { + await share(owner.actor, { + uid: file.uuid, + recipient: recipientSpec, + mode: 'read', + }); + } + for (const reader of [stranger, recipient, member]) { + expect(await canRead(reader.actor, file.path)).toBe(true); + } + + await server.stores.fsEntry.updateEntry(file.uuid, { + userId: newOwner.user.id, + }); + const moved = await server.stores.fsEntry.getEntryByUuid(file.uuid); + + const listByFsentry = vi.spyOn( + server.stores.share, + 'listByFsentry', + ); + const getEntriesByIds = vi.spyOn( + server.stores.fsEntry, + 'getEntriesByIds', + ); + const revokeUserGrants = vi.spyOn( + server.stores.permission, + 'deleteUserUserPermsByPermissionPrefixes', + ); + const revokeGroupGrants = vi.spyOn( + server.stores.permission, + 'deleteUserGroupPermsByPermissionPrefixes', + ); + const deleteRows = vi.spyOn( + server.stores.share, + 'deleteByFsentryIds', + ); + try { + await server.services.share.onEntryOwnerChanged(moved!); + + expect(listByFsentry).not.toHaveBeenCalled(); + expect(getEntriesByIds).not.toHaveBeenCalled(); + const prefixes = [`fs:${file.uuid}`, `manage:fs:${file.uuid}`]; + expect(revokeUserGrants).toHaveBeenCalledWith(prefixes); + expect(revokeGroupGrants).toHaveBeenCalledWith(prefixes); + expect(deleteRows).toHaveBeenCalledWith([file.id]); + // Grants go before the rows that record them. + expect( + revokeUserGrants.mock.invocationCallOrder[0]!, + ).toBeLessThan(revokeGroupGrants.mock.invocationCallOrder[0]!); + expect( + revokeGroupGrants.mock.invocationCallOrder[0]!, + ).toBeLessThan(deleteRows.mock.invocationCallOrder[0]!); + } finally { + for (const spy of [ + listByFsentry, + getEntriesByIds, + revokeUserGrants, + revokeGroupGrants, + deleteRows, + ]) { + spy.mockRestore(); + } + } + + expect( + await server.clients.db.read( + 'SELECT * FROM `share` WHERE `fsentry_id` = ?', + [file.id], + ), + ).toEqual([]); + for (const reader of [stranger, recipient, member]) { + expect(await canRead(reader.actor, file.path)).toBe(false); + } + }); + + // A just-moved row can be missing from a lagging replica; a file's + // grants go by the uuid in hand regardless. + it('goes with a file that changes owner even when its row reads back missing', async () => { + const owner = await makeUser(); + const recipient = await makeUser(); + const file = await makeFile(owner.user); + await share(owner.actor, { + uid: file.uuid, + recipient: { email: recipient.email }, + mode: 'read', + }); + const grants = () => + server.clients.db.read( + 'SELECT * FROM `user_to_user_permissions` WHERE `permission` LIKE ?', + [`fs:${file.uuid}%`], + ); + expect(await grants()).not.toEqual([]); + expect(await canRead(recipient.actor, file.path)).toBe(true); + + const lagging = vi + .spyOn(server.stores.fsEntry, 'getEntriesByIds') + .mockResolvedValue(new Map()); + try { + await server.services.share.onEntryOwnerChanged(file); + } finally { + lagging.mockRestore(); + } + + expect(await grants()).toEqual([]); + expect(await canRead(recipient.actor, file.path)).toBe(false); + }); + + it('goes with a file its owner moves to Trash', async () => { + const owner = await makeUser(); + paid.add(owner.user.uuid); + const stranger = await makeUser(); + const recipient = await makeUser(); + const file = await makeFile(owner.user); + for (const recipientSpec of [anyone, { email: recipient.email }]) { + await share(owner.actor, { + uid: file.uuid, + recipient: recipientSpec, + mode: 'read', + }); + } + + const trash = await server.stores.fsEntry.getEntryByPath( + `/${owner.user.username}/Trash`, + ); + const trashed = await server.services.fs.move(owner.user.id, { + source: file, + destinationParent: trash!, + }); + + expect(await linkRows(file.id)).toEqual([]); + for (const reader of [stranger, recipient]) { + expect(await canRead(reader.actor, trashed.path)).toBe(false); + } + }); + it('shows the owner a link row another issuer left behind', async () => { const owner = await makeUser(); paid.add(owner.user.uuid); @@ -6062,4 +6298,285 @@ describe('ShareService', () => { expect(reachingCalls).toBeGreaterThan(0); }); }); + + describe('listings read independent parts together', () => { + const failure = (what: string) => new Error(`${what} failed`); + + /** Unhandled rejections raised while `fn` runs, and shortly after. */ + const unhandledDuring = async (fn: () => Promise) => { + const seen: unknown[] = []; + const onUnhandled = (reason: unknown) => seen.push(reason); + process.on('unhandledRejection', onUnhandled); + try { + await fn(); + await new Promise((resolve) => setTimeout(resolve, 20)); + } finally { + process.off('unhandledRejection', onUnhandled); + } + return seen; + }; + + /** Holds `method`'s next call until the returned release runs. */ + const holdNextCall = (target: T, method: keyof T) => { + const real = (target[method] as (...a: unknown[]) => unknown).bind( + target, + ); + let release!: () => void; + const gate = new Promise((resolve) => { + release = resolve; + }); + const spy = vi + .spyOn(target, method as never) + .mockImplementationOnce((async (...args: unknown[]) => { + await gate; + return real(...args); + }) as never); + return { spy, release }; + }; + + const sharedWith = async () => { + const owner = await makeUser(); + const recipient = await makeUser(); + const file = await makeFile(owner.user); + await share(owner.actor, { + uid: file.uuid, + recipient: { email: recipient.email }, + mode: 'read', + }); + return { owner, recipient, file }; + }; + + const listByMe = (actor: Actor, opts = { includeTotal: true }) => + runWithContext({ actor }, () => + server.services.share.listSharedByMe(actor, opts), + ); + + it('counts what is shared with the caller alongside the page', async () => { + const { recipient, file } = await sharedWith(); + const held = holdNextCall(server.stores.share, 'listByHolder'); + const count = vi.spyOn(server.stores.share, 'countByHolder'); + try { + const listing = server.services.share.listSharedWithMe( + recipient.actor, + { includeTotal: true }, + ); + await new Promise((resolve) => setImmediate(resolve)); + expect(count).toHaveBeenCalledTimes(1); + held.release(); + const result = await listing; + expect(result.items.map((row) => row.entryUid)).toEqual([ + file.uuid, + ]); + expect(result.total).toBe(1); + } finally { + held.spy.mockRestore(); + count.mockRestore(); + } + }); + + it('counts nothing shared with the caller unless asked, or for an app', async () => { + const { recipient } = await sharedWith(); + const app = await makeApp(recipient.user.id); + const count = vi.spyOn(server.stores.share, 'countByHolder'); + try { + await server.services.share.listSharedWithMe(recipient.actor); + await server.services.share.listSharedWithMe( + asApp(recipient, app), + { includeTotal: true }, + ); + expect(count).not.toHaveBeenCalled(); + } finally { + count.mockRestore(); + } + }); + + it("surfaces the inbound page's failure ahead of the count's", async () => { + const { recipient } = await sharedWith(); + const page = vi + .spyOn(server.stores.share, 'listByHolder') + .mockRejectedValueOnce(failure('page')); + const count = vi + .spyOn(server.stores.share, 'countByHolder') + .mockRejectedValueOnce(failure('count')); + try { + const unhandled = await unhandledDuring(async () => { + await expect( + server.services.share.listSharedWithMe( + recipient.actor, + { includeTotal: true }, + ), + ).rejects.toThrow('page failed'); + }); + expect(unhandled).toEqual([]); + } finally { + page.mockRestore(); + count.mockRestore(); + } + }); + + it('surfaces an inbound count failure once the page is built', async () => { + const { recipient } = await sharedWith(); + const count = vi + .spyOn(server.stores.share, 'countByHolder') + .mockRejectedValueOnce(failure('count')); + try { + await expect( + server.services.share.listSharedWithMe(recipient.actor, { + includeTotal: true, + }), + ).rejects.toThrow('count failed'); + } finally { + count.mockRestore(); + } + }); + + it('starts the outbound count, team names and plan check early', async () => { + const { owner, recipient, file } = await sharedWith(); + const linked = await makeFile(owner.user); + await server.stores.share.upsertAnyone({ + issuerUserId: owner.user.id, + fsentryId: linked.id, + mode: 'read', + }); + const plan = vi + .spyOn(server.services.metering, 'getActorSubscription') + .mockResolvedValue({ id: 'business' } as never); + const entries = holdNextCall( + server.stores.fsEntry, + 'getEntriesByIds', + ); + const grants = holdNextCall( + server.stores.permission, + 'readLinkedUserUserPermsForHolders', + ); + const count = vi.spyOn(server.stores.share, 'countOutbound'); + const teams = vi.spyOn( + server.stores.team, + 'getByIdsIncludingDeleted', + ); + try { + const listing = listByMe(owner.actor); + await new Promise((resolve) => setImmediate(resolve)); + expect(count).toHaveBeenCalledTimes(1); + expect(teams).toHaveBeenCalledTimes(1); + entries.release(); + await new Promise((resolve) => setTimeout(resolve, 20)); + expect(plan).toHaveBeenCalled(); + grants.release(); + const result = await listing; + expect( + result.items.map((row) => [row.entryUid, row.anyone]), + ).toEqual( + expect.arrayContaining([ + [file.uuid, undefined], + [linked.uuid, true], + ]), + ); + expect(result.items).toHaveLength(2); + expect(result.total).toBe(2); + expect( + result.items.find((row) => row.entryUid === file.uuid) + ?.holder?.username, + ).toBe(recipient.user.username); + } finally { + plan.mockRestore(); + entries.spy.mockRestore(); + grants.spy.mockRestore(); + count.mockRestore(); + teams.mockRestore(); + } + }); + + it("surfaces the outbound page's failure ahead of the count's", async () => { + const { owner } = await sharedWith(); + const page = vi + .spyOn(server.stores.share, 'listOutbound') + .mockRejectedValueOnce(failure('page')); + const count = vi + .spyOn(server.stores.share, 'countOutbound') + .mockRejectedValueOnce(failure('count')); + try { + const unhandled = await unhandledDuring(async () => { + await expect(listByMe(owner.actor)).rejects.toThrow( + 'page failed', + ); + }); + expect(unhandled).toEqual([]); + } finally { + page.mockRestore(); + count.mockRestore(); + } + }); + + it("surfaces the entry read's failure ahead of the team names'", async () => { + const { owner } = await sharedWith(); + const entries = vi + .spyOn(server.stores.fsEntry, 'getEntriesByIds') + .mockRejectedValueOnce(failure('entries')); + const teams = vi + .spyOn(server.stores.team, 'getByIdsIncludingDeleted') + .mockRejectedValueOnce(failure('teams')); + try { + const unhandled = await unhandledDuring(async () => { + await expect(listByMe(owner.actor)).rejects.toThrow( + 'entries failed', + ); + }); + expect(unhandled).toEqual([]); + } finally { + entries.mockRestore(); + teams.mockRestore(); + } + }); + + it("surfaces a grant check's failure ahead of the plan check's", async () => { + const { owner } = await sharedWith(); + const linked = await makeFile(owner.user); + await server.stores.share.upsertAnyone({ + issuerUserId: owner.user.id, + fsentryId: linked.id, + mode: 'read', + }); + const grants = vi + .spyOn( + server.stores.permission, + 'readLinkedUserUserPermsForHolders', + ) + .mockRejectedValueOnce(failure('grants')); + const plan = vi + .spyOn(server.services.metering, 'getActorSubscription') + .mockRejectedValueOnce(failure('plan')); + try { + const unhandled = await unhandledDuring(async () => { + await expect(listByMe(owner.actor)).rejects.toThrow( + 'grants failed', + ); + }); + expect(unhandled).toEqual([]); + } finally { + grants.mockRestore(); + plan.mockRestore(); + } + }); + + it('surfaces a plan check failure once the grant checks pass', async () => { + const { owner } = await sharedWith(); + const linked = await makeFile(owner.user); + await server.stores.share.upsertAnyone({ + issuerUserId: owner.user.id, + fsentryId: linked.id, + mode: 'read', + }); + const plan = vi + .spyOn(server.services.metering, 'getActorSubscription') + .mockRejectedValueOnce(failure('plan')); + try { + await expect(listByMe(owner.actor)).rejects.toThrow( + 'plan failed', + ); + } finally { + plan.mockRestore(); + } + }); + }); }); diff --git a/src/backend/services/share/ShareService.ts b/src/backend/services/share/ShareService.ts index 3c7f9626e..e56de4ff5 100644 --- a/src/backend/services/share/ShareService.ts +++ b/src/backend/services/share/ShareService.ts @@ -27,7 +27,10 @@ import { type Actor, } from '../../core/actor'; import { HttpError, isHttpError } from '../../core/http/HttpError.js'; -import { runWithConcurrencyLimitSettled } from '../../util/concurrency.js'; +import { + awaitLater, + runWithConcurrencyLimitSettled, +} from '../../util/concurrency.js'; import { isUniqueViolation } from '../../util/dbError.js'; import { abuseKey, @@ -1864,9 +1867,14 @@ export class ShareService extends PuterService { * issuer's recipients access under an owner who never agreed to them. */ async onEntryOwnerChanged(entry: FSEntry): Promise { - const rows = entry.isDir - ? await this.stores.share.listAllByFsentrySubtree(entry.id) - : await this.stores.share.listByFsentry(entry.id); + // A file's share rows can only name the file itself, so there is + // nothing to list or hydrate. + if (!entry.isDir) { + await this.onEntryDeleted([entry.uuid]); + await this.stores.share.deleteByFsentryIds([entry.id]); + return; + } + const rows = await this.stores.share.listAllByFsentrySubtree(entry.id); const fsentryIds = [ ...new Set([ entry.id, @@ -1916,6 +1924,13 @@ export class ShareService extends PuterService { const holderId = this.#requireUserId(actor); // Resolved once; team shares ride the caller's own page. const groupIds = await this.stores.team.listGroupIdsForUser(holderId); + // Account-wide, so only for a caller acting as the account. + const totalLookup = + opts.includeTotal && isAccountContext(actor) + ? awaitLater( + this.stores.share.countByHolder(holderId, { groupIds }), + ) + : null; const page = await this.stores.share.listByHolder(holderId, { limit: opts.limit, cursor: opts.cursor, @@ -1965,14 +1980,7 @@ export class ShareService extends PuterService { return { items, ...(page.cursor ? { cursor: page.cursor } : {}), - // Account-wide, so only for a caller acting as the account. - ...(opts.includeTotal && isAccountContext(actor) - ? { - total: await this.stores.share.countByHolder(holderId, { - groupIds, - }), - } - : {}), + ...(totalLookup ? { total: await totalLookup } : {}), }; } @@ -2010,6 +2018,13 @@ export class ShareService extends PuterService { ...(opts.includeTotal ? { total: 0 } : {}), }; } + const totalLookup = opts.includeTotal + ? awaitLater( + this.stores.share.countOutbound(userId, { + appUid: scope.appUid, + }), + ) + : null; const page = await this.stores.share.listOutbound(userId, { limit: opts.limit, cursor: opts.cursor, @@ -2017,6 +2032,14 @@ export class ShareService extends PuterService { }); const rows: OutboundShareRow[] = page.items; + // Named so the listing can say which team, not just that it is one. + const teamsLookup = awaitLater( + this.stores.team.getByIdsIncludingDeleted( + rows + .filter((row) => row.holder_group_id) + .map((row) => Number(row.holder_group_id)), + ), + ); const entries = await this.stores.fsEntry.getEntriesByIds( rows.map((row) => Number(row.fsentry_id)), ); @@ -2031,12 +2054,12 @@ export class ShareService extends PuterService { const nodeById = new Map( [...entries.values()].map((entry) => [entry.id, entry]), ); - // Named so the listing can say which team, not just that it is one. - const teamsById = await this.stores.team.getByIdsIncludingDeleted( - rows - .filter((row) => row.holder_group_id) - .map((row) => Number(row.holder_group_id)), - ); + const teamsById = await teamsLookup; + // Link rows are the caller's own (owner-only by construction), so one + // answer covers them all; only asked when there is one to show. + const linkCoveredLookup = rows.some((row) => row.anyone) + ? awaitLater(this.#linkSharingCovered(actor)) + : null; // Three bounds. A claimed row needs the grant *this issuer* made to // still be there — on the pair alone, a grant withdrawn outside // `unshare` stays listed while anyone else grants the same holder the @@ -2062,12 +2085,7 @@ export class ShareService extends PuterService { this.#reachableBy(actor, [...entries.values()]), this.#liveGroupGrants(rows, nodeById), ]); - - // Link rows are the caller's own (owner-only by construction), so one - // answer covers them all; only asked when there is one to show. - const linkCovered = rows.some((row) => row.anyone) - ? await this.#linkSharingCovered(actor) - : true; + const linkCovered = linkCoveredLookup ? await linkCoveredLookup : true; const items: ResolvedShare[] = []; for (const row of rows) { @@ -2123,13 +2141,7 @@ export class ShareService extends PuterService { return { items, ...(page.cursor ? { cursor: page.cursor } : {}), - ...(opts.includeTotal - ? { - total: await this.stores.share.countOutbound(userId, { - appUid: scope.appUid, - }), - } - : {}), + ...(totalLookup ? { total: await totalLookup } : {}), }; } @@ -2569,13 +2581,22 @@ export class ShareService extends PuterService { ); if (own.length === 0) return new Map(); - // A link share nobody can use must not badge the item as shared. - const sharedIds = await this.stores.share.getSharedFsentryIds( + const shared = await this.stores.share.getSharedFsentryIds( own.map((entry) => entry.id), - { includeAnyone: await this.#linkSharingCovered(actor) }, ); + // A link share nobody can use must not badge the item as shared, so + // the plan is asked only when some item's only share is a link. + const linkCovered = [...shared.values()].some((direct) => !direct) + ? await this.#linkSharingCovered(actor) + : false; return new Map( - own.map((entry) => [entry.uuid, sharedIds.has(entry.id)]), + own.map((entry) => { + const direct = shared.get(entry.id); + return [ + entry.uuid, + direct === true || (direct === false && linkCovered), + ]; + }), ); } diff --git a/src/backend/stores/app/AppStore.js b/src/backend/stores/app/AppStore.js index 9d6ff037d..0f3e8b918 100644 --- a/src/backend/stores/app/AppStore.js +++ b/src/backend/stores/app/AppStore.js @@ -401,6 +401,44 @@ export class AppStore extends PuterStore { return typeof uid === 'string' && uid ? uid : null; } + /** + * `ownerUserId`'s apps whose `index_url` is one of `candidates`, private + * first, then by id. At most two, so a caller can tell an ambiguous match; + * only the columns the hosted-site gate reads. + * + * @param {number} ownerUserId + * @param {string[]} candidates + * @param {{ requirePrivate?: boolean }} [opts] + * @returns {Promise< + * { + * id: number; + * uid: string; + * name: string; + * title: string; + * is_private: number | boolean | null; + * owner_user_id: number; + * index_url: string; + * }[] + * >} + */ + async listOwnedByIndexUrlCandidates( + ownerUserId, + candidates, + { requirePrivate = false } = {}, + ) { + if (!Array.isArray(candidates) || candidates.length === 0) return []; + const isPrivate = `\`is_private\` = ${this.clients.db.booleanLiteral(true)}`; + const placeholders = candidates.map(() => '?').join(', '); + return this.clients.db.read( + 'SELECT `id`, `uid`, `name`, `title`, `is_private`, `owner_user_id`, `index_url` ' + + 'FROM `apps` WHERE `owner_user_id` = ? ' + + (requirePrivate ? `AND ${isPrivate} ` : '') + + `AND \`index_url\` IN (${placeholders}) ` + + `ORDER BY CASE WHEN ${isPrivate} THEN 0 ELSE 1 END, \`id\` LIMIT 2`, + [ownerUserId, ...candidates], + ); + } + /** * One row per distinct `index_url` in `candidates` with its lowest private * id (`private_id`, null if none) and lowest id (`min_id`). An external diff --git a/src/backend/stores/app/AppStore.test.js b/src/backend/stores/app/AppStore.test.js index b35e654ba..5896b84d6 100644 --- a/src/backend/stores/app/AppStore.test.js +++ b/src/backend/stores/app/AppStore.test.js @@ -963,6 +963,59 @@ describe('AppStore CRUD and cache invalidation', () => { expect(found.id).toBe(app.id); }); + it("lists at most two of an owner's index_url matches, private first, with only the gate's columns", async () => { + const url = `https://${freshName()}.example.com/`; + const candidates = [url, `${url}index.html`]; + const publicApp = await createApp({ + index_url: url, + icon: `data:image/png;base64,${'A'.repeat(512)}`, + description: 'not read by the gate', + }); + const privateApp = await createApp({ index_url: candidates[1] }); + await db.write('UPDATE `apps` SET `is_private` = 1 WHERE `id` = ?', [ + privateApp.id, + ]); + await createApp({ index_url: url }); + await createApp({ index_url: url }, { ownerUserId: OTHER_OWNER }); + + const rows = await appStore.listOwnedByIndexUrlCandidates( + OWNER, + candidates, + ); + expect(rows.map((row) => row.id)).toEqual([ + privateApp.id, + publicApp.id, + ]); + expect(Object.keys(rows[1]).sort()).toEqual([ + 'id', + 'index_url', + 'is_private', + 'name', + 'owner_user_id', + 'title', + 'uid', + ]); + + const privateOnly = await appStore.listOwnedByIndexUrlCandidates( + OWNER, + candidates, + { requirePrivate: true }, + ); + expect(privateOnly.map((row) => row.id)).toEqual([privateApp.id]); + }); + + it('lists no index_url matches for no candidates, without a query', async () => { + const read = vi.spyOn(db, 'read'); + try { + expect( + await appStore.listOwnedByIndexUrlCandidates(OWNER, []), + ).toEqual([]); + expect(read).not.toHaveBeenCalled(); + } finally { + read.mockRestore(); + } + }); + // -- createFromOrigin ---------------------------------------------- it('creates an origin-bootstrap app with the marker shape', async () => { diff --git a/src/backend/stores/events/DurableSubscriptionStore.integration.test.ts b/src/backend/stores/events/DurableSubscriptionStore.integration.test.ts index 0701d87b4..07e71b2d3 100644 --- a/src/backend/stores/events/DurableSubscriptionStore.integration.test.ts +++ b/src/backend/stores/events/DurableSubscriptionStore.integration.test.ts @@ -24,7 +24,15 @@ * it has not looked. */ -import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest'; +import { + afterAll, + beforeAll, + beforeEach, + describe, + expect, + it, + vi, +} from 'vitest'; import { v4 as uuidv4 } from 'uuid'; import { EVENTS_DURABLE_SUBSCRIPTIONS_MAX } from '../../controllers/events/limits.js'; import { isHttpError } from '../../core/http/HttpError.js'; @@ -170,6 +178,39 @@ describe('creating a subscription', () => { await expect(cache().isRegionWarm(userId)).resolves.toBe(true); }); + // A snapshot taken any earlier widens the window in which a concurrent + // removal of the owner's other rows gets cached again. + it('reads the rows it caches only once the recount is back', async () => { + const db = env.server.clients.db; + const realPread = db.pread.bind(db); + let recounts = 0; + let readBeforeRecount = false; + const pread = vi + .spyOn(db, 'pread') + .mockImplementation(async (sql, ...rest) => { + const text = String(sql); + if ( + text.includes( + '`owner_user_id` = ? AND `suspended_at` IS NULL', + ) && + recounts < 2 + ) + readBeforeRecount = true; + const rows = await realPread(sql, ...rest); + if (text.includes('SUM(CASE WHEN `app_uid`')) recounts++; + return rows; + }); + try { + const { row } = await durable().create(input()); + expect(readBeforeRecount).toBe(false); + await expect( + cache().getForTokens(userId, [token()]), + ).resolves.toEqual([row]); + } finally { + pread.mockRestore(); + } + }); + it('indexes a shared anchor under its owner, not its subscriber', async () => { const { row, bump } = await durable().create( input({ holderUserId: otherUserId }), diff --git a/src/backend/stores/events/EventSubscriptionStore.test.ts b/src/backend/stores/events/EventSubscriptionStore.test.ts index 6952925c5..374aa0c03 100644 --- a/src/backend/stores/events/EventSubscriptionStore.test.ts +++ b/src/backend/stores/events/EventSubscriptionStore.test.ts @@ -945,6 +945,65 @@ describe('reading rows for dispatch', () => { }); }); +describe('anyone watching, when the region is warm', () => { + it('answers null while the region is cold, whatever is watched', async () => { + await store.add(makeSub()); + + await expect(store.userHasAnyIfWarm(USER)).resolves.toBeNull(); + }); + + it('answers what userHasAny would once the region is warm', async () => { + await store.markRegionWarm(USER); + await expect(store.userHasAnyIfWarm(USER)).resolves.toBe(false); + + await store.add(makeSub()); + await expect(store.userHasAnyIfWarm(USER)).resolves.toBe(true); + }); + + it('counts a peer watching one of the owner`s tokens', async () => { + await store.markRegionWarm(USER); + await store.noteRemoteWatch(USER, 'f#anchor', 'peer-region', 'add'); + + await expect(store.userHasAny(USER)).resolves.toBe(true); + await expect(store.userHasAnyIfWarm(USER)).resolves.toBe(true); + }); + + it('goes back to null once the region is marked cold', async () => { + await store.markRegionWarm(USER); + await store.add(makeSub()); + await store.markRegionCold(USER); + + await expect(store.userHasAnyIfWarm(USER)).resolves.toBeNull(); + }); + + // One pipeline in a cluster has to stay in one slot. + it('asks in one pipeline, on keys under the owner`s hash tag', async () => { + await store.markRegionWarm(USER); + const keys: string[] = []; + let pipelines = 0; + const realPipeline = redis.pipeline.bind(redis); + const spy = vi.spyOn(redis, 'pipeline').mockImplementation(() => { + pipelines++; + const pipeline = realPipeline(); + const exists = pipeline.exists.bind(pipeline); + pipeline.exists = ((key: string) => { + keys.push(key); + return exists(key); + }) as typeof pipeline.exists; + return pipeline; + }); + try { + await store.userHasAnyIfWarm(USER); + } finally { + spy.mockRestore(); + } + + expect(pipelines).toBe(1); + expect(keys).toHaveLength(3); + for (const key of keys) expect(key).toContain(`{${USER}}`); + }); +}); + describe('the generation counter', () => { it('advances on every registration and removal', async () => { const first = await store.add(makeSub({ subId: 'a' })); diff --git a/src/backend/stores/events/EventSubscriptionStore.ts b/src/backend/stores/events/EventSubscriptionStore.ts index e57e1733d..8a8471462 100644 --- a/src/backend/stores/events/EventSubscriptionStore.ts +++ b/src/backend/stores/events/EventSubscriptionStore.ts @@ -917,6 +917,21 @@ export class EventSubscriptionStore extends PuterStore { return Number(results[0]?.[1]) === 1 || Number(results[1]?.[1]) === 1; } + /** + * {@link userHasAny} and {@link isRegionWarm} in one round trip, for the + * common case of a warm region. `null` when the region is cold, since the + * answer means nothing until its durable rows are rebuilt. + */ + async userHasAnyIfWarm(ownerUserId: number): Promise { + const pipeline = this.clients.redis.pipeline(); + pipeline.exists(durableWarmKey(ownerUserId)); + pipeline.exists(watchedKey(ownerUserId)); + pipeline.exists(remoteWatchKey(ownerUserId)); + const results = (await pipeline.exec()) ?? []; + if (Number(results[0]?.[1]) !== 1) return null; + return Number(results[1]?.[1]) === 1 || Number(results[2]?.[1]) === 1; + } + /** * Which of an event's tokens anyone is watching locally — the dispatch hot * path, and one command whatever the depth of the tree. Delegates to diff --git a/src/backend/stores/fs/FSEntryStore.test.ts b/src/backend/stores/fs/FSEntryStore.test.ts index 164f96ff5..ea59e94f6 100644 --- a/src/backend/stores/fs/FSEntryStore.test.ts +++ b/src/backend/stores/fs/FSEntryStore.test.ts @@ -60,7 +60,7 @@ describe('FSEntryStore', () => { }, }, event: { - emitAndWait: async () => undefined, + emitAndWaitConcurrently: async () => undefined, }, }; const store = new FSEntryStore( @@ -75,7 +75,7 @@ describe('FSEntryStore', () => { }); expect(queries).toEqual([ 'SELECT COALESCE(SUM(size), 0) AS "totalUsage" FROM fsentries WHERE user_id = ?', - 'SELECT free_storage AS "freeStorage" FROM "user" WHERE id = ? LIMIT 1', + 'SELECT uuid, free_storage AS "freeStorage" FROM "user" WHERE id = ? LIMIT 1', ]); }); }); @@ -643,6 +643,124 @@ describe('FSEntryStore legacy NULL-path healing', () => { ); }); + /** + * Holds each path-lineage read for a tick so overlapping lookups show up, + * and fails the ones `failures` names (by last segment) after their delay. + */ + const watchLineageReads = ( + failures: Record = {}, + ) => { + const db = server.clients.db; + const originalRead = (Object.getPrototypeOf(db) as typeof db).read; + const stats = { inFlight: 0, peak: 0, calls: 0 }; + const spy = vi + .spyOn(db, 'read') + .mockImplementation(async (sql: string, params?: unknown[]) => { + if (!sql.includes('segments(depth, name)')) { + return originalRead.call(db, sql, params); + } + stats.calls++; + stats.inFlight++; + stats.peak = Math.max(stats.peak, stats.inFlight); + const failure = failures[String((params ?? []).at(-1))]; + try { + await new Promise((resolve) => + setTimeout(resolve, failure?.delayMs ?? 5), + ); + if (failure) throw failure.error; + return await originalRead.call(db, sql, params); + } finally { + stats.inFlight--; + } + }); + return { stats, spy }; + }; + + it('runs the lineage lookups of a batch concurrently, resolving each path', async () => { + const user = await makeUser(); + const healed = await Promise.all( + [1, 2, 3].map((i) => + createFile(user, `${user.home}/Documents/lineage-${i}.txt`), + ), + ); + for (const file of healed) await blankPath(file.uuid); + const absent = [1, 2, 3].map( + (i) => `${user.home}/Documents/fresh-${i}.txt`, + ); + const paths = [...healed.map((file) => file.path), ...absent]; + + const { stats, spy } = watchLineageReads(); + let entries: (FSEntry | null)[]; + try { + entries = await store.getEntriesByPathsForUser(user.userId, paths, { + skipCache: true, + }); + } finally { + spy.mockRestore(); + } + + expect(stats.calls).toBe(paths.length); + expect(stats.peak).toBeGreaterThan(1); + expect(entries.map((entry) => entry?.uuid ?? null)).toEqual([ + ...healed.map((file) => file.uuid), + null, + null, + null, + ]); + for (const file of healed) { + await expect(pathOf(file.uuid)).resolves.toBe(file.path); + } + }); + + it('surfaces the failure of the earliest path, whichever fails first', async () => { + const user = await makeUser(); + const paths = ['a', 'b', 'c', 'd'].map( + (name) => `${user.home}/Documents/${name}.txt`, + ); + const { spy } = watchLineageReads({ + 'b.txt': { error: new Error('b failed'), delayMs: 30 }, + 'd.txt': { error: new Error('d failed'), delayMs: 1 }, + }); + try { + await expect( + store.getEntriesByPathsForUser(user.userId, paths, { + skipCache: true, + }), + ).rejects.toThrow('b failed'); + } finally { + spy.mockRestore(); + } + }); + + it('skips the lineage walk when asked to', async () => { + const user = await makeUser(); + const file = await createFile(user, `${user.home}/Documents/nolin.txt`); + await blankPath(file.uuid); + + const { stats, spy } = watchLineageReads(); + try { + await expect( + store.getEntryByPath(file.path, { lineage: false }), + ).resolves.toBeNull(); + await expect( + store.getEntryByPath(file.path, { + lineage: false, + useTryHardRead: true, + }), + ).resolves.toBeNull(); + } finally { + spy.mockRestore(); + } + expect(stats.calls).toBe(0); + await expect(pathOf(file.uuid)).resolves.toBeNull(); + + // A row that stores its path is found either way. + const stored = await createFile(user, `${user.home}/Documents/lin.txt`); + await expect( + store.getEntryByPath(stored.path, { lineage: false }), + ).resolves.toMatchObject({ uuid: stored.uuid }); + }); + it('returns null when a path segment cannot be resolved', async () => { const user = await makeUser(); await expect( @@ -1327,6 +1445,172 @@ describe('FSEntryStore timestamps and updates', () => { }); }); + /** Every thumbnail UPDATE sent during `run`. */ + const captureThumbnailUpdates = async ( + run: () => Promise, + failBatch = false, + ) => { + const db = server.clients.db; + const originalWrite = (Object.getPrototypeOf(db) as typeof db).write; + const statements: unknown[][] = []; + const spy = vi + .spyOn(db, 'write') + .mockImplementation(async (sql: string, params?: unknown[]) => { + if (sql.includes('SET thumbnail = CASE')) { + statements.push(params ?? []); + // A batch of more than one uuid: (2n + 3) params + n. + if (failBatch && (params ?? []).length > 6) { + throw new Error('batch rejected'); + } + } + return originalWrite.call(db, sql, params); + }); + try { + await run(); + } finally { + spy.mockRestore(); + } + return statements; + }; + + it("updates a batch of the owner's thumbnails in one statement", async () => { + const user = await makeUser(); + const files = await Promise.all( + ['b1', 'b2', 'b3'].map((name) => + createFile(user, `${user.home}/Documents/${name}.png`), + ), + ); + + let results!: PromiseSettledResult[]; + const statements = await captureThumbnailUpdates(async () => { + results = await store.updateEntryThumbnailsByUuidForUser( + user.userId, + files.map((file, i) => ({ + uuid: file.uuid, + thumbnail: `thumb-${i}`, + })), + ); + }); + + expect(statements).toHaveLength(1); + results.forEach((result, i) => { + expect(result).toMatchObject({ + status: 'fulfilled', + value: { uuid: files[i]!.uuid, thumbnail: `thumb-${i}` }, + }); + }); + for (const [i, file] of files.entries()) { + const stored = await store.getEntryByUuidFromPrimary(file.uuid); + expect(stored?.thumbnail).toBe(`thumb-${i}`); + const fulfilled = results[i] as PromiseFulfilledResult; + expect(stored?.modified).toBe(fulfilled.value.modified); + expect(stored?.accessed).toBe(fulfilled.value.accessed); + await expect(store.getEntryByUuid(file.uuid)).resolves.toEqual( + fulfilled.value, + ); + } + }); + + it('closes a batch early once its thumbnails reach the size budget', async () => { + const user = await makeUser(); + const files = await Promise.all( + ['s1', 's2', 's3', 's4'].map((name) => + createFile(user, `${user.home}/Documents/${name}.png`), + ), + ); + // Inline data URLs: 1.5M + 1.5M fit one batch, a third would not, and + // one past the budget goes alone. + const inline = (chars: number, i: number) => + `data:image/png;base64,${String(i).repeat(chars)}`; + const thumbnails = [ + inline(1_500_000, 0), + inline(1_500_000, 1), + inline(1_500_000, 2), + inline(5_000_000, 3), + ]; + + let results!: PromiseSettledResult[]; + const statements = await captureThumbnailUpdates(async () => { + results = await store.updateEntryThumbnailsByUuidForUser( + user.userId, + files.map((file, i) => ({ + uuid: file.uuid, + thumbnail: thumbnails[i]!, + })), + ); + }); + + // Params per batch of n: 2n (CASE) + 3 + n (IN). + expect(statements.map((params) => (params.length - 3) / 3)).toEqual([ + 2, 1, 1, + ]); + expect(results.map((result) => result.status)).toEqual([ + 'fulfilled', + 'fulfilled', + 'fulfilled', + 'fulfilled', + ]); + for (const [i, file] of files.entries()) { + const stored = await store.getEntryByUuidFromPrimary(file.uuid); + expect(stored?.thumbnail).toBe(thumbnails[i]); + } + }); + + it('settles each entry alone when the batch matches only some', async () => { + const owner = await makeUser(); + const other = await makeUser(); + const mine = await createFile(owner, `${owner.home}/Documents/m.png`); + const theirs = await createFile(other, `${other.home}/Documents/t.png`); + + let results!: PromiseSettledResult[]; + const statements = await captureThumbnailUpdates(async () => { + results = await store.updateEntryThumbnailsByUuidForUser( + owner.userId, + [ + { uuid: theirs.uuid, thumbnail: 'hijacked' }, + { uuid: mine.uuid, thumbnail: 'mine' }, + ], + ); + }); + + // The batch, then one statement per entry. + expect(statements).toHaveLength(3); + expect(results[0]).toMatchObject({ + status: 'rejected', + reason: { statusCode: 404 }, + }); + expect(results[1]).toMatchObject({ + status: 'fulfilled', + value: { thumbnail: 'mine' }, + }); + await expect( + store.getEntryByUuidFromPrimary(theirs.uuid), + ).resolves.toMatchObject({ thumbnail: null }); + }); + + it('settles each entry alone when the batch statement fails', async () => { + const user = await makeUser(); + const files = await Promise.all( + ['f1', 'f2'].map((name) => + createFile(user, `${user.home}/Documents/${name}.png`), + ), + ); + + let results!: PromiseSettledResult[]; + const statements = await captureThumbnailUpdates(async () => { + results = await store.updateEntryThumbnailsByUuidForUser( + user.userId, + files.map((file) => ({ uuid: file.uuid, thumbnail: 'x' })), + ); + }, true); + + expect(statements).toHaveLength(3); + expect(results.map((result) => result.status)).toEqual([ + 'fulfilled', + 'fulfilled', + ]); + }); + it('updates a thumbnail by uuid and refreshes every cached key', async () => { const user = await makeUser(); const file = await createFile( @@ -1517,6 +1801,210 @@ describe('FSEntryStore timestamps and updates', () => { }); }); +describe('FSEntryStore read-path cache backfill', () => { + const cached = async (key: string) => + (await server.clients.redis.get(key)) as string | null; + + /** True while `promise` is still unsettled after a short wait. */ + const stillPending = async (promise: Promise) => { + const waiting = Symbol('waiting'); + const first = await Promise.race([ + promise, + new Promise((resolve) => setTimeout(() => resolve(waiting), 20)), + ]); + return first === waiting; + }; + + /** + * Holds every pipeline built until released, counting the SETEXs queued on + * it and any sent one at a time. + */ + const holdPipelines = () => { + const redis = server.clients.redis; + const realPipeline = redis.pipeline.bind(redis); + let release!: () => void; + const gate = new Promise((resolve) => { + release = resolve; + }); + const queued: string[][] = []; + const pipelineSpy = vi + .spyOn(redis, 'pipeline') + .mockImplementation( + (...args: Parameters) => { + const pipeline = realPipeline(...args); + const keys: string[] = []; + queued.push(keys); + const setex = pipeline.setex.bind(pipeline); + pipeline.setex = ((key: string, ...rest: never[]) => { + keys.push(key); + return setex(key, ...rest); + }) as typeof pipeline.setex; + const exec = pipeline.exec.bind(pipeline); + pipeline.exec = (async () => { + await gate; + return exec(); + }) as typeof pipeline.exec; + return pipeline; + }, + ); + const setexSpy = vi.spyOn(redis, 'setex'); + return { + queued, + setexSpy, + release: () => { + release(); + pipelineSpy.mockRestore(); + setexSpy.mockRestore(); + }, + }; + }; + + /** Holds every SETEX sent one at a time until released. */ + const holdSetex = () => { + const redis = server.clients.redis; + const realSetex = redis.setex.bind(redis); + let release!: () => void; + const gate = new Promise((resolve) => { + release = resolve; + }); + const setexSpy = vi.spyOn(redis, 'setex').mockImplementation((async ( + ...args: Parameters + ) => { + await gate; + return realSetex(...args); + }) as typeof redis.setex); + return { + setexSpy, + release: () => { + release(); + setexSpy.mockRestore(); + }, + }; + }; + + it('lists children once the backfill lands, all keys in one pipeline', async () => { + const user = await makeUser(); + const dir = await store.createNonFileEntry({ + parent: (await store.getEntryByPath(`${user.home}/Documents`))!, + name: 'backfill', + kind: 'directory', + }); + const files = await Promise.all( + ['x.txt', 'y.txt', 'z.txt'].map((name) => + createFile(user, `${dir.path}/${name}`), + ), + ); + await server.clients.redis.flushall?.(); + + const held = holdPipelines(); + let listing!: Promise; + try { + listing = store.listChildren(dir.uuid); + expect(await stillPending(listing)).toBe(true); + expect(held.setexSpy).not.toHaveBeenCalled(); + } finally { + held.release(); + } + const listed = await listing; + + expect(listed.map((entry) => entry.uuid).sort()).toEqual( + files.map((file) => file.uuid).sort(), + ); + expect(held.queued).toEqual([ + listed.flatMap((entry) => entryCacheKeys(entry)), + ]); + for (const entry of listed) { + for (const key of entryCacheKeys(entry)) { + expect(JSON.parse((await cached(key))!)).toEqual(entry); + } + } + }); + + it('lets a later invalidation from this process win over a read backfill', async () => { + const user = await makeUser(); + const file = await createFile(user, `${user.home}/Documents/order.txt`); + await server.clients.redis.flushall?.(); + + const byPath = await store.getEntryByPath(file.path); + await store.invalidateEntryCacheByUuid(file.uuid); + for (const key of entryCacheKeys(byPath!)) { + await expect(cached(key)).resolves.toBeNull(); + } + + const [listed] = await store.listChildren(byPath!.parentUid!); + await store.invalidateEntryCacheByUuid(listed!.uuid); + for (const key of entryCacheKeys(listed!)) { + await expect(cached(key)).resolves.toBeNull(); + } + }); + + it('answers a single-entry read once its backfill lands', async () => { + const user = await makeUser(); + const file = await createFile(user, `${user.home}/Documents/one.txt`); + const reads = [ + () => store.getEntryByPath(file.path), + () => store.getEntryById(file.id), + () => store.getRootEntryForUser(user.userId), + async () => + (await store.getEntriesByPaths([file.path])).get(file.path), + async () => (await store.getEntriesByIds([file.id])).get(file.id), + ]; + + for (const read of reads) { + await server.clients.redis.flushall?.(); + const held = holdSetex(); + let reading!: Promise; + try { + reading = read(); + expect(await stillPending(reading)).toBe(true); + expect(held.setexSpy).toHaveBeenCalled(); + } finally { + held.release(); + } + const entry = (await reading)!; + for (const key of entryCacheKeys(entry)) { + expect(JSON.parse((await cached(key))!)).toEqual(entry); + } + } + }); + + it('still answers a read whose backfill fails', async () => { + const user = await makeUser(); + const file = await createFile(user, `${user.home}/Documents/down.txt`); + await server.clients.redis.flushall?.(); + + const redis = server.clients.redis; + const setexSpy = vi + .spyOn(redis, 'setex') + .mockRejectedValue(new Error('cache down')); + const realPipeline = redis.pipeline.bind(redis); + const pipelineSpy = vi + .spyOn(redis, 'pipeline') + .mockImplementation( + (...args: Parameters) => { + const pipeline = realPipeline(...args); + pipeline.exec = (async () => { + throw new Error('cache down'); + }) as typeof pipeline.exec; + return pipeline; + }, + ); + try { + await expect( + store.getEntryByPath(file.path), + ).resolves.toMatchObject({ uuid: file.uuid }); + await expect(store.listChildren(file.parentUid!)).resolves.toEqual([ + expect.objectContaining({ uuid: file.uuid }), + ]); + expect(setexSpy).toHaveBeenCalled(); + expect(pipelineSpy).toHaveBeenCalled(); + } finally { + setexSpy.mockRestore(); + pipelineSpy.mockRestore(); + } + }); +}); + describe('FSEntryStore listing and pagination', () => { let user: StoreUser; let parent: FSEntry; @@ -1657,6 +2145,23 @@ describe('FSEntryStore listing and pagination', () => { await expect(store.countChildren(uuidv4())).resolves.toBe(0); }); + it('reports children exactly when a one-row listing finds one', async () => { + const sub = (await store.getEntryByPath(`${parent.path}/sub`))!; + const leaf = (await store.getEntryByPath(`${parent.path}/a.txt`))!; + for (const uid of [parent.uuid, sub.uuid, leaf.uuid, uuidv4()]) { + const listed = await store.listChildren(uid, { limit: 1 }); + await expect(store.hasChildrenByParentUid(uid)).resolves.toBe( + listed.length > 0, + ); + } + await expect(store.hasChildrenByParentUid(leaf.uuid)).resolves.toBe( + false, + ); + await expect(store.hasChildrenByParentUid(parent.uuid)).resolves.toBe( + true, + ); + }); + it('lists and counts descendants, refusing to walk from root', async () => { const descendants = await store.listDescendantsByPath(parent.path); expect(descendants.map((entry) => entry.name).sort()).toEqual([ @@ -2491,10 +2996,9 @@ describe('FSEntryStore pending upload sessions', () => { it('creates the real entry when a session completes', async () => { const user = await makeUser(); - const session = makeSession(user); - await store.createPendingEntry(session); + const session = await store.createPendingEntry(makeSession(user)); - const entry = await store.completePendingEntry(session.sessionId, { + const entry = await store.completePendingEntry(session, { userId: user.userId, uuid: session.objectKey, path: session.targetPath, @@ -2521,14 +3025,15 @@ describe('FSEntryStore pending upload sessions', () => { const second = makeSession(user, { targetPath: `${user.home}/Documents/bp2.txt`, }); - await store.batchCreatePendingEntries([first, second]); + const [firstSession, secondSession] = + await store.batchCreatePendingEntries([first, second]); await expect(store.batchCompletePendingEntries([])).resolves.toEqual( [], ); const entries = await store.batchCompletePendingEntries([ { - sessionId: first.sessionId, + session: firstSession!, finalData: { userId: user.userId, uuid: first.objectKey, @@ -2539,7 +3044,7 @@ describe('FSEntryStore pending upload sessions', () => { }, }, { - sessionId: second.sessionId, + session: secondSession!, finalData: { userId: user.userId, uuid: second.objectKey, @@ -2562,6 +3067,78 @@ describe('FSEntryStore pending upload sessions', () => { } }); + const finalDataFor = ( + user: StoreUser, + session: { objectKey: string; targetPath: string }, + ): FSEntryCreateInput => ({ + userId: user.userId, + uuid: session.objectKey, + path: session.targetPath, + size: 4, + bucket: 'puter-local', + bucketRegion: 'us-west-2', + }); + + it('completes from the caller’s copy without reading the session back', async () => { + const user = await makeUser(); + const session = await store.createPendingEntry( + makeSession(user, { + targetPath: `${user.home}/Documents/in-hand.txt`, + }), + ); + // Written in between: the completion still rewrites every field a + // status change touches. + await store.markPendingEntryFailed(session.sessionId, 'flaky'); + + const get = vi.spyOn(server.stores.kv, 'get'); + try { + await store.completePendingEntry( + session, + finalDataFor(user, session), + ); + expect(get).not.toHaveBeenCalled(); + } finally { + get.mockRestore(); + } + + const stored = (await store.getPendingEntryBySessionId( + session.sessionId, + ))!; + expect(stored).toEqual({ + ...session, + status: 'completed', + failureReason: null, + updatedAt: stored.completedAt, + consumedAt: stored.completedAt, + completedAt: expect.any(Number), + }); + }); + + it('leaves an expired session for the sweep instead of rewriting it', async () => { + const user = await makeUser(); + const session = await store.createPendingEntry( + makeSession(user, { + targetPath: `${user.home}/Documents/lapsed.txt`, + expiresAt: Date.now() - 1_000, + }), + ); + await expect( + store.getPendingEntryBySessionId(session.sessionId), + ).resolves.toBeNull(); + + const put = vi.spyOn(server.stores.kv, 'batchPut'); + try { + const entry = await store.completePendingEntry( + session, + finalDataFor(user, session), + ); + expect(entry.path).toBe(session.targetPath); + expect(put).not.toHaveBeenCalled(); + } finally { + put.mockRestore(); + } + }); + describe('partitioning', () => { const readStored = async (sessionId: string, partitioned: boolean) => { const { res } = await server.stores.kv.get( @@ -2699,6 +3276,290 @@ describe('FSEntryStore storage allowance', () => { server.clients.event.off?.('storage.quota.bonus', bonus); }); + + it('runs bonus listeners together, each adding its share, and names the user by uuid', async () => { + const user = await makeUser(); + const { uuid } = (await server.stores.user.getById(user.userId))!; + let inFlight = 0; + let peak = 0; + const seen: unknown[] = []; + const share = + (bytes: number) => + async ( + _key: string, + event: { userId: number; userUuid?: string; extra: number }, + ) => { + if (event.userId !== user.userId) return; + seen.push(event.userUuid); + inFlight++; + peak = Math.max(peak, inFlight); + const granted = await new Promise((resolve) => + setTimeout(() => resolve(bytes), 10), + ); + inFlight--; + event.extra += granted; + }; + const listeners = [share(100), share(1000)]; + for (const listener of listeners) { + server.clients.event.on('storage.quota.bonus', listener); + } + // The shared server doesn't limit storage, so `max` would ignore the + // bonus; same clients, limited config. + const limited = new FSEntryStore( + { port: 0, extensions: [], is_storage_limited: true }, + server.clients as ConstructorParameters[1], + server.stores as ConstructorParameters[2], + ); + try { + for (const consistentRead of [false, true]) { + await expect( + limited.getUserStorageAllowance(user.userId, { + consistentRead, + }), + ).resolves.toEqual({ curr: 0, max: 100 * 1024 * 1024 + 1100 }); + } + } finally { + for (const listener of listeners) { + server.clients.event.off('storage.quota.bonus', listener); + } + } + expect(peak).toBe(2); + expect(seen).toEqual([uuid, uuid, uuid, uuid]); + }); + + it('surfaces a failed allowance read and never asks the listeners without the user row', async () => { + const user = await makeUser(); + const db = server.clients.db; + const originalRead = (Object.getPrototypeOf(db) as typeof db).read; + const asked: unknown[] = []; + let firstAsked!: () => void; + const askedOnce = new Promise((resolve) => { + firstAsked = resolve; + }); + const listener = (_key: string, event: { userId: number }) => { + if (event.userId !== user.userId) return; + asked.push(event); + firstAsked(); + }; + server.clients.event.on('storage.quota.bonus', listener); + const failOn = (fragment: string) => + vi + .spyOn(db, 'read') + .mockImplementation(async (sql: string, params?: unknown[]) => { + if (sql.includes(fragment)) throw new Error(fragment); + return originalRead.call(db, sql, params); + }); + try { + // The listeners start off the user row, beside the usage read, so + // a failed usage read still surfaces while they run. + const usageSpy = failOn('SUM(size)'); + await expect( + store.getUserStorageAllowance(user.userId), + ).rejects.toThrow('SUM(size)'); + usageSpy.mockRestore(); + await askedOnce; + + const userSpy = failOn('free_storage'); + await expect( + store.getUserStorageAllowance(user.userId), + ).rejects.toThrow('free_storage'); + userSpy.mockRestore(); + await new Promise((resolve) => setTimeout(resolve, 50)); + expect(asked).toHaveLength(1); + } finally { + server.clients.event.off('storage.quota.bonus', listener); + } + }); +}); + +describe('FSEntryStore created-file row', () => { + /** Primary reads of rows just inserted, during `run`. */ + const countReadBacks = async (run: () => Promise) => { + const spy = vi.spyOn(server.clients.db, 'pread'); + try { + await run(); + return spy.mock.calls.filter(([sql]) => + String(sql).includes('WHERE uuid IN'), + ).length; + } finally { + spy.mockRestore(); + } + }; + + /** Mapped and serialized, the entry must match the stored row exactly. */ + const expectStored = async (entry: FSEntry) => { + const stored = await store.getEntryByUuidFromPrimary(entry.uuid); + expect(entry).toStrictEqual(stored); + expect(JSON.stringify(entry)).toBe(JSON.stringify(stored)); + return stored!; + }; + + /** Runs `run` with fsentries inserts reporting `warningStatus`. */ + const withInsertWarnings = async ( + warningStatus: number, + run: () => Promise, + ): Promise => { + const db = server.clients.db; + const originalWrite = (Object.getPrototypeOf(db) as typeof db).write; + const spy = vi + .spyOn(db, 'write') + .mockImplementation(async (sql: string, params?: unknown[]) => { + const result = await originalWrite.call(db, sql, params); + return /INTO\s+`?fsentries`?/i.test(sql) + ? { ...result, warningStatus } + : result; + }); + try { + return await run(); + } finally { + spy.mockRestore(); + } + }; + + // SQLite never vouches for an insert, so its schema is checked against + // the built row with the clean result MySQL reports. + const vouchedOnSqlite = (run: () => Promise): Promise => + server.clients.db.engineName === 'sqlite' + ? withInsertWarnings(0, run) + : run(); + + it('returns what the database stored, field for field, without reading it back', async () => { + const user = await makeUser(); + const app = (await server.stores.app.create( + { + name: `row-${uuidv4()}`, + title: 'Created row test', + index_url: 'https://created-row.test/', + }, + { ownerUserId: user.userId }, + )) as { id: number }; + const cases: Array<[string, Partial]> = [ + ['plain.txt', {}], + [ + 'every-column.png', + { + size: 2 ** 40, + isPublic: true, + immutable: true, + thumbnail: 'data:image/png;base64,AAAA', + associatedAppId: app.id, + metadata: { note: 'é €', nested: { n: 1 } }, + contentType: 'image/png', + checksumSha256: 'abc123', + }, + ], + [ + 'raw-metadata.bin', + { size: 0, isPublic: false, metadata: '{"b":2, "a":1}' }, + ], + ['nullish.txt', { isPublic: null, metadata: null }], + ['ünïcödé 🔥 名前 trailing .txt', { thumbnail: 'café' }], + ]; + + for (const [name, over] of cases) { + let entry!: FSEntry; + const readBacks = await countReadBacks(() => + vouchedOnSqlite(async () => { + entry = await createFile( + user, + `${user.home}/Documents/${name}`, + over, + ); + }), + ); + // Postgres reports no insert id for this statement. + expect(readBacks).toBe( + server.clients.db.engineName === 'postgres' ? 1 : 0, + ); + await expectStored(entry); + await expect( + store.getEntryByPath(`${user.home}/Documents/${name}`), + ).resolves.toStrictEqual(entry); + } + }); + + it('reads the row back when a value may not be stored as sent', async () => { + const user = await makeUser(); + const cases: Array<[string, Partial]> = [ + // A lone surrogate reaches the database as U+FFFD. + ['surrogate.txt', { metadata: 'bad \ud800 text' }], + ]; + // MySQL rounds a fractional integer without a warning; Postgres + // rejects it. + if (server.clients.db.engineName !== 'postgres') { + cases.push(['fraction.txt', { size: 1.5 }]); + } + for (const [name, over] of cases) { + let entry!: FSEntry; + const readBacks = await countReadBacks(() => + vouchedOnSqlite(async () => { + entry = await createFile( + user, + `${user.home}/Documents/${name}`, + over, + ); + }), + ); + expect(readBacks).toBe(1); + await expectStored(entry); + } + }); + + it('reads the row back when the insert raised a warning', async () => { + const user = await makeUser(); + let entry!: FSEntry; + const readBacks = await countReadBacks(() => + withInsertWarnings(1, async () => { + entry = await createFile( + user, + `${user.home}/Documents/warned.txt`, + ); + }), + ); + expect(readBacks).toBe(1); + await expectStored(entry); + }); + + // SQLite hands a new row the rowid of a deleted last row, and nothing + // clears a subdomain still naming that id, so only a read sees the join. + it('returns the stored row when the new id once named a deleted row', async () => { + const user = await makeUser(); + const hosted = await createFile( + user, + `${user.home}/Public/hosted.html`, + ); + await server.stores.subdomain.create({ + userId: user.userId, + subdomain: `reused-${Math.random().toString(36).slice(2, 10)}`, + rootDirId: hosted.id, + }); + await store.deleteEntry(hosted); + + const fresh = await createFile( + user, + `${user.home}/Documents/fresh.txt`, + ); + await expectStored(fresh); + }); + + it('reads a multi-row insert back in one query', async () => { + const user = await makeUser(); + let entries!: FSEntry[]; + const readBacks = await countReadBacks(async () => { + entries = await store.batchCreateEntries( + ['m1.txt', 'm2.txt', 'm3.txt'].map((name) => ({ + userId: user.userId, + uuid: uuidv4(), + path: `${user.home}/Documents/${name}`, + size: 3, + bucket: 'puter-local', + bucketRegion: 'us-west-2', + })), + ); + }); + expect(readBacks).toBe(1); + for (const entry of entries) await expectStored(entry); + }); }); describe('FSEntryStore lost-insert recovery', () => { diff --git a/src/backend/stores/fs/FSEntryStore.ts b/src/backend/stores/fs/FSEntryStore.ts index 4a66b6af5..8ea01357b 100644 --- a/src/backend/stores/fs/FSEntryStore.ts +++ b/src/backend/stores/fs/FSEntryStore.ts @@ -21,9 +21,13 @@ import { statfs } from 'node:fs/promises'; import { posix as pathPosix } from 'node:path'; import { assertNormalized } from '../../services/fs/resolveNode.js'; import { v4 as uuidv4 } from 'uuid'; +import type { WriteResult } from '../../clients/database/DatabaseClient.js'; import { HttpError } from '../../core/http/HttpError.js'; import type { LayerInstances } from '../../types.js'; -import { runWithConcurrencyLimit } from '../../util/concurrency.js'; +import { + runWithConcurrencyLimit, + runWithConcurrencyLimitSettled, +} from '../../util/concurrency.js'; import { escapeLike } from '../../util/sqlLike.js'; import { keysetPage, @@ -113,6 +117,11 @@ const sortValue = ( } }; +const thumbnailEntryNotFound = () => + new HttpError(404, 'File entry was not found for thumbnail update', { + legacyCode: 'not_found', + }); + const ENTRY_CACHE_TTL_SECONDS = 60; const entryCacheKey = ( by: 'id' | 'uuid' | 'path', @@ -131,6 +140,13 @@ const BULK_QUERY_CHUNK_SIZE = 200; const SUBTREE_PATTERNS_PER_STATEMENT = 500; const SUBTREE_PATTERNS_MAX = 5000; const DEFAULT_DB_CHUNK_CONCURRENCY = 4; +// Two replica batcher flushes' worth of lineage lookups in flight. +const LINEAGE_LOOKUP_CONCURRENCY = 10; +// Cache refreshes after a batched update; each is a cache read and write. +const ENTRY_PATCH_CONCURRENCY = 32; +// Thumbnail text one batched UPDATE carries; inline data URLs run to +// megabytes, and the statement must stay under the server's packet limit. +const THUMBNAIL_BATCH_CHARS = 4 * 1024 * 1024; // Most entries whose invalidations one node broadcasts per broadcast flush // window (~200 bytes an entry); past the cap, invalidations stay local and // peers wait out ENTRY_CACHE_TTL_SECONDS. @@ -404,6 +420,27 @@ export class FSEntryStore extends PuterStore { } } + /** `#writeEntryToCache` for many entries, in one round trip. */ + async #writeEntriesToCache(entries: FSEntry[]): Promise { + if (entries.length === 0) return; + try { + const pipeline = this.clients.redis.pipeline(); + for (const entry of entries) { + const serialized = JSON.stringify(entry); + for (const cacheKey of this.#entryCacheKeys(entry)) { + pipeline.setex( + cacheKey, + ENTRY_CACHE_TTL_SECONDS, + serialized, + ); + } + } + await pipeline.exec(); + } catch { + // Best effort cache write. + } + } + async #invalidateEntryCache(entry: FSEntry): Promise { await this.#invalidateEntriesCache([entry]); } @@ -766,16 +803,25 @@ export class FSEntryStore extends PuterStore { // Per-path lineage fallback for entries the bulk SELECT missed — // legacy rows with a NULL `path` column (see #resolveEntryByPathLineage). - // Sequential to keep the round-trip cost predictable; the common case - // is a single missing path. - const stillMissing = normalizedPaths.filter( - (path) => !entriesByPath.has(path), + // Every path of a fresh upload lands here, so the lookups run + // concurrently and share batcher flushes; the first failure in path + // order is the one surfaced. + const stillMissing = + options.lineage === false + ? [] + : normalizedPaths.filter((path) => !entriesByPath.has(path)); + const lineageResults = await runWithConcurrencyLimitSettled( + stillMissing, + LINEAGE_LOOKUP_CONCURRENCY, + async (path) => ({ + path, + entry: await this.#resolveEntryByPathLineage(path), + }), ); - for (const path of stillMissing) { - const entry = await this.#resolveEntryByPathLineage(path); - if (entry) { - entriesByPath.set(path, entry); - } + for (const result of lineageResults) { + if (result.status === 'rejected') throw result.reason; + const { path, entry } = result.value; + if (entry) entriesByPath.set(path, entry); } return entriesByPath; @@ -1152,6 +1198,7 @@ export class FSEntryStore extends PuterStore { // segment, then walk down via `parent_uid` chains. Backfills the // `path` column on every resolved row so subsequent calls take // the fast `WHERE path = ?` branch above. + if (options.lineage === false) return null; return this.#resolveEntryByPathLineage(normalizedPath); } @@ -1596,60 +1643,112 @@ export class FSEntryStore extends PuterStore { uuid: string, thumbnail: string | null, ): Promise { - const now = Math.floor(Date.now() / 1000); - const writeResult = await this.clients.db.write( - `UPDATE fsentries - SET thumbnail = ?, - modified = ?, - accessed = ? - WHERE uuid = ? AND user_id = ?`, - [thumbnail, now, now, uuid, userId], - ); - if (typeof writeResult === 'object' && writeResult !== null) { - const writeResultRecord = writeResult as unknown as Record< - string, - unknown - >; - const anyRowsAffected = writeResultRecord.anyRowsAffected; - if (typeof anyRowsAffected === 'boolean' && !anyRowsAffected) { - throw new HttpError( - 404, - 'File entry was not found for thumbnail update', - { legacyCode: 'not_found' }, - ); - } + const [result] = await this.updateEntryThumbnailsByUuidForUser(userId, [ + { uuid, thumbnail }, + ]); + if (result?.status !== 'fulfilled') { + throw result?.reason ?? thumbnailEntryNotFound(); + } + return result.value; + } - const affectedRowsRaw = writeResultRecord.affectedRows; - const affectedRows = Number(affectedRowsRaw); + /** + * Set the thumbnails of entries `userId` owns with one UPDATE per chunk, + * each result in input order: the entry as patched, or why that one failed. + * A chunk the UPDATE fails or only partly matches is redone one entry at a + * time, which tells the misses apart. + */ + async updateEntryThumbnailsByUuidForUser( + userId: number, + updates: Array<{ uuid: string; thumbnail: string | null }>, + ): Promise[]> { + const chunks: Array = []; + let chunk: typeof updates = []; + let chars = 0; + for (const update of updates) { + const size = update.thumbnail?.length ?? 0; if ( - affectedRowsRaw !== undefined && - Number.isFinite(affectedRows) && - affectedRows <= 0 + chunk.length >= BULK_QUERY_CHUNK_SIZE || + (chunk.length > 0 && chars + size > THUMBNAIL_BATCH_CHARS) ) { - throw new HttpError( - 404, - 'File entry was not found for thumbnail update', - { legacyCode: 'not_found' }, - ); + chunks.push(chunk); + chunk = []; + chars = 0; } + chunk.push(update); + chars += size; + } + if (chunk.length > 0) chunks.push(chunk); + + const results: PromiseSettledResult[] = []; + for (const rows of chunks) { + results.push(...(await this.#updateThumbnailChunk(userId, rows))); + } + return results; + } + + async #updateThumbnailChunk( + userId: number, + chunk: Array<{ uuid: string; thumbnail: string | null }>, + ): Promise[]> { + const now = Math.floor(Date.now() / 1000); + const oneByOne = () => + runWithConcurrencyLimitSettled( + chunk, + DEFAULT_DB_CHUNK_CONCURRENCY, + ({ uuid, thumbnail }) => + this.updateEntryThumbnailByUuidForUser( + userId, + uuid, + thumbnail, + ), + ); + let matched: number; + try { + const writeResult = await this.clients.db.write( + `UPDATE fsentries + SET thumbnail = CASE uuid ${chunk.map(() => 'WHEN ? THEN ?').join(' ')} END, + modified = ?, + accessed = ? + WHERE user_id = ? AND uuid IN (${chunk.map(() => '?').join(', ')})`, + [ + ...chunk.flatMap(({ uuid, thumbnail }) => [ + uuid, + thumbnail, + ]), + now, + now, + userId, + ...chunk.map(({ uuid }) => uuid), + ], + ); + matched = this.#affectedRows(writeResult); + } catch (error) { + if (chunk.length > 1) return oneByOne(); + return [{ status: 'rejected', reason: error }]; + } + if (matched !== chunk.length) { + if (chunk.length > 1) return oneByOne(); + return [{ status: 'rejected', reason: thumbnailEntryNotFound() }]; } - // The UPDATE above already proved the row exists and belongs to this - // user, and named every column that moved — so the result is knowable - // without reading the row back. - const updatedEntry = await this.#entryAfterPatch(uuid, { - thumbnail, - modified: now, - accessed: now, - }); - if (!updatedEntry || updatedEntry.userId !== userId) { - throw new HttpError( - 404, - 'File entry was not found for thumbnail update', - { legacyCode: 'not_found' }, - ); - } - return updatedEntry; + // The UPDATE proved each row exists and belongs to this user, and + // named every column that moved, so no row needs reading back. + return runWithConcurrencyLimitSettled( + chunk, + ENTRY_PATCH_CONCURRENCY, + async ({ uuid, thumbnail }) => { + const updatedEntry = await this.#entryAfterPatch(uuid, { + thumbnail, + modified: now, + accessed: now, + }); + if (!updatedEntry || updatedEntry.userId !== userId) { + throw thumbnailEntryNotFound(); + } + return updatedEntry; + }, + ); } async resolveParentDirectory( @@ -2084,6 +2183,7 @@ export class FSEntryStore extends PuterStore { insertCandidates, BULK_QUERY_CHUNK_SIZE, ); + const insertedEntriesByUuid = new Map(); await runWithConcurrencyLimit( insertChunks, DEFAULT_DB_CHUNK_CONCURRENCY, @@ -2094,6 +2194,7 @@ export class FSEntryStore extends PuterStore { const valuePlaceholders: string[] = []; const values: unknown[] = []; + const rows: FSEntryRow[] = []; for (const entry of insertChunk) { const parentEntry = parentByPath.get(entry.parentPath); if (!parentEntry) { @@ -2104,33 +2205,33 @@ export class FSEntryStore extends PuterStore { ); } + const row = this.#fileInsertRow( + entry, + parentEntry, + now, + ); + rows.push(row); valuePlaceholders.push( `(?, ?, ?, ?, ?, ?, ?, ${this.clients.db.booleanLiteral(false)}, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, ); values.push( - entry.input.uuid, - entry.bucket, - entry.bucketRegion, - parentEntry.userId, - parentEntry.id, - parentEntry.uuid, - entry.input.associatedAppId ?? null, - entry.input.isPublic === undefined - ? null - : this.clients.db.booleanValue( - entry.input.isPublic, - ), - entry.input.thumbnail ?? null, - this.clients.db.booleanValue( - Boolean(entry.input.immutable), - ), - entry.fileName, - entry.targetPath, - entry.metadataJson, - now, - now, - now, - entry.size, + row.uuid, + row.bucket, + row.bucket_region, + row.user_id, + row.parent_id, + row.parent_uid, + row.associated_app_id, + row.is_public, + row.thumbnail, + row.immutable, + row.name, + row.path, + row.metadata, + row.modified, + row.created, + row.accessed, + row.size, ); } @@ -2140,7 +2241,7 @@ export class FSEntryStore extends PuterStore { // detecting uuids missing from the post-INSERT SELECT // and running the same overwrite-or-409 logic the // upfront existence check uses. - await this.clients.db.write( + const written = await this.clients.db.write( `${this.#insertIgnoreIntoFsentriesSql()} ( uuid, bucket, @@ -2163,13 +2264,34 @@ export class FSEntryStore extends PuterStore { ) VALUES ${valuePlaceholders.join(', ')}${this.clients.db.insertIgnoreSuffix()}`, values, ); + + const [onlyRow] = rows; + if (rows.length === 1 && onlyRow) { + const stored = this.#storedAsWritten(onlyRow, written); + if (stored) { + insertedEntriesByUuid.set( + stored.uuid, + this.#mapFSEntryRow(stored), + ); + } + } }, ); - const insertedEntriesByUuid = new Map(); - if (insertCandidates.length > 0) { + if (insertedEntriesByUuid.size > 0) { + await Promise.all( + [...insertedEntriesByUuid.values()].map((entry) => + this.#writeEntryToCache(entry), + ), + ); + } + + const unreadCandidates = insertCandidates.filter( + (entry) => !insertedEntriesByUuid.has(entry.input.uuid), + ); + if (unreadCandidates.length > 0) { const insertUuidChunks = this.#chunk( - insertCandidates.map((entry) => entry.input.uuid), + unreadCandidates.map((entry) => entry.input.uuid), BULK_QUERY_CHUNK_SIZE, ); @@ -2381,6 +2503,89 @@ export class FSEntryStore extends PuterStore { return createdEntries; } + /** The columns a file INSERT sets, exactly as sent. */ + #fileInsertRow( + entry: NormalizedEntryWrite, + parent: FSEntry, + now: number, + ): FSEntryRow { + return { + id: 0, + uuid: entry.input.uuid, + bucket: entry.bucket, + bucket_region: entry.bucketRegion, + public_token: null, + file_request_token: null, + is_shortcut: 0, + shortcut_to: null, + user_id: parent.userId, + parent_id: parent.id, + parent_uid: parent.uuid, + associated_app_id: entry.input.associatedAppId ?? null, + is_dir: this.clients.db.booleanValue(false), + layout: null, + sort_by: null, + sort_order: null, + is_public: + entry.input.isPublic === undefined + ? null + : this.clients.db.booleanValue(entry.input.isPublic), + thumbnail: entry.input.thumbnail ?? null, + immutable: this.clients.db.booleanValue( + Boolean(entry.input.immutable), + ), + name: entry.fileName, + metadata: entry.metadataJson, + modified: now, + created: now, + accessed: now, + size: entry.size, + symlink_path: null, + is_symlink: 0, + path: entry.targetPath, + // A row this new has no subdomains. + subdomains_agg: null, + }; + } + + /** + * `row` with its new id, when the write proves the database stored it as + * sent: one row inserted, no warning (a truncated or converted value warns) + * and an id. Null otherwise, and for values stored without a warning but + * not as sent: a fractional integer is rounded, a lone surrogate replaced. + */ + #storedAsWritten(row: FSEntryRow, written: WriteResult): FSEntryRow | null { + const insertId = Number(written.insertId); + if ( + written.affectedRows !== 1 || + written.warningStatus !== 0 || + !Number.isSafeInteger(insertId) || + insertId <= 0 + ) { + return null; + } + const exactInteger = (value: unknown) => + value === null || Number.isSafeInteger(value); + const exactText = (value: unknown) => + value === null || + (typeof value === 'string' && value.isWellFormed()); + if ( + ![row.size, row.associated_app_id].every(exactInteger) || + ![ + row.uuid, + row.bucket, + row.bucket_region, + row.thumbnail, + row.name, + row.metadata, + row.path, + ].every(exactText) + ) { + return null; + } + return { ...row, id: insertId }; + } + async createPendingEntry( entry: PendingUploadCreateInput, ): Promise { @@ -2466,11 +2671,11 @@ export class FSEntryStore extends PuterStore { } async completePendingEntry( - sessionId: string, + session: PendingUploadSession, finalData: FSEntryCreateInput, ): Promise { const [completedEntry] = await this.batchCompletePendingEntries([ - { sessionId, finalData }, + { session, finalData }, ]); if (!completedEntry) { throw new Error('Failed to complete pending entry'); @@ -2478,8 +2683,16 @@ export class FSEntryStore extends PuterStore { return completedEntry; } + /** + * Marks each session completed from the caller's copy rather than a fresh + * read: a completion rewrites every field that changes after creation, so + * the stored item comes out the same either way. + */ async batchCompletePendingEntries( - entries: { sessionId: string; finalData: FSEntryCreateInput }[], + entries: { + session: PendingUploadSession; + finalData: FSEntryCreateInput; + }[], ): Promise { if (entries.length === 0) { return []; @@ -2490,10 +2703,18 @@ export class FSEntryStore extends PuterStore { true, ); - await this.#markPendingSessionsWithStatus( - entries.map((entry) => entry.sessionId), - 'completed', - null, + const now = Date.now(); + // An expired session reads as missing; leave it to the sweep. + const live = entries.filter( + ({ session }) => + toPendingUploadSessionExpiresAtSeconds(session.expiresAt) > + now / 1000, + ); + await this.#writePendingUploadSessions( + live.map(({ session }) => + withPendingUploadSessionStatus(session, 'completed', null, now), + ), + 'mark pending upload sessions as completed', ); return completedEntries; @@ -2823,15 +3044,25 @@ export class FSEntryStore extends PuterStore { return Number(rows[0]?.n ?? 0); } + /** + * Whether `listChildren(parentUid)` would return any row: same key, same + * replica. `hasChildren` is the primary read that deletes rely on. + */ + async hasChildrenByParentUid(parentUid: string): Promise { + const rows = await this.clients.db.read( + 'SELECT 1 AS present FROM fsentries WHERE parent_uid = ? LIMIT 1', + [parentUid], + ); + return rows.length > 0; + } + async #finalizeChildEntries(rows: FSEntryRow[]): Promise { const entries = rows.map((row) => this.#mapFSEntryRow(row)); // Heal NULL-path rows before they reach the controller — readdir // consumers (GUI, suggested-apps, downstream `pathPosix.dirname` // callers) all expect a populated path on every child. await this.#healEntriesWithMissingPathsInPlace(entries); - await Promise.all( - entries.map((entry) => this.#writeEntryToCache(entry)), - ); + await this.#writeEntriesToCache(entries); return entries; } @@ -3712,15 +3943,18 @@ export class FSEntryStore extends PuterStore { const read = opts.consistentRead ? this.clients.db.pread.bind(this.clients.db) : this.clients.db.read.bind(this.clients.db); + const usageRowsPromise = read( + `SELECT COALESCE(SUM(size), 0) AS ${this.clients.db.quoteIdentifier('totalUsage')} FROM fsentries WHERE user_id = ?`, + [userId], + ) as Promise<{ totalUsage: number }[]>; + const userRowsPromise = read( + `SELECT uuid, free_storage AS ${this.clients.db.quoteIdentifier('freeStorage')} FROM ${this.clients.db.quoteIdentifier('user')} WHERE id = ? LIMIT 1`, + [userId], + ) as Promise<{ uuid: string; freeStorage: number | null }[]>; + const bonusPromise = this.#storageQuotaBonus(userId, userRowsPromise); const [usageRows, userRows] = await Promise.all([ - read( - `SELECT COALESCE(SUM(size), 0) AS ${this.clients.db.quoteIdentifier('totalUsage')} FROM fsentries WHERE user_id = ?`, - [userId], - ) as Promise<{ totalUsage: number }[]>, - read( - `SELECT free_storage AS ${this.clients.db.quoteIdentifier('freeStorage')} FROM ${this.clients.db.quoteIdentifier('user')} WHERE id = ? LIMIT 1`, - [userId], - ) as Promise<{ freeStorage: number | null }[]>, + usageRowsPromise, + userRowsPromise, ]); const usageRow = usageRows[0]; const userRow = userRows[0]; @@ -3730,18 +3964,9 @@ export class FSEntryStore extends PuterStore { userRow?.freeStorage ?? this.config.storage_capacity ?? 0, ); - const event: { userId: number; extra: number } = { userId, extra: 0 }; - try { - await this.clients.event.emitAndWait( - 'storage.quota.bonus', - event, - {}, - ); - } catch { - /* best-effort */ - } - if (Number.isFinite(event.extra) && event.extra > 0) { - max += event.extra; + const extra = await bonusPromise; + if (Number.isFinite(extra) && extra > 0) { + max += extra; } if (!this.config.is_storage_limited) { @@ -3762,6 +3987,34 @@ export class FSEntryStore extends PuterStore { return { curr, max }; } + /** + * What `storage.quota.bonus` listeners add, or 0. They start once the user + * row names the uuid to pass them, alongside the usage read, and never + * reject: a failed user read is the caller's to surface. + */ + async #storageQuotaBonus( + userId: number, + userRows: Promise>, + ): Promise { + let userUuid: string | undefined; + try { + userUuid = (await userRows)[0]?.uuid; + } catch { + return 0; + } + const event = { userId, ...(userUuid ? { userUuid } : {}), extra: 0 }; + try { + await this.clients.event.emitAndWaitConcurrently( + 'storage.quota.bonus', + event, + {}, + ); + } catch { + /* best-effort */ + } + return event.extra; + } + async #getFreeDeviceBytes(): Promise { try { const stats = await statfs(process.cwd()); diff --git a/src/backend/stores/fs/types.ts b/src/backend/stores/fs/types.ts index 23dffdb7e..c1cf46448 100644 --- a/src/backend/stores/fs/types.ts +++ b/src/backend/stores/fs/types.ts @@ -79,4 +79,9 @@ export interface ReadEntriesByPathsOptions { * paths in shared folders the writer has been granted access to). */ crossNamespace?: boolean; + /** + * `false` skips the walk that finds legacy rows stored without a `path`. + * Only for paths this backend wrote, which always store one. + */ + lineage?: boolean; } diff --git a/src/backend/stores/session/SessionStore.js b/src/backend/stores/session/SessionStore.js index 7033047d4..37c1f5dbd 100644 --- a/src/backend/stores/session/SessionStore.js +++ b/src/backend/stores/session/SessionStore.js @@ -20,6 +20,7 @@ import { v4 as uuidv4 } from 'uuid'; import { HttpError } from '../../core/http/HttpError.js'; +import { awaitLater } from '../../util/concurrency.js'; import { isMissingParentViolation, isUniqueViolation, @@ -215,7 +216,10 @@ export class SessionStore extends PuterStore { * @returns The created session row. */ async create(userId, opts = {}) { - return this.#insertSession(userId, opts, { ignoreConflict: false }); + const { row } = await this.#insertSession(userId, opts, { + ignoreConflict: false, + }); + return row; } /** @@ -223,7 +227,8 @@ export class SessionStore extends PuterStore { * `getOrCreate*` paths. With `ignoreConflict: true`, only a duplicate- key * error from the partial unique indexes is swallowed (concurrent caller won * the race); every other failure — CHECK, NOT NULL, FK, type — throws. The - * caller then re-SELECTs to find the winning row. + * caller then re-SELECTs to find the winning row. `inserted` is false only + * for that swallowed duplicate. */ async #insertSession( userId, @@ -245,6 +250,7 @@ export class SessionStore extends PuterStore { ) { const uuid = uuidv4(); const now = nowSeconds(); + let inserted = true; meta.created = new Date().toISOString(); meta.created_unix = now; @@ -294,6 +300,7 @@ export class SessionStore extends PuterStore { // `row` object below is a placeholder for the caller's // `winner ?? created` fallback shape and is never cached on // the ignoreConflict path. + inserted = false; } const row = { @@ -325,7 +332,7 @@ export class SessionStore extends PuterStore { this.#writeCache(row).catch(() => {}); } - return row; + return { row, inserted }; } /** @@ -701,7 +708,7 @@ export class SessionStore extends PuterStore { } } - const row = await this.#insertSession(userId, { + const { row } = await this.#insertSession(userId, { kind: 'access_token', label, parent_session_id: parentSessionUid, @@ -751,9 +758,16 @@ export class SessionStore extends PuterStore { } const selectRow = (opts) => this.#selectAppRow(userId, appUid, opts); - const existing = await this.#selectUnlessEnded(selectRow); + const { row: existing, primary: onlyOnPrimary } = + await this.#selectUnlessEnded(selectRow); if (existing && !predates(existing, notBefore)) { - await this.#cacheComposite(cacheKey, existing); + // Auth that misses the cache reads the replica, which already has + // a row it returned here; a row only the primary had is cached + // before the token goes out. + const caching = awaitLater( + this.#cacheComposite(cacheKey, existing), + ); + if (onlyOnPrimary) await caching; return existing; } const stale = existing ?? (cachedLive ? cached : null); @@ -762,7 +776,7 @@ export class SessionStore extends PuterStore { // INSERT-or-IGNORE so concurrent racers don't throw on the // partial unique index; we re-SELECT below to find the row // that actually won. - const created = await this.#insertSession( + const { row: created, inserted } = await this.#insertSession( userId, { kind: 'app', @@ -778,10 +792,10 @@ export class SessionStore extends PuterStore { { ignoreConflict: true }, ); - // After a revoke the replica may still show the old row as live. - const winner = await this.#selectUnlessEnded(selectRow, { - primary: stale !== null, - }); + // Only a lost race needs the winner read back, and from the primary: a + // replica that hasn't seen it would leave `created`, which was never + // written. + const winner = inserted ? null : await selectRow({ primary: true }); const row = winner ?? created; await this.#cacheComposite(cacheKey, row); return row; @@ -822,7 +836,7 @@ export class SessionStore extends PuterStore { const cacheKey = this.#cacheKeyWorker(userId, appUid, workerName); const now = nowSeconds(); - const created = await this.#insertSession( + const { row: created } = await this.#insertSession( userId, { kind: 'worker', @@ -845,7 +859,7 @@ export class SessionStore extends PuterStore { // re-SELECT under the partial unique index to find whichever // row actually won. // After a revoke the replica may still show the old row as live. - const winner = await this.#selectUnlessEnded( + const { row: winner } = await this.#selectUnlessEnded( (o) => this.#selectWorkerRow(userId, appUid, workerName, o), { primary: existing !== null }, ); @@ -872,7 +886,7 @@ export class SessionStore extends PuterStore { return cached; } - const existing = await this.#selectUnlessEnded((o) => + const { row: existing } = await this.#selectUnlessEnded((o) => this.#selectWorkerRow(userId, appUid, workerName, o), ); if (existing) await this.#cacheComposite(cacheKey, existing); @@ -1233,16 +1247,19 @@ export class SessionStore extends PuterStore { /** * Run a row select on the replica, or on the primary when asked or when the * replica's row ended moments ago (its uuid key is tombstoned) and the - * replica may not have the revoke or delete yet. + * replica may not have the revoke or delete yet. `primary` says which + * answered. */ async #selectUnlessEnded(select, { primary = false } = {}) { - if (primary) return select({ primary: true }); + if (primary) return { row: await select({ primary: true }), primary }; const row = await select({ primary: false }); - if (!row) return row; - if (!(await this.isCacheKeyTombstoned([this.#cacheKey(row.uuid)]))) { - return row; + if ( + !row || + !(await this.isCacheKeyTombstoned([this.#cacheKey(row.uuid)])) + ) { + return { row, primary: false }; } - return select({ primary: true }); + return { row: await select({ primary: true }), primary: true }; } /** Cache a row under its composite key and its uuid key, unless it ended. */ diff --git a/src/backend/stores/session/SessionStore.test.ts b/src/backend/stores/session/SessionStore.test.ts index 80c213f0b..265a2c4b5 100644 --- a/src/backend/stores/session/SessionStore.test.ts +++ b/src/backend/stores/session/SessionStore.test.ts @@ -625,6 +625,150 @@ describe('SessionStore', () => { uuid: second.uuid, }); }); + + it('answers a lost insert race with the stored winner while the replica lags', async () => { + const user = await makeUser(); + const appUid = `app-${uuidv4()}`; + // The racer that got there first: written, but not yet on the + // replica and not cached under the (user, app) key. + const winner = await target.create(user.id, { + kind: 'app', + app_uid: appUid, + }); + + const restore = lagReplica({ [winner.uuid]: null }); + let row; + try { + row = await target.getOrCreateApp(user.id, appUid); + } finally { + restore(); + } + expect(row.uuid).toBe(winner.uuid); + expect(await rawRow(row.uuid)).toMatchObject({ uuid: winner.uuid }); + // The next call is served the same row from the cache. + expect((await target.getOrCreateApp(user.id, appUid)).uuid).toBe( + winner.uuid, + ); + }); + + const appSelects = (calls: unknown[][]) => + calls.filter(([sql]) => + String(sql).includes("FROM `sessions` WHERE `kind` = 'app'"), + ).length; + + it('returns the row it inserted without reading it back', async () => { + const user = await makeUser(); + const appUid = `app-${uuidv4()}`; + const db = server.clients.db; + const read = vi.spyOn(db, 'read'); + const pread = vi.spyOn(db, 'pread'); + let row; + try { + row = await target.getOrCreateApp(user.id, appUid, { + auth_id: user.uuid, + }); + // Only the lookup that found nothing to reuse. + expect(appSelects(read.mock.calls)).toBe(1); + expect(appSelects(pread.mock.calls)).toBe(0); + } finally { + read.mockRestore(); + pread.mockRestore(); + } + // What it hands back is the row as stored. + const stored = await rawRow(row.uuid); + for (const column of [ + 'uuid', + 'user_id', + 'kind', + 'app_uid', + 'parent_session_id', + 'created_at', + 'last_activity', + 'expires_at', + 'revoked_at', + 'auth_id', + ]) { + expect(row[column], column).toEqual(stored[column]); + } + expect(await target.getByUuid(row.uuid)).toMatchObject({ + uuid: row.uuid, + }); + }); + + it('returns a replica row before its cache write lands, and still caches it', async () => { + const user = await makeUser(); + const appUid = `app-${uuidv4()}`; + const first = await target.getOrCreateApp(user.id, appUid); + const compositeKey = `sessions:v2:app:${user.id}:${appUid}`; + await server.clients.redis.del(compositeKey); + + const redis = server.clients.redis; + const set = redis.set.bind(redis); + let release!: () => void; + const held = new Promise((resolve) => { + release = resolve; + }); + const hold = vi + .spyOn(redis, 'set') + .mockImplementation(async (...args: unknown[]) => { + await held; + return (set as (...a: unknown[]) => Promise)( + ...args, + ); + }); + try { + const again = await Promise.race([ + target.getOrCreateApp(user.id, appUid), + new Promise((resolve) => + setTimeout(() => resolve('waited on the cache'), 1000), + ), + ]); + expect(again).toMatchObject({ uuid: first.uuid }); + expect(await redis.get(compositeKey)).toBeNull(); + } finally { + release(); + hold.mockRestore(); + } + await vi.waitFor(async () => { + expect(JSON.parse((await redis.get(compositeKey))!).uuid).toBe( + first.uuid, + ); + }); + }); + + it('caches a row only the primary has before returning it', async () => { + const user = await makeUser(); + const appUid = `app-${uuidv4()}`; + const first = await target.getOrCreateApp(user.id, appUid); + const firstLive = await rawRow(first.uuid); + await target.revokeCascade(first.uuid); + const second = await target.getOrCreateApp(user.id, appUid); + const secondLive = await rawRow(second.uuid); + // Another region: none of this cached, the replica still shows the + // revoked row live and has yet to see the new one. + const redis = server.clients.redis; + await redis.del( + `sessions:v2:app:${user.id}:${appUid}`, + `sessions:v2:uuid:${first.uuid}`, + `sessions:v2:uuid:${second.uuid}`, + ); + + const restore = lagReplica({ + [first.uuid]: firstLive, + [second.uuid]: null, + }); + try { + const found = await target.getOrCreateApp(user.id, appUid); + expect(found.uuid).toBe(second.uuid); + // Token auth reads it back through the lagging replica. + expect(await target.getByUuid(second.uuid)).toMatchObject({ + uuid: second.uuid, + revoked_at: secondLive.revoked_at, + }); + } finally { + restore(); + } + }); }); describe('getOrCreateGodmodeToken', () => { diff --git a/src/backend/stores/share/ShareStore.js b/src/backend/stores/share/ShareStore.js index d02ffe855..c42334ba2 100644 --- a/src/backend/stores/share/ShareStore.js +++ b/src/backend/stores/share/ShareStore.js @@ -427,31 +427,33 @@ export class ShareStore extends PuterStore { } /** - * Which of `fsentryIds` carry a share, pending invites included. Link - * shares count unless `includeAnyone` is false — what the caller passes - * while the owner's plan has them switched off. + * Which of `fsentryIds` carry a share, pending invites included, each + * mapped to whether it has a row other than a link share — a link alone + * counts only while the owner's plan covers link sharing. * * @param {number[]} fsentryIds - * @param {{ includeAnyone?: boolean }} [opts] - * @returns {Promise>} + * @returns {Promise>} */ - async getSharedFsentryIds(fsentryIds, { includeAnyone = true } = {}) { + async getSharedFsentryIds(fsentryIds) { const ids = [ ...new Set( fsentryIds.map(Number).filter((id) => Number.isFinite(id)), ), ]; - const shared = new Set(); + const shared = new Map(); for (let i = 0; i < ids.length; i += SHARED_IDS_CHUNK_SIZE) { const chunk = ids.slice(i, i + SHARED_IDS_CHUNK_SIZE); const placeholders = chunk.map(() => '?').join(', '); const rows = await this.clients.db.read( - 'SELECT DISTINCT `fsentry_id` FROM `share` ' + - `WHERE \`fsentry_id\` IN (${placeholders})` + - (includeAnyone ? '' : ' AND `anyone` IS NULL'), + 'SELECT `fsentry_id`, ' + + 'MAX(CASE WHEN `anyone` IS NULL THEN 1 ELSE 0 END) AS `direct` ' + + `FROM \`share\` WHERE \`fsentry_id\` IN (${placeholders}) ` + + 'GROUP BY `fsentry_id`', chunk, ); - for (const row of rows) shared.add(Number(row.fsentry_id)); + for (const row of rows) { + shared.set(Number(row.fsentry_id), Number(row.direct) === 1); + } } return shared; } diff --git a/src/backend/stores/share/ShareStore.test.js b/src/backend/stores/share/ShareStore.test.js index 448fc1e31..ea79b4e2e 100644 --- a/src/backend/stores/share/ShareStore.test.js +++ b/src/backend/stores/share/ShareStore.test.js @@ -441,7 +441,7 @@ describe('ShareStore', () => { expect( await store.getSharedFsentryIds([shared.id, untouched.id]), - ).toEqual(new Set([shared.id])); + ).toEqual(new Map([[shared.id, true]])); await store.deleteActive({ holderUserId: holder.id, @@ -450,7 +450,7 @@ describe('ShareStore', () => { expect( await store.getSharedFsentryIds([shared.id, untouched.id]), - ).toEqual(new Set()); + ).toEqual(new Map()); }); it('counts a node whose only share is an unclaimed invite', async () => { @@ -463,7 +463,34 @@ describe('ShareStore', () => { }); expect(await store.getSharedFsentryIds([entry.id])).toEqual( - new Set([entry.id]), + new Map([[entry.id, true]]), + ); + }); + + it('tells a node whose only share is a link from one that also has a holder', async () => { + const linkOnly = await makeEntry(issuer); + const both = await makeEntry(issuer); + for (const entry of [linkOnly, both]) { + await store.upsertAnyone({ + issuerUserId: issuer.id, + fsentryId: entry.id, + mode: 'read', + }); + } + await store.upsertActive({ + issuerUserId: issuer.id, + holderUserId: holder.id, + fsentryId: both.id, + mode: 'read', + }); + + expect( + await store.getSharedFsentryIds([linkOnly.id, both.id]), + ).toEqual( + new Map([ + [linkOnly.id, false], + [both.id, true], + ]), ); }); @@ -522,13 +549,13 @@ describe('ShareStore', () => { expect( await store.getSharedFsentryIds([...padding, entry.id]), - ).toEqual(new Set([entry.id])); + ).toEqual(new Map([[entry.id, true]])); }); it('answers an empty request without a query', async () => { const read = vi.spyOn(store.clients.db, 'read'); try { - expect(await store.getSharedFsentryIds([])).toEqual(new Set()); + expect(await store.getSharedFsentryIds([])).toEqual(new Map()); expect(read).not.toHaveBeenCalled(); } finally { read.mockRestore(); diff --git a/src/backend/util/concurrency.test.ts b/src/backend/util/concurrency.test.ts index 42f8a3869..d6b2841ed 100644 --- a/src/backend/util/concurrency.test.ts +++ b/src/backend/util/concurrency.test.ts @@ -19,6 +19,7 @@ import { describe, expect, it } from 'vitest'; import { + awaitLater, runWithConcurrencyLimit, runWithConcurrencyLimitSettled, } from './concurrency.ts'; @@ -194,3 +195,29 @@ describe('runWithConcurrencyLimitSettled', () => { ).rejects.toThrow('Missing value at index 0'); }); }); + +describe('awaitLater', () => { + it('leaves no unhandled rejection when the promise is never awaited', async () => { + const unhandled: unknown[] = []; + const onUnhandled = (reason: unknown) => unhandled.push(reason); + process.on('unhandledRejection', onUnhandled); + try { + awaitLater(Promise.reject(new Error('never awaited'))); + await new Promise((resolve) => setTimeout(resolve, 10)); + expect(unhandled).toEqual([]); + } finally { + process.off('unhandledRejection', onUnhandled); + } + }); + + it('still rejects where it is awaited', async () => { + const failure = new Error('surfaced'); + const started = awaitLater(Promise.reject(failure)); + await new Promise((resolve) => setTimeout(resolve, 5)); + await expect(started).rejects.toBe(failure); + }); + + it('resolves to the same value', async () => { + expect(await awaitLater(Promise.resolve(42))).toBe(42); + }); +}); diff --git a/src/backend/util/concurrency.ts b/src/backend/util/concurrency.ts index 379cf49ed..9d0723907 100644 --- a/src/backend/util/concurrency.ts +++ b/src/backend/util/concurrency.ts @@ -94,3 +94,19 @@ export async function runWithConcurrencyLimitSettled( await Promise.all(Array.from({ length: workerCount }, () => runWorker())); return results; } + +/** + * For a promise started before the code that needs it: marks it handled, so an + * early return or throw that never awaits it doesn't leave an unhandled + * rejection. Its rejection still surfaces where it is awaited. + */ +export function awaitLater(promise: Promise): Promise { + void (async () => { + try { + await promise; + } catch { + // Surfaced where awaited. + } + })(); + return promise; +} diff --git a/src/backend/util/userDetails.test.ts b/src/backend/util/userDetails.test.ts index 494b922e4..d9a81c7d3 100644 --- a/src/backend/util/userDetails.test.ts +++ b/src/backend/util/userDetails.test.ts @@ -18,7 +18,7 @@ */ import { v4 as uuidv4 } from 'uuid'; -import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; import { configContainer } from '../exports.js'; import { PuterServer } from '../server.js'; import { setupTestServer } from '../testUtil.js'; @@ -98,6 +98,141 @@ describe('buildUserDetails', () => { }); }); +describe('buildUserDetails lookups', () => { + /** A dependency answer that records when it was asked and answered. */ + const timed = ( + events: string[], + label: string, + ms: number, + answer: () => T, + ) => + new Promise((resolve, reject) => { + events.push(`${label}:start`); + setTimeout(() => { + events.push(`${label}:end`); + try { + resolve(answer()); + } catch (error) { + reject(error); + } + }, ms); + }); + + const tailKeys = (details: Record) => { + const keys = Object.keys(details); + return keys.slice(keys.indexOf('hasDevAccountAccess') + 1); + }; + + it('reads the linked provider, taskbar and seat together, listed in a fixed order', async () => { + const user = { + ...(await makeUser()), + password: null, + last_activity_ts: '2026-01-02 03:04:05', + }; + const events: string[] = []; + const d = deps(); + const details = await buildUserDetails( + user, + { + ...d, + config: { ...d.config, teams_enabled: true }, + services: { + oidc: { + getLinkedProviderForUser: () => + timed(events, 'oidc', 30, () => 'google'), + }, + }, + stores: Object.assign(Object.create(d.stores), { + team: { + getOrgSeat: () => + timed(events, 'seat', 10, () => ({ + team_uid: 'team-1', + team_name: 'Team One', + })), + }, + }), + }, + { isUser: true }, + ); + + // The seat was asked for before the slower provider answered. + expect(events.indexOf('seat:start')).toBeLessThan( + events.indexOf('oidc:end'), + ); + expect(tailKeys(details)).toEqual([ + 'oidc_revalidate_url', + 'taskbar_items', + 'directories', + 'team', + 'last_activity_ts', + ]); + expect(details.oidc_revalidate_url).toContain( + '/auth/oidc/google/start?flow=revalidate', + ); + expect(details.team).toEqual({ uid: 'team-1', name: 'Team One' }); + }); + + it('keeps each failure to its own part, warning in payload order', async () => { + const user = { + ...(await makeUser()), + password: null, + taskbar_items: JSON.stringify([{ type: 'app', name: 'editor' }]), + last_activity_ts: '2026-01-02 03:04:05', + }; + const events: string[] = []; + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + const d = deps(); + let details: Record; + try { + details = await buildUserDetails( + user, + { + ...d, + config: { ...d.config, teams_enabled: true }, + services: { + oidc: { + getLinkedProviderForUser: () => + timed(events, 'oidc', 1, () => { + throw new Error('oidc down'); + }), + }, + }, + stores: Object.assign(Object.create(d.stores), { + app: { + getByNames: () => + timed(events, 'taskbar', 20, () => { + throw new Error('taskbar down'); + }), + getByUids: async () => new Map(), + getByIds: async () => new Map(), + }, + // Fails first, but is reported after the taskbar. + team: { + getOrgSeat: () => + timed(events, 'seat', 5, () => { + throw new Error('seat down'); + }), + }, + }), + }, + { isUser: true }, + ); + expect(warn.mock.calls).toEqual([ + ['[user-details] taskbar_items failed:', expect.any(Error)], + ['[user-details] team lookup failed:', expect.any(Error)], + ]); + } finally { + warn.mockRestore(); + } + expect(tailKeys(details)).toEqual([ + 'taskbar_items', + 'directories', + 'last_activity_ts', + ]); + expect(details.taskbar_items).toEqual([]); + }); +}); + describe('scrubSensitive', () => { it('removes credentials and identifiers at any depth', () => { const payload = { diff --git a/src/backend/util/userDetails.ts b/src/backend/util/userDetails.ts index 518e66b05..feb213bc0 100644 --- a/src/backend/util/userDetails.ts +++ b/src/backend/util/userDetails.ts @@ -182,38 +182,65 @@ export async function buildUserDetails( hasDevAccountAccess: !!user.metadata?.hasDevAccountAccess, }; - if (oidcOnly) { + // Not an inline literal, so a `TaskbarDeps` without `clients` still + // accepts it. + const taskbarDeps = { + clients, + stores, + apiBaseUrl: String(config.api_base_url ?? ''), + config, + }; + const linkedProvider = async () => { + if (!oidcOnly) return null; try { - const provider = await services.oidc.getLinkedProviderForUser( - user.id, - ); - if (provider) { - const origin = (config.origin ?? '').replace(/\/$/, ''); - details.oidc_revalidate_url = `${origin}/auth/oidc/${provider}/start?flow=revalidate&user_uuid=${encodeURIComponent(user.uuid)}`; - } + return await services.oidc.getLinkedProviderForUser(user.id); } catch { // OIDC not configured + return null; } + }; + const taskbarItems = async () => { + if (!isUser) return null; + try { + return { + items: await getTaskbarItems(user, taskbarDeps, { + iconSize: opts.iconSize, + noIcons: opts.noIcons, + }), + }; + } catch (error) { + return { error }; + } + }; + // The team that pays for the account; every seat restriction keys on it. + const orgSeat = async () => { + if (!isUser || config.teams_enabled !== true) return null; + try { + return { seat: await stores.team.getOrgSeat(user.id) }; + } catch (error) { + return { error }; + } + }; + // Independent and each best-effort, so read together; the results land + // below in the order the payload has always listed them. + const [provider, taskbar, team] = await Promise.all([ + linkedProvider(), + taskbarItems(), + orgSeat(), + ]); + + if (provider) { + const origin = (config.origin ?? '').replace(/\/$/, ''); + details.oidc_revalidate_url = `${origin}/auth/oidc/${provider}/start?flow=revalidate&user_uuid=${encodeURIComponent(user.uuid)}`; } - if (isUser) { + if (taskbar) { // Best-effort: the account still works without them. - // Not an inline literal, so a `TaskbarDeps` without `clients` still - // accepts it. - const taskbarDeps = { - clients, - stores, - apiBaseUrl: String(config.api_base_url ?? ''), - config, - }; - try { - details.taskbar_items = await getTaskbarItems(user, taskbarDeps, { - iconSize: opts.iconSize, - noIcons: opts.noIcons, - }); - } catch (e) { - console.warn('[user-details] taskbar_items failed:', e); + if ('error' in taskbar) { + console.warn('[user-details] taskbar_items failed:', taskbar.error); details.taskbar_items = []; + } else { + details.taskbar_items = taskbar.items; } const directories: Record = {}; @@ -231,18 +258,14 @@ export async function buildUserDetails( details.directories = directories; } - // The team that pays for the account; every seat restriction keys on it. - if (isUser && config.teams_enabled === true) { - try { - const seat = await stores.team.getOrgSeat(user.id); - if (seat) { - details.team = { - uid: seat.team_uid, - name: seat.team_name ?? null, - }; - } - } catch (e) { - console.warn('[user-details] team lookup failed:', e); + if (team) { + if ('error' in team) { + console.warn('[user-details] team lookup failed:', team.error); + } else if (team.seat) { + details.team = { + uid: team.seat.team_uid, + name: team.seat.team_name ?? null, + }; } }