From 7fde9a311f45da54656bef229aba7d055ca86d22 Mon Sep 17 00:00:00 2001 From: trevor-signal <131492920+trevor-signal@users.noreply.github.com> Date: Thu, 25 Sep 2025 16:23:40 -0400 Subject: [PATCH] Avoid logging an error when weakly referenced attachment is missing --- app/attachment_channel.ts | 17 +++++++++++++++++ ts/util/getLocalAttachmentUrl.ts | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/app/attachment_channel.ts b/app/attachment_channel.ts index f8243cde28e..ed4bdfb92af 100644 --- a/app/attachment_channel.ts +++ b/app/attachment_channel.ts @@ -18,6 +18,7 @@ import { pipeline } from 'node:stream/promises'; import z from 'zod'; import GrowingFile from 'growing-file'; import lodash from 'lodash'; +import { pathExists } from 'fs-extra'; import { type DecryptAttachmentToSinkOptionsType, @@ -611,6 +612,22 @@ export async function handleAttachmentRequest(req: Request): Promise { return new Response('Access denied', { status: 401 }); } + // Some attachments have weak references (e.g. copied quotes) and we + // don't want to treat those attachments missing as an error + const weakReferenceParam = url.searchParams.get('weakReference'); + if (weakReferenceParam != null) { + strictAssert( + disposition === 'attachment', + 'Only attachments can have weak references' + ); + const fileExists = await pathExists(path); + if (!fileExists) { + return new Response('Weakly referenced attachment does not exist', { + status: 404, + }); + } + } + // Get attachment size to trim the padding const sizeParam = url.searchParams.get('size'); let maybeSize: number | undefined; diff --git a/ts/util/getLocalAttachmentUrl.ts b/ts/util/getLocalAttachmentUrl.ts index e446366fd9b..1493922e422 100644 --- a/ts/util/getLocalAttachmentUrl.ts +++ b/ts/util/getLocalAttachmentUrl.ts @@ -35,6 +35,7 @@ export function getLocalAttachmentUrl( | 'path' | 'size' | 'version' + | 'copied' > >, { @@ -106,5 +107,10 @@ export function getLocalAttachmentUrl( url.searchParams.set('chunkSize', attachment.chunkSize.toString()); } + // For weak references (e.g. copied quotes) don't error if path is missing + if (attachment.copied) { + url.searchParams.set('weakReference', '1'); + } + return url.toString(); }