diff --git a/server/routes/api/attachments/attachments.test.ts b/server/routes/api/attachments/attachments.test.ts index b52058211e..b2e331809c 100644 --- a/server/routes/api/attachments/attachments.test.ts +++ b/server/routes/api/attachments/attachments.test.ts @@ -613,6 +613,22 @@ describe("#attachments.redirect", () => { expect(res.status).toEqual(302); }); + it("should not cache the redirect for longer than the signed URL is valid", async () => { + const user = await buildUser(); + const attachment = await buildAttachment({ + teamId: user.teamId, + userId: user.id, + }); + const res = await server.post("/api/attachments.redirect", user, { + body: { + id: attachment.id, + }, + redirect: "manual", + }); + expect(res.status).toEqual(302); + expect(res.headers.get("Cache-Control")).toEqual("max-age=150, immutable"); + }); + it("should return a redirect for the attachment if id supplied via query params", async () => { const user = await buildUser(); const attachment = await buildAttachment({ diff --git a/server/routes/api/attachments/attachments.ts b/server/routes/api/attachments/attachments.ts index 0fd3543efc..13b377ccba 100644 --- a/server/routes/api/attachments/attachments.ts +++ b/server/routes/api/attachments/attachments.ts @@ -335,7 +335,7 @@ const handleAttachmentsRedirect = async ( } else { ctx.set( "Cache-Control", - `max-age=${BaseStorage.defaultSignedUrlExpires}, immutable` + `max-age=${BaseStorage.getSignedUrlMinimumLifetime()}, immutable` ); ctx.redirect(await attachment.signedUrl); } diff --git a/server/routes/api/emojis/emojis.ts b/server/routes/api/emojis/emojis.ts index 3f9c1b275e..649074341f 100644 --- a/server/routes/api/emojis/emojis.ts +++ b/server/routes/api/emojis/emojis.ts @@ -105,7 +105,7 @@ router.get( ctx.set( "Cache-Control", - `max-age=${BaseStorage.defaultSignedUrlExpires}, immutable` + `max-age=${BaseStorage.getSignedUrlMinimumLifetime()}, immutable` ); ctx.redirect(await emoji.attachment.signedUrl); } diff --git a/server/storage/files/BaseStorage.test.ts b/server/storage/files/BaseStorage.test.ts index 24fa366841..47202fa9f5 100644 --- a/server/storage/files/BaseStorage.test.ts +++ b/server/storage/files/BaseStorage.test.ts @@ -129,6 +129,43 @@ describe("BaseStorage", () => { }); }); + describe("getSignedUrlMinimumLifetime", () => { + afterEach(() => { + vi.useRealTimers(); + }); + + it("should return half of the default lifetime", () => { + expect(BaseStorage.getSignedUrlMinimumLifetime()).toBe( + BaseStorage.defaultSignedUrlExpires / 2 + ); + }); + + it("should subtract the capped window from long lifetimes", () => { + expect(BaseStorage.getSignedUrlMinimumLifetime(Hour.seconds)).toBe( + Hour.seconds - BaseStorage.maxSigningWindow + ); + }); + + it("should return the full lifetime when it is too short to align", () => { + expect(BaseStorage.getSignedUrlMinimumLifetime(1)).toBe(1); + }); + + it("should not exceed the remaining lifetime at the end of a window", () => { + const expiresIn = BaseStorage.defaultSignedUrlExpires; + const now = new Date("2026-04-16T12:07:29.999Z"); + vi.useFakeTimers(); + vi.setSystemTime(now); + + const expiresAt = + MockStorage.signingDateFor(expiresIn).getTime() + expiresIn * 1000; + const remaining = (expiresAt - now.getTime()) / 1000; + + expect( + BaseStorage.getSignedUrlMinimumLifetime(expiresIn) + ).toBeLessThanOrEqual(remaining); + }); + }); + describe("getPresignedPut", () => { it("should return undefined from default implementation", async () => { const storage = new MockStorage(); diff --git a/server/storage/files/BaseStorage.ts b/server/storage/files/BaseStorage.ts index 650d65e50c..6b0dc74b49 100644 --- a/server/storage/files/BaseStorage.ts +++ b/server/storage/files/BaseStorage.ts @@ -31,6 +31,20 @@ export default abstract class BaseStorage { */ public static maxSigningWindow = 15 * Minute.seconds; + /** + * Returns the number of seconds that a signed URL is always valid for, after + * signing time alignment. Use this as the upper limit when a response that + * points to a signed URL is cached. + * + * @param expiresIn The number of seconds until the signed URL expires. + * @returns The minimum number of seconds the signed URL is valid for. + */ + public static getSignedUrlMinimumLifetime( + expiresIn = BaseStorage.defaultSignedUrlExpires + ): number { + return expiresIn - BaseStorage.getSigningWindow(expiresIn); + } + /** * Rounds the current time down to a fixed window so that repeated signatures * of the same file produce an identical URL. Without this every signature is @@ -46,10 +60,7 @@ export default abstract class BaseStorage { * @returns The timestamp to sign with. */ protected static getSigningDate(expiresIn: number): Date { - const window = Math.min( - BaseStorage.maxSigningWindow, - Math.floor(expiresIn / 2) - ); + const window = BaseStorage.getSigningWindow(expiresIn); if (window < 1) { return new Date(); @@ -387,4 +398,15 @@ export default abstract class BaseStorage { "image/gif", "image/webp", ]; + + /** + * Returns the number of seconds that the signing timestamp is held constant + * for a signed URL with the given lifetime. + * + * @param expiresIn The number of seconds until the signed URL expires. + * @returns The window in seconds, or 0 if the lifetime is too short to align. + */ + private static getSigningWindow(expiresIn: number): number { + return Math.min(BaseStorage.maxSigningWindow, Math.floor(expiresIn / 2)); + } }