mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
fix(server): unicode-safe content-disposition for gpx, ics, feed and wallet downloads (#2165)
This commit is contained in:
@@ -998,6 +998,21 @@ describe('Tool: export_trip_gpx', () => {
|
||||
});
|
||||
});
|
||||
|
||||
// Same gpxFilename as the REST route (#2165): the title's script survives in
|
||||
// the filename now — the header folding is the controller's job, not ours.
|
||||
it('keeps a non-ASCII trip title in the filename', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id, { title: '沖縄 4泊5日' });
|
||||
createPlace(testDb, trip.id, { name: '首里城', lat: 26.217, lng: 127.719 });
|
||||
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({ name: 'export_trip_gpx', arguments: { tripId: trip.id } });
|
||||
const data = parseToolResult(result) as { gpx: string; filename: string };
|
||||
expect(data.filename).toBe('沖縄-4泊5日.gpx');
|
||||
expect(data.gpx).toContain('首里城');
|
||||
});
|
||||
});
|
||||
|
||||
it('writes each planned day as a route through its stops in order', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id, { title: 'Paris Weekend' });
|
||||
|
||||
@@ -491,6 +491,18 @@ describe('Tool: export_trip_ics', () => {
|
||||
expect(result.isError).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// Same calendar filename as the REST route (#2165): U+3000 used to slip
|
||||
// through the \s keep-class and crash the header there; it folds to _ now.
|
||||
it('folds header-hostile whitespace out of the filename', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id, { title: '沖縄 4泊5日', start_date: '2025-06-01', end_date: '2025-06-05' });
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({ name: 'export_trip_ics', arguments: { tripId: trip.id } });
|
||||
const data = parseToolResult(result) as { filename: string };
|
||||
expect(data.filename).toBe('___4_5_.ics');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -233,6 +233,22 @@ describe('exportICS', () => {
|
||||
expect(filename).toMatch(/My.Trip.2025\.ics/);
|
||||
});
|
||||
|
||||
// JS \s admitted U+3000 (and \v/\f) into the filename, and Node's header
|
||||
// validation then threw ERR_INVALID_CHAR on the export route (#2165).
|
||||
it('TRIP-SVC-008b: ideographic whitespace in the title folds to _ in the filename', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id, { title: '沖縄 4泊5日' });
|
||||
|
||||
expect(svc.exportICS(trip.id).filename).toBe('___4_5_.ics');
|
||||
});
|
||||
|
||||
it('TRIP-SVC-008c: vertical tab and form feed fold too', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id, { title: 'A\vB\fC' });
|
||||
|
||||
expect(svc.exportICS(trip.id).filename).toBe('A_B_C.ics');
|
||||
});
|
||||
|
||||
it('TRIP-SVC-009: reservation with end time includes DTEND', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id, { title: 'Paris Trip' });
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { contentDisposition } from '../../../src/nest/common/content-disposition';
|
||||
|
||||
// Node refuses header values with codepoints above 0xFF (ERR_INVALID_CHAR), so
|
||||
// the builder must hand back printable ASCII no matter what the name holds —
|
||||
// and stay byte-identical to the old hand-built header for plain ASCII names.
|
||||
describe('contentDisposition (#2165)', () => {
|
||||
it('keeps a plain ASCII name byte-identical to the hand-built header', () => {
|
||||
expect(contentDisposition('Alpine-week.gpx', 'attachment')).toBe('attachment; filename="Alpine-week.gpx"');
|
||||
expect(contentDisposition('all-trips.ics', 'inline')).toBe('inline; filename="all-trips.ics"');
|
||||
// Spaces are fine inside a quoted-string; no reason to grow a filename*.
|
||||
expect(contentDisposition('My Trip 2025.ics', 'attachment')).toBe('attachment; filename="My Trip 2025.ics"');
|
||||
expect(contentDisposition('', 'attachment')).toBe('attachment; filename=""');
|
||||
});
|
||||
|
||||
it('adds filename* only when the quoted fallback loses something', () => {
|
||||
expect(contentDisposition('沖縄-4泊5日.gpx', 'attachment')).toBe(
|
||||
'attachment; filename="__-4_5_.gpx"; filename*=UTF-8\'\'%E6%B2%96%E7%B8%84-4%E6%B3%8A5%E6%97%A5.gpx',
|
||||
);
|
||||
});
|
||||
|
||||
it('folds an accent in both its composed and combining forms', () => {
|
||||
expect(contentDisposition('Café.gpx', 'attachment')).toBe(
|
||||
'attachment; filename="Caf_.gpx"; filename*=UTF-8\'\'Caf%C3%A9.gpx',
|
||||
);
|
||||
// NFKD output: 'e' + U+0301 — the combining mark alone is already > 0xFF.
|
||||
expect(contentDisposition('Café.gpx', 'attachment')).toBe(
|
||||
'attachment; filename="Cafe_.gpx"; filename*=UTF-8\'\'Cafe%CC%81.gpx',
|
||||
);
|
||||
});
|
||||
|
||||
it('neutralizes CR/LF and quotes — nothing escapes the quoted-string', () => {
|
||||
expect(contentDisposition('evil\r\nContent-Length: 0"; x="y.gpx', 'attachment')).toBe(
|
||||
'attachment; filename="evil Content-Length: 0_; x=_y.gpx"; filename*=UTF-8\'\'evil%20%20Content-Length%3A%200%22%3B%20x%3D%22y.gpx',
|
||||
);
|
||||
});
|
||||
|
||||
it('folds a tab to a space instead of riding it into the header', () => {
|
||||
expect(contentDisposition('a\tb.ics', 'attachment')).toBe(
|
||||
'attachment; filename="a b.ics"; filename*=UTF-8\'\'a%20b.ics',
|
||||
);
|
||||
});
|
||||
|
||||
it('catches the JS-\\s stragglers like U+3000', () => {
|
||||
expect(contentDisposition('a b.ics', 'inline')).toBe(
|
||||
'inline; filename="a_b.ics"; filename*=UTF-8\'\'a%E3%80%80b.ics',
|
||||
);
|
||||
});
|
||||
|
||||
it("escapes the RFC 5987 stragglers encodeURIComponent leaves bare: ' ( ) *", () => {
|
||||
expect(contentDisposition("日(1)'*.gpx", 'attachment')).toBe(
|
||||
'attachment; filename="_(1)\'*.gpx"; filename*=UTF-8\'\'%E6%97%A5%281%29%27%2A.gpx',
|
||||
);
|
||||
});
|
||||
|
||||
it('falls back to download when the folding erased everything readable, keeping the extension', () => {
|
||||
expect(contentDisposition('日本語.gpx', 'attachment')).toBe(
|
||||
'attachment; filename="download.gpx"; filename*=UTF-8\'\'%E6%97%A5%E6%9C%AC%E8%AA%9E.gpx',
|
||||
);
|
||||
expect(contentDisposition('日本語', 'attachment')).toBe(
|
||||
'attachment; filename="download"; filename*=UTF-8\'\'%E6%97%A5%E6%9C%AC%E8%AA%9E',
|
||||
);
|
||||
});
|
||||
|
||||
it('carries an emoji through filename* and survives a name cut mid-emoji', () => {
|
||||
expect(contentDisposition('\u{1F5FE}.gpx', 'attachment')).toBe(
|
||||
'attachment; filename="download.gpx"; filename*=UTF-8\'\'%F0%9F%97%BE.gpx',
|
||||
);
|
||||
// A lone surrogate would make encodeURIComponent throw "URI malformed" —
|
||||
// exactly the crash class this helper exists to end.
|
||||
expect(contentDisposition('\uD83D.gpx', 'attachment')).toBe(
|
||||
'attachment; filename="download.gpx"; filename*=UTF-8\'\'%EF%BF%BD.gpx',
|
||||
);
|
||||
});
|
||||
|
||||
it('always yields a value Node accepts: printable ASCII, whatever comes in', () => {
|
||||
const nasty = [
|
||||
'沖縄 4泊5日.gpx',
|
||||
'a
b
c.ics',
|
||||
'"\\.pkpass',
|
||||
'\x00\x1f\x7f.gpx',
|
||||
'\u{1F9ED}'.repeat(40),
|
||||
'plain.txt',
|
||||
'',
|
||||
];
|
||||
for (const name of nasty) {
|
||||
expect(contentDisposition(name, 'attachment')).toMatch(/^[\x20-\x7e]+$/);
|
||||
expect(contentDisposition(name, 'inline')).toMatch(/^[\x20-\x7e]+$/);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -279,6 +279,19 @@ describe('FilesDownloadController', () => {
|
||||
disposition: 'inline; filename="pass.pkpass"',
|
||||
});
|
||||
});
|
||||
|
||||
// Uploads store original_name raw, so a Japanese boarding pass used to hand
|
||||
// setHeader a value it throws on (#2165) — storage.service.ts documents the
|
||||
// throw as reachable; now the header is ASCII with the real name in filename*.
|
||||
it('folds a non-ASCII original name to ASCII + filename* instead of crashing setHeader (#2165)', async () => {
|
||||
const storage = dstor();
|
||||
const s = dsvc({ getFileById: vi.fn().mockReturnValue({ filename: 'pass.pkpass', original_name: '搭乗券.pkpass' }) });
|
||||
await new FilesDownloadController(s, storage).download(req, res, '5', '9');
|
||||
expect(storage.sendToResponse).toHaveBeenCalledWith('files', 'pass.pkpass', res, {
|
||||
contentType: 'application/vnd.apple.pkpass',
|
||||
disposition: 'inline; filename="download.pkpass"; filename*=UTF-8\'\'%E6%90%AD%E4%B9%97%E5%88%B8.pkpass',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('PhotosController', () => {
|
||||
|
||||
@@ -174,4 +174,17 @@ describe('gpxFilename', () => {
|
||||
expect(gpxFilename('///')).toBe('trip.gpx');
|
||||
expect(gpxFilename('')).toBe('trip.gpx');
|
||||
});
|
||||
|
||||
// Header safety moved into contentDisposition() (#2165) — the filename may
|
||||
// keep the title's own script instead of tripping setHeader's Latin-1 check.
|
||||
it('keeps a non-ASCII title as typed', () => {
|
||||
expect(gpxFilename('沖縄 4泊5日')).toBe('沖縄-4泊5日.gpx');
|
||||
// No NFKD any more: the composed é stays composed instead of decaying into
|
||||
// 'e' + a combining mark that only looked stripped.
|
||||
expect(gpxFilename('Café')).toBe('Café.gpx');
|
||||
});
|
||||
|
||||
it('trims on codepoint boundaries, never through the middle of an emoji', () => {
|
||||
expect(gpxFilename('\u{1F5FE}'.repeat(70))).toBe('\u{1F5FE}'.repeat(60) + '.gpx');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -147,6 +147,29 @@ describe('PlacesController (parity with the legacy /api/trips/:tripId/places rou
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /export.gpx (#2165)', () => {
|
||||
const makeRes = () => ({ setHeader: vi.fn(), send: vi.fn() });
|
||||
const exportQuery = {} as never;
|
||||
|
||||
it('an ASCII filename keeps the exact legacy header', () => {
|
||||
const res = makeRes();
|
||||
const s = svc({ exportGpx: vi.fn().mockReturnValue({ gpx: '<gpx/>', filename: 'Alpine-week.gpx' }) } as Partial<PlacesService>);
|
||||
new PlacesController(s, new RuntimeEnvService(), storageStub).exportGpx(user, '5', exportQuery, res as never);
|
||||
expect(res.setHeader).toHaveBeenCalledWith('Content-Disposition', 'attachment; filename="Alpine-week.gpx"');
|
||||
expect(res.send).toHaveBeenCalledWith('<gpx/>');
|
||||
});
|
||||
|
||||
it('a non-ASCII filename no longer reaches setHeader raw: ASCII fallback + filename*', () => {
|
||||
const res = makeRes();
|
||||
const s = svc({ exportGpx: vi.fn().mockReturnValue({ gpx: '<gpx/>', filename: '沖縄-4泊5日.gpx' }) } as Partial<PlacesService>);
|
||||
new PlacesController(s, new RuntimeEnvService(), storageStub).exportGpx(user, '5', exportQuery, res as never);
|
||||
expect(res.setHeader).toHaveBeenCalledWith(
|
||||
'Content-Disposition',
|
||||
'attachment; filename="__-4_5_.gpx"; filename*=UTF-8\'\'%E6%B2%96%E7%B8%84-4%E6%B3%8A5%E6%97%A5.gpx',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /import/google-list + naver-list', () => {
|
||||
// The legacy 'URL is required' 400 is gone: placeImportListRequestSchema
|
||||
// pins `url`, so the ZodValidationPipe rejects a urlless body before the
|
||||
|
||||
@@ -386,6 +386,16 @@ describe('TripsController (parity with the legacy /api/trips route)', () => {
|
||||
expect(res.send).toHaveBeenCalledWith('BEGIN:VCALENDAR');
|
||||
});
|
||||
|
||||
it('folds a non-ASCII filename into an ASCII header with filename* instead of crashing setHeader (#2165)', () => {
|
||||
const res = { setHeader: vi.fn(), send: vi.fn() };
|
||||
const cal = { exportICS: vi.fn().mockReturnValue({ ics: 'BEGIN:VCALENDAR', filename: '沖縄.ics' }) };
|
||||
tc(svc(), cal).exportIcs(user, '9', res as never);
|
||||
expect(res.setHeader).toHaveBeenCalledWith(
|
||||
'Content-Disposition',
|
||||
'attachment; filename="download.ics"; filename*=UTF-8\'\'%E6%B2%96%E7%B8%84.ics',
|
||||
);
|
||||
});
|
||||
|
||||
it('maps a NotFoundError from the export to 404 and re-throws others', () => {
|
||||
const nf = { exportICS: vi.fn().mockImplementation(() => { throw new NotFoundError('gone'); }) };
|
||||
expect(thrown(() => tc(svc(), nf).exportIcs(user, '9', makeRes()))).toEqual({ status: 404, body: { error: 'gone' } });
|
||||
|
||||
Reference in New Issue
Block a user