Invites and fixed image caching
This commit is contained in:
1 parent
1706a906f0
commit
693a30f437
7 files changed
+193
-5
No files matched your search
+5
-2
@@ -1,3 +1,4 @@
|
||||
import { photoCacheControl } from './storage/photoCache.js';
|
||||
import { canUpdateWorkspaceSettings, subscriptionAllowsWrite } from './workspaceAccess.js';
|
||||
import { buildWorkspaceInviteEmail } from './emails/workspaceInviteEmail.js';
|
||||
import { exportFlock } from './repositories/flockExportRepository.js';
|
||||
@@ -3496,6 +3497,8 @@ app.post(
|
||||
);
|
||||
|
||||
app.get('/api/birds/:birdId/photo', async (req: Request, res: Response, next: NextFunction) => {
|
||||
// Invalid tokens, missing photos and upstream failures must never be cached.
|
||||
res.setHeader('Cache-Control', 'no-store');
|
||||
try {
|
||||
const token = typeof req.query.token === 'string' ? req.query.token : '';
|
||||
const photoAccess = verifyBirdPhotoAccessToken(token);
|
||||
@@ -3525,9 +3528,8 @@ app.get('/api/birds/:birdId/photo', async (req: Request, res: Response, next: Ne
|
||||
expiresInSeconds: 5 * 60,
|
||||
});
|
||||
|
||||
res.setHeader('Cache-Control', 'private, max-age=900');
|
||||
|
||||
if (photoDeliveryMode === 'redirect') {
|
||||
res.setHeader('Cache-Control', photoCacheControl('redirect', photoAccess.expiresAt));
|
||||
res.redirect(302, signedUrl);
|
||||
return;
|
||||
}
|
||||
@@ -3543,6 +3545,7 @@ app.get('/api/birds/:birdId/photo', async (req: Request, res: Response, next: Ne
|
||||
const contentLength = imageResponse.headers.get('content-length');
|
||||
const imageBuffer = Buffer.from(await imageResponse.arrayBuffer());
|
||||
|
||||
res.setHeader('Cache-Control', photoCacheControl('proxy', photoAccess.expiresAt));
|
||||
res.setHeader('Content-Type', contentType);
|
||||
|
||||
if (contentLength) {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { photoCacheControl } from './photoCache.js';
|
||||
|
||||
test('storage redirects cannot be cached beyond the signed destination lifetime', () => {
|
||||
assert.equal(photoCacheControl('redirect', 1900, 1000), 'no-store');
|
||||
});
|
||||
|
||||
test('proxy caching is bounded by the remaining app token lifetime', () => {
|
||||
assert.equal(photoCacheControl('proxy', 1900, 1000), 'private, max-age=900, must-revalidate');
|
||||
assert.equal(photoCacheControl('proxy', 1050, 1000), 'private, max-age=50, must-revalidate');
|
||||
assert.equal(photoCacheControl('proxy', 999, 1000), 'private, max-age=0, must-revalidate');
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
// Never cache a redirect to a signed storage URL: its lifetime is independent of
|
||||
// the app token. Successful proxy responses cannot outlive the app token.
|
||||
export const photoCacheControl = (mode: 'proxy' | 'redirect', expiresAt: number, now = Math.floor(Date.now() / 1000)) =>
|
||||
mode === 'redirect' ? 'no-store' : `private, max-age=${Math.max(0, Math.min(900, expiresAt - now))}, must-revalidate`;
|
||||
Reference in new issue
Block a user