Files
openmotion/shared/http-header.ts
Dery Ferdika f4ac2ed0e1 fix(http): use one sanitised rule for download disposition headers (#623)
Download responses built the header in five places. Studio's render download and the shared studio host stripped quotes and non-ASCII from the plain parameter, but the diagnostics, integration-guide, asset, brand-kit and project-archive downloads did not, and the two sanitised sites disagreed on the delimiter set: one escaped ! alongside '()* and the other did not.

Node rejects CRLF with ERR_INVALID_CHAR, so this was never response splitting, but a bare quote passed straight through into a header value. Add shared/http-header.ts with the reasoning and route every site through it, including the render download, which keeps its portable OpenMotion<extension> plain name through the helper's override.

The override is sanitised inside the helper, so passing a plain name explicitly cannot bypass the rule. RFC 5987 attr-char technically allows !, and the shared encoder escapes the whole delimiter set anyway: percent-encoding an allowed character stays valid, and one predictable rule is easier to trust than a per-site choice.

Two sites still built their own header when this change first landed: the scenario bundle download ran a second sanitizer over the plain name and the Design Studio project download interpolated the raw name with only quotes removed. Both now use attachmentDisposition, with the scenario caller defaulting its name first, so no download response keeps a private rule that can drift from the shared one.

Covered by a unit test for the sanitiser and encoder, including an explicit override and the attr-char rationale, and by the server suite. The scenario bundle, scenario run, design project and archive suites run in CI; locally they need FFmpeg, symlink privileges and the refreshed dependency tree this checkout does not carry.

Validation:
- node --experimental-strip-types --test tests/server.test.ts
- npm run check
- node --test tests/ci-scope.test.mjs
2026-11-23 11:18:48 +08:00

21 lines
904 B
TypeScript

/**
* One download-header rule for every response that hands a file to the browser.
*
* A raw quote or backslash must never reach the header value: Node rejects CRLF with
* ERR_INVALID_CHAR, but it passes a bare quote through, so the plain parameter has to be
* sanitised while the exact name travels in the RFC 5987 extended parameter.
*/
export function encodedFilename(filename: string) {
return encodeURIComponent(filename).replace(/[!'()*]/g, character =>
'%' + character.charCodeAt(0).toString(16).toUpperCase());
}
/** Strip anything that cannot sit safely inside a quoted header value. */
export function plainFilename(value: string) {
return value.replace(/[^\x20-\x7e]|["\\]/g, '_');
}
export function attachmentDisposition(filename: string, plain: string = filename) {
return `attachment; filename="${plainFilename(plain)}"; filename*=UTF-8''${encodedFilename(filename)}`;
}