Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

Decode ISO-8859-1 filename* in Content-Disposition (#78)

kevin4995e051c79a
diff --git a/e2e/download.spec.ts b/e2e/download.spec.ts
index 2fc6d9f..0a33550 100644
--- a/e2e/download.spec.ts
+++ b/e2e/download.spec.ts
@@ -122,7 +122,12 @@ test.describe('happy paths @smoke', () => {
test.describe('file names @smoke', () => {
const cases: [string, string, RegExp][] = [
['path traversal is flattened', 'attachment; filename="../../evil.sh"', /^\.\._\.\._evil\.sh$/],
- ['control characters are replaced', 'attachment; filename="a%0Ab.txt"', /^a_b\.txt$/]
+ ['control characters are replaced', 'attachment; filename="a%0Ab.txt"', /^a_b\.txt$/],
+ [
+ 'an ISO-8859-1 filename* is decoded',
+ "attachment; filename*=iso-8859-1'en'%A3%20rates.txt",
+ /^£ rates\.txt$/
+ ]
]
for (const [label, disposition, expected] of cases) {
test(label, async ({ plexo, serve, dirs }) => {
diff --git a/src/main/download/probe.ts b/src/main/download/probe.ts
index e5e13a9..2176799 100644
--- a/src/main/download/probe.ts
+++ b/src/main/download/probe.ts
@@ -65,12 +65,19 @@ function requestOneByte(url: URL, deadline: number): Promise<ProbeResponse> {
function parseContentDispositionFilename(disposition: string): string | null {
// RFC 6266 / RFC 5987: filename* takes precedence over filename
// format: filename*=charset'language'encoded-value
- const extMatch = /\bfilename\*=(?:[a-zA-Z0-9_-]+)'[^']*'([^;\s]+)/i.exec(disposition)
- if (extMatch?.[1]) {
+ const extMatch = /\bfilename\*=([a-zA-Z0-9_-]+)'[^']*'([^;\s]+)/i.exec(disposition)
+ if (extMatch?.[2]) {
+ // RFC 5987 requires both UTF-8 and ISO-8859-1. decodeURIComponent only reads UTF-8, so a
+ // Latin-1 byte like %A3 (£) would throw and leave the raw escapes as the name.
+ if (/^iso-8859-1$/i.test(extMatch[1])) {
+ return extMatch[2].replace(/%([0-9a-f]{2})/gi, (_, hex: string) =>
+ String.fromCharCode(parseInt(hex, 16))
+ )
+ }
try {
- return decodeURIComponent(extMatch[1])
+ return decodeURIComponent(extMatch[2])
} catch {
- return extMatch[1]
+ return extMatch[2]
}
}