Files
outline/server/storage/files/LocalStorage.test.ts
Tom MoorandClaude Opus 5 6905c9ba9c fix: Stabilize signed attachment urls so browser caches can be reused (#13462)
* fix: Stabilize signed attachment urls so caches can be reused

Every call to getSignedUrl produced a unique signature, so the same
attachment was downloaded again for each URL. On a public share the
first image was fetched twice at full size: once by the preload scanner
from the server-rendered HTML, and again when the client rendered the
document from the API response.

- Round the signing timestamp down to a window so repeated signatures of
  the same key are identical. The window is capped at half the requested
  lifetime, so a URL is always valid for at least half of expiresIn and
  never outlives the lifetime it had before.
- Apply to all three signing paths: the S3 presigner, CloudFront, and
  the JWT used by local storage.
- Sign the share page HTML with the same lifetime the API presenter
  uses, so the two agree.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test: Freeze time in signed url tests

The suites read the wall clock, so two calls could straddle a signing
window boundary and fail intermittently. Freeze the clock at a time
offset from a boundary, then advance it explicitly to cover both cases:
within a window the signature holds, across a boundary it rotates.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-16 17:35:23 -04:00

77 lines
2.4 KiB
TypeScript

import JWT from "jsonwebtoken";
import { Hour } from "@shared/utils/time";
import env from "@server/env";
import LocalStorage from "./LocalStorage";
describe("LocalStorage", () => {
describe("getSignedUrl", () => {
// A time deliberately offset from a window boundary, so that rounding is
// exercised and two calls cannot straddle a boundary.
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-04-16T12:07:31.500Z"));
});
afterEach(() => {
vi.useRealTimers();
});
it("should return an identical url for repeated calls", async () => {
const storage = new LocalStorage();
const first = await storage.getSignedUrl(
"uploads/test.png",
Hour.seconds
);
// Move on within the same window, which is what separates the server
// rendered page from the API request that follows it.
vi.setSystemTime(new Date("2026-04-16T12:08:01.500Z"));
const second = await storage.getSignedUrl(
"uploads/test.png",
Hour.seconds
);
expect(second).toBe(first);
});
it("should return a different url for a different key", async () => {
const storage = new LocalStorage();
const first = await storage.getSignedUrl("uploads/one.png", Hour.seconds);
const second = await storage.getSignedUrl(
"uploads/two.png",
Hour.seconds
);
expect(second).not.toBe(first);
});
it("should return a different url once the window has passed", async () => {
const storage = new LocalStorage();
const first = await storage.getSignedUrl(
"uploads/test.png",
Hour.seconds
);
vi.setSystemTime(new Date("2026-04-16T12:22:31.500Z"));
const second = await storage.getSignedUrl(
"uploads/test.png",
Hour.seconds
);
expect(second).not.toBe(first);
});
it("should remain valid for at least half of the requested lifetime", async () => {
const storage = new LocalStorage();
const url = await storage.getSignedUrl("uploads/test.png", Hour.seconds);
const sig = new URL(url).searchParams.get("sig");
const payload = JWT.verify(sig!, env.SECRET_KEY) as { exp: number };
const remaining = payload.exp - Math.floor(Date.now() / 1000);
expect(remaining).toBeGreaterThanOrEqual(Hour.seconds / 2);
expect(remaining).toBeLessThanOrEqual(Hour.seconds);
});
});
});