Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Export an audit trail whose envelope title has non-Latin-1 characters
njarrarfeed02c934e6
diff --git a/src/app/api/admin/audit/route.ts b/src/app/api/admin/audit/route.tsindex 144d7b6..7122097 100644--- a/src/app/api/admin/audit/route.ts+++ b/src/app/api/admin/audit/route.ts@@ -4,6 +4,7 @@ import { exportSystemAuditCsv } from "@/lib/audit-export";import { requireAdminSession } from "@/lib/auth/admin";import { logRouteError } from "@/lib/security/logging";import { getClientIpAddress, getRequestPathname } from "@/lib/security/request";+import { buildContentDisposition } from "@/lib/services/storage";import {listSystemAuditEvents,verifySystemAuditChain,@@ -44,7 +45,7 @@ export async function GET(request: Request) {return new Response(file.body, {headers: {...noStore,- "content-disposition": `attachment; filename="${file.fileName}"`,+ "content-disposition": buildContentDisposition(file.fileName),"content-type": "text/csv; charset=utf-8","x-content-type-options": "nosniff",},diff --git a/src/app/api/envelopes/[id]/audit/route.ts b/src/app/api/envelopes/[id]/audit/route.tsindex 7f4c79f..f5d1994 100644--- a/src/app/api/envelopes/[id]/audit/route.ts+++ b/src/app/api/envelopes/[id]/audit/route.ts@@ -4,6 +4,7 @@ import { AuditExportError, exportEnvelopeAuditCsv } from "@/lib/audit-export";import { requireVerifiedSenderSession } from "@/lib/auth/route-session";import { logRouteError } from "@/lib/security/logging";import { getClientIpAddress, getRequestPathname } from "@/lib/security/request";+import { buildContentDisposition } from "@/lib/services/storage";type EnvelopeAuditRouteContext = {params: Promise<{@@ -30,7 +31,9 @@ export async function GET(return new Response(file.body, {headers: {"cache-control": "private, no-store",- "content-disposition": `attachment; filename="${file.fileName}"`,+ // The name comes from the envelope's title, which may hold+ // characters a header cannot carry as they are.+ "content-disposition": buildContentDisposition(file.fileName),"content-type": "text/csv; charset=utf-8","x-content-type-options": "nosniff",},diff --git a/src/lib/download-headers.test.ts b/src/lib/download-headers.test.tsnew file mode 100644index 0000000..3a93a9c--- /dev/null+++ b/src/lib/download-headers.test.ts@@ -0,0 +1,57 @@+import assert from "node:assert/strict";+import { globSync, readFileSync } from "node:fs";+import path from "node:path";+import { test } from "node:test";++import { buildContentDisposition } from "./services/storage.ts";++// A header value may only hold Latin-1, and `new Response` throws on+// anything else. A file name taken from an envelope's title can hold any+// character, so it has to go through buildContentDisposition.++const titles = ["NDA — Acme", "Mary’s lease", "合同", "Résumé"];++test("a file name with any character gives a header a response accepts", () => {+ for (const title of titles) {+ const fileName = `${title}-audit-trail.csv`;+ const value = buildContentDisposition(fileName);+ const response = new Response("", {+ headers: { "content-disposition": value },+ });++ assert.equal(response.headers.get("content-disposition"), value);+ assert.match(value, /^attachment; filename="[\x20-\x7e]*"; filename\*=/);+ assert.ok(+ value.endsWith(`filename*=UTF-8''${encodeURIComponent(fileName)}`),+ value,+ );+ }+});++test("writing such a name into the header by hand throws", () => {+ assert.throws(+ () =>+ new Response("", {+ headers: {+ "content-disposition": `attachment; filename="合同-audit-trail.csv"`,+ },+ }),+ );+});++test("no route writes a computed file name into a header by hand", () => {+ const apiRoot = path.resolve(import.meta.dirname, "..", "app", "api");+ const routes = globSync("**/route.ts", { cwd: apiRoot });++ assert.ok(routes.length > 0, "found no route files to check");++ for (const route of routes) {+ const source = readFileSync(path.join(apiRoot, route), "utf8");++ for (const line of source.split("\n")) {+ if (/filename="\$\{/.test(line)) {+ assert.fail(`${route} builds a file name by hand: ${line.trim()}`);+ }+ }+ }+});