mirror of
https://github.com/outline/outline.git
synced 2026-11-28 00:09:58 +03:00
fix: Cap signed URL redirect caching at guaranteed signature lifetime (#14169)
This commit is contained in:
1 parent
f7d4afe26c
commit
878f41393d
5 files changed
+81
-6
No files matched your search
@@ -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({
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user