fix(journey): keep a protected link's photos out of shared caches

The photo proxies answer public, max-age, so a proxy or CDN in front of
TREK could hand a password-protected link's photo to somebody who never
unlocked it. For a link with a password every Cache-Control the proxy
sets is turned private.
This commit is contained in:
Maurice
2026-11-30 15:20:05 +01:00
parent 6ac15213d9
commit cbdc6850ca
3 changed files with 70 additions and 6 deletions
@@ -7,6 +7,8 @@ import {
checkSharePassword,
hashSharePassword,
JOURNEY_UNLOCK_COOKIE,
keepCachesPrivate,
privateCacheControl,
proofUnlocks,
shareLinkExpired,
unlockCookieOptions,
@@ -17,7 +19,7 @@ import {
} from '../../../../src/nest/journey/journey-share-access';
import { InMemoryJourneyShareViewStore } from '../../../../src/nest/journey/journey-share-views.store';
import type { Request } from 'express';
import type { Request, Response } from 'express';
import { describe, expect, it } from 'vitest';
const BASE = { cookies: {}, headers: { 'user-agent': 'Firefox' }, ip: '203.0.113.9', secure: false };
@@ -125,3 +127,32 @@ describe('InMemoryJourneyShareViewStore', () => {
expect(await store.firstToday('a', day)).toBe(true);
});
});
describe('the photo caches of a protected link', () => {
it('JSHAREACC-011: turns public into private and leaves every other value alone', () => {
expect(privateCacheControl('public, max-age=86400')).toBe('private, max-age=86400');
expect(privateCacheControl('Public, max-age=3600')).toBe('private, max-age=3600');
expect(privateCacheControl('no-store, max-age=0')).toBe('no-store, max-age=0');
expect(privateCacheControl(['public', 'max-age=60'])).toEqual(['private', 'max-age=60']);
expect(privateCacheControl(60)).toBe(60);
});
it('JSHAREACC-012: rewrites every Cache-Control the response is given afterwards, and only that header', () => {
const headers: Record<string, unknown> = {};
const res = {
setHeader(name: string, value: unknown) {
headers[name] = value;
return res;
},
} as unknown as Response;
keepCachesPrivate(res);
res.setHeader('Cache-Control', 'public, max-age=86400');
res.setHeader('cache-control', 'public, max-age=3600');
res.setHeader('Content-Type', 'image/public');
expect(headers).toEqual({
'Cache-Control': 'private, max-age=86400',
'cache-control': 'private, max-age=3600',
'Content-Type': 'image/public',
});
});
});