Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Chat files open only through short-lived signed links, so access ends with membership (Ken) (#2460)

0xken15c1dbc34f196e
diff --git a/backend/integration/superTabBuddies.test.ts b/backend/integration/superTabBuddies.test.ts
index 37ef51e4e..0c309ba69 100644
--- a/backend/integration/superTabBuddies.test.ts
+++ b/backend/integration/superTabBuddies.test.ts
@@ -416,6 +416,8 @@ describe('a Buddy through the store', () => {
['plan.md', 'text/markdown', true], ['shot.png', 'image/png', true], ['sync.log', 'text/plain', true]])
expect((await f.store.messages(owner, f.tab, 'lead', {})).messages.find(m => m.id === id)?.attachments).toHaveLength(3)
// Each opens from its link as the apps open it: the picture shown in place, the plan and the log as their own text.
+ // And from the signed link each read adds (fileLinks.ts), as apps that know links open it.
+ expect(read!.attachments!.map(a => f.files.open(a.link!)?.length)).toEqual(read!.attachments!.map(a => a.size))
const opened = (url: string) => f.files.objects.get(new URL(url).pathname.slice(1))!
const [planFile, shotFile, logFile] = read!.attachments!.map(a => opened(a.url))
expect(openPng(shotFile!.bytes)).toEqual({ width: 1, height: 1, pixel: [255, 0, 0, 255] })
diff --git a/backend/integration/superTabNotAvailable.test.ts b/backend/integration/superTabNotAvailable.test.ts
index a5beaf320..6e6f33893 100644
--- a/backend/integration/superTabNotAvailable.test.ts
+++ b/backend/integration/superTabNotAvailable.test.ts
@@ -98,6 +98,8 @@ function calls(tab: string, chat: string, project: string) {
json('POST', 'POST /api/super-tabs/:tabId/native', `${t}/native`, { operationId: op(), conversationId: chat, agentId: MISSING }),
json('GET', 'GET /api/super-tabs/:tabId/conversations/:conversationId/messages', `${t}/conversations/${chat}/messages`),
json('GET', 'GET /api/super-tabs/:tabId/conversations/:conversationId/catch-up', `${t}/conversations/${chat}/catch-up?after=0`),
+ json('GET', 'GET /api/super-tabs/:tabId/conversations/:conversationId/messages/:messageId/attachments/:attachmentId',
+ `${t}/conversations/${chat}/messages/${MISSING}/attachments/${MISSING}`),
json('POST', 'POST /api/super-tabs/:tabId/conversations/:conversationId/read', `${t}/conversations/${chat}/read`, { sequence: 1 }),
json('POST', 'POST /api/super-tabs/:tabId/conversations/:conversationId/threads/:threadId/follow', `${t}/conversations/${chat}/threads/${MISSING}/follow`,
{ follow: true }),
diff --git a/backend/integration/superTabProfile.test.ts b/backend/integration/superTabProfile.test.ts
index 1b554d0ac..e033d18b6 100644
--- a/backend/integration/superTabProfile.test.ts
+++ b/backend/integration/superTabProfile.test.ts
@@ -9,7 +9,7 @@ import { PrismaClient } from '@prisma/client'
import Fastify, { type FastifyInstance } from 'fastify'
import type { AuthUser } from '../src/lib/ssoAuth.js'
import { SuperTabStore } from '../src/superTabs/store.js'
-import { CdnAvatarStore, MemoryAttachmentStore, MemoryAvatarStore } from '../src/superTabs/attachments.js'
+import { MemoryAttachmentStore, MemoryAvatarStore, SharedAvatarStore } from '../src/superTabs/attachments.js'
import { superTabRoutes } from '../src/routes/superTabs.js'
import { errorHandler } from '../src/middlewares/errorHandler.js'
import type { Command } from '../src/superTabs/commands.js'
@@ -279,15 +279,15 @@ describe('setting avatars', () => {
})
})
-// Prod has no avatar bucket (Ken, 2026-10-28): pictures go in the attachments bucket under avatars/ and are linked on its
-// CDN, as chat files are.
+// Prod has no avatar bucket (Ken, 2026-10-28): pictures go in the attachments bucket under avatars/ and are linked as chat
+// files are, by signed links that stop opening (fileLinks.ts), so they keep working once the bucket is no longer public.
describe('avatars in the attachments bucket', () => {
const bucket = new MemoryAttachmentStore()
let shared: SuperTabStore
let reads: FastifyInstance
beforeAll(async () => {
shared = new SuperTabStore(db, Date.now, { store: bucket, prefix: 'super-tabs', publicBase: 'https://cdn.example.test',
- avatars: new CdnAvatarStore(bucket, 'https://cdn.example.test'), avatarPrefix: 'avatars' })
+ avatars: new SharedAvatarStore(bucket), avatarPrefix: 'avatars' })
reads = Fastify()
reads.setErrorHandler(errorHandler)
reads.addHook('onRequest', async request => { request.user = [owner, peer].find(u => `Bearer ${u.sub}` === request.headers.authorization) })
@@ -306,19 +306,20 @@ describe('avatars in the attachments bucket', () => {
return response.json().data.derived?.avatars?.[`person:${owner.sub}`] as string | undefined
}
- it('uploads, reads and replaces a picture, linked on the CDN', async () => {
+ it('uploads, reads and replaces a picture, by a signed link that stops opening', async () => {
const tab = (await shared.create(owner, randomUUID(), 'Shared bucket')).id
const invitation = await shared.execute(owner, tab, { operationId: randomUUID(), command: { type: 'invite.create', email: peer.email, role: 'collaborator', conversationId: 'lead' } })
await shared.execute(peer, tab, { operationId: randomUUID(), command: { type: 'invite.accept', inviteId: invitation.result.inviteId as string } })
const first = await picture(tab, image(64))
expect(first).toMatch(new RegExp(`^avatars/${tab}/[0-9a-f]{32}/avatar$`))
expect(bucket.objects.get(first)).toMatchObject({ contentType: 'image/png' })
- expect(await linkFor(tab)).toBe(`https://cdn.example.test/${first}`)
- expect(bucket.objects.get(first)!.bytes).toEqual(Buffer.from(image(64)))
+ const firstLink = (await linkFor(tab))!
+ expect(firstLink).toMatch(new RegExp(`^https://storage\\.memory\\.test/${first}\\?expires=\\d+&signature=`))
+ expect(bucket.open(firstLink)).toEqual(Buffer.from(image(64)))
+ expect(bucket.open(firstLink, Number(new URL(firstLink).searchParams.get('expires')))).toBeNull()
const second = await picture(tab, image(96))
expect(second).not.toBe(first)
- expect(await linkFor(tab)).toBe(`https://cdn.example.test/${second}`)
- expect(bucket.objects.get(second)!.bytes).toEqual(Buffer.from(image(96)))
+ expect(bucket.open((await linkFor(tab))!)).toEqual(Buffer.from(image(96)))
})
it('keeps chat files out of the avatar folder and out of avatar links', async () => {
diff --git a/backend/integration/superTabs.test.ts b/backend/integration/superTabs.test.ts
index 322f272cb..09dc12b0c 100644
--- a/backend/integration/superTabs.test.ts
+++ b/backend/integration/superTabs.test.ts
@@ -2470,7 +2470,7 @@ describe('Super Tab chat attachments', () => {
})
afterAll(async () => { await upload?.close() })
type Part = [string, string] | [string, Uint8Array, string, string]
- type Uploaded = { id: string; name: string; mime: string; size: number; url: string }
+ type Uploaded = { id: string; name: string; mime: string; size: number; url: string; link?: string; expiresAt?: number }
const post = async (tab: string, parts: Part[], user = owner, asAgentId?: string, server = upload) => {
const form = new FormData()
for (const part of parts) {
@@ -2488,6 +2488,10 @@ describe('Super Tab chat attachments', () => {
const send = (tab: string, attachmentIds: string[], user = owner, asAgentId?: string, text = '', operationId = randomUUID()) =>
attaching.execute(user, tab, { operationId, command: { type: 'message.send', conversationId: 'lead', text, replyTo: null, recipientIds: [], attachmentIds } }, asAgentId)
const objectKey = (url: string) => url.slice('https://cdn.example.test/'.length)
+ // Every read adds a signed link of its own to each file (fileLinks.ts), on the bucket's own host.
+ const signed = (path: string) => new RegExp(`^https://storage\\.memory\\.test/${path}\\?expires=\\d+&signature=[0-9a-f]{64}$`)
+ /** A file as two reads hand it: the same object, each with a link of its own read. */
+ const sameFile = (file: Uploaded) => ({ ...file, link: expect.stringMatching(signed(objectKey(file.url).replaceAll('.', '\\.'))), expiresAt: expect.any(Number) })
it('keeps a credential out of a file\'s name, which the chat shows like the message', async () => {
const { tab } = await fixture()
@@ -2507,6 +2511,8 @@ describe('Super Tab chat attachments', () => {
expect(status).toBe(200)
expect(attachment).toMatchObject({ name: 'Screen Shot 1.png', mime: 'image/png', size: bytes.byteLength })
expect(attachment.url).toMatch(new RegExp(`^https://cdn\\.example\\.test/super-tabs/${tab}/[0-9a-f]{32}/Screen_Shot_1\\.png$`))
+ expect(attachment.link).toMatch(signed(`super-tabs/${tab}/[0-9a-f]{32}/Screen_Shot_1\\.png`))
+ expect(files.open(attachment.link!)!.equals(Buffer.from(bytes))).toBe(true)
expect(files.objects.get(objectKey(attachment.url))!.bytes.equals(Buffer.from(bytes))).toBe(true)
expect(files.objects.get(objectKey(attachment.url))).toMatchObject({ contentType: 'image/png', contentDisposition: "inline; filename*=UTF-8''Screen%20Shot%201.png" })
const html = await file(tab, 'page.html', new Uint8Array([60]), 'text/html')
@@ -2552,10 +2558,10 @@ describe('Super Tab chat attachments', () => {
const { tab, lead } = await fixture()
const { attachment } = await file(tab, 'log.txt', new Uint8Array(10), 'text/plain')
const sent = await send(tab, [attachment.id])
- const row = (await store.messages(owner, tab, 'lead', {})).messages.find(m => m.id === sent.result.messageId)!
- expect(row).toMatchObject({ text: '', attachments: [attachment] })
- const delivery = (await store.deliveries(host, tab, lead)).find(d => d.messageId === sent.result.messageId)!
- expect((await store.delivery(host, tab, lead, delivery.id, { type: 'claim', claimId: randomUUID() })).message!.attachments).toEqual([attachment])
+ const row = (await attaching.messages(owner, tab, 'lead', {})).messages.find(m => m.id === sent.result.messageId)!
+ expect(row).toMatchObject({ text: '', attachments: [sameFile(attachment)] })
+ const delivery = (await attaching.deliveries(host, tab, lead)).find(d => d.messageId === sent.result.messageId)!
+ expect((await attaching.delivery(host, tab, lead, delivery.id, { type: 'claim', claimId: randomUUID() })).message!.attachments).toEqual([sameFile(attachment)])
await expect(send(tab, [attachment.id])).rejects.toMatchObject({ code: 'ATTACHMENT_USED' })
await expect(send(tab, [])).rejects.toMatchObject({ code: 'TEXT_REQUIRED' })
})
@@ -2612,12 +2618,13 @@ describe('Super Tab chat attachments', () => {
expect(done.status).toBe(200)
expect(done.attachment).toMatchObject({ id: link.id, name: 'AC4 - 26 Oct v2.step', mime: 'application/octet-stream', size })
expect(done.attachment.url).toMatch(new RegExp(`^https://cdn\\.example\\.test/super-tabs/${tab}/[0-9a-f]{32}/AC4_-_26_Oct_v2\\.step$`))
+ expect(done.attachment.link).toMatch(signed(`super-tabs/${tab}/[0-9a-f]{32}/AC4_-_26_Oct_v2\\.step`))
expect(files.objects.get(objectKey(done.attachment.url))).toMatchObject({ contentType: 'application/octet-stream', contentDisposition: "attachment; filename*=UTF-8''AC4%20-%2026%20Oct%20v2.step" })
// A lost answer is answered again the same way.
expect(await complete(tab, link.id)).toEqual(done)
const sent = await send(tab, [link.id])
- const delivery = (await store.deliveries(host, tab, lead)).find(d => d.messageId === sent.result.messageId)!
- expect((await store.delivery(host, tab, lead, delivery.id, { type: 'claim', claimId: randomUUID() })).message!.attachments).toEqual([done.attachment])
+ const delivery = (await attaching.deliveries(host, tab, lead)).find(d => d.messageId === sent.result.messageId)!
+ expect((await attaching.delivery(host, tab, lead, delivery.id, { type: 'claim', claimId: randomUUID() })).message!.attachments).toEqual([sameFile(done.attachment)])
files.objects.delete(objectKey(done.attachment.url))
})
@@ -2782,6 +2789,125 @@ describe('Super Tab chat attachments', () => {
error.mockRestore()
})
+ // Chat file links stop working when access ends (security review of Delete Swarm, 10-31; fileLinks.ts).
+ const fresh = async (tab: string, messageId: string, attachmentId: string, user = owner, conversationId = 'lead', asAgentId?: string) => {
+ const response = await upload.inject({ url: `/api/super-tabs/${tab}/conversations/${conversationId}/messages/${messageId}/attachments/${attachmentId}${asAgentId ? `?asAgentId=${asAgentId}` : ''}`,
+ headers: { authorization: `Bearer ${user.sub}` } })
+ return { status: response.statusCode, body: response.body, attachment: response.json().data?.attachment as Uploaded | undefined }
+ }
+ const sentFile = async (tab: string, body = 'hello') => {
+ const { attachment } = await file(tab, 'notes.txt', Buffer.from(body), 'text/plain')
+ const messageId = String((await send(tab, [attachment.id])).result.messageId)
+ return { attachment, messageId }
+ }
+ const read = async (tab: string, messageId: string, user = owner, asAgentId?: string) =>
+ (await attaching.messages(user, tab, 'lead', { messageId }, asAgentId)).messages[0]!.attachments as unknown as Uploaded[]
+
+ it('hands each reader a short-lived signed link beside the public address older apps open, and stores neither link', async () => {
+ const { tab, lead } = await fixture()
+ const { attachment, messageId } = await sentFile(tab)
+ const [file] = await read(tab, messageId)
+ expect(file!.url).toBe(attachment.url)
+ expect(file!.link).toMatch(signed(`super-tabs/${tab}/[0-9a-f]{32}/notes\\.txt`))
+ expect(file!.expiresAt! - Date.now()).toBeGreaterThan(4 * 60_000)
+ expect(file!.expiresAt! - Date.now()).toBeLessThanOrEqual(10 * 60_000)
+ expect(files.open(file!.link!)?.toString()).toBe('hello')
+ expect(files.open(file!.link!, file!.expiresAt!)).toBeNull()
+ // An agent reads the same way; the stored row keeps only the address.
+ expect((await read(tab, messageId, host, lead))[0]!.link).toMatch(signed(`super-tabs/${tab}/[0-9a-f]{32}/notes\\.txt`))
+ const stored = (await db.superTabMessage.findFirst({ where: { id: messageId } }))!.attachments as unknown as Uploaded[]
+ expect(Object.keys(stored[0]!).sort()).toEqual(['id', 'mime', 'name', 'size', 'url'])
+ // A fresh link opens the same file, for the same time.
+ const again = await fresh(tab, messageId, attachment.id)
+ expect(again.status).toBe(200)
+ expect(again.attachment).toEqual(sameFile(attachment))
+ expect(files.open(again.attachment!.link!)?.toString()).toBe('hello')
+ })
+
+ it('gives a removed member no new link, and a link they kept stops opening within its lifetime', async () => {
+ const { tab } = await fixture()
+ await invite(tab)
+ const { attachment, messageId } = await sentFile(tab)
+ const [kept] = await read(tab, messageId, peer)
+ expect(files.open(kept!.link!)?.toString()).toBe('hello')
+ expect((await fresh(tab, messageId, attachment.id, peer)).status).toBe(200)
+ await attaching.execute(owner, tab, { operationId: randomUUID(), command: { type: 'member.remove', userId: peer.sub } })
+ const refused = await fresh(tab, messageId, attachment.id, peer)
+ expect(refused.status).toBe(404)
+ expect(refused.body).not.toContain('notes')
+ await expect(attaching.messages(peer, tab, 'lead', { messageId })).rejects.toBeTruthy()
+ expect(files.open(kept!.link!, kept!.expiresAt!)).toBeNull()
+ // Members who still read the chat still open it.
+ expect(files.open((await fresh(tab, messageId, attachment.id)).attachment!.link!)?.toString()).toBe('hello')
+ })
+
+ it('gives no link once the Swarm is deleted, though its files wait for the purge', async () => {
+ const { tab } = await fixture()
+ const { attachment, messageId } = await sentFile(tab)
+ const [kept] = await read(tab, messageId)
+ await attaching.execute(owner, tab, { operationId: randomUUID(), command: { type: 'swarm.delete', name: 'Pilot' } })
+ expect(files.open(kept!.link!)?.toString()).toBe('hello')
+ const refused = await fresh(tab, messageId, attachment.id)
+ expect(refused.status).toBe(404)
+ expect(refused.body).not.toContain('notes')
+ expect(files.open(kept!.link!, kept!.expiresAt!)).toBeNull()
+ })
+
+ it('answers a file the message does not carry, a message elsewhere or not there, and a hidden row alike', async () => {
+ const { tab, lead } = await fixture()
+ await invite(tab)
+ const { attachment, messageId } = await sentFile(tab)
+ const other = await sentFile(tab, 'other')
+ const plain = String((await send(tab, [], owner, undefined, 'no file')).result.messageId)
+ // An agent-only note to the Lead: the person does not read it, so its file is not theirs to open either.
+ const { attachment: noted } = await file(tab, 'note.txt', Buffer.from('agent only'), 'text/plain', 'lead', host, lead)
+ const hidden = String((await attaching.execute(host, tab, { operationId: randomUUID(), command: { type: 'message.send', conversationId: 'lead',
+ text: 'for the lead', replyTo: null, recipientIds: [lead], attachmentIds: [noted.id], agentOnly: true } as Command }, lead)).result.messageId)
+ const answers = await Promise.all([
+ fresh(tab, messageId, other.attachment.id, peer),
+ fresh(tab, plain, attachment.id, peer),
+ fresh(tab, 'ffffffffffffffffffffffff', attachment.id, peer),
+ fresh(tab, hidden, noted.id, peer),
+ ])
+ expect(answers.map(a => [a.status, a.body])).toEqual(answers.map(() => [404, answers[0]!.body]))
+ expect(answers[0]!.body).toContain('FILE_UNAVAILABLE')
+ expect((await fresh(tab, messageId, attachment.id, peer, 'nope')).status).toBe(404)
+ })
+
+ // Review 1 of #2460: a brief signing error read as lost access, and apps could not tell it from an older server.
+ it('marks a file it could not sign, answers its fresh-link read "try again", and links it once signing works', async () => {
+ const { tab, lead } = await fixture()
+ const sign = files.signedUrl.bind(files)
+ const failing = vi.spyOn(files, 'signedUrl').mockImplementation(async (key, at) => {
+ if (key.startsWith(`super-tabs/${tab}/`)) throw Object.assign(new Error('signBlob denied'), { code: 403 })
+ return sign(key, at)
+ })
+ try {
+ const { attachment, messageId } = await sentFile(tab)
+ expect(attachment).toMatchObject({ unlinked: true })
+ expect(attachment.link).toBeUndefined()
+ const [file] = await read(tab, messageId)
+ expect(file).toMatchObject({ unlinked: true, url: attachment.url })
+ const refused = await fresh(tab, messageId, attachment.id)
+ expect([refused.status, JSON.parse(refused.body).error.code]).toEqual([503, 'FILE_LINK_UNAVAILABLE'])
+ expect((await fresh(tab, messageId, attachment.id, host, 'lead', lead)).status).toBe(503)
+ failing.mockRestore()
+ const again = await fresh(tab, messageId, attachment.id)
+ expect(again.status).toBe(200)
+ expect(again.attachment!.unlinked).toBeUndefined()
+ expect(files.open(again.attachment!.link!)?.toString()).toBe('hello')
+ } finally { failing.mockRestore() }
+ })
+
+ it('adds no link on a server with no bucket, and a fresh-link read there answers not available', async () => {
+ const { tab } = await fixture()
+ const { attachment, messageId } = await sentFile(tab)
+ const plain = new SuperTabStore(db)
+ const page = await plain.messages(owner, tab, 'lead', { messageId })
+ expect((page.messages[0]!.attachments as unknown as Uploaded[]).map(a => [a.name, a.link, a.expiresAt])).toEqual([['notes.txt', undefined, undefined]])
+ await expect(plain.fileLink(owner, tab, 'lead', messageId, attachment.id)).rejects.toMatchObject({ code: 'FILE_UNAVAILABLE' })
+ })
+
it('refuses a viewer before storing anything', async () => {
const { tab } = await fixture()
await invite(tab, 'viewer')
diff --git a/backend/src/lib/teamSession.ts b/backend/src/lib/teamSession.ts
index f9d9581bf..adb59e504 100644
--- a/backend/src/lib/teamSession.ts
+++ b/backend/src/lib/teamSession.ts
@@ -134,6 +134,8 @@ export const TEAM_ROUTES: readonly TeamRoute[] = [
// The decisions its agent asked of people, and their answers (superTabs/decisions.ts).
{ method: 'GET', url: '/api/super-tabs/:tabId/decisions', scope: 'agent' },
{ method: 'GET', url: '/api/super-tabs/:tabId/conversations/:conversationId/messages', scope: 'agent' },
+ // A fresh link to one file of a message its agent may read (superTabs/fileLinks.ts).
+ { method: 'GET', url: '/api/super-tabs/:tabId/conversations/:conversationId/messages/:messageId/attachments/:attachmentId', scope: 'agent' },
{ method: 'GET', url: '/api/super-tabs/:tabId/conversations/:conversationId/attempts', scope: 'agent' },
{ method: 'GET', url: '/api/super-tabs/:tabId/terminal/:requestId', scope: 'agent' },
{ method: 'POST', url: '/api/super-tabs/:tabId/attachments', scope: 'agent' },
diff --git a/backend/src/routes/superTabs.ts b/backend/src/routes/superTabs.ts
index 2f93201b6..819d35146 100644
--- a/backend/src/routes/superTabs.ts
+++ b/backend/src/routes/superTabs.ts
@@ -79,6 +79,7 @@ const followBody = z.object({ follow: z.boolean().nullable() }).strict()
const notifyBody = z.object({ notify: z.enum(NOTIFY_LEVELS) }).strict()
const activityReadBody = z.object({ through: z.string().datetime() }).strict()
const threadParams = messageParams.extend({ threadId: id })
+const fileParams = messageParams.extend({ messageId: id, attachmentId: id })
const noQuery = z.object({}).strict()
// The reader's own catch-up (superTabs/catchUp.ts): no asAgentId, since agents read every message as it comes.
const catchUpQuery = z.object({ after: z.coerce.number().int().nonnegative().safe() }).strict()
@@ -143,6 +144,7 @@ export const NOT_AVAILABLE_ROUTES: ReadonlySet<string> = new Set([
'GET /api/super-tabs/:tabId/groups', 'GET /api/super-tabs/:tabId/groups/:conversationId',
'POST /api/super-tabs/:tabId/join', 'POST /api/super-tabs/:tabId/join/preview', 'POST /api/super-tabs/:tabId/native',
'GET /api/super-tabs/:tabId/conversations/:conversationId/messages', 'GET /api/super-tabs/:tabId/conversations/:conversationId/catch-up',
+ 'GET /api/super-tabs/:tabId/conversations/:conversationId/messages/:messageId/attachments/:attachmentId',
'POST /api/super-tabs/:tabId/conversations/:conversationId/read', 'POST /api/super-tabs/:tabId/conversations/:conversationId/threads/:threadId/follow',
'GET /api/super-tabs/:tabId/projects/:projectId/github', 'POST /api/super-tabs/:tabId/projects/:projectId/github/approve',
'GET /api/super-tabs/:tabId/projects/:projectId/ci-load',
@@ -600,6 +602,11 @@ export async function superTabRoutes(app: FastifyInstance, options: { store?: Su
app.get<{ Params: z.infer<typeof messageParams>; Querystring: z.infer<typeof messageQuery> }>('/api/super-tabs/:tabId/conversations/:conversationId/messages',
{ preHandler: [validateParams(messageParams), validateQuery(messageQuery)] },
(req, reply) => respond(reply, () => store.messages(req.user!, req.params.tabId, req.params.conversationId, req.query, req.query.asAgentId)))
+ // A fresh short-lived link to one file of one message, for whoever may read that message now (superTabs/fileLinks.ts).
+ app.get<{ Params: z.infer<typeof fileParams>; Querystring: z.infer<typeof agentQuery> }>('/api/super-tabs/:tabId/conversations/:conversationId/messages/:messageId/attachments/:attachmentId',
+ { preHandler: [validateParams(fileParams), validateQuery(agentQuery)] },
+ (req, reply) => respond(reply, () => store.fileLink(req.user!, req.params.tabId, req.params.conversationId, req.params.messageId,
+ req.params.attachmentId, req.query.asAgentId)))
app.post<{ Params: z.infer<typeof messageParams>; Querystring: z.infer<typeof noQuery>; Body: z.infer<typeof readMarkBody> }>('/api/super-tabs/:tabId/conversations/:conversationId/read',
{ preHandler: [validateParams(messageParams), validateQuery(noQuery), validateBody(readMarkBody)] },
(req, reply) => respond(reply, () => reads.markRead(req.user!, req.params.tabId, req.params.conversationId, req.body.sequence)))
diff --git a/backend/src/superTabs/attachments.test.ts b/backend/src/superTabs/attachments.test.ts
index 08c848a28..a04f97820 100644
--- a/backend/src/superTabs/attachments.test.ts
+++ b/backend/src/superTabs/attachments.test.ts
@@ -50,12 +50,15 @@ describe('attachment bucket credentials', () => {
expect(attachmentsFromEnv({ ...env(''), NODE_ENV: 'production' })?.publicBase).toBe('https://cdn.example.test')
})
- // Prod has the attachments bucket only (Ken, 2026-10-28): avatars live there under their own folder and are linked on
- // its CDN like chat files, so nothing is signed (no signing permission is needed either).
- it('keeps avatars in the attachments bucket under avatars/, on its CDN, without a bucket of their own', async () => {
+ // Prod has the attachments bucket only (Ken, 2026-10-28): avatars live there under their own folder and are linked as chat
+ // files are, by signed links (fileLinks.ts), so they keep opening once the bucket is no longer public, and a copied link stops.
+ it('keeps avatars in the attachments bucket under avatars/, by signed links, without a bucket of their own', async () => {
const shared = attachmentsFromEnv(env(encoded(key)))!
expect(shared.avatarPrefix).toBe('avatars')
- expect(await shared.avatars!.signedUrl('avatars/tab/0123/avatar', Date.now() + 3_600_000)).toBe('https://cdn.example.test/avatars/tab/0123/avatar')
+ const link = new URL(await shared.avatars!.signedUrl('avatars/tab/0123/avatar', Date.now() + 3_600_000))
+ expect(link.host).toBe('storage.googleapis.com')
+ expect(link.pathname).toBe('/attachments/avatars/tab/0123/avatar')
+ expect(Number(link.searchParams.get('X-Goog-Expires'))).toBeLessThanOrEqual(3600)
const bucket = await (shared.avatars as unknown as { store: { files(): Promise<import('@google-cloud/storage').Bucket> } }).store.files()
expect(bucket.name).toBe('attachments')
})
@@ -97,10 +100,22 @@ describe('attachment bucket credentials', () => {
expect(link.searchParams.get('X-Goog-SignedHeaders')!.split(';')).toEqual(['cache-control', 'content-disposition', 'content-type', 'host',
'x-goog-content-length-range', 'x-goog-if-generation-match'])
expect(headers).toEqual({ 'content-type': 'application/octet-stream', 'content-disposition': "attachment; filename*=UTF-8''AC4%20-%2026%20Oct%20v2.step",
- 'cache-control': 'public, max-age=31536000, immutable', 'x-goog-content-length-range': '157286400,157286400', 'x-goog-if-generation-match': '0' })
+ 'cache-control': 'private, max-age=600', 'x-goog-content-length-range': '157286400,157286400', 'x-goog-if-generation-match': '0' })
expect(directUploads(store)).toBe(store)
})
+ // Each read of a chat file hands one of these (fileLinks.ts): a GET of one object, for minutes, opening whether or not the
+ // bucket is public.
+ it('signs a chat file read link with the configured key, for minutes, without a network call', async () => {
+ const store = attachmentsFromEnv(env(encoded(key)))!.store
+ const link = new URL(await store.signedUrl('super-tabs/tab/0123/notes.txt', Date.now() + 600_000))
+ expect(link.host).toBe('storage.googleapis.com')
+ expect(link.pathname).toBe('/attachments/super-tabs/tab/0123/notes.txt')
+ expect(link.searchParams.get('X-Goog-Credential')).toContain(key.client_email)
+ expect(Number(link.searchParams.get('X-Goog-Expires'))).toBeLessThanOrEqual(600)
+ expect(link.searchParams.get('X-Goog-SignedHeaders')).toBe('host')
+ })
+
it('builds the storage client from the configured key, without a network call', async () => {
const store = attachmentsFromEnv(env(encoded(key)))!.store as GcsAttachmentStore
const bucket = await (store as unknown as { files(): Promise<import('@google-cloud/storage').Bucket> }).files()
diff --git a/backend/src/superTabs/attachments.ts b/backend/src/superTabs/attachments.ts
index ee63e6d28..23a13cc82 100644
--- a/backend/src/superTabs/attachments.ts
+++ b/backend/src/superTabs/attachments.ts
@@ -3,8 +3,12 @@ import { Readable, Transform, Writable } from 'node:stream'
import { pipeline } from 'node:stream/promises'
import { z } from 'zod'
-/** Metadata as a message stores and returns it. The bytes stay in the bucket. */
-export interface Attachment { id: string; name: string; mime: string; size: number; url: string }
+/** Metadata as a message stores it. The bytes stay in the bucket. `url` is the file's public address on the CDN: it names the
+ * object, and apps from before signed links open it, until the bucket is no longer public (docs/swarms.md, "Chat file
+ * links"). Each read adds `link`, a short-lived signed link of that read, and `expiresAt` (epoch ms), when it stops opening
+ * (fileLinks.ts). A read that could not sign one says so with `unlinked: true` instead, so an app asks again rather than
+ * open the public address; only a file with neither comes from a server from before links. None of the three is stored. */
+export interface Attachment { id: string; name: string; mime: string; size: number; url: string; link?: string; expiresAt?: number; unlinked?: true }
/** One chat file, whichever way it comes. Every client checks the same number before it sends (attachments.limits.test.ts). */
export const MAX_ATTACHMENT_BYTES = 256 * 1024 * 1024
@@ -71,18 +75,21 @@ export function linkHeaders(name: string, size: number, cacheControl: string): R
/** A storage error's code or HTTP status, for a log line: its text can quote the object's key, which is the file's link. */
export const errorCode = (error: unknown): string | number | null =>
error && typeof error === 'object' && 'code' in error && (typeof error.code === 'string' || typeof error.code === 'number') ? error.code : null
-export const CHAT_CACHE_CONTROL = 'public, max-age=31536000, immutable'
+/** A chat file is read only through a signed link that lasts minutes (fileLinks.ts): no shared cache keeps a copy, and a
+ * browser keeps one no longer than its link lasts. Files stored before this say `public, max-age=31536000, immutable`; the
+ * release step that makes the bucket private rewrites theirs (docs/swarms.md, "Chat file links"). */
+export const CHAT_CACHE_CONTROL = 'private, max-age=600'
-/** The bucket behind the CDN. Tests and the local rig use the in-memory store below. */
+/** The bucket the chat files are in. Tests and the local rig use the in-memory store below. */
export interface AttachmentStore {
/** Streams `body` into the object and answers its size. Past `cap` bytes it throws AttachmentTooLarge and leaves no object. */
put(objectKey: string, file: { name: string; mime: string }, body: Readable, cap: number): Promise<number>
-}
-/** Where avatars are kept, and the link a member read carries for one (avatarLinks.ts). */
-export interface AvatarStore extends AttachmentStore {
- /** A link that opens [objectKey]: in a private bucket until [expiresAt] (epoch ms) and never after; on the CDN, for good. */
+ /** A link that opens [objectKey] until [expiresAt] (epoch ms) and never after, whether or not the bucket is public. Storage
+ * checks it when a request starts, so a long download that started in time finishes. */
signedUrl(objectKey: string, expiresAt: number): Promise<string>
}
+/** Where avatars are kept, and the link a member read carries for one (avatarLinks.ts). */
+export interface AvatarStore extends AttachmentStore {}
/** `avatars`: where avatars are kept and how they are linked (AvatarStore); without it, avatar uploads are refused and presets
* still work. `avatarPrefix`: the folder avatar keys start with, and the only one a link is signed for (avatarLinks.ts);
* `prefix` when unset. */
@@ -91,7 +98,7 @@ export interface Attachments { store: AttachmentStore; prefix: string; publicBas
export const SHARED_AVATAR_PREFIX = 'avatars'
export const trimPrefix = (prefix: string) => prefix.replace(/^\/+|\/+$/g, '')
-/** Anyone holding a link can open the file, so the key carries 128 random bits; the name segment is for people. */
+/** The key carries 128 random bits, so no one finds a file by guessing; the name segment is for people. */
export function objectKeyFor(prefix: string, tabId: string, name: string): string {
const safe = name.replaceAll(/[^A-Za-z0-9._-]+/g, '_').replace(/^[._]+/, '').slice(-120) || 'file'
return [trimPrefix(prefix), tabId, randomBytes(16).toString('hex'), safe].filter(Boolean).join('/')
@@ -220,28 +227,46 @@ export class GcsAttachmentStore implements AttachmentStore, DirectUploads {
async delete(objectKey: string) {
await (await this.files()).file(objectKey).delete({ ignoreNotFound: true })
}
-}
-
-/** Avatars in a bucket that grants nobody read access: each read hands out V4 signed links instead (avatarLinks.ts). */
-export class GcsAvatarStore extends GcsAttachmentStore implements AvatarStore {
- constructor(name: string, account: ServiceAccount | null = null) { super(name, account, 'private, max-age=3600') }
+ /** A V4 signed GET, on Cloud Storage's own host. Signed with the service account key when one is set, else through the
+ * cloud's signing API, a network call: fileLinks.ts and avatarLinks.ts reuse each link through its window. */
async signedUrl(objectKey: string, expiresAt: number) {
const [url] = await (await this.files()).file(objectKey).getSignedUrl({ version: 'v4', action: 'read', expires: expiresAt })
return url
}
}
-/** Avatars in the attachments bucket, linked on its CDN as chat files are: permanent public links, so nothing is signed. */
-export class CdnAvatarStore implements AvatarStore {
- constructor(readonly store: AttachmentStore, private readonly publicBase: string) {}
+/** Avatars in a bucket of their own, which grants nobody read access: each read hands out signed links (avatarLinks.ts). */
+export class GcsAvatarStore extends GcsAttachmentStore implements AvatarStore {
+ constructor(name: string, account: ServiceAccount | null = null) { super(name, account, 'private, max-age=3600') }
+}
+
+/** Avatars in the attachments bucket's avatar folder (prod, Ken 2026-10-28 option B1), linked as chat files are: by signed
+ * links, so a removed member's copied link stops opening and nothing breaks once the bucket is no longer public. */
+export class SharedAvatarStore implements AvatarStore {
+ constructor(readonly store: AttachmentStore) {}
put(objectKey: string, file: { name: string; mime: string }, body: Readable, cap: number) { return this.store.put(objectKey, file, body, cap) }
- async signedUrl(objectKey: string) { return publicUrl(this.publicBase, objectKey) }
+ signedUrl(objectKey: string, expiresAt: number) { return this.store.signedUrl(objectKey, expiresAt) }
}
export class MemoryAttachmentStore implements AttachmentStore, DirectUploads {
readonly objects = new Map<string, { bytes: Buffer; contentType: string; contentDisposition: string; cacheControl?: string }>()
private readonly linkSecret = randomBytes(32)
- constructor(private readonly now: () => number = Date.now) {}
+ private readonly readSecret = randomBytes(32)
+ /** [readBase]: where its signed read links point, as Cloud Storage's own host for a real bucket. */
+ constructor(private readonly now: () => number = Date.now, private readonly readBase = 'https://storage.memory.test') {}
+ private readSignature(objectKey: string, expiresAt: number) {
+ return createHmac('sha256', this.readSecret).update(`${objectKey}\n${expiresAt}`).digest('hex')
+ }
+ async signedUrl(objectKey: string, expiresAt: number) {
+ return `${this.readBase}/${objectKey}?expires=${expiresAt}&signature=${this.readSignature(objectKey, expiresAt)}`
+ }
+ /** What the bucket answers a GET of [url] at [at]: the bytes, or null once the link is over, was not signed here, or names
+ * nothing. A link checks its own key only, as a real signed link does. */
+ open(url: string, at = this.now()): Buffer | null {
+ const link = new URL(url), objectKey = decodeURIComponent(link.pathname.slice(1)), expiresAt = Number(link.searchParams.get('expires'))
+ if (`${link.origin}` !== new URL(this.readBase).origin || !(expiresAt > at) || link.searchParams.get('signature') !== this.readSignature(objectKey, expiresAt)) return null
+ return this.objects.get(objectKey)?.bytes ?? null
+ }
async put(objectKey: string, file: { name: string; mime: string }, body: Readable, cap: number) {
const chunks: Buffer[] = []
const size = await capped(body, cap, new Writable({ write(chunk: Buffer, _encoding, done) { chunks.push(chunk); done() } }))
@@ -283,26 +308,13 @@ export class MemoryAttachmentStore implements AttachmentStore, DirectUploads {
/** Avatars for tests and the local rig: links carry their expiry and an HMAC of it, and `open` honours both as the bucket would. */
export class MemoryAvatarStore extends MemoryAttachmentStore implements AvatarStore {
- private readonly secret = randomBytes(32)
- constructor(private readonly base: string) { super() }
- private sign(objectKey: string, expiresAt: number) {
- return createHmac('sha256', this.secret).update(`${objectKey}\n${expiresAt}`).digest('hex')
- }
- async signedUrl(objectKey: string, expiresAt: number) {
- return `${this.base}/${objectKey}?expires=${expiresAt}&signature=${this.sign(objectKey, expiresAt)}`
- }
- /** What the bucket answers [url] at [now]: the bytes, or null once the link is over or was not signed here. */
- open(url: string, now: number): Buffer | null {
- const link = new URL(url), objectKey = decodeURIComponent(link.pathname.slice(1)), expiresAt = Number(link.searchParams.get('expires'))
- if (!(expiresAt > now) || link.searchParams.get('signature') !== this.sign(objectKey, expiresAt)) return null
- return this.objects.get(objectKey)?.bytes ?? null
- }
+ constructor(base: string) { super(Date.now, base) }
}
export function attachmentsFromEnv(env: { NODE_ENV: string; SUPER_TAB_ATTACHMENTS_BUCKET: string; SUPER_TAB_ATTACHMENTS_CREDENTIALS: string;
SUPER_TAB_ATTACHMENTS_PREFIX: string; SUPER_TAB_ATTACHMENTS_PUBLIC_BASE: string; SUPER_TAB_AVATARS_BUCKET: string }): Attachments | null {
- // Every message carries these links and every agent's computer downloads from them: in production only over HTTPS.
- // A local or test run may serve them from a plain loopback server.
+ // Stored messages name their files by an address on this base (fileLinks.ts reads the object's key from it): in production
+ // only HTTPS, as every link once handed out was. A local or test run may use a plain loopback address.
if (env.NODE_ENV === 'production' && new URL(env.SUPER_TAB_ATTACHMENTS_PUBLIC_BASE).protocol !== 'https:') {
throw new Error('SUPER_TAB_ATTACHMENTS_PUBLIC_BASE must be an https:// address in production.')
}
@@ -314,11 +326,11 @@ export function attachmentsFromEnv(env: { NODE_ENV: string; SUPER_TAB_ATTACHMENT
// A bucket of their own wins: nobody reads it without a signed link.
if (env.SUPER_TAB_AVATARS_BUCKET) return { ...base, avatars: new GcsAvatarStore(env.SUPER_TAB_AVATARS_BUCKET, account), avatarPrefix: trimPrefix(base.prefix) }
// Prod has only the attachments bucket (Ken, 2026-10-28, option B1): avatars live there in their own folder and are linked
- // on its CDN like chat files, so anyone who once had a picture's link keeps it (docs/swarms.md). Chat files never go in
- // the avatar folder, and avatar links are made for that folder alone.
+ // as chat files are, by signed links (docs/swarms.md). Chat files never go in the avatar folder, and avatar links are made
+ // for that folder alone.
const chat = trimPrefix(base.prefix)
if (chat === SHARED_AVATAR_PREFIX || chat.startsWith(`${SHARED_AVATAR_PREFIX}/`)) {
throw new Error(`SUPER_TAB_ATTACHMENTS_PREFIX must not be ${SHARED_AVATAR_PREFIX}/ or inside it while avatars share the attachments bucket. Pick another prefix, or set SUPER_TAB_AVATARS_BUCKET.`)
}
- return { ...base, avatars: new CdnAvatarStore(base.store, base.publicBase), avatarPrefix: SHARED_AVATAR_PREFIX }
+ return { ...base, avatars: new SharedAvatarStore(base.store), avatarPrefix: SHARED_AVATAR_PREFIX }
}
diff --git a/backend/src/superTabs/avatarLinks.test.ts b/backend/src/superTabs/avatarLinks.test.ts
index a60d00c2b..c5fca8846 100644
--- a/backend/src/superTabs/avatarLinks.test.ts
+++ b/backend/src/superTabs/avatarLinks.test.ts
@@ -16,4 +16,26 @@ describe('avatar links', () => {
expect(Object.keys(links)).toEqual(['person:p1'])
expect(links['person:p1']).toMatch(/^https:\/\/bucket\.example\.test\/avatars\/tab\/0123\/avatar\?/)
})
+
+ // Review 1 of #2460: one picture the cloud would not sign (a missing permission, a brief error) failed every member's read.
+ it('leaves out a picture it cannot sign, keeps the rest, shares signatures in flight and tries again on the next read', async () => {
+ const calls: string[] = []
+ let failing = true
+ const bucket = { signedUrl: async (key: string) => {
+ calls.push(key)
+ await new Promise(resolve => setTimeout(resolve, 5))
+ if (failing && key.endsWith('/bad/avatar')) throw Object.assign(new Error(`signBlob denied for ${key}`), { code: 403 })
+ return `https://bucket.example.test/${key}?signed`
+ } }
+ const links = new AvatarLinks(bucket as never, 'avatars')
+ const state = { people: [{ id: 'p1', avatar: { kind: 'upload', key: 'avatars/tab/good/avatar' } },
+ { id: 'p2', avatar: { kind: 'upload', key: 'avatars/tab/bad/avatar' } }] } as never
+ const [first, second] = await Promise.all([links.forReader(state, [], now), links.forReader(state, [], now)])
+ expect(first).toEqual({ 'person:p1': 'https://bucket.example.test/avatars/tab/good/avatar?signed' })
+ expect(second).toEqual(first)
+ expect(calls.sort()).toEqual(['avatars/tab/bad/avatar', 'avatars/tab/good/avatar'])
+ failing = false
+ expect(Object.keys(await links.forReader(state, [], now)).sort()).toEqual(['person:p1', 'person:p2'])
+ expect(calls).toHaveLength(3)
+ })
})
diff --git a/backend/src/superTabs/avatarLinks.ts b/backend/src/superTabs/avatarLinks.ts
index d2ae523d1..ab7eee674 100644
--- a/backend/src/superTabs/avatarLinks.ts
+++ b/backend/src/superTabs/avatarLinks.ts
@@ -1,17 +1,20 @@
-import type { AvatarStore } from './attachments.js'
+import { logger } from '../utils/logger.js'
+import { errorCode, type AvatarStore } from './attachments.js'
import type { Avatar } from './profile.js'
import type { SuperTabState } from './model.js'
-/** A link is the same through each half hour, so a picture is fetched once in it rather than on every poll. In a private
- * avatar bucket it lasts an hour from that half hour's start: a copied link stops opening within an hour, and a member
- * removed gets no new one. On the attachments CDN (no avatar bucket) it is the picture's permanent address. */
+/** A link is the same through each half hour, so a picture is fetched once in it rather than on every poll. It lasts an hour
+ * from that half hour's start, whether the pictures have a bucket of their own or share the chat files' (fileLinks.ts): a
+ * copied link stops opening within an hour once that bucket is private, and a member removed gets no new one. */
export const AVATAR_LINK_WINDOW_MS = 30 * 60_000
export const AVATAR_LINK_TTL_MS = 60 * 60_000
export const avatarLinkExpiry = (now: number) => Math.floor(now / AVATAR_LINK_WINDOW_MS) * AVATAR_LINK_WINDOW_MS + AVATAR_LINK_TTL_MS
-/** Signs at most once per picture and window: signing may call the cloud (IAM signBlob) without a key of our own. */
+/** Signs at most once per picture and window, and reads asking at once share that one signature: signing may call the cloud
+ * (IAM signBlob) without a key of our own. A picture that cannot be signed is left out of that read, never the read itself
+ * (review 1 of #2460): its person shows their default face, and the next read tries again. */
export class AvatarLinks {
- private readonly signed = new Map<string, string>()
+ private readonly signed = new Map<string, Promise<string>>()
/** [folder]: the only keys signed. In the shared attachments bucket a link opens whatever key it names, so a key outside
* the avatar folder (a chat file's) never gets one, whatever a record says. Empty: the whole (avatar-only) bucket. */
constructor(private readonly bucket: AvatarStore, private readonly folder = '') {}
@@ -22,18 +25,26 @@ export class AvatarLinks {
const holders = [...state.people.map(p => [`person:${p.id}`, p.avatar] as const), ...agents.map(a => [`agent:${a.id}`, a.avatar] as const)]
const links = await Promise.all(holders.flatMap(([who, avatar]) =>
avatar?.kind === 'upload' && this.inFolder(avatar.key) ? [this.link(avatar.key, expiresAt).then(url => [who, url] as const)] : []))
- return Object.fromEntries(links)
+ return Object.fromEntries(links.flatMap(([who, url]) => url === null ? [] : [[who, url] as const]))
}
private inFolder(objectKey: string) { return !this.folder || objectKey.startsWith(`${this.folder}/`) }
- private async link(objectKey: string, expiresAt: number): Promise<string> {
- const cached = this.signed.get(`${objectKey}@${expiresAt}`)
- if (cached) return cached
- const url = await this.bucket.signedUrl(objectKey, expiresAt)
- // Links of past windows are never asked again.
- for (const key of this.signed.keys()) if (!key.endsWith(`@${expiresAt}`)) this.signed.delete(key)
- this.signed.set(`${objectKey}@${expiresAt}`, url)
- return url
+ /** The window's link to [objectKey], or null when it could not be signed. A failure is not kept, and is logged by its code
+ * alone: storage's message may quote the key. */
+ private async link(objectKey: string, expiresAt: number): Promise<string | null> {
+ const cacheKey = `${objectKey}@${expiresAt}`
+ let pending = this.signed.get(cacheKey)
+ if (!pending) {
+ // Links of past windows are never asked again.
+ for (const key of this.signed.keys()) if (!key.endsWith(`@${expiresAt}`)) this.signed.delete(key)
+ pending = this.bucket.signedUrl(objectKey, expiresAt)
+ this.signed.set(cacheKey, pending)
+ }
+ try { return await pending } catch (error) {
+ if (this.signed.get(cacheKey) === pending) this.signed.delete(cacheKey)
+ logger.warn('[avatar-links] could not sign an avatar link', { code: errorCode(error) })
+ return null
+ }
}
}
diff --git a/backend/src/superTabs/fileLinks.test.ts b/backend/src/superTabs/fileLinks.test.ts
new file mode 100644
index 000000000..aad21e4fd
--- /dev/null
+++ b/backend/src/superTabs/fileLinks.test.ts
@@ -0,0 +1,95 @@
+import { describe, expect, it } from 'vitest'
+import { MemoryAttachmentStore, type Attachment } from './attachments.js'
+import { FILE_LINK_TTL_MS, FILE_LINK_WINDOW_MS, FileLinks, fileLinkExpiry, MAX_CACHED_LINKS } from './fileLinks.js'
+
+const base = 'https://cdn.example.test'
+const now = Date.UTC(2026, 9, 31, 17, 2)
+const file = (key: string, id = 'f1'): Attachment => ({ id, name: 'notes.txt', mime: 'text/plain', size: 5, url: `${base}/${key}` })
+const row = (...files: Attachment[]) => ({ id: 'm1', attachments: files })
+
+describe('chat file links', () => {
+ it('adds a signed link that opens until it expires, beside the public address older apps open', async () => {
+ const bucket = new MemoryAttachmentStore(() => now)
+ bucket.objects.set('super-tabs/tab1/abc/notes.txt', { bytes: Buffer.from('hello'), contentType: 'text/plain', contentDisposition: 'attachment' })
+ const [read] = await new FileLinks(bucket, base, 'super-tabs').forRows('tab1', [row(file('super-tabs/tab1/abc/notes.txt'))], now)
+ const [linked] = read!.attachments
+ expect(linked!.url).toBe(`${base}/super-tabs/tab1/abc/notes.txt`)
+ expect(linked!.link!.startsWith('https://storage.memory.test/')).toBe(true)
+ expect(linked!.expiresAt).toBe(fileLinkExpiry(now))
+ expect(linked!.expiresAt! - now).toBeGreaterThan(FILE_LINK_TTL_MS - FILE_LINK_WINDOW_MS)
+ expect(linked!.expiresAt! - now).toBeLessThanOrEqual(FILE_LINK_TTL_MS)
+ expect(bucket.open(linked!.link!, now)?.toString()).toBe('hello')
+ // A copied link stops opening once its time is over: the removed member's case.
+ expect(bucket.open(linked!.link!, linked!.expiresAt!)).toBeNull()
+ // Only the fields a message stores for a file go out, and the link.
+ expect(Object.keys(linked!).sort()).toEqual(['expiresAt', 'id', 'link', 'mime', 'name', 'size', 'url'])
+ })
+
+ it('links only files in the reading Swarm\'s own folder', async () => {
+ const links = new FileLinks(new MemoryAttachmentStore(() => now), base, 'super-tabs')
+ const [read] = await links.forRows('tab1', [row(
+ file('super-tabs/tab1/abc/ok.txt', 'ok'),
+ file('super-tabs/tab2/abc/other-swarm.txt', 'other'),
+ file('avatars/tab1/abc/avatar', 'avatar'),
+ file('super-tabs/tab1x/abc/prefix-lookalike.txt', 'lookalike'),
+ file('super-tabs/tab1/../tab2/abc/escape.txt', 'escape'),
+ file('super-tabs/tab1/abc/x.txt?y=1', 'query'),
+ { ...file('super-tabs/tab1/abc/elsewhere.txt', 'elsewhere'), url: 'https://evil.example.test/super-tabs/tab1/abc/elsewhere.txt' },
+ )], now)
+ expect(Object.fromEntries(read!.attachments.map(a => [a.id, !!a.link]))).toEqual(
+ { ok: true, other: false, avatar: false, lookalike: false, escape: false, query: false, elsewhere: false })
+ expect(read!.attachments.filter(a => !a.link).every(a => a.expiresAt === undefined && a.unlinked === true)).toBe(true)
+ expect(read!.attachments.find(a => a.link)!.unlinked).toBeUndefined()
+ })
+
+ it('signs each file once per window however many reads ask at once, and afresh in the next window', async () => {
+ const signed: string[] = []
+ const bucket = { signedUrl: async (key: string, expiresAt: number) => { signed.push(key); await new Promise(r => setTimeout(r, 5)); return `https://storage.example.test/${key}?e=${expiresAt}` } }
+ const links = new FileLinks(bucket, base, 'super-tabs')
+ const page = [row(file('super-tabs/tab1/a/one.txt', 'one')), row(file('super-tabs/tab1/a/one.txt', 'one'), file('super-tabs/tab1/b/two.txt', 'two'))]
+ const reads = await Promise.all([links.forRows('tab1', page, now), links.forRows('tab1', page, now + 1000)])
+ expect(signed.sort()).toEqual(['super-tabs/tab1/a/one.txt', 'super-tabs/tab1/b/two.txt'])
+ expect(reads[0]![0]!.attachments[0]!.link).toBe(reads[1]![1]!.attachments[0]!.link)
+ await links.forRows('tab1', page, now + FILE_LINK_WINDOW_MS)
+ expect(signed).toHaveLength(4)
+ })
+
+ it('signs at most eight at once for one read', async () => {
+ let running = 0, most = 0
+ const bucket = { signedUrl: async (key: string) => { running++; most = Math.max(most, running); await new Promise(r => setTimeout(r, 2)); running--; return `https://s.test/${key}` } }
+ const page = Array.from({ length: 40 }, (_, i) => row(file(`super-tabs/tab1/k${i}/f.txt`, `f${i}`)))
+ const read = await new FileLinks(bucket, base, 'super-tabs').forRows('tab1', page, now)
+ expect(read.every(r => r.attachments[0]!.link!.startsWith('https://s.test/'))).toBe(true)
+ expect(most).toBe(8)
+ })
+
+ it('a signature that fails reads as not available and is tried again on the next read', async () => {
+ let fail = true
+ const bucket = { signedUrl: async (key: string) => { if (fail) throw Object.assign(new Error(`denied ${key}`), { code: 403 }); return `https://s.test/${key}` } }
+ const links = new FileLinks(bucket, base, 'super-tabs')
+ const first = await links.forRows('tab1', [row(file('super-tabs/tab1/a/one.txt'))], now)
+ expect(first[0]!.attachments[0]!.link).toBeUndefined()
+ expect(first[0]!.attachments[0]!.expiresAt).toBeUndefined()
+ // Marked, so an app asks again or says to try again rather than open the public address (review 1 of #2460).
+ expect(first[0]!.attachments[0]!.unlinked).toBe(true)
+ fail = false
+ const second = await links.forRows('tab1', [row(file('super-tabs/tab1/a/one.txt'))], now)
+ expect(second[0]!.attachments[0]!.link).toBe('https://s.test/super-tabs/tab1/a/one.txt')
+ })
+
+ it('keeps no more than its cap of links', async () => {
+ let count = 0
+ const links = new FileLinks({ signedUrl: async key => { count++; return `https://s.test/${key}` } }, base, 'super-tabs')
+ const page = (from: number, n: number) => Array.from({ length: n }, (_, i) => row(file(`super-tabs/tab1/k${from + i}/f.txt`, `f${i}`)))
+ await links.forRows('tab1', page(0, MAX_CACHED_LINKS), now)
+ expect((links as unknown as { signed: Map<string, unknown> }).signed.size).toBe(MAX_CACHED_LINKS)
+ await links.forRows('tab1', page(MAX_CACHED_LINKS, 1), now)
+ expect((links as unknown as { signed: Map<string, unknown> }).signed.size).toBe(1)
+ expect(count).toBe(MAX_CACHED_LINKS + 1)
+ })
+
+ it('leaves rows without files as they are', async () => {
+ const rows = [{ id: 'a' }, { id: 'b', attachments: null }, { id: 'c', attachments: [] }]
+ expect(await new FileLinks({ signedUrl: async () => { throw new Error('never') } }, base, 'super-tabs').forRows('tab1', rows, now)).toBe(rows)
+ })
+})
diff --git a/backend/src/superTabs/fileLinks.ts b/backend/src/superTabs/fileLinks.ts
new file mode 100644
index 000000000..7ce9c4546
--- /dev/null
+++ b/backend/src/superTabs/fileLinks.ts
@@ -0,0 +1,100 @@
+import { logger } from '../utils/logger.js'
+import { errorCode, trimPrefix, type Attachment, type AttachmentStore } from './attachments.js'
+
+/**
+ * Chat files are opened only through short-lived signed links, made on each read for whoever can read the message now
+ * (security review of Delete Swarm, 10-31): a member removed from a project or a Swarm, or anyone once the Swarm is deleted,
+ * gets no new link, and a link already handed out stops opening within FILE_LINK_TTL_MS. Reads still carry each file's
+ * public address too (`url`), which apps from before this open: it keeps working only until the release step makes the
+ * bucket private (docs/swarms.md, "Chat file links"), and from then only a signed link opens a file.
+ *
+ * A link is the same through each five minutes, so a picture is fetched once in that window rather than on every poll, and
+ * signing (a network call when the cloud signs it) happens at most once per file and window on a server. It opens for ten
+ * minutes from the window's start: between five and ten after it is read.
+ */
+export const FILE_LINK_WINDOW_MS = 5 * 60_000
+export const FILE_LINK_TTL_MS = 10 * 60_000
+export const fileLinkExpiry = (now: number) => Math.floor(now / FILE_LINK_WINDOW_MS) * FILE_LINK_WINDOW_MS + FILE_LINK_TTL_MS
+/** Links kept for the current window; past it the cache starts again, which costs one more signature per file shown. */
+export const MAX_CACHED_LINKS = 20_000
+/** Signatures one read waits on at once: a page of 100 messages may carry hundreds of files. */
+const SIGN_AT_ONCE = 8
+
+type Signer = Pick<AttachmentStore, 'signedUrl'>
+
+export class FileLinks {
+ private readonly signed = new Map<string, Promise<string>>()
+ private window = 0
+
+ /** [publicBase] and [prefix]: where stored addresses point and the folder chat files are in (attachments.ts). */
+ constructor(private readonly bucket: Signer, private readonly publicBase: string, private readonly prefix: string) {}
+
+ /** The object a stored address names, only inside [tabId]'s own folder: a row of one Swarm never gets a link to another
+ * Swarm's file, an avatar, or anything else in the bucket, whatever the row says. */
+ keyOf(tabId: string, url: unknown): string | null {
+ if (typeof url !== 'string') return null
+ const base = `${this.publicBase.replace(/\/+$/, '')}/`
+ if (!url.startsWith(base)) return null
+ const key = url.slice(base.length)
+ const folder = `${[trimPrefix(this.prefix), tabId].filter(Boolean).join('/')}/`
+ const parts = key.split('/')
+ return key.startsWith(folder) && !/[?#\\%]/.test(key) && parts.every(part => part && part !== '.' && part !== '..') ? key : null
+ }
+
+ /** [rows] with each file's signed link and its expiry added. A file that cannot be linked (its signature failed, or its row
+ * names nothing in this Swarm's folder) is marked `unlinked`: apps that know links then ask for a fresh one or say to try
+ * again, and never fall back to the public address. Only the fields a message stores for a file are kept. */
+ async forRows<T extends { attachments?: unknown }>(tabId: string, rows: T[], now: number): Promise<T[]> {
+ if (!rows.some(row => Array.isArray(row.attachments) && row.attachments.length)) return rows
+ const expiresAt = fileLinkExpiry(now)
+ if (expiresAt !== this.window || this.signed.size >= MAX_CACHED_LINKS) { this.signed.clear(); this.window = expiresAt }
+ const keys = new Set<string>()
+ for (const row of rows) for (const file of files(row)) { const key = this.keyOf(tabId, file.url); if (key) keys.add(key) }
+ const links = await this.signAll([...keys], expiresAt)
+ return rows.map(row => !Array.isArray(row.attachments) ? row : { ...row, attachments: files(row).map(file => {
+ const key = this.keyOf(tabId, file.url), url = key ? links.get(key) : undefined
+ return { id: file.id, name: file.name, mime: file.mime, size: file.size, url: typeof file.url === 'string' ? file.url : '',
+ ...url ? { link: url, expiresAt } : { unlinked: true as const } } satisfies Attachment
+ }) })
+ }
+
+ /** One file's link, as forRows hands it. */
+ async forFile(tabId: string, file: Attachment, now: number): Promise<Attachment> {
+ return (await this.forRows(tabId, [{ attachments: [file] }], now))[0]!.attachments[0] as Attachment
+ }
+
+ private async signAll(keys: string[], expiresAt: number): Promise<Map<string, string>> {
+ const links = new Map<string, string>()
+ let next = 0
+ const worker = async () => {
+ while (next < keys.length) {
+ const key = keys[next++]!
+ const url = await this.link(key, expiresAt)
+ if (url) links.set(key, url)
+ }
+ }
+ await Promise.all(Array.from({ length: Math.min(SIGN_AT_ONCE, keys.length) }, worker))
+ return links
+ }
+
+ /** The window's link to [key], signed once however many reads ask at the same moment. A failure is not kept, so the next
+ * read tries again, and is logged by its code alone: storage's message may quote the key. */
+ private async link(key: string, expiresAt: number): Promise<string | null> {
+ const cacheKey = `${expiresAt} ${key}`
+ let pending = this.signed.get(cacheKey)
+ if (!pending) {
+ pending = this.bucket.signedUrl(key, expiresAt)
+ this.signed.set(cacheKey, pending)
+ }
+ try { return await pending } catch (error) {
+ if (this.signed.get(cacheKey) === pending) this.signed.delete(cacheKey)
+ logger.warn('[file-links] could not sign a chat file link', { code: errorCode(error) })
+ return null
+ }
+ }
+}
+
+function files(row: { attachments?: unknown }): Attachment[] {
+ if (!Array.isArray(row.attachments)) return []
+ return row.attachments.filter((file): file is Attachment => !!file && typeof file === 'object' && typeof (file as Attachment).id === 'string')
+}
diff --git a/backend/src/superTabs/profile.ts b/backend/src/superTabs/profile.ts
index c5b6c45d3..9b437acb3 100644
--- a/backend/src/superTabs/profile.ts
+++ b/backend/src/superTabs/profile.ts
@@ -31,7 +31,7 @@ export function setStatus(state: SuperTabState, actor: Actor, status: z.infer<ty
export const AVATAR_PRESETS = ['fox', 'owl', 'cat', 'panda', 'octopus', 'cactus', 'rocket', 'sun'] as const
/** What a member or agent shows: an uploaded picture or a preset. Absent: the default, initials for a person, a pattern from
* the name for an agent (Desktop). An upload keeps its object's key on the server; readers never get the key, only a link with
- * each read (avatarLinks.ts): short-lived in a private avatar bucket, permanent on the attachments CDN. */
+ * each read (avatarLinks.ts), signed for an hour wherever the picture is kept. */
export type Avatar = { kind: 'upload'; key: string } | { kind: 'preset'; key: (typeof AVATAR_PRESETS)[number] }
/** An avatar as readers get it: an upload says only that there is one. */
export type AvatarView = { kind: 'upload' } | Extract<Avatar, { kind: 'preset' }>
diff --git a/backend/src/superTabs/purge.ts b/backend/src/superTabs/purge.ts
index 4a6ba5662..adb3ca0e6 100644
--- a/backend/src/superTabs/purge.ts
+++ b/backend/src/superTabs/purge.ts
@@ -1,6 +1,6 @@
import type { PrismaClient } from '@prisma/client'
import { logger } from '../utils/logger.js'
-import { AVATAR_PURPOSE, attachmentsFromEnv, CdnAvatarStore, errorCode, trimPrefix, type Attachments, type AttachmentStore } from './attachments.js'
+import { AVATAR_PURPOSE, attachmentsFromEnv, SharedAvatarStore, errorCode, trimPrefix, type Attachments, type AttachmentStore } from './attachments.js'
import { readBy } from './parts/indexed.js'
import { ms } from './parts/light.js'
@@ -30,7 +30,7 @@ export interface PurgeFiles { chat: FileRemover | null; prefix: string; avatars:
const removerOf = (store: AttachmentStore | null | undefined): FileRemover | null => {
if (!store) return null
- if (store instanceof CdnAvatarStore) return removerOf(store.store)
+ if (store instanceof SharedAvatarStore) return removerOf(store.store)
return 'delete' in store && typeof store.delete === 'function' ? store as unknown as FileRemover : null
}
export function purgeFiles(attachments: Attachments | null): PurgeFiles | null {
diff --git a/backend/src/superTabs/store.ts b/backend/src/superTabs/store.ts
index eb0be3451..6b3ce3fd5 100644
--- a/backend/src/superTabs/store.ts
+++ b/backend/src/superTabs/store.ts
@@ -20,6 +20,7 @@ import { restoreDelivery } from './deliveryCompaction.js'
import { threadSummaries } from './threads.js'
import { attachmentMime, attachmentName, AttachmentTooLarge, AVATAR_PURPOSE, AVATAR_TYPES, checkedType, directUploads, errorCode, HOURLY_UPLOAD_BYTES, HOURLY_UPLOADS, MAX_ATTACHMENT_BYTES, MAX_AVATAR_BYTES, MAX_FORM_ATTACHMENT_BYTES, objectKeyFor, peekType, PENDING_UPLOAD_PURPOSE, publicUrl, TOO_LARGE_MESSAGE, trimPrefix, typeFromHead, UPLOAD_LINK_MS,
type Attachment, type Attachments, type AttachmentStore, type AvatarStore } from './attachments.js'
+import { FileLinks } from './fileLinks.js'
import { randomBytes } from 'node:crypto'
import type { Readable } from 'node:stream'
import { logger } from '../utils/logger.js'
@@ -518,9 +519,12 @@ export class SuperTabStore {
/** No Harness GitHub App on this server: Team accounts take no teammates' work (SuperTabStoreHooks.teamAccountsIdle). Reads
* decide New Project's Product Manager host as its start does (read/views.ts). */
get teamAccountsIdle(): boolean { return this.hooks.teamAccountsIdle === true }
+ /** Signs the links every read hands out for chat files (fileLinks.ts); null without a bucket. */
+ private readonly fileLinks: FileLinks | null
constructor(readonly db: PrismaClient, readonly now: () => number = Date.now,
private readonly attachments: Attachments | null = null, private readonly hooks: SuperTabStoreHooks = {}) {
+ this.fileLinks = attachments ? new FileLinks(attachments.store, attachments.publicBase, attachments.prefix) : null
this.polls = hooks.polls ?? localPollMemory(now)
this.reports = hooks.reports ?? localReportMemory(now)
this.native = new SuperTabNativeStore({
@@ -1787,7 +1791,7 @@ export class SuperTabStore {
const key = await this.uploader(tabId, tx => this.poster(tx, user, tabId, conversationId, asAgentId).then(p => p.actor))
const row = await this.keep(attachments.store, attachments.prefix, tabId, key, { name: name.data, conversationId },
async () => checkedType(mime.data, file.body), file.body, MAX_FORM_ATTACHMENT_BYTES)
- return { attachment: { id: row.id, name: row.name, mime: row.mime, size: row.size, url: publicUrl(attachments.publicBase, row.objectKey) } satisfies Attachment }
+ return { attachment: await this.linkedFile(tabId, { id: row.id, name: row.name, mime: row.mime, size: row.size, url: publicUrl(attachments.publicBase, row.objectKey) }) }
}
/** A link to put one file of `size` bytes straight into the bucket, for a file too big for this server's form route. The
@@ -1856,7 +1860,8 @@ export class SuperTabStore {
await this.poster(tx, user, tabId, found!.conversationId, asAgentId)
return found!
})
- const sendable = (mime: string) => ({ attachment: { id: row.id, name: row.name, mime, size: row.size, url: publicUrl(attachments.publicBase, row.objectKey) } satisfies Attachment })
+ const sendable = async (mime: string) => ({ attachment: await this.linkedFile(tabId, { id: row.id, name: row.name, mime, size: row.size,
+ url: publicUrl(attachments.publicBase, row.objectKey) }) })
if (!row.purpose) return sendable(row.mime)
let stored: { size: number } | null, head: Buffer
try {
@@ -2350,6 +2355,28 @@ export class SuperTabStore {
if (told) afterCommit(tx, () => told(waiting))
}
+ /** [rows] as a reader is handed them: each file with a signed link of this read (fileLinks.ts). A server without a bucket
+ * links nothing. */
+ private async linked<T extends { attachments?: unknown }>(tabId: string, rows: T[]): Promise<T[]> {
+ return this.fileLinks ? this.fileLinks.forRows(tabId, rows, this.now()) : rows
+ }
+ private async linkedFile(tabId: string, file: Attachment): Promise<Attachment> {
+ return (await this.linked(tabId, [{ attachments: [file] }]))[0]!.attachments[0] as Attachment
+ }
+
+ /** A fresh link to one file of one message, for a client whose earlier link is about to stop opening. Read as that exact
+ * message is (messages() with its id), so who may open it is exactly who may read the message now: anyone else, a message
+ * that is not there, and a file it does not carry are answered alike. A file the reader may open whose link could not be
+ * signed just now is answered as a failure to try again (503), never as lost access (review 1 of #2460). */
+ async fileLink(user: AuthUser, tabId: string, conversationId: string, messageId: string, attachmentId: string, asAgentId?: string) {
+ const page = await this.messages(user, tabId, conversationId, { messageId }, asAgentId)
+ const message = page.messages.find(m => m.id === messageId)
+ const file = Array.isArray(message?.attachments) ? (message.attachments as unknown as Attachment[]).find(a => a.id === attachmentId) : undefined
+ if (!file || !this.fileLinks?.keyOf(tabId, file.url)) fail('FILE_UNAVAILABLE', 'This file is no longer available.', 404)
+ if (!file!.link) fail('FILE_LINK_UNAVAILABLE', 'Couldn\'t open the file just now. Try again.', 503)
+ return { attachment: file }
+ }
+
/** One page of a conversation, read by (sequence, id): rows written by one command before the store by parts share a
* sequence, so a page edge inside such a run must not skip any (design section 4.6). `after` alone reads strictly after
* a sequence, which is enough to poll for new messages. */
@@ -2441,7 +2468,9 @@ export class SuperTabStore {
// A session's own thread read carries the main-chat excerpt it started from (buddySessions.ts sessionContext).
// A project session of a person's one Buddy chat reads no excerpt: the main chat may hold any project's material (spec: Guard).
const context = own && !own.projectId && !forward && cursor.before === undefined && !cursor.messageId ? await this.sessionExcerpt(tx, tabId, conversationId, own.from, m => rowRead(m) && sessionReadsRow(undefined, group, state)(m)) : undefined
- return { messages: shown.map(m => ({ ...m, mentions: m.mentions ?? [], reactions: reactions.get(m.id) ?? [],
+ // Only the rows this reader is shown get file links, each signed afresh for this read (fileLinks.ts).
+ const linked = await this.linked(tabId, shown)
+ return { messages: linked.map(m => ({ ...m, mentions: m.mentions ?? [], reactions: reactions.get(m.id) ?? [],
deliveries: deliveries.filter(d => d.messageId === m.id && shownTo(m, d)).sort(receiptOrder(m)) })), next, threads, ...(context ? { context } : {}),
// A poll for new messages learns here that a reaction changed on a message it already shows (reactions.ts).
reactionSeq: group.reactionSeq ?? 0 }
@@ -3145,7 +3174,11 @@ export class SuperTabStore {
| { type: 'hold'; claimId: string; reason: HoldReason }) {
const reached = { authorized: false }
try {
- return await this.deliver(user, tabId, agentId, deliveryId, action, reached)
+ const answer = await this.deliver(user, tabId, agentId, deliveryId, action, reached)
+ // The claimed message's files as links of this claim, signed after its transaction (fileLinks.ts): a claim again, as the host
+ // makes when a download failed, links them afresh.
+ if (!('message' in answer) || !answer.message) return answer
+ return { ...answer, message: (await this.linked(tabId, [answer.message]))[0]! }
} catch (error) {
// Prod 10-30, v1.3.71: 2 hours of SWARM_BUSY claims left no trace on the delivery. Retries end that way (retry.ts), so it is
// the only code to record; the client still gets the original error. Only once an attempt got past the authorization
diff --git a/cli/src/config/env.ts b/cli/src/config/env.ts
index ac945f1a2..6af12ffd1 100644
--- a/cli/src/config/env.ts
+++ b/cli/src/config/env.ts
@@ -250,6 +250,11 @@ const fields = {
SUPER_TAB_ATTACHMENTS_PUBLIC_BASE: (raw: string | undefined) => url((address) => !address.username && !address.password && !address.search
&& (address.protocol === 'https:' || (address.protocol === 'http:' && ['127.0.0.1', 'localhost', '[::1]'].includes(address.hostname))))(raw)
?? 'https://cdn.harness.autonomous.ai',
+ // Where the backend's short-lived signed links to Super Tab chat files point: Cloud Storage's own host (backend
+ // superTabs/fileLinks.ts). The host downloads from here or from the public base above (an older backend), nowhere else.
+ SUPER_TAB_FILES_BASE: (raw: string | undefined) => url((address) => !address.username && !address.password && !address.search
+ && (address.protocol === 'https:' || (address.protocol === 'http:' && ['127.0.0.1', 'localhost', '[::1]'].includes(address.hostname))))(raw)
+ ?? 'https://storage.googleapis.com',
// This computer's stable id, minted once and never regenerated (see computerIdFile above). Pin it
// explicitly on a box with no durable home — a container or CI job that gets a fresh ~/.harness on
// every boot would otherwise look like a NEW computer each time and collect a machine per start.
diff --git a/cli/src/superTabs/attachments.spec.ts b/cli/src/superTabs/attachments.spec.ts
index 116281b5a..0025f739f 100644
--- a/cli/src/superTabs/attachments.spec.ts
+++ b/cli/src/superTabs/attachments.spec.ts
@@ -2,7 +2,7 @@ import { readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
import { basename, dirname, join } from 'node:path'
import { describe, expect, it, vi } from 'vitest'
import { PASTE_DROPS_DIR } from '../lib/pasteDropFiles.js'
-import { attachedFiles, attachmentsOf, diskName, formatSize, MAX_ATTACHMENT_BYTES, servedFrom } from './attachments.js'
+import { attachedFiles, attachmentsOf, diskName, formatSize, MAX_ATTACHMENT_BYTES, sameObject, servedFrom } from './attachments.js'
const base = 'https://cdn.example.test'
const file = (over: Record<string, unknown> = {}) => ({ id: 'a1', name: 'screen.png', mime: 'image/png', size: 5, url: `${base}/super-tabs/tab/abc/screen.png`, ...over })
@@ -60,7 +60,7 @@ describe('Super Tab attachment downloads', () => {
expect(flaky).toHaveBeenCalledTimes(2)
const down = replying(new Uint8Array(0), 503)
expect(await attachedFiles({ attachments: [file()] }, { base, fetch: down }))
- .toEqual({ text: `Attached files:\n- Could not download ${file().url} (sent as "screen.png", 5 B): HTTP 503`, complete: false, paths: [] })
+ .toEqual({ text: `Attached files:\n- Could not download a file (sent as "screen.png", 5 B): HTTP 503`, complete: false, paths: [] })
expect(down).toHaveBeenCalledTimes(3)
const short = replying(new Uint8Array(2))
expect((await attachedFiles({ attachments: [file()] }, { base, fetch: short })).text).toContain('incomplete download')
@@ -71,7 +71,7 @@ describe('Super Tab attachment downloads', () => {
expect(await attachedFiles({ attachments: [file()] }, { base, fetch: slow, now: () => clock, budgetMs: 45_000 })).toMatchObject({ complete: false })
expect(slow).toHaveBeenCalledTimes(2)
expect(await attachedFiles({ attachments: [file()] }, { base, fetch: slow, now: () => clock, budgetMs: 0 }))
- .toEqual({ text: `Attached files:\n- Could not download ${file().url} (sent as "screen.png", 5 B): out of time`, complete: false, paths: [] })
+ .toEqual({ text: `Attached files:\n- Could not download a file (sent as "screen.png", 5 B): out of time`, complete: false, paths: [] })
})
it('fetches only from the public base, and never retries a refusal', async () => {
const fetcher = replying(new Uint8Array(5))
@@ -79,7 +79,7 @@ describe('Super Tab attachment downloads', () => {
file({ url: `${base}/y.png`, size: 3 })] }, { base: `${base}/`, fetch: fetcher })
expect(listed.text).toContain('- Could not download a file (sent as "screen.png", 5 B): not a Harness file address')
expect(listed.text).not.toContain('evil')
- expect(listed.text).toContain(`- Could not download ${base}/y.png (sent as "screen.png", 3 B): larger than declared`)
+ expect(listed.text).toContain(`- Could not download a file (sent as "screen.png", 3 B): larger than declared`)
expect(fetcher).toHaveBeenCalledTimes(1)
const missing = replying(new Uint8Array(0), 404)
expect((await attachedFiles({ attachments: [file()] }, { base, fetch: missing })).text).toContain('HTTP 404')
@@ -168,7 +168,7 @@ describe('Super Tab attachment downloads', () => {
// Room for the file but not the 512 MB kept free beside it.
const statfs = vi.fn(async () => ({ bavail: (size + 256 * 1024 * 1024) / 4096, bsize: 4096 }))
const listed = await attachedFiles({ attachments: [file({ size })] }, { base, fetch: fetcher, statfs })
- expect(listed).toEqual({ text: `Attached files:\n- Could not download ${file().url} (sent as "screen.png", 100.0 MB): not enough free disk space`,
+ expect(listed).toEqual({ text: `Attached files:\n- Could not download a file (sent as "screen.png", 100.0 MB): not enough free disk space`,
complete: false, paths: [] })
expect(fetcher).not.toHaveBeenCalled()
expect(statfs).toHaveBeenCalledWith(PASTE_DROPS_DIR)
@@ -220,4 +220,133 @@ describe('Super Tab attachment downloads', () => {
expect([servedFrom(base, `${base}/a`), servedFrom(`${base}/files`, `${base}/filesx/a`), servedFrom(base, 'not a url'),
servedFrom(base, `https://user:[email protected]/a`)]).toEqual([true, false, false, false])
})
+
+ // Chat files open through short-lived signed links (backend superTabs/fileLinks.ts, security review of Delete Swarm 10-31).
+ describe('signed links', () => {
+ const signedBase = 'https://storage.example.test'
+ const signed = (over: Record<string, unknown> = {}) => file({ link: `${signedBase}/bucket/super-tabs/tab/abc/screen.png?X-Goog-Signature=secret`,
+ expiresAt: 600_000, ...over })
+ const fresh = (link = `${signedBase}/bucket/super-tabs/tab/abc/screen.png?X-Goog-Signature=fresh`) =>
+ vi.fn(async (sent: { id: string }) => ({ ...signed(), id: sent.id, link, expiresAt: 1_200_000 }))
+
+ it('downloads by the signed link over the public address, from the signed-link host, and from nowhere that only looks like it', async () => {
+ const fetcher = replying(new Uint8Array(5))
+ expect((await attachedFiles({ attachments: [signed()] }, { base, signedBase, fetch: fetcher, now: () => 0 })).complete).toBe(true)
+ expect(fetcher).toHaveBeenCalledWith(expect.stringContaining("Signature=secret"), expect.anything())
+ // An older backend sends no link: the public address, as before.
+ const older = replying(new Uint8Array(5))
+ expect((await attachedFiles({ attachments: [file()] }, { base, signedBase, fetch: older, now: () => 0 })).complete).toBe(true)
+ expect(older).toHaveBeenCalledWith(file().url, expect.anything())
+ expect([servedFrom(base, `${signedBase}/b/k`, signedBase), servedFrom(base, 'https://storage.example.test.evil/b/k', signedBase),
+ servedFrom(base, 'http://storage.example.test/b/k', signedBase), servedFrom(base, 'https://u:[email protected]/b/k', signedBase)])
+ .toEqual([true, false, false, false])
+ })
+
+ it('never writes a link into the prompt, only the file\'s name and size', async () => {
+ const listed = await attachedFiles({ attachments: [signed()] }, { base, signedBase, fetch: replying(new Uint8Array(0), 503), now: () => 0 })
+ expect(listed.complete).toBe(false)
+ expect(listed.text).toBe('Attached files:\n- Could not download a file (sent as "screen.png", 5 B): HTTP 503')
+ expect(listed.text).not.toContain('Signature')
+ })
+
+ it('starts with a fresh link when its own is about to end, and takes a fresh one when storage says it is over', async () => {
+ const ending = fresh(), fetcher = replying(new Uint8Array(5))
+ expect((await attachedFiles({ attachments: [signed({ expiresAt: 10_000 })] }, { base, signedBase, fetch: fetcher, now: () => 0, relink: ending })).complete).toBe(true)
+ expect(ending).toHaveBeenCalledTimes(1)
+ expect(fetcher).toHaveBeenCalledWith(expect.stringContaining('Signature=fresh'), expect.anything())
+ // A link that was over by the time the request started (403): one fresh link, then the bytes.
+ const over = vi.fn().mockResolvedValueOnce(new Response(null, { status: 403 })).mockResolvedValue(new Response(new Uint8Array(5)))
+ const relink = fresh()
+ expect((await attachedFiles({ attachments: [signed()] }, { base, signedBase, fetch: over, now: () => 0, relink })).complete).toBe(true)
+ expect(relink).toHaveBeenCalledTimes(1)
+ expect(over.mock.calls.map(c => String(c[0]).split('=').at(-1))).toEqual(['secret', 'fresh'])
+ })
+
+ it('stops at once when the reader may no longer read the file, and never retries without a way to relink', async () => {
+ const over = vi.fn(async () => new Response(null, { status: 403 }))
+ const refused = vi.fn(async () => { throw Object.assign(new Error('This file is no longer available.'), { status: 404 }) })
+ const listed = await attachedFiles({ attachments: [signed()] }, { base, signedBase, fetch: over, now: () => 0, relink: refused })
+ expect(listed).toEqual({ text: 'Attached files:\n- Could not download a file (sent as "screen.png", 5 B): no longer available', complete: false, paths: [] })
+ expect([over.mock.calls.length, refused.mock.calls.length]).toEqual([1, 1])
+ // The backend could not answer just now: said so, not as access lost.
+ const down = vi.fn(async () => { throw Object.assign(new Error('Could not reach this Super Tab.'), { status: 503 }) })
+ expect((await attachedFiles({ attachments: [signed()] }, { base, signedBase, fetch: vi.fn(async () => new Response(null, { status: 403 })), now: () => 0,
+ relink: down })).text).toContain('could not get a fresh link')
+ // No relink: one try, the expired link is not fetched again.
+ const alone = vi.fn(async () => new Response(null, { status: 403 }))
+ expect((await attachedFiles({ attachments: [signed()] }, { base, signedBase, fetch: alone, now: () => 0 })).complete).toBe(false)
+ expect(alone).toHaveBeenCalledTimes(1)
+ // A file the server could not link reads as not available, and is never fetched.
+ const none = vi.fn()
+ expect((await attachedFiles({ attachments: [signed({ link: '', url: '' })] }, { base, signedBase, fetch: none, now: () => 0 })).text).toContain('no longer available')
+ expect(none).not.toHaveBeenCalled()
+ })
+
+ it('lets a slow download that started in time finish after its link ends', async () => {
+ // Storage checks a signed link when the request starts (fileLinks.ts): the bytes keep coming after expiresAt.
+ let clock = 0
+ const chunk = new Uint8Array(64 * 1024)
+ const slow = vi.fn(async () => new Response(new ReadableStream<Uint8Array>({ async pull(controller) {
+ clock += 60_000
+ await new Promise(resolve => setTimeout(resolve, 1))
+ if (clock > 16 * 60_000) { controller.close(); return }
+ controller.enqueue(chunk)
+ } })))
+ const relink = fresh()
+ const big = signed({ size: 16 * chunk.byteLength, expiresAt: 5 * 60_000 })
+ const listed = await attachedFiles({ attachments: [big] }, { base, signedBase, fetch: slow, now: () => clock, relink, idleMs: 60_000 })
+ expect(listed.complete).toBe(true)
+ expect(clock).toBeGreaterThan(5 * 60_000)
+ expect([slow.mock.calls.length, relink.mock.calls.length]).toEqual([1, 0])
+ })
+
+ it('shares one download of a file between deliveries whose links differ', async () => {
+ const inFlight = new Map()
+ const fetcher = replying(new Uint8Array(5))
+ const [one, two] = await Promise.all([
+ attachedFiles({ attachments: [signed()] }, { base, signedBase, fetch: fetcher, now: () => 0, inFlight }),
+ attachedFiles({ attachments: [signed({ link: `${signedBase}/bucket/super-tabs/tab/abc/screen.png?X-Goog-Signature=other` })] },
+ { base, signedBase, fetch: fetcher, now: () => 0, inFlight })])
+ expect([one.complete, two.complete]).toEqual([true, true])
+ expect(fetcher).toHaveBeenCalledTimes(1)
+ })
+
+ // Review 1 of #2460: a file the server could not sign must not open by its public address, a dead page once the bucket is
+ // private; and a signed link may name only the object its message's file names.
+ it('asks a fresh link for a file the server could not sign, and never opens its public address', async () => {
+ const unsigned = file({ unlinked: true })
+ const fetcher = replying(new Uint8Array(5)), relink = fresh()
+ expect((await attachedFiles({ attachments: [unsigned] }, { base, signedBase, fetch: fetcher, now: () => 0, relink })).complete).toBe(true)
+ expect(relink).toHaveBeenCalledTimes(1)
+ expect(fetcher).toHaveBeenCalledWith(expect.stringContaining('Signature=fresh'), expect.anything())
+ expect(fetcher).not.toHaveBeenCalledWith(file().url, expect.anything())
+ // Without a way to ask, and while signing still fails: said so, and nothing fetched.
+ const none = vi.fn()
+ expect((await attachedFiles({ attachments: [unsigned] }, { base, signedBase, fetch: none, now: () => 0 })).text)
+ .toBe('Attached files:\n- Could not download a file (sent as "screen.png", 5 B): could not get a link just now')
+ const busy = vi.fn(async () => { throw Object.assign(new Error('Couldn\'t open the file just now. Try again.'), { status: 503 }) })
+ expect((await attachedFiles({ attachments: [unsigned] }, { base, signedBase, fetch: none, now: () => 0, relink: busy })).text)
+ .toContain('could not get a fresh link')
+ expect(none).not.toHaveBeenCalled()
+ })
+
+ it('downloads a signed link only when it names the object its file names, in one bucket', async () => {
+ const fetcher = replying(new Uint8Array(5))
+ const elsewhere = [`${signedBase}/bucket/super-tabs/tab/OTHER/screen.png?s=1`, `${signedBase}/bucket/extra/super-tabs/tab/abc/screen.png?s=1`,
+ `${signedBase}/super-tabs/tab/abc/screen.png?s=1`, `${signedBase}//super-tabs/tab/abc/screen.png?s=1`]
+ for (const link of elsewhere) {
+ expect((await attachedFiles({ attachments: [signed({ link })] }, { base, signedBase, fetch: fetcher, now: () => 0 })).text, link)
+ .toContain('not a Harness file address')
+ }
+ expect(fetcher).not.toHaveBeenCalled()
+ expect(sameObject(base, file().url, `${signedBase}/bucket/super-tabs/tab/abc/screen.png?X-Goog-Signature=1`)).toBe(true)
+ expect(sameObject(base, 'https://elsewhere.test/super-tabs/tab/abc/screen.png', `${signedBase}/bucket/super-tabs/tab/abc/screen.png`)).toBe(false)
+ })
+
+ it('reads a file\'s link expiry, and nothing else it does not know', () => {
+ expect(attachmentsOf({ attachments: [{ ...signed(), extra: 'x' }, { ...file(), expiresAt: 'soon' }, { ...file(), expiresAt: 5 }, { ...file(), link: 7 },
+ { ...file(), unlinked: true }, { ...file(), unlinked: 'yes' }, { ...signed(), unlinked: true }] }))
+ .toEqual([signed(), file(), file(), file(), file({ unlinked: true }), file(), signed()])
+ })
+ })
})
diff --git a/cli/src/superTabs/attachments.ts b/cli/src/superTabs/attachments.ts
index 93ff424b7..74b500615 100644
--- a/cli/src/superTabs/attachments.ts
+++ b/cli/src/superTabs/attachments.ts
@@ -6,8 +6,18 @@ import { object } from './wire.js'
/** The most a file sent by the multipart route may be (lib/superTabAttachmentProxy.ts); a larger one goes by a link. */
export { MAX_FORM_ATTACHMENT_BYTES } from '../lib/superTabAttachmentProxy.js'
-/** As the backend stores it on a message (backend/src/superTabs/attachments.ts). */
-export interface MessageAttachment { id: string; name: string; mime: string; size: number; url: string }
+/** As the backend hands it with a message (backend/src/superTabs/attachments.ts): `link` is a signed link of that read, which
+ * opens until `expiresAt` (epoch ms); `unlinked` says the server could not sign one just now; `url` is the file's public
+ * address, which stops opening once the bucket is private. The link is used whenever there is one, a fresh one is asked for
+ * an unlinked file, and the address only for a file with neither, which only an older backend sends. */
+export interface MessageAttachment { id: string; name: string; mime: string; size: number; url: string; link?: string; expiresAt?: number
+ unlinked?: true }
+/** A fresh link to [file], from the backend, for whoever this host delivers to (superTabs/host.ts): the read checks that they
+ * may still read the message. It throws when they may not, or the file is gone. */
+export type Relink = (file: MessageAttachment) => Promise<MessageAttachment>
+/** A link this close to its end is fetched afresh before a try starts: storage checks it when a request starts, and a
+ * download that starts in time finishes however long it takes. */
+const RELINK_MARGIN_MS = 30_000
/** The backend's per-file limit. A download never reads past it, whatever a message declares. */
export const MAX_ATTACHMENT_BYTES = 256 * 1024 * 1024
@@ -44,13 +54,22 @@ export function attachmentsOf(message: Record<string, unknown>): MessageAttachme
return message.attachments.slice(0, 10).map(object).filter((a): a is Record<string, unknown> & MessageAttachment =>
typeof a.name === 'string' && typeof a.url === 'string' && typeof a.mime === 'string'
&& Number.isSafeInteger(a.size) && (a.size as number) >= 0)
+ .map(a => ({ id: typeof a.id === 'string' ? a.id : '', name: a.name, mime: a.mime, size: a.size, url: a.url,
+ ...typeof a.link === 'string' && a.link ? { link: a.link,
+ ...typeof a.expiresAt === 'number' && Number.isFinite(a.expiresAt) ? { expiresAt: a.expiresAt } : {} }
+ : a.unlinked === true ? { unlinked: true as const } : {} }))
}
export const formatSize = (bytes: number) => bytes < 1024 ? `${bytes} B`
: bytes < 1024 * 1024 ? `${(bytes / 1024).toFixed(1)} KB` : `${(bytes / 1024 / 1024).toFixed(1)} MB`
-/** Only files under the configured public base: a message's URL must not make this computer fetch another address. */
-export function servedFrom(base: string, address: string): boolean {
+/** Only files on Harness's own file storage, under the backend's public base (an older backend) or on the signed-link host:
+ * a message's URL must not make this computer fetch another address. A signed link must also name the very object its file's
+ * public address names (sameObject). */
+export function servedFrom(base: string, address: string, signedBase = env.SUPER_TAB_FILES_BASE): boolean {
+ return under(base, address) || under(signedBase, address)
+}
+function under(base: string, address: string): boolean {
try {
const root = new URL(base), target = new URL(address)
const prefix = root.pathname.endsWith('/') ? root.pathname : `${root.pathname}/`
@@ -65,9 +84,31 @@ class Refused extends Error {}
* shell reads as syntax ($(...), quotes, spaces, ;). The original name stays in the label beside it. */
export const diskName = (name: string) => name.replaceAll(/[^A-Za-z0-9._-]+/g, '_').replace(/^[.-]+/, '').slice(-120) || 'file'
-async function download(file: MessageAttachment, base: string, fetcher: typeof fetch, ms: number, idleMs: number, stop: AbortSignal | undefined,
- statfs: Statfs): Promise<string> {
- if (!servedFrom(base, file.url)) throw new Refused('not a Harness file address')
+/** Whether signed [link] names the same object as public [url]: `/<one bucket>/<the key url names under base>`. Without the
+ * bucket's name, this binds a link to the one file Harness stored for the message, in a single bucket, on the signed-link
+ * host (review 1 of #2460); `SUPER_TAB_FILES_BASE` may also name the bucket itself. */
+export function sameObject(base: string, url: string, link: string): boolean {
+ try {
+ const root = new URL(base), stored = new URL(url), signed = new URL(link)
+ const prefix = root.pathname.endsWith('/') ? root.pathname : `${root.pathname}/`
+ if (stored.origin !== root.origin || !stored.pathname.startsWith(prefix)) return false
+ const key = decodeURIComponent(stored.pathname.slice(prefix.length))
+ const [blank, bucket, ...rest] = signed.pathname.split('/')
+ return blank === '' && !!bucket && !!key && rest.map(decodeURIComponent).join('/') === key
+ } catch { return false }
+}
+
+/** Storage's answer to a signed link that is over, or a file the server could not sign: the next try asks for a fresh one
+ * (attachedFiles). */
+class Expired extends Error {}
+
+async function download(file: MessageAttachment, base: string, signedBase: string, fetcher: typeof fetch, ms: number, idleMs: number,
+ stop: AbortSignal | undefined, statfs: Statfs): Promise<string> {
+ // An unlinked file is never opened by its public address: once the bucket is private that is a dead page.
+ if (!file.link && file.unlinked) throw new Expired('not signed just now')
+ const address = file.link ?? file.url
+ if (!address) throw new Refused('no longer available')
+ if (!servedFrom(base, address, signedBase) || (file.link && !sameObject(base, file.url, file.link))) throw new Refused('not a Harness file address')
if (file.size > MAX_ATTACHMENT_BYTES) throw new Refused(`over the ${megabytes(MAX_ATTACHMENT_BYTES)} limit`)
// Another try would find the same disk: refused, not tried again.
if (!await roomFor(file.size, statfs)) throw new Refused('not enough free disk space')
@@ -82,9 +123,11 @@ async function download(file: MessageAttachment, base: string, fetcher: typeof f
moved()
try {
// A redirect could leave the public base, so it is a failure, not followed.
- const response = await fetcher(file.url, { redirect: 'error', signal: AbortSignal.any([idle.signal, AbortSignal.timeout(ms), ...stop ? [stop] : []]) })
+ const response = await fetcher(address, { redirect: 'error', signal: AbortSignal.any([idle.signal, AbortSignal.timeout(ms), ...stop ? [stop] : []]) })
if (!response.ok || !response.body) {
await response.body?.cancel().catch(() => {})
+ // A signed link that is over, or whose file is no longer readable through it: a fresh link may still open it.
+ if ((response.status === 400 || response.status === 403) && file.link) throw new Expired(`HTTP ${response.status}`)
const Failure = response.status >= 400 && response.status < 500 && ![408, 429].includes(response.status) ? Refused : Error
throw new Failure(`HTTP ${response.status}`)
}
@@ -116,9 +159,11 @@ async function download(file: MessageAttachment, base: string, fetcher: typeof f
/** The prompt's list of a message's files, downloaded into this computer's paste-drops folder (kept 24 h), so an
* agent on any machine can read them. Each file is streamed to disk, and gets up to three tries within one time budget for
- * the message, which grows with its bytes; the host's single poll loop waits on it. A file that could not be downloaded is named by its URL instead, and
- * `complete` is false, so the host tries again before it next delivers the same message. `paths` are the files written,
- * which a Buddy's guard may list when its own person sent them (lib/buddyGuard.ts). */
+ * the message, which grows with its bytes; the host's single poll loop waits on it. A try whose link is over or about to be
+ * starts with a fresh one from [relink]. A file that could not be downloaded is named by its name and size alone (a signed
+ * link carries a credential, and is never written into a prompt), and `complete` is false, so the host tries again, with a
+ * fresh claim and fresh links, before it next delivers the same message. `paths` are the files written, which a Buddy's
+ * guard may list when its own person sent them (lib/buddyGuard.ts). */
/** A file being downloaded, by its address and size: a second delivery of the same file waits on the first's download
* rather than start its own (superTabs/host.ts keeps one such map). [wanting]: the deliveries still waiting for it; the last
* to leave stops it. */
@@ -126,28 +171,39 @@ export type FilesInFlight = Map<string, SharedDownload>
interface SharedDownload { result: Promise<{ line: string; path: string | null }>; wanting: number; stop: AbortController }
export async function attachedFiles(message: Record<string, unknown>,
- options: { base?: string; fetch?: typeof fetch; budgetMs?: number; idleMs?: number; now?: () => number
- inFlight?: FilesInFlight; signal?: AbortSignal; statfs?: Statfs } = {}):
+ options: { base?: string; signedBase?: string; fetch?: typeof fetch; budgetMs?: number; idleMs?: number; now?: () => number
+ inFlight?: FilesInFlight; signal?: AbortSignal; statfs?: Statfs; relink?: Relink } = {}):
Promise<{ text: string; complete: boolean; paths: string[] }> {
const files = attachmentsOf(message)
if (!files.length) return { text: '', complete: true, paths: [] }
- const base = options.base ?? env.SUPER_TAB_ATTACHMENTS_PUBLIC_BASE
+ const base = options.base ?? env.SUPER_TAB_ATTACHMENTS_PUBLIC_BASE, signedBase = options.signedBase ?? env.SUPER_TAB_FILES_BASE
const now = options.now ?? Date.now, deadline = now() + (options.budgetMs ?? budgetMs(files))
// The sender's name, quoted so it reads as a name and is never taken for the path, which is the shell-safe one.
const labelOf = (file: MessageAttachment) => `sent as ${JSON.stringify(file.name)}, ${formatSize(file.size)}`
- const failed = (file: MessageAttachment, why: string) => ({ path: null, line: servedFrom(base, file.url)
- ? `- Could not download ${file.url} (${labelOf(file)}): ${why}` : `- Could not download a file (${labelOf(file)}): ${why}` })
- const one = async (file: MessageAttachment, signal: AbortSignal | undefined) => {
- const label = labelOf(file)
- let failure: unknown
+ const failed = (file: MessageAttachment, why: string) => ({ path: null, line: `- Could not download a file (${labelOf(file)}): ${why}` })
+ const one = async (sent: MessageAttachment, signal: AbortSignal | undefined) => {
+ const label = labelOf(sent)
+ let file = sent, failure: unknown, stale = false
for (let attempt = 0; attempt < 3 && deadline - now() > 0 && !signal?.aborted; attempt++) {
try {
- const path = await download(file, base, options.fetch ?? fetch, Math.min(downloadMs(file.size), deadline - now()), options.idleMs ?? IDLE_MS,
- signal, options.statfs ?? fsStatfs)
+ if (options.relink && (stale || file.unlinked || (file.expiresAt !== undefined && file.expiresAt - now() < RELINK_MARGIN_MS))) {
+ try { file = await options.relink(file) } catch (error) {
+ // Refused (404) or forbidden (403): its reader may no longer read the message, or the file is gone.
+ const status = error && typeof error === 'object' && 'status' in error ? error.status : null
+ throw new Refused(status === 404 || status === 403 ? 'no longer available' : 'could not get a fresh link')
+ }
+ stale = false
+ }
+ const path = await download(file, base, signedBase, options.fetch ?? fetch, Math.min(downloadMs(file.size), deadline - now()),
+ options.idleMs ?? IDLE_MS, signal, options.statfs ?? fsStatfs)
return { line: `- ${path} (${label})`, path }
} catch (error) {
failure = error
if (error instanceof Refused) break
+ if (error instanceof Expired) {
+ if (!options.relink) { failure = file.unlinked ? new Error('could not get a link just now') : error; break }
+ stale = true
+ }
}
}
return failed(file, signal?.aborted ? 'stopped' : failure instanceof Error ? failure.message : 'out of time')
@@ -155,7 +211,8 @@ export async function attachedFiles(message: Record<string, unknown>,
const lines = await Promise.all(files.map(async file => {
const inFlight = options.inFlight
if (!inFlight) return one(file, options.signal)
- const key = `${file.size} ${file.url}`
+ // By the file, not its link: each read signs a link of its own.
+ const key = `${file.size} ${file.id || file.url.split('?')[0]}`
let shared = inFlight.get(key)
const joined = !!shared
if (!shared) {
diff --git a/cli/src/superTabs/host.spec.ts b/cli/src/superTabs/host.spec.ts
index 29a31fee4..23fed6492 100644
--- a/cli/src/superTabs/host.spec.ts
+++ b/cli/src/superTabs/host.spec.ts
@@ -16,6 +16,7 @@ vi.mock('../lib/buddyGuard.js', async original => {
})
import type { BackendNotice, CoreApi, TurnDelivery } from '../core/api.js'
import { fakeCore } from '../testing/fakeCore.js'
+import type { attachedFiles as attachedFilesType } from './attachments.js'
import { backend, BUDDY_SESSION_GONE_MS, BUDDY_SESSION_IDLE_MS, BUDDY_SESSION_SLEEP_GAP_MS, buddyChatIdOf, swarmRoleFor, hostJournalFile, SuperTabHost, type SuperTabHostOptions } from './host.js'
import { TerminalHost } from './terminal.js'
import { hostForm, swarmReply } from './fixtures/read/index.js'
@@ -1260,6 +1261,40 @@ describe('Super Tab attachments in delivered input', () => {
expect(vi.mocked(t.core.turns.deliver).mock.calls[1]?.[1]).toContain('/drops/1-screen.png')
expect(attachedFiles).toHaveBeenCalledTimes(1)
})
+ // Chat files open through short-lived signed links (backend superTabs/fileLinks.ts): a retry whose link is over gets a fresh one,
+ // read as the agent reads the message now, and nothing once it may no longer read it.
+ it('gives the download a fresh link, read as its agent reads the message, and nothing once it may not', async () => {
+ const file = { id: 'a1', name: 'a.log', mime: 'text/plain', size: 1, url: 'https://cdn.example.test/super-tabs/tab/k/a.log',
+ link: 'https://storage.googleapis.com/b/super-tabs/tab/k/a.log?X-Goog-Signature=old', expiresAt: 1 }
+ const t = setup({ text: 'Logs.', attachments: [file] })
+ const answer = vi.mocked(t.call).getMockImplementation()!
+ const asked: string[] = []
+ let gone = false
+ vi.mocked(t.call).mockImplementation(async (method, path, body) => {
+ if (!path.includes('/attachments/')) return answer(method, path, body)
+ asked.push(`${method} ${path}`)
+ return gone ? { status: 404, body: { success: false, error: { code: 'FILE_UNAVAILABLE', message: 'This file is no longer available.' } } }
+ : { status: 200, body: { success: true, data: { attachment: { ...file, link: 'https://storage.googleapis.com/b/super-tabs/tab/k/a.log?X-Goog-Signature=new', expiresAt: 2 } } } }
+ })
+ const seen: Array<{ fresh?: unknown; refused?: unknown; outside?: unknown }> = []
+ const attachedFiles = vi.fn(async (_message: Record<string, unknown>, options: Parameters<typeof attachedFilesType>[1] = {}) => {
+ const fresh = await options.relink!(file)
+ gone = true
+ const refused = await options.relink!(file).catch((error: unknown) => error)
+ const outside = await options.relink!({ ...file, id: '../other' }).catch((error: unknown) => error)
+ seen.push({ fresh, refused, outside })
+ return { text: 'Attached files:\n- /drops/a.log (a.log, 1 B)', complete: true, paths: ['/drops/a.log'] }
+ })
+ const host = new SuperTabHost(t.core, 'machine-1', { log: vi.fn(), attachedFiles }); hosts.push(host)
+ await host.tick()
+ const path = 'GET /api/super-tabs/tab/conversations/lead/messages/message/attachments/a1?asAgentId=agent'
+ expect(asked).toEqual([path, path])
+ expect(seen[0]!.fresh).toEqual({ ...file, link: 'https://storage.googleapis.com/b/super-tabs/tab/k/a.log?X-Goog-Signature=new', expiresAt: 2 })
+ expect(seen[0]!.refused).toMatchObject({ status: 404 })
+ expect(seen[0]!.outside).toMatchObject({ status: 404 })
+ // The link never reaches the prompt.
+ expect(String(vi.mocked(t.core.turns.deliver).mock.calls[0]?.[1])).not.toContain('X-Goog-Signature')
+ })
it('downloads a failed file again before the next delivery of the held input', async () => {
const t = setup({ text: 'Logs.', attachments: [{ id: 'a1', name: 'a.log', mime: 'text/plain', size: 1, url: 'https://cdn.example.test/a.log' }] })
const attachedFiles = vi.fn()
diff --git a/cli/src/superTabs/host.ts b/cli/src/superTabs/host.ts
index fd2b6e710..681545392 100644
--- a/cli/src/superTabs/host.ts
+++ b/cli/src/superTabs/host.ts
@@ -4,7 +4,7 @@ import { SwarmGrids } from './swarmGrids.js'
import { retainContributions } from './retention.js'
import { configurationObservation } from './configuration.js'
import { NativeRecorder, nativeJournalFile } from './native.js'
-import { attachedFiles, attachmentsOf, formatSize, type FilesInFlight } from './attachments.js'
+import { attachedFiles, attachmentsOf, formatSize, type FilesInFlight, type Relink } from './attachments.js'
import { createHash, randomUUID } from 'node:crypto'
import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, renameSync, writeFileSync } from 'node:fs'
import { readdir, readFile } from 'node:fs/promises'
@@ -1618,7 +1618,7 @@ export class SuperTabHost {
// Downloaded before the lease: a large file must not use up the 2.5 s the core gives a write.
let files = this.files.get(deliveryId)
if (files === undefined) {
- const downloaded = await this.downloaded(deliveryId, message, `list:${target.tabId}:${target.agent.id}`)
+ const downloaded = await this.downloaded(deliveryId, message, `list:${target.tabId}:${target.agent.id}`, this.relink(target, message))
files = { text: downloaded.text, paths: downloaded.paths }
if (downloaded.complete) this.files.set(deliveryId, files)
}
@@ -1706,11 +1706,11 @@ export class SuperTabHost {
/** A delivery's files, downloaded off the poll loop. A pass waits only on the downloads it starts, all together for at most
* downloadWaitMs (passWait); one still running is FilesDownloading, checked without a wait, and starts a pass of its own once
* in. A download whose tries all failed answers its "Could not download" lines, delivered as before. */
- private async downloaded(deliveryId: string, message: Json, listKey: string): Promise<Awaited<ReturnType<typeof attachedFiles>>> {
+ private async downloaded(deliveryId: string, message: Json, listKey: string, relink: Relink): Promise<Awaited<ReturnType<typeof attachedFiles>>> {
let download = this.downloads.get(deliveryId)
if (!download) {
const stop = new AbortController()
- const pending = this.attachedFiles(message, { inFlight: this.inFlight, signal: stop.signal })
+ const pending = this.attachedFiles(message, { inFlight: this.inFlight, signal: stop.signal, relink })
.then(done => ({ done }), (error: unknown) => ({ error }))
const started: Download = download = { pending, stop, said: false }
this.downloads.set(deliveryId, started)
@@ -1737,6 +1737,21 @@ export class SuperTabHost {
return download.result.done
}
+ /** A fresh link to one of [message]'s files, read as [target]'s agent reads the message now (backend superTabs/fileLinks.ts):
+ * refused once it may no longer read it, so a removed agent's retry gets nothing. */
+ private relink(target: HostAgent, message: Json): Relink {
+ return async file => {
+ const ids = [target.tabId, message.conversationId, message.id, file.id, target.agent.id]
+ if (!ids.every(identifier)) throw new SuperTabHostError('FILE_UNAVAILABLE', 'This file is no longer available.', 404)
+ const [tabId, conversationId, messageId, attachmentId, agentId] = ids as string[]
+ const data = object(await backend(this.core, 'GET',
+ `/api/super-tabs/${tabId}/conversations/${conversationId}/messages/${messageId}/attachments/${attachmentId}?asAgentId=${agentId}`))
+ const [fresh] = attachmentsOf({ attachments: [data.attachment] })
+ if (!fresh?.link || fresh.id !== file.id) throw new SuperTabHostError('FILE_UNAVAILABLE', 'This file is no longer available.', 404)
+ return fresh
+ }
+ }
+
/** A delivery's download it no longer waits for: stopped, unless another delivery shares its file (attachments.ts). */
private dropDownload(deliveryId: string): void {
const download = this.downloads.get(deliveryId)
diff --git a/desktop/lib/super_tabs/attachment_chips.dart b/desktop/lib/super_tabs/attachment_chips.dart
index cbe46a508..be1a38616 100644
--- a/desktop/lib/super_tabs/attachment_chips.dart
+++ b/desktop/lib/super_tabs/attachment_chips.dart
@@ -14,72 +14,110 @@ import 'attachment_upload.dart';
import 'attachment_text_viewer.dart'
if (dart.library.js_interop) '../web/parts/attachment_text_viewer.dart';
import 'attachment_viewer.dart';
+import 'file_links.dart';
import 'model.dart';
-/// The files a message carries, as Slack shows them: each picture on the
-/// attachments host in place (`attachment_image.dart`, on a computer), every
-/// other file one chip. A chip only names its file, and a click opens it — a text file in a
-/// viewer that fetches it then, anything else in the browser.
+/// The files a message carries, as Slack shows them: each picture on
+/// Harness's file storage in place (`attachment_image.dart`, on a computer),
+/// every other file one chip. A chip only names its file, and a click opens it
+/// — a text file in a viewer that fetches it then, anything else in the
+/// browser. Links last minutes (backend fileLinks.ts): one about to end is
+/// asked for again, of message [messageId] in [conversationId], before it is
+/// opened.
class SuperTabAttachments extends StatelessWidget {
- const SuperTabAttachments(this.attachments, {super.key});
+ const SuperTabAttachments(
+ this.attachments, {
+ super.key,
+ this.conversationId,
+ this.messageId,
+ });
final List<SuperTabAttachment> attachments;
+ final String? conversationId, messageId;
@override
Widget build(BuildContext context) {
- final pictures = <(SuperTabAttachment, Uri)>[];
+ final fresh = SuperTabFiles.of(context);
+ SuperTabFileRef ref(SuperTabAttachment file) => SuperTabFileRef(
+ file,
+ conversationId: conversationId,
+ messageId: messageId,
+ fresh: fresh,
+ );
+ final pictures = <SuperTabAttachment>[];
final chips = <SuperTabAttachment>[];
for (final file in attachments) {
// A browser's fetch follows a redirect off the attachments host, so
// there a picture stays a chip that opens in a new tab. A picture over
// the form's limit is a chip too: every reader would fetch and decode
- // it whole to draw it in place.
- final url =
+ // it whole to draw it in place. So is one whose link this app refuses.
+ final inPlace =
file.isImage &&
- file.size <= kSuperTabFormAttachmentMaxBytes &&
- !kIsWeb
- ? attachmentLink(file.url)
- : null;
- url == null ? chips.add(file) : pictures.add((file, url));
+ file.size <= kSuperTabFormAttachmentMaxBytes &&
+ !kIsWeb &&
+ !ref(file).refused;
+ (inPlace ? pictures : chips).add(file);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
spacing: grid.AppDesktop.controlGap,
children: [
- for (final (file, url) in pictures)
- SuperTabInlineImage(file: file, url: url),
+ for (final file in pictures)
+ SuperTabInlineImage(
+ key: ValueKey('super-attachment-picture:${file.id}'),
+ file: ref(file),
+ ),
if (chips.isNotEmpty)
Wrap(
spacing: grid.AppDesktop.menuInset,
runSpacing: grid.AppDesktop.menuInset,
- children: [for (final file in chips) _AttachmentChip(file: file)],
+ children: [
+ for (final file in chips)
+ _AttachmentChip(key: ValueKey(file.id), file: ref(file)),
+ ],
),
],
);
}
}
-class _AttachmentChip extends StatelessWidget {
- const _AttachmentChip({required this.file});
+class _AttachmentChip extends StatefulWidget {
+ const _AttachmentChip({super.key, required this.file});
- final SuperTabAttachment file;
+ final SuperTabFileRef file;
- Future<void> _open(BuildContext context) async {
- final url = attachmentLink(file.url);
- if (url == null) {
- ScaffoldMessenger.maybeOf(context)?.showSnackBar(
- const SnackBar(
- content: Text(
- 'This file isn’t on Harness’s file storage, so it wasn’t opened.',
- ),
- ),
- );
+ @override
+ State<_AttachmentChip> createState() => _AttachmentChipState();
+}
+
+class _AttachmentChipState extends State<_AttachmentChip> {
+ /// A fresh link is being asked for: another click waits for it.
+ bool _opening = false;
+
+ void _say(String text) =>
+ ScaffoldMessenger.maybeOf(context)
+ ?.showSnackBar(SnackBar(content: Text(text)));
+
+ Future<void> _open() async {
+ final ref = widget.file, file = ref.file;
+ if (ref.refused) {
+ _say('This file isn’t on Harness’s file storage, so it wasn’t opened.');
return;
}
+ if (_opening) return;
+ setState(() => _opening = true);
+ final Uri? found;
+ try {
+ found = await superTabOpenableLink(context, ref, onProblem: _say);
+ } finally {
+ if (mounted) setState(() => _opening = false);
+ }
+ final url = found;
+ if (url == null || !mounted) return;
if (opensAsText(file)) {
await showAppDialog<void>(
context: context,
- builder: (_) => AttachmentTextViewer(file: file, url: url),
+ builder: (_) => AttachmentTextViewer(file: file, url: url, ref: ref),
);
} else {
await openInBrowser(url);
@@ -88,6 +126,7 @@ class _AttachmentChip extends StatelessWidget {
@override
Widget build(BuildContext context) {
+ final file = widget.file.file;
final size = formatDownloadSize(file.size);
return Material(
color: DesktopChrome.foreground.withValues(alpha: .055),
@@ -95,7 +134,7 @@ class _AttachmentChip extends StatelessWidget {
child: InkWell(
key: ValueKey('super-attachment:${file.id}'),
borderRadius: BorderRadius.circular(20),
- onTap: () => _open(context),
+ onTap: _opening ? null : _open,
child: Container(
constraints: const BoxConstraints(maxWidth: 280),
padding: const EdgeInsets.symmetric(horizontal: 10),
diff --git a/desktop/lib/super_tabs/attachment_image.dart b/desktop/lib/super_tabs/attachment_image.dart
index ccae889ba..e14465087 100644
--- a/desktop/lib/super_tabs/attachment_image.dart
+++ b/desktop/lib/super_tabs/attachment_image.dart
@@ -4,23 +4,110 @@ import '../shared/theme/app_icons.dart';
import '../shared/theme/app_theme.dart' as grid;
import '../shared/widgets/app_dialog.dart';
import '../widgets/desktop_chrome.dart';
+import 'attachment_upload.dart';
import 'attachment_viewer.dart';
+import 'file_links.dart';
import 'model.dart';
/// A picture in a message, as Slack shows one: at most [maxSide] each way
-/// with its shape kept, and a click opens it full screen. [url] is the
-/// message's own link on the attachments host, which only members are given.
-class SuperTabInlineImage extends StatelessWidget {
- const SuperTabInlineImage({super.key, required this.file, required this.url});
+/// with its shape kept, and a click opens it full screen. Drawn from the
+/// message's own short-lived link while it lasts, else from a fresh one
+/// (backend fileLinks.ts); a signed link that fails to load is asked for once
+/// more, and a second failure says the picture can't be shown.
+class SuperTabInlineImage extends StatefulWidget {
+ const SuperTabInlineImage({super.key, required this.file});
static const maxSide = 360.0;
- final SuperTabAttachment file;
- final Uri url;
+ final SuperTabFileRef file;
+
+ @override
+ State<SuperTabInlineImage> createState() => _SuperTabInlineImageState();
+}
+
+class _SuperTabInlineImageState extends State<SuperTabInlineImage> {
+ late SuperTabFileRef _ref = widget.file;
+
+ /// The link drawn from; null while one is asked for, or when none came.
+ Uri? _url;
+ bool _asking = false, _retried = false, _failed = false;
+
+ @override
+ void initState() {
+ super.initState();
+ _start();
+ }
+
+ @override
+ void didUpdateWidget(SuperTabInlineImage old) {
+ super.didUpdateWidget(old);
+ final was = old.file.file, now = widget.file.file;
+ if (was.id == now.id &&
+ was.url == now.url &&
+ old.file.fresh == widget.file.fresh) {
+ return;
+ }
+ _ref = widget.file;
+ _retried = _failed = false;
+ _start();
+ }
+
+ void _start() {
+ _url = _ref.current;
+ // Asked after this frame: the view's fresh-link source is read then.
+ if (_url == null) {
+ WidgetsBinding.instance.addPostFrameCallback((_) => _ask(stale: false));
+ }
+ }
+
+ Future<void> _ask({required bool stale}) async {
+ if (!mounted || _asking) return;
+ final ref = _ref;
+ setState(() => _asking = true);
+ final url = await superTabOpenableLink(context, ref, stale: stale);
+ if (!mounted) return;
+ // Another file, Swarm or account now: this answer is not for what is
+ // shown. The ask is over all the same, and the one shown now is asked for.
+ if (!identical(ref, _ref)) {
+ _asking = false;
+ if (_url == null && !_failed) _ask(stale: false);
+ return;
+ }
+ setState(() {
+ _asking = false;
+ _url = url;
+ _failed = url == null;
+ });
+ }
+
+ /// The picture did not load. A short-lived link may have ended under it:
+ /// asked for once more. An older server's permanent link is not.
+ void _broke() {
+ final signed = _url?.host == kSuperTabFilesHost;
+ if (_retried || !signed || _failed) {
+ if (!_failed) {
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ if (mounted) setState(() => _failed = true);
+ });
+ }
+ return;
+ }
+ _retried = true;
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ if (!mounted) return;
+ setState(() => _url = null);
+ _ask(stale: true);
+ });
+ }
@override
Widget build(BuildContext context) {
grid.AppTheme.watch(context);
+ final file = _ref.file;
+ final url = _url;
+ // No link at all: nothing to open either. A link that failed to draw
+ // stays a click away, as the full-screen viewer may still read it.
+ if (url == null) return _failed ? _broken(file) : _loading();
final radius = BorderRadius.circular(DesktopChrome.controlRadius);
return Semantics(
button: true,
@@ -35,12 +122,13 @@ class SuperTabInlineImage extends StatelessWidget {
mouseCursor: SystemMouseCursors.click,
onTap: () => showAppDialog<void>(
context: context,
- builder: (_) => SuperTabImageViewer(file: file, url: url),
+ builder: (_) =>
+ SuperTabImageViewer(file: file, url: url, ref: _ref),
),
child: ConstrainedBox(
constraints: const BoxConstraints(
- maxWidth: maxSide,
- maxHeight: maxSide,
+ maxWidth: SuperTabInlineImage.maxSide,
+ maxHeight: SuperTabInlineImage.maxSide,
),
child: ClipRRect(
borderRadius: radius,
@@ -50,7 +138,11 @@ class SuperTabInlineImage extends StatelessWidget {
fit: BoxFit.contain,
frameBuilder: (_, child, frame, synchronous) =>
frame == null && !synchronous ? _loading() : child,
- errorBuilder: (_, _, _) => _broken(file),
+ errorBuilder: (_, _, _) {
+ if (_failed) return _broken(file);
+ _broke();
+ return _loading();
+ },
),
),
),
@@ -62,8 +154,8 @@ class SuperTabInlineImage extends StatelessWidget {
/// Still, not pulsing: a picture's shape is unknown until it arrives.
static Widget _loading() => SizedBox(
- width: maxSide * 2 / 3,
- height: maxSide / 2,
+ width: SuperTabInlineImage.maxSide * 2 / 3,
+ height: SuperTabInlineImage.maxSide / 2,
child: ColoredBox(color: DesktopChrome.field),
);
@@ -91,12 +183,19 @@ class SuperTabInlineImage extends StatelessWidget {
}
/// A message's picture over the whole window, fetched when it opens, with
-/// Download, Open in browser and Close.
+/// Download, Open in browser and Close. [ref] gives those two a fresh link
+/// once [url] is about to end.
class SuperTabImageViewer extends StatelessWidget {
- const SuperTabImageViewer({super.key, required this.file, required this.url});
+ const SuperTabImageViewer({
+ super.key,
+ required this.file,
+ required this.url,
+ this.ref,
+ });
final SuperTabAttachment file;
final Uri url;
+ final SuperTabFileRef? ref;
@override
Widget build(BuildContext context) => Dialog.fullscreen(
@@ -133,7 +232,7 @@ class SuperTabImageViewer extends StatelessWidget {
),
),
const SizedBox(height: grid.AppDesktop.groupGap),
- AttachmentViewerActions(file: file, url: url),
+ AttachmentViewerActions(file: file, url: url, ref: ref),
],
),
),
diff --git a/desktop/lib/super_tabs/attachment_text_viewer.dart b/desktop/lib/super_tabs/attachment_text_viewer.dart
index b56664178..3781a1cbd 100644
--- a/desktop/lib/super_tabs/attachment_text_viewer.dart
+++ b/desktop/lib/super_tabs/attachment_text_viewer.dart
@@ -17,19 +17,23 @@ import '../shared/widgets/empty_state.dart';
import '../shared/widgets/skeleton.dart';
import '../widgets/desktop_chrome.dart';
import 'attachment_viewer.dart';
+import 'file_links.dart';
import 'model.dart';
/// A text file from the chat, read only: Markdown rendered with its source a
/// click away, anything else in Finder's editor. Fetched when this opens.
+/// [ref] gives Download and Open in browser a fresh link once [url] ends.
class AttachmentTextViewer extends StatefulWidget {
const AttachmentTextViewer({
super.key,
required this.file,
required this.url,
+ this.ref,
});
final SuperTabAttachment file;
final Uri url;
+ final SuperTabFileRef? ref;
@override
State<AttachmentTextViewer> createState() => _AttachmentTextViewerState();
@@ -102,7 +106,11 @@ class _AttachmentTextViewerState extends State<AttachmentTextViewer> {
const SizedBox(height: 12),
Expanded(child: _body(read)),
const SizedBox(height: 12),
- AttachmentViewerActions(file: widget.file, url: widget.url),
+ AttachmentViewerActions(
+ file: widget.file,
+ url: widget.url,
+ ref: widget.ref,
+ ),
],
),
),
diff --git a/desktop/lib/super_tabs/attachment_upload.dart b/desktop/lib/super_tabs/attachment_upload.dart
index 360acbbd0..543453f29 100644
--- a/desktop/lib/super_tabs/attachment_upload.dart
+++ b/desktop/lib/super_tabs/attachment_upload.dart
@@ -57,13 +57,29 @@ const kSuperTabAttachmentHost = String.fromEnvironment(
defaultValue: 'cdn.harness.autonomous.ai',
);
-/// [url] as a link to open, or null unless it is https on the attachments host:
-/// a message's url is whatever its sender's backend row says.
-Uri? attachmentLink(String url) {
+/// Where the backend's short-lived signed links to chat files point (backend
+/// superTabs/fileLinks.ts): Cloud Storage's own host.
+const kSuperTabFilesHost = String.fromEnvironment(
+ 'SUPER_TAB_FILES_HOST',
+ defaultValue: 'storage.googleapis.com',
+);
+
+/// A message file's stored address as a link to open, or null unless it is
+/// https, with no sign-in in it, on the attachments host: a message's url is
+/// whatever its sender's backend row says. An empty one is no address.
+Uri? attachmentLink(String url) => _https(url, kSuperTabAttachmentHost);
+
+/// A file's short-lived signed link (its `link`) as a link to open, or null
+/// unless it is https, with no sign-in in it, on [kSuperTabFilesHost].
+Uri? attachmentSignedLink(String? link) =>
+ link == null ? null : _https(link, kSuperTabFilesHost);
+
+Uri? _https(String url, String host) {
final link = Uri.tryParse(url);
return link != null &&
link.scheme == 'https' &&
- link.host == kSuperTabAttachmentHost
+ link.userInfo.isEmpty &&
+ link.host == host
? link
: null;
}
diff --git a/desktop/lib/super_tabs/attachment_viewer.dart b/desktop/lib/super_tabs/attachment_viewer.dart
index d7bc53ee9..cff347878 100644
--- a/desktop/lib/super_tabs/attachment_viewer.dart
+++ b/desktop/lib/super_tabs/attachment_viewer.dart
@@ -11,6 +11,7 @@ import '../core/open_in_browser.dart';
import '../finder/finder_file_kind.dart';
import '../widgets/desktop_chrome.dart';
import 'attachment_upload.dart' show kSuperTabFormAttachmentMaxBytes;
+import 'file_links.dart';
import 'model.dart';
/// The largest file read into the chat's text viewer; a bigger one opens in
@@ -94,16 +95,20 @@ Future<bool> downloadAttachment(SuperTabAttachment file, Uri url) async {
return true;
}
-/// A viewer's buttons: save the file, open it in the browser, or close.
+/// A viewer's buttons: save the file, open it in the browser, or close. A
+/// viewer may stay open longer than [url] lasts: with [ref], each button asks
+/// for a link that opens now first.
class AttachmentViewerActions extends StatefulWidget {
const AttachmentViewerActions({
super.key,
required this.file,
required this.url,
+ this.ref,
});
final SuperTabAttachment file;
final Uri url;
+ final SuperTabFileRef? ref;
@override
State<AttachmentViewerActions> createState() =>
@@ -114,6 +119,18 @@ class _AttachmentViewerActionsState extends State<AttachmentViewerActions> {
bool _saving = false;
String? _outcome;
+ /// A link that opens now, or null with the reason shown.
+ Future<Uri?> _link() async {
+ final ref = widget.ref;
+ if (ref == null) return widget.url;
+ try {
+ return await ref.link();
+ } on SuperTabFileUnavailable catch (problem) {
+ if (mounted) setState(() => _outcome = problem.message);
+ return null;
+ }
+ }
+
Future<void> _download() async {
setState(() {
_saving = true;
@@ -121,7 +138,12 @@ class _AttachmentViewerActionsState extends State<AttachmentViewerActions> {
});
String? outcome;
try {
- if (await downloadAttachment(widget.file, widget.url)) outcome = 'Saved';
+ final url = await _link();
+ if (url == null || !mounted) {
+ if (mounted) setState(() => _saving = false);
+ return;
+ }
+ if (await downloadAttachment(widget.file, url)) outcome = 'Saved';
} catch (_) {
outcome = 'Couldn’t save this file.';
}
@@ -132,6 +154,11 @@ class _AttachmentViewerActionsState extends State<AttachmentViewerActions> {
});
}
+ Future<void> _openInBrowser() async {
+ final url = await _link();
+ if (url != null && mounted) await openInBrowser(url);
+ }
+
@override
Widget build(BuildContext context) => Row(
children: [
@@ -144,7 +171,7 @@ class _AttachmentViewerActionsState extends State<AttachmentViewerActions> {
child: const Text('Download'),
),
TextButton(
- onPressed: () => openInBrowser(widget.url),
+ onPressed: _openInBrowser,
child: const Text('Open in browser'),
),
TextButton(
diff --git a/desktop/lib/super_tabs/backend_transport.dart b/desktop/lib/super_tabs/backend_transport.dart
index cb6221459..fc49c8f91 100644
--- a/desktop/lib/super_tabs/backend_transport.dart
+++ b/desktop/lib/super_tabs/backend_transport.dart
@@ -19,7 +19,8 @@ final _swarmRoute = RegExp(
'|/list|/invitations'
'|/$_tab/(?:parts|changes|commands|mentions|decisions|leaderboard|sharing'
'|routing-policy|groups|groups/$_id|join|join/preview|native'
- '|conversations/$_id/(?:messages|catch-up|read|threads/$_id/read)'
+ '|conversations/$_id/(?:messages|catch-up|read|threads/$_id/read'
+ '|messages/$_id/attachments/$_id)'
'|projects/$_id/(?:github|github/approve'
'|instructions(?:/revisions(?:/[0-9]+)?)?)'
'|route-decisions/$_id|machines/$_id/removal)'
diff --git a/desktop/lib/super_tabs/controller.dart b/desktop/lib/super_tabs/controller.dart
index c5e362ccb..a2155836b 100644
--- a/desktop/lib/super_tabs/controller.dart
+++ b/desktop/lib/super_tabs/controller.dart
@@ -1921,6 +1921,25 @@ class SuperTabController extends ChangeNotifier {
int readMark(String id) =>
max(_readAt[id] ?? 0, snapshot?.readMarks[id] ?? 0);
+ /// A fresh short-lived link to one file of one message (backend
+ /// fileLinks.ts), answered only while the reader may read that message.
+ Future<SuperTabAttachment> readFileLink(
+ String conversationId,
+ String messageId,
+ String attachmentId,
+ ) async => SuperTabAttachment.fromJson(
+ superTabMap(
+ superTabMap(
+ await request(
+ 'GET',
+ '$_path/conversations/${Uri.encodeComponent(conversationId)}'
+ '/messages/${Uri.encodeComponent(messageId)}'
+ '/attachments/${Uri.encodeComponent(attachmentId)}',
+ ),
+ )['attachment'],
+ ),
+ );
+
/// What the reader missed in [id] after [after] (backend catchUp.ts).
Future<SuperTabCatchUp> readCatchUp(
String id,
diff --git a/desktop/lib/super_tabs/file_links.dart b/desktop/lib/super_tabs/file_links.dart
new file mode 100644
index 000000000..af13eae98
--- /dev/null
+++ b/desktop/lib/super_tabs/file_links.dart
@@ -0,0 +1,164 @@
+import 'package:flutter/widgets.dart';
+
+import 'attachment_upload.dart';
+import 'controller.dart' show SuperTabFailure;
+import 'model.dart';
+
+/// Asks the backend for a fresh link to one file of one message (backend
+/// superTabs/fileLinks.ts): answered only while the reader may read that
+/// message.
+typedef SuperTabFreshFileLink = Future<SuperTabAttachment> Function(
+ String conversationId,
+ String messageId,
+ String attachmentId,
+);
+
+/// Said when the reader may no longer open a file: removed from the project
+/// or the Swarm, the Swarm deleted, or the message gone.
+const kSuperTabFileGone = 'This file is no longer available.';
+
+/// Said when a fresh link could not be had just now; a later click tries
+/// again.
+const kSuperTabFileRetry = 'Couldn’t open the file. Try again.';
+
+/// A link with less than this left is asked for again before it is opened, so
+/// it does not stop opening while the browser or a fetch starts.
+const kSuperTabLinkMargin = Duration(seconds: 60);
+
+/// The clock links are judged by; tests set it.
+@visibleForTesting
+DateTime Function() superTabFileClock = DateTime.now;
+
+/// The Swarm view's way to ask for fresh file links, for the messages under
+/// it. A new Swarm or account brings a new one, so an answer asked for under
+/// the old one is dropped ([superTabOpenableLink]).
+class SuperTabFiles extends InheritedWidget {
+ const SuperTabFiles({super.key, required this.fresh, required super.child});
+
+ final SuperTabFreshFileLink fresh;
+
+ /// The fresh-link source over [context], and a rebuild when it changes.
+ static SuperTabFreshFileLink? of(BuildContext context) =>
+ context.dependOnInheritedWidgetOfExactType<SuperTabFiles>()?.fresh;
+
+ /// The same without a rebuild, for a check after an answer arrives.
+ static SuperTabFreshFileLink? peek(BuildContext context) =>
+ context.getInheritedWidgetOfExactType<SuperTabFiles>()?.fresh;
+
+ @override
+ bool updateShouldNotify(SuperTabFiles oldWidget) => fresh != oldWidget.fresh;
+}
+
+/// Why a file was not opened: [gone] when the reader may no longer open it,
+/// else a failure another try may get past.
+class SuperTabFileUnavailable implements Exception {
+ const SuperTabFileUnavailable({required this.gone});
+ final bool gone;
+ String get message => gone ? kSuperTabFileGone : kSuperTabFileRetry;
+ @override
+ String toString() => message;
+}
+
+/// One file of one message, and the newest link to it this screen has: the
+/// message's own, then any fresh one asked for since.
+class SuperTabFileRef {
+ SuperTabFileRef(this.file, {this.conversationId, this.messageId, this.fresh})
+ : _latest = file;
+
+ final SuperTabAttachment file;
+ final String? conversationId, messageId;
+
+ /// Where fresh links come from; null where none can be asked for.
+ final SuperTabFreshFileLink? fresh;
+
+ SuperTabAttachment _latest;
+
+ /// The newest link at hand: its signed [SuperTabAttachment.link] while it
+ /// opens for more than [kSuperTabLinkMargin], else null. A file the server
+ /// could not sign this time ([SuperTabAttachment.unlinked]) has none: a
+ /// fresh one is asked for. Only from an older server, which signs nothing,
+ /// the stored address on the attachments CDN, as ever. A signed link is
+ /// never passed over for the stored address.
+ Uri? get current {
+ if (_latest.link case final signed?) {
+ final link = attachmentSignedLink(signed), ends = _latest.expiresAt;
+ if (link == null || ends == null) return null;
+ final left = ends - superTabFileClock().millisecondsSinceEpoch;
+ return left > kSuperTabLinkMargin.inMilliseconds ? link : null;
+ }
+ if (_latest.unlinked) return null;
+ return attachmentLink(_latest.url);
+ }
+
+ /// Whether the message names the file only by addresses this app refuses
+ /// to open: a signed link off [kSuperTabFilesHost] or not https, or, from
+ /// an older server, a stored address off the attachments host. A file not
+ /// signed this time, or with no address, is only unlinked for now.
+ bool get refused => file.link != null
+ ? attachmentSignedLink(file.link) == null
+ : !file.unlinked &&
+ file.url.isNotEmpty &&
+ attachmentLink(file.url) == null;
+
+ /// A link that opens now: [current], or a fresh one. [stale]: the one at
+ /// hand just failed to open, so a fresh one is asked for whatever its time.
+ /// Throws [SuperTabFileUnavailable].
+ Future<Uri> link({bool stale = false}) async {
+ if (!stale) {
+ if (current case final link?) return link;
+ }
+ final ask = fresh, conversation = conversationId, message = messageId;
+ if (ask == null || conversation == null || message == null) {
+ throw const SuperTabFileUnavailable(gone: false);
+ }
+ final SuperTabAttachment next;
+ try {
+ next = await ask(conversation, message, file.id);
+ } on SuperTabFailure catch (failure) {
+ throw SuperTabFileUnavailable(gone: _gone(failure));
+ } catch (_) {
+ throw const SuperTabFileUnavailable(gone: false);
+ }
+ final link = next.id == file.id && next.expiresAt != null
+ ? attachmentSignedLink(next.link)
+ : null;
+ // Not signed this time either: another try may sign it.
+ if (link == null) throw SuperTabFileUnavailable(gone: !next.unlinked);
+ _latest = next;
+ return link;
+ }
+
+ /// Gone: the reader may no longer read it. Signing that failed for a
+ /// reader who may (503 FILE_LINK_UNAVAILABLE) is worth another try.
+ static bool _gone(SuperTabFailure failure) =>
+ failure.code != 'FILE_LINK_UNAVAILABLE' &&
+ (const {401, 403, 404}.contains(failure.statusCode) ||
+ const {
+ 'FILE_UNAVAILABLE',
+ 'NOT_FOUND',
+ 'FORBIDDEN',
+ 'SIGNED_OUT',
+ 'ACCOUNT_CHANGED',
+ }.contains(failure.code));
+}
+
+/// [ref]'s link for a widget at [context]: null, with [onProblem] told why,
+/// when none could be had; null and silent when the widget has gone or the
+/// Swarm view over it changed while the link was asked for, so a late answer
+/// opens nothing and changes nothing.
+Future<Uri?> superTabOpenableLink(
+ BuildContext context,
+ SuperTabFileRef ref, {
+ bool stale = false,
+ void Function(String message)? onProblem,
+}) async {
+ final source = SuperTabFiles.peek(context);
+ bool still() => context.mounted && SuperTabFiles.peek(context) == source;
+ try {
+ final link = await ref.link(stale: stale);
+ return still() ? link : null;
+ } on SuperTabFileUnavailable catch (problem) {
+ if (still()) onProblem?.call(problem.message);
+ return null;
+ }
+}
diff --git a/desktop/lib/super_tabs/message_row.dart b/desktop/lib/super_tabs/message_row.dart
index 7b69ea65f..8a3c75fa0 100644
--- a/desktop/lib/super_tabs/message_row.dart
+++ b/desktop/lib/super_tabs/message_row.dart
@@ -508,6 +508,8 @@ class _SuperTabMessageRowState extends State<SuperTabMessageRow> {
),
child: SuperTabAttachments(
message.attachments,
+ conversationId: message.conversationId,
+ messageId: message.id,
),
),
),
diff --git a/desktop/lib/super_tabs/model.dart b/desktop/lib/super_tabs/model.dart
index c8c415be3..75209a035 100644
--- a/desktop/lib/super_tabs/model.dart
+++ b/desktop/lib/super_tabs/model.dart
@@ -952,10 +952,33 @@ class SuperTabAttachment {
name = _text(row, 'name'),
mime = _text(row, 'mime'),
size = (row['size'] as num).toInt(),
- url = _text(row, 'url');
+ url = _text(row, 'url'),
+ // Read defensively: a field of the wrong type reads as absent, never as
+ // a failed row.
+ link = row['link'] is String ? row['link'] as String : null,
+ unlinked = row['unlinked'] == true,
+ expiresAt = row['expiresAt'] is num
+ ? (row['expiresAt'] as num).toInt()
+ : null;
+
+ /// [url] is the address the file was stored with, on the attachments CDN:
+ /// what an older server hands out, and what stops opening once the bucket
+ /// is private.
final String id, name, mime, url;
final int size;
+ /// A short-lived signed link to the file (backend fileLinks.ts), made for
+ /// this read; null from an older server, or when none could be signed.
+ /// Opened in place of [url] whenever there is one.
+ final String? link;
+
+ /// The server could not sign a link this time: [url] is never opened then,
+ /// a fresh link is asked for first (it may sign now).
+ final bool unlinked;
+
+ /// When [link] stops opening, in epoch milliseconds.
+ final int? expiresAt;
+
/// The kinds the backend serves to be seen in place; every other kind is
/// served as a download.
bool get isImage => const {
diff --git a/desktop/lib/super_tabs/view.dart b/desktop/lib/super_tabs/view.dart
index d43415160..ba2226692 100644
--- a/desktop/lib/super_tabs/view.dart
+++ b/desktop/lib/super_tabs/view.dart
@@ -39,6 +39,7 @@ import 'activity.dart';
import 'activity_view.dart';
import 'attention.dart';
import 'controller.dart';
+import 'file_links.dart';
import 'dialog_scope.dart';
import 'folder_zip.dart';
import 'instructions_panel.dart';
@@ -3930,8 +3931,15 @@ class _SuperTabViewState extends State<SuperTabView>
);
}
+ // Every message's files ask this Swarm's controller for fresh links; another
+ // Swarm or account is another source, so a late answer is dropped.
@override
- Widget build(BuildContext context) => LayoutBuilder(
+ Widget build(BuildContext context) => SuperTabFiles(
+ fresh: widget.controller.readFileLink,
+ child: _swarm(context),
+ );
+
+ Widget _swarm(BuildContext context) => LayoutBuilder(
builder: (context, constraints) {
grid.AppTheme.watch(context);
final terminal = widget.terminal;
diff --git a/desktop/lib/web/parts/attachment_text_viewer.dart b/desktop/lib/web/parts/attachment_text_viewer.dart
index d0b1f160e..9ff57ff58 100644
--- a/desktop/lib/web/parts/attachment_text_viewer.dart
+++ b/desktop/lib/web/parts/attachment_text_viewer.dart
@@ -1,6 +1,7 @@
import 'package:flutter/widgets.dart';
import '../../super_tabs/attachment_text_viewer.dart' deferred as viewer;
+import '../../super_tabs/file_links.dart';
import '../../super_tabs/model.dart';
import 'deferred_part.dart';
@@ -17,14 +18,16 @@ class AttachmentTextViewer extends StatelessWidget {
super.key,
required this.file,
required this.url,
+ this.ref,
});
final SuperTabAttachment file;
final Uri url;
+ final SuperTabFileRef? ref;
@override
Widget build(BuildContext context) => DeferredPartView(
part: attachmentTextPart,
- builder: (_) => viewer.AttachmentTextViewer(file: file, url: url),
+ builder: (_) => viewer.AttachmentTextViewer(file: file, url: url, ref: ref),
);
}
diff --git a/desktop/test/super_tab_backend_transport_test.dart b/desktop/test/super_tab_backend_transport_test.dart
index 86e7fa40b..0d0b67369 100644
--- a/desktop/test/super_tab_backend_transport_test.dart
+++ b/desktop/test/super_tab_backend_transport_test.dart
@@ -253,6 +253,13 @@ void main() {
('POST', '/api/super-tabs/routing-policy'),
('GET', '/api/super-tabs/tab1/projects/p1/roles/r1'),
('POST', '/api/super-tabs/tab1/projects/p1/roles/r1'),
+ // A file link names one message and one file, nothing above or past it.
+ ('GET', '/api/super-tabs/tab1/conversations/c1/messages/m1/attachments'),
+ ('GET', '/api/super-tabs/tab1/conversations/c1/attachments/a1'),
+ (
+ 'GET',
+ '/api/super-tabs/tab1/conversations/c1/messages/m1/attachments/a1/x',
+ ),
]) {
final failure = await refusal(transport(method, path));
expect(failure.code, 'INVALID_REQUEST', reason: '$method $path');
@@ -279,6 +286,11 @@ void main() {
('GET', '/api/super-tabs/tab1/groups/g1'),
('GET', '/api/super-tabs/tab1/conversations/c1/messages?before=5'),
('GET', '/api/super-tabs/tab1/conversations/c1/catch-up?after=4'),
+ // A fresh short-lived link to one file (backend fileLinks.ts).
+ (
+ 'GET',
+ '/api/super-tabs/tab1/conversations/c1/messages/m1/attachments/a1',
+ ),
('POST', '/api/super-tabs/tab1/conversations/c1/read'),
('POST', '/api/super-tabs/tab1/conversations/c1/threads/m1/read'),
('GET', '/api/super-tabs/tab1/projects/p1/github'),
diff --git a/desktop/test/super_tab_file_links_test.dart b/desktop/test/super_tab_file_links_test.dart
new file mode 100644
index 000000000..9065472b1
--- /dev/null
+++ b/desktop/test/super_tab_file_links_test.dart
@@ -0,0 +1,616 @@
+// Chat files open through short-lived signed links (backend fileLinks.ts):
+// a link about to end is asked for again before anything opens, a reader who
+// lost access is told so and opens nothing, a failed ask can be tried again,
+// and an answer that lands after its screen or Swarm went changes nothing.
+import 'dart:async';
+import 'dart:ui' as ui;
+
+import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/core/open_in_browser.dart';
+import 'package:harness/super_tabs/attachment_viewer.dart';
+import 'package:harness/super_tabs/controller.dart';
+import 'package:harness/super_tabs/file_links.dart';
+import 'package:harness/super_tabs/model.dart';
+import 'package:harness/super_tabs/view.dart';
+
+import 'support/super_tab_fixture.dart';
+
+final _now = DateTime.utc(2026, 10, 31, 18);
+final _ms = _now.millisecondsSinceEpoch;
+
+String _signed(String id, String name, {String sig = '1'}) =>
+ 'https://storage.googleapis.com/bucket/super-tabs/tab/$id/$name'
+ '?X-Goog-Signature=$sig';
+
+/// A file as a read hands it: [url] its stored CDN address, [link] its
+/// signed link (none when [unsigned]).
+Map<String, dynamic> _file(
+ String id,
+ String name, {
+ String mime = 'application/pdf',
+ String? url,
+ String? link,
+ bool unsigned = false,
+ int? expiresAt,
+ int size = 1024,
+}) => {
+ 'id': id,
+ 'name': name,
+ 'mime': mime,
+ 'size': size,
+ 'url': url ?? _cdn(id, name),
+ if (!unsigned) 'link': link ?? _signed(id, name),
+ 'expiresAt': ?expiresAt,
+};
+
+String _cdn(String id, String name) =>
+ 'https://cdn.harness.autonomous.ai/super-tabs/tab/$id/$name';
+
+Future<Uint8List> _png(WidgetTester tester) async =>
+ (await tester.runAsync(() async {
+ final recorder = ui.PictureRecorder();
+ Canvas(recorder).drawRect(
+ const Rect.fromLTWH(0, 0, 40, 30),
+ Paint()..color = const Color(0xFF3060A0),
+ );
+ final image = await recorder.endRecording().toImage(40, 30);
+ final data = await image.toByteData(format: ui.ImageByteFormat.png);
+ return data!.buffer.asUint8List();
+ }))!;
+
+void main() {
+ late List<Uri> opened;
+ late List<Uri> drawn;
+ late Map<String, Uint8List> pictures;
+ late Future<bool> Function(Uri) browser;
+ late ImageProvider Function(Uri) image;
+
+ setUp(() {
+ opened = [];
+ drawn = [];
+ pictures = {};
+ browser = browserOpener;
+ image = attachmentImage;
+ superTabFileClock = () => _now;
+ browserOpener = (url) async {
+ opened.add(url);
+ return true;
+ };
+ attachmentImage = (url) {
+ drawn.add(url);
+ return MemoryImage(pictures[url.toString()] ?? Uint8List(3));
+ };
+ });
+ tearDown(() {
+ browserOpener = browser;
+ attachmentImage = image;
+ superTabFileClock = DateTime.now;
+ });
+
+ Widget view(SuperTabController controller) => MaterialApp(
+ home: Scaffold(
+ body: SuperTabView(
+ controller: controller,
+ onInvite: () {},
+ onAddAgent: (_) {},
+ onOpenTerminal: (_) {},
+ machineName: (_) => 'Test Mac',
+ ),
+ ),
+ );
+
+ Future<(ConversationServer, SuperTabController)> show(
+ WidgetTester tester,
+ List<Map<String, dynamic>> files, {
+ FutureOr<Map<String, dynamic>> Function(String id)? fileLink,
+ }) async {
+ final server = ConversationServer()
+ ..append('Here', attachments: files)
+ ..fileLink = fileLink;
+ final controller = server.controller();
+ await controller.refresh();
+ await tester.binding.setSurfaceSize(const Size(1100, 800));
+ addTearDown(() => tester.binding.setSurfaceSize(null));
+ await tester.pumpWidget(view(controller));
+ await tester.pumpAndSettle();
+ await tester.runAsync(() => Future<void>.delayed(Durations.short2));
+ await tester.pumpAndSettle();
+ return (server, controller);
+ }
+
+ Future<void> close(WidgetTester tester, SuperTabController c) async {
+ expect(tester.takeException(), isNull);
+ await tester.pumpWidget(const SizedBox());
+ await c.flush();
+ c.dispose();
+ }
+
+ Finder chip(String id) => find.byKey(ValueKey('super-attachment:$id'));
+
+ group('a chip', () {
+ testWidgets('opens a link with more than a minute left as it is', (
+ tester,
+ ) async {
+ final (server, c) = await show(tester, [
+ _file('a1', 'report.pdf', expiresAt: _ms + 5 * 60000),
+ ]);
+
+ await tester.tap(chip('a1'));
+ await tester.pumpAndSettle();
+ expect(opened, [Uri.parse(_signed('a1', 'report.pdf'))]);
+ expect(server.fileLinkReads, isEmpty);
+ await close(tester, c);
+ });
+
+ testWidgets('asks for a fresh link when its own is about to end', (
+ tester,
+ ) async {
+ final (server, c) = await show(tester, [
+ _file('a1', 'report.pdf', expiresAt: _ms + 30000),
+ ]);
+ server.fileLink = (id) => _file(
+ id,
+ 'report.pdf',
+ link: _signed(id, 'report.pdf', sig: 'fresh'),
+ expiresAt: _ms + 10 * 60000,
+ );
+
+ await tester.tap(chip('a1'));
+ await tester.pumpAndSettle();
+ expect(server.fileLinkReads, [
+ '/api/super-tabs/tab/conversations/lead/messages/message-1/attachments/a1',
+ ]);
+ expect(opened, [Uri.parse(_signed('a1', 'report.pdf', sig: 'fresh'))]);
+ await close(tester, c);
+ });
+
+ testWidgets('with no signed link, the stored address opens as before', (
+ tester,
+ ) async {
+ final (server, c) = await show(tester, [
+ _file('a1', 'report.pdf', unsigned: true),
+ ]);
+
+ await tester.tap(chip('a1'));
+ await tester.pumpAndSettle();
+ expect(opened, [Uri.parse(_cdn('a1', 'report.pdf'))]);
+ expect(server.fileLinkReads, isEmpty);
+ await close(tester, c);
+ });
+
+ testWidgets('a signed link that ended never falls back to the stored one', (
+ tester,
+ ) async {
+ final (server, c) = await show(tester, [
+ _file('a1', 'report.pdf', expiresAt: _ms - 1000),
+ ]);
+ // The fresh answer carries no signed link: not available.
+ server.fileLink = (id) => _file(id, 'report.pdf', unsigned: true);
+
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ await tester.pump();
+ expect(server.fileLinkReads, hasLength(1));
+ expect(find.text(kSuperTabFileGone), findsOneWidget);
+ expect(opened, isEmpty);
+ await close(tester, c);
+ });
+
+ testWidgets('a file with no address at all is asked for when clicked', (
+ tester,
+ ) async {
+ final (server, c) = await show(tester, [
+ _file('a1', 'report.pdf', url: '', unsigned: true),
+ ]);
+ server.fileLink = (id) =>
+ _file(id, 'report.pdf', expiresAt: _ms + 10 * 60000);
+
+ await tester.tap(chip('a1'));
+ await tester.pumpAndSettle();
+ expect(server.fileLinkReads, hasLength(1));
+ expect(opened, [Uri.parse(_signed('a1', 'report.pdf'))]);
+ await close(tester, c);
+ });
+
+ testWidgets(
+ 'access lost: says the file is no longer there, opens nothing',
+ (tester) async {
+ final (server, c) = await show(tester, [
+ _file('a1', 'report.pdf', expiresAt: _ms - 1000),
+ ]);
+ // Unset: the server answers 404, as for a removed member.
+
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ await tester.pump();
+ expect(find.text(kSuperTabFileGone), findsOneWidget);
+ expect(server.fileLinkReads, hasLength(1));
+ expect(opened, isEmpty);
+ await close(tester, c);
+ },
+ );
+
+ testWidgets('a fresh link that is not on file storage opens nothing', (
+ tester,
+ ) async {
+ final (server, c) = await show(tester, [
+ _file('a1', 'report.pdf', expiresAt: _ms - 1000),
+ ]);
+ server.fileLink = (id) => _file(
+ id,
+ 'report.pdf',
+ link: 'https://evil.example/a1/report.pdf',
+ expiresAt: _ms + 600000,
+ );
+
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ await tester.pump();
+ expect(find.text(kSuperTabFileGone), findsOneWidget);
+ expect(opened, isEmpty);
+ await close(tester, c);
+ });
+
+ testWidgets('a failed ask says try again, and the next click works', (
+ tester,
+ ) async {
+ final (server, c) = await show(tester, [
+ _file('a1', 'report.pdf', expiresAt: _ms - 1000),
+ ]);
+ server.fileLink = (_) => throw StateError('Offline');
+
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ await tester.pump();
+ expect(find.text(kSuperTabFileRetry), findsOneWidget);
+ expect(opened, isEmpty);
+
+ server.fileLink = (id) => _file(
+ id,
+ 'report.pdf',
+ link: _signed(id, 'report.pdf', sig: 'again'),
+ expiresAt: _ms + 600000,
+ );
+ ScaffoldMessenger.of(tester.element(chip('a1'))).clearSnackBars();
+ await tester.pumpAndSettle();
+ await tester.tap(chip('a1'));
+ await tester.pumpAndSettle();
+ expect(server.fileLinkReads, hasLength(2));
+ expect(opened, [Uri.parse(_signed('a1', 'report.pdf', sig: 'again'))]);
+ await close(tester, c);
+ });
+
+ testWidgets('an answer after the chat closed opens nothing', (
+ tester,
+ ) async {
+ final (server, c) = await show(tester, [
+ _file('a1', 'report.pdf', expiresAt: _ms - 1000),
+ ]);
+ final answer = Completer<Map<String, dynamic>>();
+ server.fileLink = (_) => answer.future;
+
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ expect(server.fileLinkReads, hasLength(1));
+ await tester.pumpWidget(const SizedBox());
+ answer.complete(_file('a1', 'report.pdf', expiresAt: _ms + 600000));
+ await tester.pumpAndSettle();
+ expect(opened, isEmpty);
+ expect(tester.takeException(), isNull);
+ await c.flush();
+ c.dispose();
+ });
+
+ testWidgets('an answer after the Swarm changed under it opens nothing', (
+ tester,
+ ) async {
+ final (server, c) = await show(tester, [
+ _file('a1', 'report.pdf', expiresAt: _ms - 1000),
+ ]);
+ final answer = Completer<Map<String, dynamic>>();
+ server.fileLink = (_) => answer.future;
+
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ // Another account's controller over the same chat: the same chip, a new
+ // fresh-link source.
+ final other = server.controller(account: 'other');
+ await other.refresh();
+ await tester.pumpWidget(view(other));
+ await tester.pump();
+ expect(chip('a1'), findsOneWidget);
+ answer.complete(_file('a1', 'report.pdf', expiresAt: _ms + 600000));
+ await tester.pumpAndSettle();
+ expect(opened, isEmpty);
+ expect(find.text(kSuperTabFileGone), findsNothing);
+ expect(find.text(kSuperTabFileRetry), findsNothing);
+ await close(tester, other);
+ await c.flush();
+ c.dispose();
+ });
+
+ testWidgets('a link off file storage is refused without asking', (
+ tester,
+ ) async {
+ final (server, c) = await show(tester, [
+ _file(
+ 'a1',
+ 'one.pdf',
+ link: 'http://storage.googleapis.com/bucket/a1/one.pdf',
+ ),
+ _file(
+ 'a2',
+ 'two.pdf',
+ link: 'https://storage.googleapis.com.evil.example/a2/two.pdf',
+ ),
+ _file(
+ 'a3',
+ 'three.pdf',
+ link: 'https://me:[email protected]/bucket/a3/three.pdf',
+ ),
+ ]);
+ server.fileLink = (id) => _file(id, 'x.pdf', expiresAt: _ms + 600000);
+
+ for (final id in ['a1', 'a2', 'a3']) {
+ await tester.tap(chip(id));
+ await tester.pump();
+ expect(
+ find.text(
+ 'This file isn’t on Harness’s file storage, so it wasn’t opened.',
+ ),
+ findsOneWidget,
+ reason: id,
+ );
+ ScaffoldMessenger.of(tester.element(chip(id))).clearSnackBars();
+ await tester.pumpAndSettle();
+ }
+ expect(opened, isEmpty);
+ expect(server.fileLinkReads, isEmpty);
+ await close(tester, c);
+ });
+ });
+
+ group('a picture', () {
+ final inline = find.byKey(const ValueKey('super-attachment-inline:p1'));
+
+ testWidgets('whose link ended is drawn from a fresh one', (tester) async {
+ final fresh = _signed('p1', 'shot.png', sig: 'fresh');
+ pictures[fresh] = await _png(tester);
+ final server = ConversationServer()
+ ..append(
+ 'Here',
+ attachments: [
+ _file('p1', 'shot.png', mime: 'image/png', expiresAt: _ms - 1),
+ ],
+ );
+ server.fileLink = (id) => _file(
+ id,
+ 'shot.png',
+ mime: 'image/png',
+ link: fresh,
+ expiresAt: _ms + 600000,
+ );
+ final c = server.controller();
+ await c.refresh();
+ await tester.pumpWidget(view(c));
+ await tester.pumpAndSettle();
+ await tester.runAsync(() => Future<void>.delayed(Durations.short2));
+ await tester.pumpAndSettle();
+
+ expect(server.fileLinkReads, hasLength(1));
+ expect(drawn.toSet(), {Uri.parse(fresh)});
+ expect(inline, findsOneWidget);
+ expect(find.text('shot.png can’t be shown here.'), findsNothing);
+ await close(tester, c);
+ });
+
+ testWidgets('that fails to load is asked for once more, then given up', (
+ tester,
+ ) async {
+ final first = _signed('p1', 'shot.png');
+ final second = _signed('p1', 'shot.png', sig: 'two');
+ final (server, c) = await show(
+ tester,
+ [_file('p1', 'shot.png', mime: 'image/png', expiresAt: _ms + 600000)],
+ fileLink: (id) => _file(
+ id,
+ 'shot.png',
+ mime: 'image/png',
+ link: second,
+ expiresAt: _ms + 600000,
+ ),
+ );
+ // Neither link draws: the first ended under it, the second is broken.
+ for (var i = 0; i < 4; i++) {
+ await tester.runAsync(() => Future<void>.delayed(Durations.short2));
+ await tester.pumpAndSettle();
+ }
+
+ expect(drawn.toSet(), {Uri.parse(first), Uri.parse(second)});
+ expect(server.fileLinkReads, hasLength(1));
+ expect(find.text('shot.png can’t be shown here.'), findsOneWidget);
+ await close(tester, c);
+ });
+
+ testWidgets('whose fresh link is refused says it can\'t be shown', (
+ tester,
+ ) async {
+ final (server, c) = await show(tester, [
+ _file('p1', 'shot.png', mime: 'image/png', expiresAt: _ms - 1),
+ ]);
+
+ expect(server.fileLinkReads, hasLength(1));
+ expect(drawn, isEmpty);
+ expect(find.text('shot.png can’t be shown here.'), findsOneWidget);
+ await close(tester, c);
+ });
+
+ testWidgets('full screen, Open in browser asks again once the link ends', (
+ tester,
+ ) async {
+ final first = _signed('p1', 'shot.png');
+ pictures[first] = await _png(tester);
+ final (server, c) = await show(tester, [
+ _file('p1', 'shot.png', mime: 'image/png', expiresAt: _ms + 120000),
+ ]);
+ server.fileLink = (id) => _file(
+ id,
+ 'shot.png',
+ mime: 'image/png',
+ link: _signed(id, 'shot.png', sig: 'later'),
+ expiresAt: _ms + 900000,
+ );
+
+ await tester.tap(inline);
+ await tester.pumpAndSettle();
+ // The viewer stayed open past the link's last minute.
+ superTabFileClock = () => _now.add(const Duration(minutes: 2));
+ await tester.tap(find.text('Open in browser'));
+ await tester.pumpAndSettle();
+ expect(server.fileLinkReads, hasLength(1));
+ expect(opened, [Uri.parse(_signed('p1', 'shot.png', sig: 'later'))]);
+ await tester.tap(find.text('Close'));
+ await tester.pumpAndSettle();
+ await close(tester, c);
+ });
+
+ testWidgets('an answer after the Swarm changed under it still lets the '
+ 'new one load', (tester) async {
+ final fresh = _signed('p1', 'shot.png', sig: 'fresh');
+ pictures[fresh] = await _png(tester);
+ final answer = Completer<Map<String, dynamic>>();
+ final (server, c) = await show(tester, [
+ _file('p1', 'shot.png', mime: 'image/png', expiresAt: _ms - 1),
+ ], fileLink: (_) => answer.future);
+ expect(server.fileLinkReads, hasLength(1));
+ // Another account's controller over the same chat: the same picture,
+ // a new fresh-link source, asked for while the first ask still runs.
+ final other = server.controller(account: 'other');
+ await other.refresh();
+ await tester.pumpWidget(view(other));
+ await tester.pump();
+ answer.complete(
+ _file(
+ 'p1',
+ 'shot.png',
+ mime: 'image/png',
+ link: fresh,
+ expiresAt: _ms + 600000,
+ ),
+ );
+ for (var i = 0; i < 4; i++) {
+ await tester.runAsync(() => Future<void>.delayed(Durations.short2));
+ await tester.pumpAndSettle();
+ }
+ expect(server.fileLinkReads, hasLength(2));
+ expect(drawn.toSet(), {Uri.parse(fresh)});
+ expect(inline, findsOneWidget);
+ await close(tester, other);
+ await c.flush();
+ c.dispose();
+ });
+ });
+
+ group('a file the server could not sign', () {
+ testWidgets('never opens its stored address: a fresh link is asked for', (
+ tester,
+ ) async {
+ final (server, c) = await show(tester, [
+ {..._file('a1', 'report.pdf', unsigned: true), 'unlinked': true},
+ ], fileLink: (id) => _file(id, 'report.pdf', expiresAt: _ms + 600000));
+
+ await tester.tap(chip('a1'));
+ await tester.pumpAndSettle();
+ expect(server.fileLinkReads, hasLength(1));
+ expect(opened, [Uri.parse(_signed('a1', 'report.pdf'))]);
+ await close(tester, c);
+ });
+
+ testWidgets('a signing failure says try again, not gone', (tester) async {
+ final (server, c) = await show(
+ tester,
+ [
+ {..._file('a1', 'report.pdf', unsigned: true), 'unlinked': true},
+ ],
+ fileLink: (_) => throw const SuperTabFailure(
+ 'FILE_LINK_UNAVAILABLE',
+ 'Couldn’t open the file just now. Try again.',
+ statusCode: 503,
+ ),
+ );
+
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ await tester.pump();
+ expect(find.text(kSuperTabFileRetry), findsOneWidget);
+ expect(find.text(kSuperTabFileGone), findsNothing);
+ expect(server.fileLinkReads, hasLength(1));
+ expect(opened, isEmpty);
+ await close(tester, c);
+ });
+ });
+
+ test('a link or expiry of another type reads as none, not a failed row', () {
+ final file = SuperTabAttachment.fromJson({
+ ..._file('a1', 'report.pdf'),
+ 'link': 42,
+ 'expiresAt': '1793466886541',
+ });
+ expect(file.link, isNull);
+ expect(file.expiresAt, isNull);
+ final signed = SuperTabAttachment.fromJson({
+ ..._file('a2', 'report.pdf'),
+ 'expiresAt': 'soon',
+ });
+ // A signed link with no readable end is asked for again, never trusted.
+ expect(signed.expiresAt, isNull);
+ expect(SuperTabFileRef(signed).current, isNull);
+ });
+
+ testWidgets('a text file\'s viewer asks for a fresh link once its own ends', (
+ tester,
+ ) async {
+ final fetched = <Uri>[];
+ final fetcher = attachmentFetcher;
+ attachmentFetcher = (url, {maxBytes}) async {
+ fetched.add(url);
+ return Uint8List.fromList('# Notes'.codeUnits);
+ };
+ addTearDown(() => attachmentFetcher = fetcher);
+ final (server, c) = await show(
+ tester,
+ [
+ _file(
+ 'a1',
+ 'notes.md',
+ mime: 'text/markdown',
+ size: 7,
+ expiresAt: _ms + 120000,
+ ),
+ ],
+ fileLink: (id) => _file(
+ id,
+ 'notes.md',
+ mime: 'text/markdown',
+ link: _signed(id, 'notes.md', sig: 'later'),
+ expiresAt: _ms + 900000,
+ ),
+ );
+
+ await tester.tap(chip('a1'));
+ await tester.pumpAndSettle();
+ expect(fetched, [Uri.parse(_signed('a1', 'notes.md'))]);
+ expect(server.fileLinkReads, isEmpty);
+ // The viewer stayed open past the link's last minute.
+ superTabFileClock = () => _now.add(const Duration(minutes: 2));
+ await tester.tap(find.text('Open in browser'));
+ await tester.pumpAndSettle();
+ expect(server.fileLinkReads, hasLength(1));
+ expect(opened, [Uri.parse(_signed('a1', 'notes.md', sig: 'later'))]);
+ await tester.tap(find.text('Close'));
+ await tester.pumpAndSettle();
+ await close(tester, c);
+ });
+}
diff --git a/desktop/test/support/super_tab_fixture.dart b/desktop/test/support/super_tab_fixture.dart
index b91a5873f..6982c0d1f 100644
--- a/desktop/test/support/super_tab_fixture.dart
+++ b/desktop/test/support/super_tab_fixture.dart
@@ -632,6 +632,12 @@ class ConversationServer {
/// The read marks this device sent, in order: {conversationId, sequence}.
final readMarkPosts = <Map<String, dynamic>>[];
+ /// Every fresh file-link read (backend fileLinks.ts), by path. [fileLink]
+ /// answers one, by attachment id: an attachment row, or a throw; unset, it
+ /// answers as a server that no longer shows the reader that message.
+ final fileLinkReads = <String>[];
+ FutureOr<Map<String, dynamic>> Function(String attachmentId)? fileLink;
+
/// The catch-up summary the backend answers per conversation (catchUp.ts);
/// one not set answers an empty summary.
final catchUps = <String, Map<String, dynamic>>{};
@@ -1489,6 +1495,19 @@ class ConversationServer {
uri.pathSegments[5] == 'instructions') {
return _readInstructions(path, uri);
}
+ if (uri.pathSegments.length > 2 &&
+ uri.pathSegments[uri.pathSegments.length - 2] == 'attachments') {
+ fileLinkReads.add(path);
+ final answer = fileLink;
+ if (answer == null) {
+ throw const SuperTabFailure(
+ 'FILE_UNAVAILABLE',
+ 'This file is no longer available.',
+ statusCode: 404,
+ );
+ }
+ return {'attachment': await answer(uri.pathSegments.last)};
+ }
if (uri.pathSegments.last == 'catch-up') {
catchUpReads.add(path);
if (holdCatchUp case final hold?) await hold.future;
diff --git a/docs/swarms.md b/docs/swarms.md
index 1b3af7854..09c05b047 100644
--- a/docs/swarms.md
+++ b/docs/swarms.md
@@ -670,6 +670,106 @@ before it is sent, with "Files can be up to 256 MB."
- Agents' computers save each message's files straight to disk, never more than
the size the message names.
+### Chat file links
+
+A file opens only for someone who can read its message now. Each read of a
+message gives each of its files a signed link that opens for 5 to 10 minutes
+(`backend/src/superTabs/fileLinks.ts`). It stays the same link through each 5
+minutes, so a picture is not fetched again on every poll. Links are made only
+for files in the reading Swarm's own folder.
+
+- Desktop, the web and the phone open a file through its link. When the link
+ has less than a minute left, they first ask the server for a fresh one. The
+ server answers that request as it answers reading that exact message:
+ someone removed from the Swarm or the project, a deleted Swarm, a private
+ chat or another project's chat all get the same "This file is no longer
+ available."
+- A file the server could not sign just now (the cloud's signing failed) is
+ marked as such. The apps then ask for a fresh link, and the server answers
+ "Couldn't open the file just now. Try again." while signing still fails. They
+ never open the public address instead. Only a file with no link and no such
+ mark, which comes from a server from before signed links, opens by its public
+ address.
+- Agents' computers download through the same links. A try whose link is over
+ asks for a fresh one, read as the agent reads the message now. Storage checks
+ a link when a download starts, so a slow download that started in time
+ finishes. A link never appears in an agent's prompt.
+- Uploaded avatars kept in the chat files' bucket get signed links too, which
+ stop opening within an hour.
+- Reads still carry each file's public address, because apps from before signed
+ links open nothing else.
+
+**The guarantee starts only when the bucket goes private.** Until then, the
+public address of every file still opens it, for anyone who once had it: a
+removed member, someone after a Swarm was deleted, anyone they passed it to.
+Signed links alone do not end that; the release step does.
+
+**Release step (Ken's go).** The code never changes the bucket. In order:
+
+1. **Before the backend ships**, grant the signing permission: the same one
+ the 256 MB upload links need (step 2 of "Release steps for Ken" in #2441).
+ Then ship the backend, then the CLI, Desktop and the phone. Without the
+ permission, every file reads as not
+ signed, new apps say "Try again" instead of opening it, uploaded avatars show
+ the default face, and the backend logs
+ `[file-links] could not sign a chat file link` and
+ `[avatar-links] could not sign an avatar link`. For the web build's text
+ viewer, also add `GET` to the bucket's CORS rule from #2441's step 1.
+2. Wait until old apps are rare. Run `cd backend && npm run usage:daily -- 7`
+ with a read-only `DATABASE_URL` (docs/product-measurement.md), and read the
+ share of Desktop, phone and CLI activity by release
+ (`breakdown:release:<platform>:<version>` counters in
+ `product_usage_totals`). Go on once releases from before this one are under
+ 5% of each for two days running. An old app can then still read messages,
+ but it cannot open their files.
+3. Make the bucket private. Remove the `allUsers` (and any
+ `allAuthenticatedUsers`) `roles/storage.objectViewer` binding. Then rewrite
+ the cache setting that files stored before this release kept
+ (`public, max-age=31536000, immutable`, chat files and avatars alike), so
+ nothing keeps serving a cached copy:
+ `gcloud storage objects update "gs://BUCKET/PREFIX/**" "gs://BUCKET/avatars/**" --cache-control="private, max-age=600"`.
+ Purge the CDN in front of the bucket (`SUPER_TAB_ATTACHMENTS_PUBLIC_BASE`)
+ and remove the bucket as its origin.
+4. Check: an old public address and an expired signed link answer 403. A file
+ and an avatar still open in Desktop, on the phone and for an agent.
+
+Rollback: add the `allUsers` binding back. Old apps open files again; new ones
+never stopped. That rollback covers only the bucket. **Once the bucket is
+private, rolling the backend back past this release breaks every chat file and
+uploaded avatar at once**: an older backend hands out only public addresses,
+and a private bucket refuses them. So roll back in this order: make the bucket
+public again first, then roll the backend back. A later release can stop sending the public address, so that
+an old app says the file is not on Harness's file storage instead of opening a
+dead link.
+
+Design (`backend/src/superTabs/fileLinks.ts`):
+
+- Growth: nothing is stored. Links live in a per-server cache of the current
+ 5 minutes, at most 20,000, cleared when the window turns.
+- Concurrency: no writes. Reads asking at the same moment share one signature
+ per file.
+- Read cost: only the rows a reader is shown are signed, once per file per
+ 5 minutes, at most 8 at a time. With a key, a signature is local (under 1 ms).
+ Without one, each is a call to the cloud's signing API.
+- Scale: at 10x files per page, the first read in a window signs 10x. The
+ first metric to warn is message page latency (`recordMessageRead`), and the
+ signing warnings in the log.
+- Compatibility: new apps use the link, and use the address only for a file
+ with neither a link nor the not-signed mark, which only an older server sends. Old apps and old agents' computers use the address until the
+ bucket is private. The fresh-link read is new; an older server answers it
+ 404.
+- Migration + rollback: no data migration. The release step above is the
+ migration. Its rollback restores public reading.
+- Audience: the server decides every link from the reader's access at that
+ read. Storage enforces the end time.
+- Identity: the signed-in person's or the agent's own read. A Team account's
+ session reads as its agent.
+- Failure: a file that cannot be signed is marked not signed. Its fresh-link
+ read answers "try again" (503), never "no longer available", and the apps and
+ agents say to try again. An avatar that cannot be signed is left out of that
+ read alone. The backend logs the storage error's code only.
+- Platforms: backend, CLI host, Desktop (and the web build), phone.
+
## Avatars, status and celebrations
People and agents each show one avatar wherever they appear: messages, the
@@ -702,13 +802,11 @@ server:
gets no new links.
- Without one (production today, by the owner's choice on 2026-10-28),
pictures are kept in the chat files' bucket, in a folder of their own
- (`avatars/`), and linked on the same public CDN. **They work like chat files:
- a picture's link never expires.** Anyone who once had it, including someone
- later removed from the Swarm or anyone they pass it to, can still open that
- picture. Only the 128 random bits in its address keep it from being guessed.
- A new picture gets a new address; the old one stays reachable. Chat files
- never go in the avatar folder, and the server makes avatar links for that
- folder alone.
+ (`avatars/`). They are linked like chat files (see Chat file links): signed
+ links that stop opening within an hour. Until the release step makes that
+ bucket private, a picture's old public address also still opens it. Chat
+ files never go in the avatar folder, and the server makes avatar links for
+ that folder alone.
A server with no attachments bucket refuses uploads; the eight pictures still
work.
@@ -1051,7 +1149,8 @@ cannot be deleted, and only the owner sees the item.
- For 7 days its owner can restore it from the same dropdown (**Restore "name"**, with the days left). People, projects,
chats and files come back; agents and machines do not, and are added again.
- After the 7 days the worker removes its records and stored files, and only its own (`backend/src/superTabs/purge.ts`).
- Chat file links keep working until then.
+ From the delete on, nobody gets a new file link. Links already handed out stop opening within 10 minutes, once the
+ bucket is private (see Chat file links).
Design (`backend/src/superTabs/deletion.ts`):
diff --git a/mobile/lib/swarm/model.dart b/mobile/lib/swarm/model.dart
index 8a24800ae..03c484643 100644
--- a/mobile/lib/swarm/model.dart
+++ b/mobile/lib/swarm/model.dart
@@ -977,10 +977,33 @@ class SuperTabAttachment {
name = _text(row, 'name'),
mime = _text(row, 'mime'),
size = (row['size'] as num).toInt(),
- url = _text(row, 'url');
+ url = _text(row, 'url'),
+ // Read defensively: a field of the wrong type reads as absent, never as
+ // a failed row.
+ link = row['link'] is String ? row['link'] as String : null,
+ unlinked = row['unlinked'] == true,
+ expiresAt = row['expiresAt'] is num
+ ? (row['expiresAt'] as num).toInt()
+ : null;
+
+ /// [url] is the address the file was stored with, on the attachments CDN:
+ /// what an older server hands out, and what stops opening once the bucket
+ /// is private.
final String id, name, mime, url;
final int size;
+ /// A short-lived signed link to the file (backend fileLinks.ts), made for
+ /// this read; null from an older server, or when none could be signed.
+ /// Opened in place of [url] whenever there is one (swarm_file_links.dart).
+ final String? link;
+
+ /// The server could not sign a link this time: [url] is never opened then,
+ /// a fresh link is asked for first (it may sign now).
+ final bool unlinked;
+
+ /// When [link] stops opening, in epoch milliseconds.
+ final int? expiresAt;
+
/// The kinds the backend serves to be seen in place; every other kind is
/// served as a download.
bool get isImage => const {
diff --git a/mobile/lib/swarm/swarm_attachment.dart b/mobile/lib/swarm/swarm_attachment.dart
index e025cb757..cf5a448cf 100644
--- a/mobile/lib/swarm/swarm_attachment.dart
+++ b/mobile/lib/swarm/swarm_attachment.dart
@@ -13,6 +13,7 @@ import 'package:harness_mobile/shared/widgets/skeleton.dart';
import 'model.dart';
import 'swarm_attachment_store.dart';
+import 'swarm_file_links.dart';
import 'swarm_outgoing_files.dart' show swarmFormAttachmentMaxBytes;
import 'swarm_style.dart';
@@ -27,6 +28,7 @@ class SwarmAttachment extends StatelessWidget {
required this.link,
required this.chip,
this.store,
+ this.fileRef,
});
final SuperTabAttachment file;
@@ -36,20 +38,26 @@ class SwarmAttachment extends StatelessWidget {
final Uri? link;
final Widget chip;
+ /// Where a fresh link comes from when [link] is missing or has ended
+ /// (swarm_file_links.dart): asked for before the picture is fetched, and
+ /// once more when a signed link fails.
+ final SwarmFileRef? fileRef;
+
/// Where the picture is kept; the app's own ([swarmAttachments]) by default.
final SwarmAttachmentStore? store;
@override
Widget build(BuildContext context) {
- final url = link;
+ final ref = fileRef;
if (!file.isImage ||
- url == null ||
+ (link == null && (ref == null || ref.refused)) ||
file.size > swarmFormAttachmentMaxBytes) {
return chip;
}
return _Preview(
file: file,
- url: url,
+ url: link,
+ fileRef: ref,
chip: chip,
store: store ?? swarmAttachments,
);
@@ -68,12 +76,16 @@ class _Preview extends StatefulWidget {
const _Preview({
required this.file,
required this.url,
+ required this.fileRef,
required this.chip,
required this.store,
});
final SuperTabAttachment file;
- final Uri url;
+
+ /// The link at hand; null when a fresh one is asked of [fileRef] first.
+ final Uri? url;
+ final SwarmFileRef? fileRef;
final Widget chip;
final SwarmAttachmentStore store;
@@ -102,7 +114,11 @@ class _PreviewState extends State<_Preview> {
@override
void didUpdateWidget(_Preview old) {
super.didUpdateWidget(old);
- if (old.url == widget.url && identical(old.store, widget.store)) return;
+ if (old.url == widget.url &&
+ identical(old.store, widget.store) &&
+ identical(old.fileRef?.reader, widget.fileRef?.reader)) {
+ return;
+ }
old.store.turns.removeListener(_turned);
widget.store.turns.addListener(_turned);
_ask();
@@ -116,10 +132,34 @@ class _PreviewState extends State<_Preview> {
void _ask() {
_turn = widget.store.turns.value;
- _local = widget.store.file(widget.url);
+ _local = _fetch(widget.store, widget.url, widget.fileRef);
_failed = false;
}
+ /// The picture's file: by the link at hand, else a fresh one; a signed
+ /// link that fails (it may have ended) is asked for once more, then the
+ /// failure stands.
+ Future<File> _fetch(
+ SwarmAttachmentStore store,
+ Uri? at,
+ SwarmFileRef? ref,
+ ) async {
+ var url = at ?? await (ref ?? (throw StateError('No link'))).link();
+ try {
+ return await store.file(url, key: _keyOf(url));
+ } catch (_) {
+ if (ref == null || url.host != swarmFilesHost) rethrow;
+ url = await ref.link(stale: true);
+ return store.file(url, key: _keyOf(url));
+ }
+ }
+
+ /// A signed link changes every few minutes; the file it reaches does not,
+ /// so it is kept by the file's id.
+ String _keyOf(Uri url) => url.host == swarmFilesHost
+ ? 'swarm-file:${widget.file.id}'
+ : url.toString();
+
/// The files kept changed hands: the picture is asked for again, for
/// whoever they are kept for now.
void _turned() {
diff --git a/mobile/lib/swarm/swarm_attachment_store.dart b/mobile/lib/swarm/swarm_attachment_store.dart
index 777d1da4e..8290ad114 100644
--- a/mobile/lib/swarm/swarm_attachment_store.dart
+++ b/mobile/lib/swarm/swarm_attachment_store.dart
@@ -73,13 +73,15 @@ class SwarmAttachmentStore {
/// [keepOnly] and [clear] one after the other, never over each other.
Future<void> _changing = Future.value();
- /// [url]'s file, fetched the first time and kept after. Throws while nobody's
- /// files are kept. Asked while they are being emptied, it waits for that.
- Future<File> file(Uri url) async {
+ /// [url]'s file, fetched the first time and kept after, by [key] when given
+ /// (a signed link that changes for the same file), else by [url]. Throws
+ /// while nobody's files are kept. Asked while they are being emptied, it
+ /// waits for that.
+ Future<File> file(Uri url, {String? key}) async {
await _changing;
final account = _account;
if (account == null) throw StateError('No account to keep files for.');
- final fetch = _fetch(account, url);
+ final fetch = _fetch(account, url, key ?? url.toString());
final done = fetch.then<void>((_) {}, onError: (Object _) {});
_fetching.add(done);
try {
@@ -89,8 +91,10 @@ class SwarmAttachmentStore {
}
}
- Future<File> _fetch(String account, Uri url) async =>
- (await (_cache ??= _cacheFor(account))).getSingleFile(url.toString());
+ Future<File> _fetch(String account, Uri url, String key) async =>
+ (await (_cache ??= _cacheFor(
+ account,
+ ))).getSingleFile(url.toString(), key: key);
/// Keeps [accountId]'s files and empties every other account's; with null
/// (nobody signed in, or Swarm off) empties everything. A file that could
diff --git a/mobile/lib/swarm/swarm_conversation_page.dart b/mobile/lib/swarm/swarm_conversation_page.dart
index de4d07492..0bf785b45 100644
--- a/mobile/lib/swarm/swarm_conversation_page.dart
+++ b/mobile/lib/swarm/swarm_conversation_page.dart
@@ -24,6 +24,7 @@ import 'swarm_post_line.dart';
import 'swarm_attach.dart';
import 'swarm_buddy.dart';
import 'swarm_buddy_chat.dart';
+import 'swarm_file_links.dart';
import 'swarm_choices.dart';
import 'swarm_conversation.dart';
import 'swarm_face.dart';
@@ -804,21 +805,25 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
unawaited(
Navigator.of(context).push(
phoneRoute(
- (_) => SwarmThreadPage(
- swarmId: widget.swarmId,
- chat: c,
- root: root,
- title: _view.value?.title,
- row: (t, m, {required grouped, required acting, required onTap}) =>
- _threadRow(
- c,
- t,
- m,
- grouped: grouped,
- acting: acting,
- onTap: onTap,
- ),
- pending: (t, text) => _threadPending(c, t, text),
+ (_) => SwarmFiles(
+ reader: c.reader,
+ child: SwarmThreadPage(
+ swarmId: widget.swarmId,
+ chat: c,
+ root: root,
+ title: _view.value?.title,
+ row:
+ (t, m, {required grouped, required acting, required onTap}) =>
+ _threadRow(
+ c,
+ t,
+ m,
+ grouped: grouped,
+ acting: acting,
+ onTap: onTap,
+ ),
+ pending: (t, text) => _threadPending(c, t, text),
+ ),
),
listensForItself: true,
),
@@ -1024,10 +1029,13 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
)
: c == null
? _unopened(context, session, tty)
- : ValueListenableBuilder(
- valueListenable: _view,
- builder: (context, view, _) =>
- _page(context, session, c, view!, tty),
+ : SwarmFiles(
+ reader: c.reader,
+ child: ValueListenableBuilder(
+ valueListenable: _view,
+ builder: (context, view, _) =>
+ _page(context, session, c, view!, tty),
+ ),
),
),
);
diff --git a/mobile/lib/swarm/swarm_file_links.dart b/mobile/lib/swarm/swarm_file_links.dart
new file mode 100644
index 000000000..55049e7ac
--- /dev/null
+++ b/mobile/lib/swarm/swarm_file_links.dart
@@ -0,0 +1,212 @@
+import 'package:flutter/widgets.dart';
+
+import 'model.dart';
+import 'swarm_reader.dart';
+import 'swarm_request.dart';
+
+/// The host the backend serves message files' stored addresses from: a link
+/// anywhere else is not opened (the desktop's `attachmentLink`).
+const swarmAttachmentHost = String.fromEnvironment(
+ 'SUPER_TAB_ATTACHMENTS_HOST',
+ defaultValue: 'cdn.harness.autonomous.ai',
+);
+
+/// [url] as a link to open, or null unless it is https, with no sign-in in
+/// it, on [swarmAttachmentHost]: a message's url is whatever its sender's row
+/// says.
+Uri? swarmAttachmentLink(String url) {
+ final link = Uri.tryParse(url);
+ return link != null &&
+ link.scheme == 'https' &&
+ link.userInfo.isEmpty &&
+ link.host == swarmAttachmentHost
+ ? link
+ : null;
+}
+
+/// Where the backend's short-lived signed links to chat files point (backend
+/// superTabs/fileLinks.ts): Cloud Storage's own host. The desktop's
+/// `kSuperTabFilesHost`.
+const swarmFilesHost = String.fromEnvironment(
+ 'SUPER_TAB_FILES_HOST',
+ defaultValue: 'storage.googleapis.com',
+);
+
+/// A file's signed link (its `link`) as a link to open, or null unless it is
+/// https, with no sign-in in it, on [swarmFilesHost].
+Uri? swarmSignedLink(String? link) {
+ final url = link == null ? null : Uri.tryParse(link);
+ return url != null &&
+ url.scheme == 'https' &&
+ url.userInfo.isEmpty &&
+ url.host == swarmFilesHost
+ ? url
+ : null;
+}
+
+/// Said when the reader may no longer open a file: removed from the project
+/// or the Swarm, the Swarm deleted, or the message gone.
+const swarmFileGone = 'This file is no longer available.';
+
+/// Said when a fresh link could not be had just now; a later tap tries again.
+const swarmFileRetry = 'Couldn’t open the file. Try again.';
+
+/// How long a fresh-link read may take before it counts as failed.
+@visibleForTesting
+Duration swarmFileLinkTimeout = const Duration(seconds: 20);
+
+/// A link with less than this left is asked for again before it is opened.
+const swarmLinkMargin = Duration(seconds: 60);
+
+/// The clock links are judged by; tests set it.
+@visibleForTesting
+DateTime Function() swarmFileClock = DateTime.now;
+
+/// The Swarm whose chat is on screen, for its messages' files: fresh links
+/// are asked of [reader], and an answer is used only while it is still the
+/// reader on screen and has not been read again from nothing or lost
+/// ([SwarmReader.generation]).
+class SwarmFiles extends InheritedWidget {
+ const SwarmFiles({super.key, required this.reader, required super.child});
+
+ final SwarmReader reader;
+
+ static SwarmReader? of(BuildContext context) =>
+ context.dependOnInheritedWidgetOfExactType<SwarmFiles>()?.reader;
+
+ static SwarmReader? peek(BuildContext context) =>
+ context.getInheritedWidgetOfExactType<SwarmFiles>()?.reader;
+
+ @override
+ bool updateShouldNotify(SwarmFiles oldWidget) =>
+ !identical(reader, oldWidget.reader);
+}
+
+/// Why a file was not opened: [gone] when the reader may no longer open it.
+class SwarmFileUnavailable implements Exception {
+ const SwarmFileUnavailable({required this.gone});
+ final bool gone;
+ String get message => gone ? swarmFileGone : swarmFileRetry;
+ @override
+ String toString() => message;
+}
+
+/// One file of one message, and the newest link to it the chat has.
+class SwarmFileRef {
+ SwarmFileRef(this.file, {this.conversationId, this.messageId, this.reader})
+ : _latest = file;
+
+ final SuperTabAttachment file;
+ final String? conversationId, messageId;
+
+ /// Where fresh links are asked; null where none can be.
+ final SwarmReader? reader;
+
+ SuperTabAttachment _latest;
+
+ /// The newest link at hand: its signed link while it opens for more than
+ /// [swarmLinkMargin], else null. A file the server could not sign this
+ /// time ([SuperTabAttachment.unlinked]) has none: a fresh one is asked for.
+ /// Only from an older server, which signs nothing, the stored address on
+ /// the attachments CDN. A signed link is never passed over for the stored
+ /// address.
+ Uri? get current {
+ if (_latest.link case final signed?) {
+ final link = swarmSignedLink(signed), ends = _latest.expiresAt;
+ if (link == null || ends == null) return null;
+ final left = ends - swarmFileClock().millisecondsSinceEpoch;
+ return left > swarmLinkMargin.inMilliseconds ? link : null;
+ }
+ if (_latest.unlinked) return null;
+ return swarmAttachmentLink(_latest.url);
+ }
+
+ /// Whether the message names the file only by addresses this app refuses.
+ bool get refused => file.link != null
+ ? swarmSignedLink(file.link) == null
+ : !file.unlinked &&
+ file.url.isNotEmpty &&
+ swarmAttachmentLink(file.url) == null;
+
+ /// A link that opens now: [current], or a fresh one; [stale] asks for a
+ /// fresh one whatever the time. Throws [SwarmFileUnavailable].
+ Future<Uri> link({bool stale = false}) async {
+ if (!stale) {
+ if (current case final link?) return link;
+ }
+ final ask = reader, conversation = conversationId, message = messageId;
+ if (ask == null || conversation == null || message == null) {
+ throw const SwarmFileUnavailable(gone: false);
+ }
+ final SuperTabAttachment next;
+ try {
+ next = SuperTabAttachment.fromJson(
+ superTabMap(
+ superTabMap(
+ await ask
+ .request(
+ 'GET',
+ '/api/super-tabs/${Uri.encodeComponent(ask.tabId)}'
+ '/conversations/${Uri.encodeComponent(conversation)}'
+ '/messages/${Uri.encodeComponent(message)}'
+ '/attachments/${Uri.encodeComponent(file.id)}',
+ )
+ // A request that never answers ends as a failure to retry,
+ // never a tap that waits for good.
+ .timeout(swarmFileLinkTimeout),
+ )['attachment'],
+ ),
+ );
+ } on SuperTabFailure catch (failure) {
+ throw SwarmFileUnavailable(gone: _gone(failure));
+ } catch (_) {
+ throw const SwarmFileUnavailable(gone: false);
+ }
+ final link = next.id == file.id && next.expiresAt != null
+ ? swarmSignedLink(next.link)
+ : null;
+ // Not signed this time either: another try may sign it.
+ if (link == null) throw SwarmFileUnavailable(gone: !next.unlinked);
+ _latest = next;
+ return link;
+ }
+
+ /// Gone: the reader may no longer read it. Signing that failed for a
+ /// reader who may (503 FILE_LINK_UNAVAILABLE) is worth another try.
+ static bool _gone(SuperTabFailure failure) =>
+ failure.code != 'FILE_LINK_UNAVAILABLE' &&
+ (const {401, 403, 404}.contains(failure.statusCode) ||
+ const {
+ 'FILE_UNAVAILABLE',
+ 'NOT_FOUND',
+ 'FORBIDDEN',
+ 'SIGNED_OUT',
+ 'ACCOUNT_CHANGED',
+ }.contains(failure.code));
+}
+
+/// [ref]'s link for a widget at [context]: null, with [onProblem] told why,
+/// when none could be had; null and silent when the widget went, the chat's
+/// reader changed, or it was read again from nothing or lost access while the
+/// link was asked for, so a late answer opens nothing.
+Future<Uri?> swarmOpenableLink(
+ BuildContext context,
+ SwarmFileRef ref, {
+ bool stale = false,
+ void Function(String message)? onProblem,
+}) async {
+ final reader = SwarmFiles.peek(context) ?? ref.reader;
+ final generation = reader?.generation;
+ bool still() =>
+ context.mounted &&
+ identical(SwarmFiles.peek(context) ?? ref.reader, reader) &&
+ reader?.generation == generation &&
+ reader?.accessRevoked != true;
+ try {
+ final link = await ref.link(stale: stale);
+ return still() ? link : null;
+ } on SwarmFileUnavailable catch (problem) {
+ if (still()) onProblem?.call(problem.message);
+ return null;
+ }
+}
diff --git a/mobile/lib/swarm/swarm_message_row.dart b/mobile/lib/swarm/swarm_message_row.dart
index f5c81d82f..7b6932ef7 100644
--- a/mobile/lib/swarm/swarm_message_row.dart
+++ b/mobile/lib/swarm/swarm_message_row.dart
@@ -11,30 +11,15 @@ import 'package:harness_mobile/shared/theme/app_ink.dart';
import 'package:harness_mobile/shared/theme/app_space.dart';
import 'package:harness_mobile/shared/theme/app_theme.dart';
+export 'swarm_file_links.dart' show swarmAttachmentHost, swarmAttachmentLink;
+
import 'model.dart';
import 'swarm_attachment.dart';
import 'swarm_code_block.dart';
+import 'swarm_file_links.dart';
import 'swarm_face.dart';
import 'swarm_ship_row.dart';
-/// The host the backend serves message files from: a link anywhere else is
-/// not opened (the desktop's `attachmentLink`).
-const swarmAttachmentHost = String.fromEnvironment(
- 'SUPER_TAB_ATTACHMENTS_HOST',
- defaultValue: 'cdn.harness.autonomous.ai',
-);
-
-/// [url] as a link to open, or null unless it is https on
-/// [swarmAttachmentHost]: a message's url is whatever its sender's row says.
-Uri? swarmAttachmentLink(String url) {
- final link = Uri.tryParse(url);
- return link != null &&
- link.scheme == 'https' &&
- link.host == swarmAttachmentHost
- ? link
- : null;
-}
-
/// The chat's prose in the app's own face, as prototype v2 draws it: the
/// messages, quotes, names and the field. Times, roles and day dividers stay
/// in the terminal's.
@@ -370,6 +355,24 @@ class SwarmMessageRow extends StatelessWidget {
/// Opens its long-press sheet: reactions, Copy link to message.
final VoidCallback? onLongPress;
+ /// One of the message's files, with what asks for a fresh link to it
+ /// (swarm_file_links.dart): the chat's Swarm, this message and the file.
+ Widget _attachment(BuildContext context, SuperTabAttachment file) {
+ final ref = SwarmFileRef(
+ file,
+ conversationId: message.conversationId,
+ messageId: message.id,
+ reader: SwarmFiles.of(context),
+ );
+ final link = ref.current;
+ return SwarmAttachment(
+ file: file,
+ link: link,
+ fileRef: ref,
+ chip: SwarmFileChip(file: file, link: link, fileRef: ref),
+ );
+ }
+
@override
Widget build(BuildContext context) {
// A ship line is a card, not a message from its author.
@@ -442,15 +445,7 @@ class SwarmMessageRow extends StatelessWidget {
),
),
),
- for (final file in message.attachments)
- SwarmAttachment(
- file: file,
- link: swarmAttachmentLink(file.url),
- chip: SwarmFileChip(
- file: file,
- link: swarmAttachmentLink(file.url),
- ),
- ),
+ for (final file in message.attachments) _attachment(context, file),
?card,
?reactions,
?thread,
@@ -793,32 +788,79 @@ String swarmFileSize(int bytes) {
return kb < 1024 ? '$kb KB' : '${(kb / 1024).toStringAsFixed(1)} MB';
}
+/// Opens a chat file's link; tests replace it.
+@visibleForTesting
+Future<bool> Function(Uri url) swarmOpenFile = (url) =>
+ launchUrl(url, mode: LaunchMode.inAppBrowserView);
+
/// A message's file: opened in the browser when its link is the backend's.
-class SwarmFileChip extends StatelessWidget {
- const SwarmFileChip({super.key, required this.file, required this.link});
+/// With [fileRef], a link about to end is asked for again first
+/// (swarm_file_links.dart); a reader who lost access is told so, and a
+/// failed ask can be tapped again.
+class SwarmFileChip extends StatefulWidget {
+ const SwarmFileChip({
+ super.key,
+ required this.file,
+ required this.link,
+ this.fileRef,
+ });
final SuperTabAttachment file;
final Uri? link;
+ final SwarmFileRef? fileRef;
+
+ @override
+ State<SwarmFileChip> createState() => _SwarmFileChipState();
+}
+
+class _SwarmFileChipState extends State<SwarmFileChip> {
+ /// A fresh link is being asked for: another tap waits for it.
+ bool _opening = false;
+
+ bool get _opens =>
+ widget.fileRef != null ? !widget.fileRef!.refused : widget.link != null;
+
+ Future<void> _open() async {
+ final ref = widget.fileRef;
+ if (ref == null) {
+ if (widget.link case final url?) unawaited(swarmOpenFile(url));
+ return;
+ }
+ if (_opening) return;
+ setState(() => _opening = true);
+ final messenger = ScaffoldMessenger.maybeOf(context);
+ final Uri? found;
+ try {
+ found = await swarmOpenableLink(
+ context,
+ ref,
+ onProblem: (text) =>
+ messenger?.showSnackBar(SnackBar(content: Text(text))),
+ );
+ } finally {
+ if (mounted) setState(() => _opening = false);
+ }
+ final url = found;
+ if (url != null && mounted) unawaited(swarmOpenFile(url));
+ }
@override
Widget build(BuildContext context) {
+ final file = widget.file;
final tty = Tty.of(context);
final size = swarmFileSize(file.size);
- final url = link;
- final color = url == null ? AppPalette.textFaint : tty.blue;
+ final opens = _opens;
+ final color = !opens ? AppPalette.textFaint : tty.blue;
return Padding(
padding: const EdgeInsets.only(top: AppSpace.s),
child: Semantics(
- button: url != null,
+ button: opens,
label: 'File: ${file.name}, $size',
excludeSemantics: true,
child: GestureDetector(
+ key: ValueKey('swarm-file-chip-${file.id}'),
behavior: HitTestBehavior.opaque,
- onTap: url == null
- ? null
- : () => unawaited(
- launchUrl(url, mode: LaunchMode.inAppBrowserView),
- ),
+ onTap: !opens || _opening ? null : () => unawaited(_open()),
child: Container(
constraints: const BoxConstraints(minHeight: 40),
padding: const EdgeInsets.symmetric(horizontal: AppSpace.m),
diff --git a/mobile/test/swarm/swarm_file_links_test.dart b/mobile/test/swarm/swarm_file_links_test.dart
new file mode 100644
index 000000000..68e91d5c8
--- /dev/null
+++ b/mobile/test/swarm/swarm_file_links_test.dart
@@ -0,0 +1,500 @@
+import 'dart:async';
+import 'dart:io';
+
+import 'package:flutter/material.dart';
+import 'package:flutter_cache_manager/flutter_cache_manager.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/swarm/model.dart';
+import 'package:harness_mobile/swarm/swarm_attachment.dart';
+import 'package:harness_mobile/swarm/swarm_attachment_store.dart';
+import 'package:harness_mobile/swarm/swarm_file_links.dart';
+import 'package:harness_mobile/swarm/swarm_message_row.dart';
+import 'package:harness_mobile/swarm/swarm_reader.dart';
+import 'package:harness_mobile/swarm/swarm_request.dart';
+
+import 'swarm_attachment_fakes.dart';
+
+/// Chat files open through short-lived signed links (backend fileLinks.ts):
+/// one about to end is asked for again before anything opens, a reader who
+/// lost access is told so and opens nothing, a failed ask can be tapped
+/// again, an answer that lands after its chat went opens nothing, and a
+/// message's stored address is used only when it carries no signed link.
+final _now = DateTime.utc(2026, 10, 31, 18);
+final _ms = _now.millisecondsSinceEpoch;
+
+String _signed(String id, {String sig = '1'}) =>
+ 'https://storage.googleapis.com/bucket/super-tabs/tab/$id/f.pdf'
+ '?X-Goog-Signature=$sig';
+String _cdn(String id) =>
+ 'https://$swarmAttachmentHost/super-tabs/tab/$id/f.pdf';
+
+Map<String, dynamic> _row(
+ String id, {
+ String? url,
+ String? link,
+ bool unsigned = false,
+ int? expiresAt,
+ String mime = 'application/pdf',
+ String name = 'f.pdf',
+}) => {
+ 'id': id,
+ 'name': name,
+ 'mime': mime,
+ 'size': 2048,
+ 'url': url ?? _cdn(id),
+ if (!unsigned) 'link': link ?? _signed(id),
+ 'expiresAt': ?expiresAt,
+};
+
+/// A backend that answers fresh-link reads by [answer], recording each path.
+class _Backend {
+ final reads = <String>[];
+ FutureOr<Map<String, dynamic>> Function(String id)? answer;
+
+ Future<Object?> request(
+ String method,
+ String path, {
+ Map<String, dynamic>? body,
+ }) async {
+ reads.add(path);
+ final reply = answer;
+ if (reply == null) {
+ throw const SuperTabFailure(
+ 'FILE_UNAVAILABLE',
+ 'This file is no longer available.',
+ statusCode: 404,
+ );
+ }
+ return {'attachment': await reply(Uri.parse(path).pathSegments.last)};
+ }
+}
+
+void main() {
+ late List<Uri> opened;
+ late _Backend backend;
+ late SwarmReader reader;
+ late Future<bool> Function(Uri) openFile;
+
+ setUp(() {
+ opened = [];
+ backend = _Backend();
+ reader = SwarmReader(
+ tabId: 'tab',
+ accountId: 'me',
+ request: backend.request,
+ );
+ openFile = swarmOpenFile;
+ swarmOpenFile = (url) async {
+ opened.add(url);
+ return true;
+ };
+ swarmFileClock = () => _now;
+ });
+ tearDown(() {
+ swarmOpenFile = openFile;
+ swarmFileClock = DateTime.now;
+ reader.dispose();
+ });
+
+ Finder chip(String id) => find.byKey(ValueKey('swarm-file-chip-$id'));
+
+ Widget chat(SwarmReader reader, Map<String, dynamic> row) {
+ final file = SuperTabAttachment.fromJson(row);
+ final ref = SwarmFileRef(
+ file,
+ conversationId: 'lead',
+ messageId: 'm1',
+ reader: reader,
+ );
+ return MaterialApp(
+ home: Scaffold(
+ body: SwarmFiles(
+ reader: reader,
+ child: SwarmFileChip(file: file, link: ref.current, fileRef: ref),
+ ),
+ ),
+ );
+ }
+
+ testWidgets('a link with more than a minute left opens as it is', (
+ tester,
+ ) async {
+ await tester.pumpWidget(chat(reader, _row('a1', expiresAt: _ms + 300000)));
+ await tester.tap(chip('a1'));
+ await tester.pumpAndSettle();
+ expect(opened, [Uri.parse(_signed('a1'))]);
+ expect(backend.reads, isEmpty);
+ });
+
+ testWidgets('a link about to end is asked for again, then opened', (
+ tester,
+ ) async {
+ backend.answer = (id) => _row(
+ id,
+ link: _signed(id, sig: 'fresh'),
+ expiresAt: _ms + 600000,
+ );
+ await tester.pumpWidget(chat(reader, _row('a1', expiresAt: _ms + 30000)));
+ await tester.tap(chip('a1'));
+ await tester.pumpAndSettle();
+ expect(backend.reads, [
+ '/api/super-tabs/tab/conversations/lead/messages/m1/attachments/a1',
+ ]);
+ expect(opened, [Uri.parse(_signed('a1', sig: 'fresh'))]);
+ });
+
+ testWidgets('with no signed link, the stored address opens as before', (
+ tester,
+ ) async {
+ await tester.pumpWidget(chat(reader, _row('a1', unsigned: true)));
+ await tester.tap(chip('a1'));
+ await tester.pumpAndSettle();
+ expect(opened, [Uri.parse(_cdn('a1'))]);
+ expect(backend.reads, isEmpty);
+ });
+
+ testWidgets('access lost: no longer available, nothing opened', (
+ tester,
+ ) async {
+ await tester.pumpWidget(chat(reader, _row('a1', expiresAt: _ms - 1)));
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ await tester.pump();
+ expect(find.text(swarmFileGone), findsOneWidget);
+ expect(backend.reads, hasLength(1));
+ expect(opened, isEmpty);
+ });
+
+ testWidgets('a fresh answer with no signed link never falls back to the '
+ 'stored address', (tester) async {
+ backend.answer = (id) => _row(id, unsigned: true);
+ await tester.pumpWidget(chat(reader, _row('a1', expiresAt: _ms - 1)));
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ await tester.pump();
+ expect(find.text(swarmFileGone), findsOneWidget);
+ expect(opened, isEmpty);
+ });
+
+ testWidgets('a failed ask says try again, and the next tap works', (
+ tester,
+ ) async {
+ backend.answer = (_) => throw StateError('Offline');
+ await tester.pumpWidget(chat(reader, _row('a1', expiresAt: _ms - 1)));
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ await tester.pump();
+ expect(find.text(swarmFileRetry), findsOneWidget);
+ expect(opened, isEmpty);
+
+ backend.answer = (id) => _row(
+ id,
+ link: _signed(id, sig: 'again'),
+ expiresAt: _ms + 600000,
+ );
+ ScaffoldMessenger.of(tester.element(chip('a1'))).clearSnackBars();
+ await tester.pumpAndSettle();
+ await tester.tap(chip('a1'));
+ await tester.pumpAndSettle();
+ expect(backend.reads, hasLength(2));
+ expect(opened, [Uri.parse(_signed('a1', sig: 'again'))]);
+ });
+
+ testWidgets('an answer after the chat closed opens nothing', (tester) async {
+ final answer = Completer<Map<String, dynamic>>();
+ backend.answer = (_) => answer.future;
+ await tester.pumpWidget(chat(reader, _row('a1', expiresAt: _ms - 1)));
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ await tester.pumpWidget(const SizedBox());
+ answer.complete(_row('a1', expiresAt: _ms + 600000));
+ await tester.pumpAndSettle();
+ expect(opened, isEmpty);
+ expect(tester.takeException(), isNull);
+ });
+
+ testWidgets('an answer after the Swarm was read again from nothing, or '
+ 'access ended, opens nothing', (tester) async {
+ for (final change in <void Function()>[
+ () => reader.generation++,
+ () => reader.accessRevoked = true,
+ ]) {
+ opened.clear();
+ reader.accessRevoked = false;
+ final answer = Completer<Map<String, dynamic>>();
+ backend.answer = (_) => answer.future;
+ await tester.pumpWidget(chat(reader, _row('a1', expiresAt: _ms - 1)));
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ change();
+ answer.complete(_row('a1', expiresAt: _ms + 600000));
+ await tester.pumpAndSettle();
+ expect(opened, isEmpty);
+ expect(find.text(swarmFileGone), findsNothing);
+ expect(find.text(swarmFileRetry), findsNothing);
+ await tester.pumpWidget(const SizedBox());
+ }
+ });
+
+ testWidgets('an answer after another Swarm took the chat opens nothing', (
+ tester,
+ ) async {
+ final answer = Completer<Map<String, dynamic>>();
+ backend.answer = (_) => answer.future;
+ await tester.pumpWidget(chat(reader, _row('a1', expiresAt: _ms - 1)));
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ final other = SwarmReader(
+ tabId: 'tab',
+ accountId: 'someone-else',
+ request: backend.request,
+ );
+ addTearDown(other.dispose);
+ await tester.pumpWidget(chat(other, _row('a1', expiresAt: _ms - 1)));
+ answer.complete(_row('a1', expiresAt: _ms + 600000));
+ await tester.pumpAndSettle();
+ expect(opened, isEmpty);
+ });
+
+ testWidgets('a file the server could not sign never opens its stored '
+ 'address: a fresh link is asked for', (tester) async {
+ backend.answer = (id) => _row(id, expiresAt: _ms + 600000);
+ await tester.pumpWidget(
+ chat(reader, {..._row('a1', unsigned: true), 'unlinked': true}),
+ );
+ await tester.tap(chip('a1'));
+ await tester.pumpAndSettle();
+ expect(backend.reads, hasLength(1));
+ expect(opened, [Uri.parse(_signed('a1'))]);
+ });
+
+ testWidgets('a signing failure says try again, not gone', (tester) async {
+ backend.answer = (_) => throw const SuperTabFailure(
+ 'FILE_LINK_UNAVAILABLE',
+ 'Couldn’t open the file just now. Try again.',
+ statusCode: 503,
+ );
+ await tester.pumpWidget(
+ chat(reader, {..._row('a1', unsigned: true), 'unlinked': true}),
+ );
+ await tester.tap(chip('a1'));
+ await tester.pump();
+ await tester.pump();
+ expect(find.text(swarmFileRetry), findsOneWidget);
+ expect(find.text(swarmFileGone), findsNothing);
+ expect(opened, isEmpty);
+ });
+
+ testWidgets('a fresh link that never answers ends as try again', (
+ tester,
+ ) async {
+ final timeout = swarmFileLinkTimeout;
+ swarmFileLinkTimeout = const Duration(seconds: 20);
+ addTearDown(() => swarmFileLinkTimeout = timeout);
+ final never = Completer<Map<String, dynamic>>();
+ backend.answer = (_) => never.future;
+ await tester.pumpWidget(chat(reader, _row('a1', expiresAt: _ms - 1)));
+ await tester.tap(chip('a1'));
+ await tester.pump(const Duration(seconds: 21));
+ await tester.pump();
+ expect(find.text(swarmFileRetry), findsOneWidget);
+ expect(opened, isEmpty);
+ // The chip takes a tap again.
+ backend.answer = (id) => _row(id, expiresAt: _ms + 600000);
+ ScaffoldMessenger.of(tester.element(chip('a1'))).clearSnackBars();
+ await tester.pumpAndSettle();
+ await tester.tap(chip('a1'));
+ await tester.pumpAndSettle();
+ expect(opened, [Uri.parse(_signed('a1'))]);
+ });
+
+ test('a link or expiry of another type reads as none, not a failed row', () {
+ final file = SuperTabAttachment.fromJson({
+ ..._row('a1'),
+ 'link': 42,
+ 'expiresAt': '1793466886541',
+ });
+ expect(file.link, isNull);
+ expect(file.expiresAt, isNull);
+ final signed = SuperTabAttachment.fromJson({
+ ..._row('a2'),
+ 'expiresAt': 'soon',
+ });
+ // A signed link with no readable end is asked for again, never trusted.
+ expect(SwarmFileRef(signed).current, isNull);
+ });
+
+ testWidgets('a link off file storage opens nothing and asks nothing', (
+ tester,
+ ) async {
+ backend.answer = (id) => _row(id, expiresAt: _ms + 600000);
+ for (final bad in [
+ 'http://storage.googleapis.com/bucket/a1/f.pdf',
+ 'https://storage.googleapis.com.evil.example/a1/f.pdf',
+ 'https://me:[email protected]/bucket/a1/f.pdf',
+ ]) {
+ await tester.pumpWidget(
+ chat(reader, _row('a1', link: bad, expiresAt: _ms + 600000)),
+ );
+ await tester.tap(chip('a1'), warnIfMissed: false);
+ await tester.pumpAndSettle();
+ expect(opened, isEmpty, reason: bad);
+ }
+ expect(backend.reads, isEmpty);
+ });
+
+ group('a picture', () {
+ late Directory root;
+ late _Storage storage;
+ late SwarmAttachmentStore store;
+
+ setUp(() {
+ root = Directory.systemTemp.createTempSync('swarm-file-links-');
+ storage = _Storage();
+ store = storeIn(root, storage);
+ });
+ tearDown(() {
+ if (root.existsSync()) root.deleteSync(recursive: true);
+ });
+
+ final preview = find.byKey(const ValueKey('swarm-attachment-preview-p1'));
+ final chipText = find.text('chip');
+
+ Future<void> show(WidgetTester tester, Map<String, dynamic> row) async {
+ await tester.runAsync(() => store.keepOnly('me'));
+ final file = SuperTabAttachment.fromJson(row);
+ final ref = SwarmFileRef(
+ file,
+ conversationId: 'lead',
+ messageId: 'm1',
+ reader: reader,
+ );
+ await tester.pumpWidget(
+ MaterialApp(
+ home: Scaffold(
+ body: SwarmFiles(
+ reader: reader,
+ child: SwarmAttachment(
+ file: file,
+ link: ref.current,
+ fileRef: ref,
+ chip: const Text('chip'),
+ store: store,
+ ),
+ ),
+ ),
+ ),
+ );
+ }
+
+ Future<void> settle(WidgetTester tester) async {
+ for (var i = 0; i < 30; i++) {
+ await tester.runAsync(
+ () => Future<void>.delayed(const Duration(milliseconds: 10)),
+ );
+ await tester.pump();
+ }
+ }
+
+ Future<void> closed(WidgetTester tester) async {
+ await tester.pumpWidget(const SizedBox());
+ await tester.pump(const Duration(minutes: 1));
+ }
+
+ testWidgets('whose link ended is fetched by a fresh one', (tester) async {
+ backend.answer = (id) => _row(
+ id,
+ name: 'p.png',
+ mime: 'image/png',
+ link: _signed(id, sig: 'fresh'),
+ expiresAt: _ms + 600000,
+ );
+ await show(
+ tester,
+ _row('p1', name: 'p.png', mime: 'image/png', expiresAt: _ms - 1),
+ );
+ await settle(tester);
+ expect(backend.reads, hasLength(1));
+ expect(storage.asked.map((a) => a.url), [_signed('p1', sig: 'fresh')]);
+ expect(preview, findsOneWidget);
+ expect(chipText, findsNothing);
+ await closed(tester);
+ });
+
+ testWidgets('that fails on its link is asked for once more, then is the '
+ 'chip', (tester) async {
+ storage.failing = true;
+ backend.answer = (id) => _row(
+ id,
+ name: 'p.png',
+ mime: 'image/png',
+ link: _signed(id, sig: 'two'),
+ expiresAt: _ms + 600000,
+ );
+ await show(
+ tester,
+ _row('p1', name: 'p.png', mime: 'image/png', expiresAt: _ms + 600000),
+ );
+ await settle(tester);
+ expect(storage.asked.map((a) => a.url), [
+ _signed('p1'),
+ _signed('p1', sig: 'two'),
+ ]);
+ expect(backend.reads, hasLength(1));
+ expect(chipText, findsOneWidget);
+ await closed(tester);
+ });
+
+ testWidgets('a signed link is kept by its file, not its address: a new '
+ 'link to the same picture fetches nothing again', (tester) async {
+ await show(
+ tester,
+ _row('p1', name: 'p.png', mime: 'image/png', expiresAt: _ms + 600000),
+ );
+ await settle(tester);
+ expect(preview, findsOneWidget);
+ // The next read hands a new link to the same file.
+ await show(
+ tester,
+ _row(
+ 'p1',
+ name: 'p.png',
+ mime: 'image/png',
+ link: _signed('p1', sig: 'next'),
+ expiresAt: _ms + 900000,
+ ),
+ );
+ await settle(tester);
+ expect(storage.asked.map((a) => a.url), [_signed('p1')]);
+ expect(preview, findsOneWidget);
+ await closed(tester);
+ });
+
+ testWidgets('one the server could not sign is fetched by a fresh link', (
+ tester,
+ ) async {
+ backend.answer = (id) =>
+ _row(id, name: 'p.png', mime: 'image/png', expiresAt: _ms + 600000);
+ await show(tester, {
+ ..._row('p1', name: 'p.png', mime: 'image/png', unsigned: true),
+ 'unlinked': true,
+ });
+ await settle(tester);
+ expect(backend.reads, hasLength(1));
+ expect(storage.asked.map((a) => a.url), [_signed('p1')]);
+ await closed(tester);
+ });
+ });
+}
+
+/// File storage that refuses every link while [failing], as Cloud Storage
+/// answers a signed link that has ended.
+class _Storage extends FakeFileStorage {
+ bool failing = false;
+
+ @override
+ Future<FileServiceResponse> get(String url, {Map<String, String>? headers}) {
+ status = failing ? 403 : 200;
+ return super.get(url, headers: headers);
+ }
+}