util: make MIMEParams accessors case-insensitive

MIME parameter names are case-insensitive and the parser already
ASCII-lowercases them, but MIMEParams.get(), has(), delete() and set()
compared against the raw argument. As a result a parsed parameter was
unreachable by the casing the caller used (e.g. params.get('Charset')
returned null for "Charset=utf-8"), and set() could create duplicate,
case-colliding parameters.

Lowercase the name in the accessors to match the parser, consistent with
the whatwg-mimetype reference implementation.

Signed-off-by: Daijiro Wachi <daijiro.wachi@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64123
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Daijiro Wachi authored and RafaelGSS committed 2026-07-29 10:31:28 -03:00
1 parent 0a46d1ef66
commit ee460404be
2 files changed
+31 -3

No files matched your search

+4 -3
View File
@@ -148,12 +148,13 @@ class MIMEParams {
*/
delete(name) {
this.#parse();
this.#data.delete(name);
this.#data.delete(toASCIILower(`${name}`));
}
get(name) {
this.#parse();
const data = this.#data;
name = toASCIILower(`${name}`);
if (data.has(name)) {
return data.get(name);
}
@@ -162,13 +163,13 @@ class MIMEParams {
has(name) {
this.#parse();
return this.#data.has(name);
return this.#data.has(toASCIILower(`${name}`));
}
set(name, value) {
this.#parse();
const data = this.#data;
name = `${name}`;
name = toASCIILower(`${name}`);
value = `${value}`;
const invalidNameIndex = SafeStringPrototypeSearch(name, NOT_HTTP_TOKEN_CODE_POINT);
if (name.length === 0 || invalidNameIndex !== -1) {
+27
View File
@@ -158,3 +158,30 @@ assert.throws(() => params.set(`x${NOT_HTTP_TOKEN_CODE_POINT}`, 'x'), /parameter
assert.throws(() => params.set('x', `${NOT_HTTP_QUOTED_STRING_CODE_POINT};`), /parameter value/i);
assert.throws(() => params.set('x', `${NOT_HTTP_QUOTED_STRING_CODE_POINT}x`), /parameter value/i);
assert.throws(() => params.set('x', `x${NOT_HTTP_QUOTED_STRING_CODE_POINT}`), /parameter value/i);
// MIME parameter names are case-insensitive. The parser lower-cases them, so
// the MIMEParams accessors must lower-case their argument too; otherwise a
// parsed parameter is unreachable by the casing the caller actually used and
// `set()` can create case-colliding duplicate parameters.
// Refs: https://mimesniff.spec.whatwg.org/ (parameter names are ASCII-lowercased)
{
const mime = new MIMEType('text/plain;Charset=value');
assert.strictEqual(mime.params.get('Charset'), 'value');
assert.strictEqual(mime.params.get('charset'), 'value');
assert.strictEqual(mime.params.get('CHARSET'), 'value');
assert.strictEqual(mime.params.has('Charset'), true);
assert.strictEqual(`${mime.params}`, 'charset=value');
// `set()` with a mixed-case name overwrites the same parameter rather than
// adding a second, case-colliding one.
const params = new MIMEType('text/plain').params;
params.set('Foo', 'a');
params.set('foo', 'b');
assert.deepStrictEqual([...params], [['foo', 'b']]);
assert.strictEqual(`${params}`, 'foo=b');
// `delete()` is case-insensitive as well.
params.delete('FOO');
assert.strictEqual(params.has('foo'), false);
assert.deepStrictEqual([...params], []);
}