Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

Decode RFC 2047 encoded-words in Content-Disposition filenames (#79)

kevinc2e8a39de671
diff --git a/scripts/contentDisposition.test.mts b/scripts/contentDisposition.test.mts
new file mode 100644
index 0000000..5b14168
--- /dev/null
+++ b/scripts/contentDisposition.test.mts
@@ -0,0 +1,50 @@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+import {
+ decodeRfc2047Filename,
+ parseContentDispositionFilename
+} from '../src/main/download/contentDisposition.ts'
+
+test('RFC 2047 Base64 filename is decoded', () => {
+ // "test.txt" in UTF-8 base64
+ const d = 'attachment; filename="=?UTF-8?B?dGVzdC50eHQ=?="'
+ assert.equal(parseContentDispositionFilename(d), 'test.txt')
+})
+
+test('RFC 2047 Q-encoding filename is decoded', () => {
+ const d = 'attachment; filename="=?UTF-8?Q?caf=C3=A9.txt?="'
+ assert.equal(parseContentDispositionFilename(d), 'caf\u00e9.txt')
+})
+
+test('ISO-8859-1 filename* still wins over RFC 2047 filename', () => {
+ const d =
+ 'attachment; filename="=?UTF-8?B?dGVzdC50eHQ=?="; filename*=iso-8859-1\'en\'%A3%20rates.txt'
+ assert.equal(parseContentDispositionFilename(d), '\u00a3 rates.txt')
+})
+
+test('plain filenames are unchanged', () => {
+ assert.equal(parseContentDispositionFilename('attachment; filename="report.pdf"'), 'report.pdf')
+ assert.equal(decodeRfc2047Filename('report.pdf'), 'report.pdf')
+})
+
+test('whitespace is kept in mixed plaintext + encoded-word filenames', () => {
+ const name = 'quarterly report =?UTF-8?Q?caf=C3=A9?= final.txt'
+ assert.equal(decodeRfc2047Filename(name), 'quarterly report caf\u00e9 final.txt')
+})
+
+test('whitespace between adjacent encoded-words is discarded', () => {
+ assert.equal(
+ decodeRfc2047Filename('=?UTF-8?Q?caf=C3=A9?= =?UTF-8?Q?_final.txt?='),
+ 'caf\u00e9 final.txt'
+ )
+})
+
+test('malformed Base64 encoded-word is left unchanged', () => {
+ const name = '=?UTF-8?B?!!!!?='
+ assert.equal(decodeRfc2047Filename(name), name)
+})
+
+test('unsupported charset encoded-word is left unchanged', () => {
+ const name = '=?x-unknown?B?Y2Fm6S50eHQ=?='
+ assert.equal(decodeRfc2047Filename(name), name)
+})
diff --git a/src/main/download/contentDisposition.ts b/src/main/download/contentDisposition.ts
new file mode 100644
index 0000000..d370ae4
--- /dev/null
+++ b/src/main/download/contentDisposition.ts
@@ -0,0 +1,121 @@
+/**
+ * Decode a single RFC 2047 encoded-word (=?charset?B|Q???=), or return the
+ * input unchanged when it is not one / cannot be decoded safely. Mail gateways
+ * still put these in Content-Disposition filename= values.
+ */
+export function decodeRfc2047Word(value: string): string {
+ const match = /^=\?([^?]+)\?([bBqQ])\?([^?]*)\?=$/.exec(value.trim())
+ if (!match) return value
+ const charset = match[1]
+ const encoding = match[2].toUpperCase()
+ const text = match[3]
+ let bytes: Buffer
+ if (encoding === 'B') {
+ // Buffer.from(base64) silently drops invalid input; require a clean alphabet
+ // and that re-encoding the decoded bytes matches (padding-insensitive).
+ if (!/^[A-Za-z0-9+/]*={0,2}$/.test(text) || text.length % 4 !== 0) return value
+ bytes = Buffer.from(text, 'base64')
+ if (bytes.length === 0 && text.replace(/=+$/, '').length > 0) return value
+ const reencoded = bytes.toString('base64').replace(/=+$/, '')
+ const stripped = text.replace(/=+$/, '')
+ if (reencoded !== stripped) return value
+ } else {
+ // Q-encoding: _ is space; =XX is a byte. Reject bare '=' that is not =XX.
+ if (/=(?![0-9A-Fa-f]{2})/.test(text)) return value
+ const expanded = text
+ .replace(/_/g, ' ')
+ .replace(/=([0-9A-Fa-f]{2})/g, (_, hex: string) => String.fromCharCode(parseInt(hex, 16)))
+ bytes = Buffer.from([...expanded].map((c) => c.charCodeAt(0) & 0xff))
+ }
+ try {
+ // fatal: unsupported charsets and undecodable bytes must not invent text.
+ return new TextDecoder(charset, { fatal: true }).decode(bytes)
+ } catch {
+ return value
+ }
+}
+
+function isEncodedWord(part: string): boolean {
+ return /^=\?[^?]+\?[bBqQ]\?[^?]*\?=$/.test(part)
+}
+
+/**
+ * If the whole filename is one encoded-word, or a sequence of them separated
+ * by whitespace (RFC 2047 ?6.2), decode them. Whitespace is discarded only
+ * between adjacent encoded-words; mixed plaintext keeps its spaces. When any
+ * encoded-word fails to decode, the original filename is returned unchanged.
+ */
+export function decodeRfc2047Filename(name: string): string {
+ const trimmed = name.trim()
+ if (!/=\?[^?]+\?[bBqQ]\?[^?]*\?=/.test(trimmed)) return name
+ const parts = trimmed.split(/(\s+)/)
+ const out: string[] = []
+ let sawEncoded = false
+ for (let i = 0; i < parts.length; i++) {
+ const part = parts[i]
+ if (/^\s+$/.test(part)) {
+ const prev = parts[i - 1]
+ const next = parts[i + 1]
+ // RFC 2047 ?6.2: linear whitespace between adjacent encoded-words is ignored.
+ if (prev && next && isEncodedWord(prev) && isEncodedWord(next)) continue
+ out.push(part)
+ continue
+ }
+ if (isEncodedWord(part)) {
+ const decoded = decodeRfc2047Word(part)
+ if (decoded === part) return name // decode failed ? keep the original spelling
+ sawEncoded = true
+ out.push(decoded)
+ continue
+ }
+ out.push(part)
+ }
+ return sawEncoded ? out.join('') : name
+}
+
+export 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?.[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[2])
+ } catch {
+ return extMatch[2]
+ }
+ }
+
+ // Quoted string: preserves semicolons inside quotes, e.g. filename="report; final.pdf"
+ const quotedMatch = /\bfilename="((?:[^"\\]|\\.)*)"/i.exec(disposition)
+ if (quotedMatch?.[1]) {
+ const unescaped = quotedMatch[1].replace(/\\(.)/g, '$1')
+ let decoded: string
+ try {
+ decoded = decodeURIComponent(unescaped)
+ } catch {
+ decoded = unescaped
+ }
+ return decodeRfc2047Filename(decoded)
+ }
+
+ // Unquoted token fallback
+ const tokenMatch = /\bfilename=([^;\s]+)/i.exec(disposition)
+ if (tokenMatch?.[1]) {
+ let decoded: string
+ try {
+ decoded = decodeURIComponent(tokenMatch[1])
+ } catch {
+ decoded = tokenMatch[1]
+ }
+ return decodeRfc2047Filename(decoded)
+ }
+
+ return null
+}
diff --git a/src/main/download/probe.ts b/src/main/download/probe.ts
index 2176799..90065b5 100644
--- a/src/main/download/probe.ts
+++ b/src/main/download/probe.ts
@@ -4,6 +4,7 @@ import { isAbsolute } from 'node:path'
import { URL } from 'node:url'
import type { ProbeResult } from '../../shared/types'
import { testKnobs } from '../testKnobs'
+import { parseContentDispositionFilename } from './contentDisposition'
import {
describeTorrent,
downloadTorrentFile,
@@ -62,49 +63,6 @@ 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?.[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[2])
- } catch {
- return extMatch[2]
- }
- }
-
- // Quoted string: preserves semicolons inside quotes, e.g. filename="report; final.pdf"
- const quotedMatch = /\bfilename="((?:[^"\\]|\\.)*)"/i.exec(disposition)
- if (quotedMatch?.[1]) {
- const unescaped = quotedMatch[1].replace(/\\(.)/g, '$1')
- try {
- return decodeURIComponent(unescaped)
- } catch {
- return unescaped
- }
- }
-
- // Unquoted token fallback
- const tokenMatch = /\bfilename=([^;\s]+)/i.exec(disposition)
- if (tokenMatch?.[1]) {
- try {
- return decodeURIComponent(tokenMatch[1])
- } catch {
- return tokenMatch[1]
- }
- }
-
- return null
-}
-
function fileNameFromHeaders(headers: Headers, url: URL): string {
const disposition = headerValue(headers, 'content-disposition')
if (disposition) {