From ac9d824e685e8eec2d456180105cccc27d22b7f9 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Wed, 18 Nov 2026 10:35:50 -0800 Subject: [PATCH] Normalize seen keys and extended decoding --- src/format.spec.ts | 68 +++++++++----- src/index.ts | 28 +++--- src/parse.spec.ts | 225 ++++++++++++++++++++++++++++++++------------- 3 files changed, 220 insertions(+), 101 deletions(-) diff --git a/src/format.spec.ts b/src/format.spec.ts index bf8b740..93a7f74 100644 --- a/src/format.spec.ts +++ b/src/format.spec.ts @@ -74,31 +74,51 @@ describe('format(obj)', function () { ); }); - it('should not repeat an extended parameter that is already present', function () { - assert.strictEqual( - format({ - type: 'attachment', - parameters: { - filename: '€ rates.pdf', - 'filename*': "UTF-8''%E2%82%AC%20rates.pdf", - }, - }), - "attachment; filename*=UTF-8''%E2%82%AC%20rates.pdf", - ); - }); + it.each([ + { + parameters: { + filename: '€ rates.pdf', + 'filename*': "UTF-8''explicit.pdf", + }, + expected: + "attachment; filename*=UTF-8''%E2%82%AC%20rates.pdf; filename*=UTF-8''explicit.pdf", + }, + { + parameters: { + 'filename*': "UTF-8''explicit.pdf", + filename: '€ rates.pdf', + }, + expected: + "attachment; filename*=UTF-8''explicit.pdf; filename*=UTF-8''%E2%82%AC%20rates.pdf", + }, + ])( + 'should emit provided and generated extended parameters in order: $expected', + function ({ parameters, expected }) { + assert.strictEqual(format({ type: 'attachment', parameters }), expected); + }, + ); - it('should not repeat an extended parameter listed before its value', function () { - assert.strictEqual( - format({ - type: 'attachment', - parameters: { - 'filename*': "UTF-8''%E2%82%AC%20rates.pdf", - filename: '€ rates.pdf', - }, - }), - "attachment; filename*=UTF-8''%E2%82%AC%20rates.pdf", - ); - }); + it.each([ + { options: {}, expected: 'attachment; filename=first; FILENAME=second' }, + { + options: { multipart: true }, + expected: 'attachment; filename="first"; FILENAME="second"', + }, + ])( + 'should emit provided case-insensitive duplicates: $expected', + function ({ options, expected }) { + assert.strictEqual( + format( + { + type: 'attachment', + parameters: { filename: 'first', FILENAME: 'second' }, + }, + options, + ), + expected, + ); + }, + ); it('should format the result of parse without repeating a parameter', function () { assert.strictEqual( diff --git a/src/index.ts b/src/index.ts index 409e60e..2315ba1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -150,6 +150,7 @@ export function parse( const type = (typeFlags & CASE_FLAGS) === 0 ? typeValue : typeValue.toLowerCase(); const parameters: Record = new NullObject(); + const seenKeys: Record = new NullObject(); parameter: while (index < len) { index++; // Skip over semicolon. @@ -184,7 +185,7 @@ export function parse( while (index < len) { const code = header.charCodeAt(index); if (code === DQUOTE) { - if (parameters[key] === undefined) { + if (parameters[key] === undefined && !seenKeys[key]) { parameters[key] = encoded ? multipart ? decodeMultipartEscapes(header, quotedStart, index) @@ -199,6 +200,7 @@ export function parse( } index++; } + seenKeys[key] = true; continue parameter; } @@ -220,12 +222,17 @@ export function parse( index++; } + seenKeys[key] = true; continue parameter; } const valueStart = index; const delimiter = header.indexOf(';', index); index = delimiter === -1 ? len : delimiter; + + if (seenKeys[key]) continue parameter; + seenKeys[key] = true; + let valueEnd = index; while ( valueEnd > valueStart && @@ -235,16 +242,16 @@ export function parse( } const value = header.slice(valueStart, valueEnd); - if (parameters[key] === undefined) { - parameters[key] = value; - - if (extended && key.charCodeAt(key.length - 1) === ASTERISK) { - const normalizedKey = key.slice(0, -1); - const decoded = decodeExtended(value); - if (decoded !== undefined) parameters[normalizedKey] = decoded; + if (extended && key.charCodeAt(key.length - 1) === ASTERISK) { + const normalizedKey = key.slice(0, -1); + const decoded = decodeExtended(value); + if (decoded !== undefined) { + parameters[normalizedKey] = decoded; + continue parameter; } } + if (parameters[key] === undefined) parameters[key] = value; continue parameter; } @@ -495,11 +502,6 @@ export function format( throw new TypeError('Invalid extended parameter value: ' + value); } - // When `param` and `param*` are provided, prefer the already extended form. - if (parameters[param + '*'] !== undefined) { - continue; - } - result += '; ' + param + '*=' + encodeExtended(value); } } diff --git a/src/parse.spec.ts b/src/parse.spec.ts index f95e467..c7339ec 100644 --- a/src/parse.spec.ts +++ b/src/parse.spec.ts @@ -2,6 +2,81 @@ import { describe, it, assert } from 'vitest'; import { parse, format } from './index.js'; describe('parse(string)', function () { + describe('with duplicate parameters', function () { + describe.each([false, true])('with multipart: %s', function (multipart) { + it.each([ + ['"first%22.txt"', '"second.txt"'], + ['"first%22.txt"', 'second.txt'], + ['first.txt', '"second%22.txt"'], + ])('should keep %s before duplicate %s', function (first, second) { + assert.deepEqual( + parse(`attachment; filename=${first}; FILENAME=${second}`, { + multipart, + }), + { + type: 'attachment', + parameters: { + filename: + first === 'first.txt' + ? 'first.txt' + : multipart + ? 'first".txt' + : 'first%22.txt', + }, + }, + ); + }); + + it.each(['', '""'])( + 'should preserve an empty first value (%s)', + function (first) { + assert.deepEqual( + parse(`attachment; filename=${first}; filename=second`, { + multipart, + }), + { + type: 'attachment', + parameters: { filename: '' }, + }, + ); + }, + ); + + it('should parse a distinct parameter after a quoted duplicate containing a semicolon', function () { + assert.deepEqual( + parse( + 'attachment; filename=first; filename="second;hidden=value"; name=last', + { multipart }, + ), + { + type: 'attachment', + parameters: { filename: 'first', name: 'last' }, + }, + ); + }); + }); + + it.each([ + ["UTF-8''first", "UTF-8''second", "UTF-8''first"], + ['"first"', "UTF-8''second", 'first'], + ["UTF-8''first", '"second"', "UTF-8''first"], + ['"first"', '"second"', 'first'], + ])( + 'should keep %s before extended duplicate %s with decoding disabled', + function (first, second, expected) { + assert.deepEqual( + parse(`attachment; filename*= ${first}; FILENAME*=${second}`, { + extended: false, + }), + { + type: 'attachment', + parameters: { 'filename*': expected }, + }, + ); + }, + ); + }); + describe('with only type', function () { it('should ignore leading spaces and tabs', function () { assert.deepEqual(parse(' \t attachment'), { @@ -206,7 +281,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: '€ rates.pdf', - 'filename*': "UTF-8''%E2%82%AC%20rates.pdf", }, }, ); @@ -219,7 +293,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: '€ rates.pdf', - 'filename*': "utf8''%E2%82%AC%20rates.pdf", }, }, ); @@ -232,7 +305,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: '€ rates.pdf', - 'filename*': "UTF-8''%E2%82%AC%20rates.pdf", }, }, ); @@ -252,7 +324,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: '£ rates.pdf', - 'filename*': "ISO-8859-1''%A3%20rates.pdf", }, }, ); @@ -265,7 +336,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: '\x82 rates.pdf', - 'filename*': "ISO-8859-1''%82%20rates.pdf", }, }, ); @@ -276,7 +346,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: 'ABC.pdf', - 'filename*': "ISO-8859-1''ABC.pdf", }, }); }); @@ -315,7 +384,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: '€ rates.pdf', - 'filename*': "utf-8''%E2%82%AC%20rates.pdf", }, }, ); @@ -338,50 +406,122 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: '€ rates.pdf', - 'filename*': "UTF-8'en'%E2%82%AC%20rates.pdf", }, }, ); }); - it('should prefer extended parameter value', function () { + it.each([ + ["filename=fallback; filename*=UTF-8''preferred", 'preferred'], + ["filename*=UTF-8''preferred; filename=fallback", 'preferred'], + [ + "filename=fallback; filename*=UTF-8''preferred; filename=last", + 'preferred', + ], + [ + 'filename=fallback; filename*=UTF-8\'\'preferred; filename="last"', + 'preferred', + ], + ['filename="fallback"; filename*=UTF-8\'\'preferred', 'preferred'], + ['filename*=UTF-8\'\'preferred; filename="fallback"', 'preferred'], + [ + 'filename="fallback"; filename*=UTF-8\'\'preferred; filename=last', + 'preferred', + ], + [ + 'filename="fallback"; filename*=UTF-8\'\'preferred; filename="last"', + 'preferred', + ], + [ + 'filename="EURO rates.pdf"; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf', + '€ rates.pdf', + ], + [ + 'filename*=UTF-8\'\'%E2%82%AC%20rates.pdf; filename="EURO rates.pdf"', + '€ rates.pdf', + ], + [ + 'filename="foo-ae.html"; filename*=UTF-8\'\'foo-%c3%a4.html', + 'foo-ä.html', + ], + [ + 'filename*=UTF-8\'\'foo-%c3%a4.html; filename="foo-ae.html"', + 'foo-ä.html', + ], + ])( + 'should prefer the extended filename in %s', + function (params, filename) { + assert.deepEqual(parse(`attachment; ${params}`), { + type: 'attachment', + parameters: { filename }, + }); + }, + ); + + it('should keep the first duplicate extended filename', function () { assert.deepEqual( parse( - 'attachment; filename="EURO rates.pdf"; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf', + "attachment; filename*=UTF-8''first.html; filename*=UTF-8''second.html", ), { type: 'attachment', parameters: { - filename: '€ rates.pdf', - 'filename*': "UTF-8''%E2%82%AC%20rates.pdf", + filename: 'first.html', }, }, ); + }); + + it('should not treat a normalized extended key as a duplicate input key', function () { assert.deepEqual( parse( - 'attachment; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf; filename="EURO rates.pdf"', + "attachment; filename**=UTF-8''first.html; filename*=UTF-8''second.html", ), { type: 'attachment', parameters: { - filename: '€ rates.pdf', - 'filename*': "UTF-8''%E2%82%AC%20rates.pdf", + 'filename*': 'first.html', + filename: 'second.html', }, }, ); }); - it('should keep the first duplicate extended filename', function () { + it.each(['fallback', '"fallback"'])( + 'should preserve a normalized extended value before an undecodable value (%s)', + function (fallback) { + assert.deepEqual( + parse( + `attachment; filename**=UTF-8''preferred; filename*=${fallback}`, + ), + { + type: 'attachment', + parameters: { 'filename*': 'preferred' }, + }, + ); + }, + ); + + it('should ignore an unquoted extended duplicate after a quoted value', function () { assert.deepEqual( parse( - "attachment; filename*=UTF-8''first.html; filename*=UTF-8''second.html", + 'attachment; filename*="first.html"; FILENAME*=UTF-8\'\'second.html', ), { type: 'attachment', - parameters: { - filename: 'first.html', - 'filename*': "UTF-8''first.html", - }, + parameters: { 'filename*': 'first.html' }, + }, + ); + }); + + it('should ignore a quoted extended duplicate after an unquoted value', function () { + assert.deepEqual( + parse( + 'attachment; filename*=UTF-8\'\'first.html; FILENAME*="second.html"', + ), + { + type: 'attachment', + parameters: { filename: 'first.html' }, }, ); }); @@ -915,7 +1055,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: 'foo-ä.html', - 'filename*': "iso-8859-1''foo-%E4.html", }, }, ); @@ -928,7 +1067,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: 'foo-ä-€.html', - 'filename*': "UTF-8''foo-%c3%a4-%e2%82%ac.html", }, }, ); @@ -951,7 +1089,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: 'foo-ä.html', - 'filename*': "UTF-8''foo-a%cc%88.html", }, }, ); @@ -964,7 +1101,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: 'foo-ä-â\x82¬.html', - 'filename*': "iso-8859-1''foo-%c3%a4-%e2%82%ac.html", }, }, ); @@ -977,7 +1113,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { 'filename ': 'foo-ä.html', - 'filename *': "UTF-8''foo-%c3%a4.html", }, }, ); @@ -990,7 +1125,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: 'foo-ä.html', - 'filename*': "UTF-8''foo-%c3%a4.html", }, }, ); @@ -1003,7 +1137,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: 'foo-ä.html', - 'filename*': "UTF-8''foo-%c3%a4.html", }, }, ); @@ -1052,7 +1185,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: 'A-%41.html', - 'filename*': "UTF-8''A-%2541.html", }, }); }); @@ -1062,7 +1194,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: '\\foo.html', - 'filename*': "UTF-8''%5cfoo.html", }, }); }); @@ -1098,7 +1229,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { 'filename*0': 'foo-ä', - 'filename*0*': "UTF-8''foo-%c3%a4", 'filename*1': '.html', }, }, @@ -1147,36 +1277,6 @@ describe('parse(string)', function () { }); describe('RFC2231 Encoding: Fallback Behaviour', function () { - it('should parse "attachment; filename="foo-ae.html"; filename*=UTF-8\'\'foo-%c3%a4.html"', function () { - assert.deepEqual( - parse( - 'attachment; filename="foo-ae.html"; filename*=UTF-8\'\'foo-%c3%a4.html', - ), - { - type: 'attachment', - parameters: { - filename: 'foo-ä.html', - 'filename*': "UTF-8''foo-%c3%a4.html", - }, - }, - ); - }); - - it('should parse "attachment; filename*=UTF-8\'\'foo-%c3%a4.html; filename="foo-ae.html"', function () { - assert.deepEqual( - parse( - 'attachment; filename*=UTF-8\'\'foo-%c3%a4.html; filename="foo-ae.html"', - ), - { - type: 'attachment', - parameters: { - filename: 'foo-ä.html', - 'filename*': "UTF-8''foo-%c3%a4.html", - }, - }, - ); - }); - it("should parse \"attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4; filename*=ISO-8859-1''currency-sign%3d%a4", function () { assert.deepEqual( parse( @@ -1186,7 +1286,6 @@ describe('parse(string)', function () { type: 'attachment', parameters: { filename: 'currency-sign=¤', - 'filename*': "ISO-8859-1''currency-sign%3d%a4", 'filename*0*': "ISO-8859-15''euro-sign%3d%a4", }, }, @@ -1337,9 +1436,7 @@ describe('parse(string, options)', function () { type: 'form-data', parameters: { name: 'foo\nbar', - 'name*': "UTF-8''foo%0Abar", filename: '"foo\rbar".txt', - 'filename*': "UTF-8''%22foo%0Dbar%22.txt", }, }, );