fix(headers): trim trailing CRLF in normalized header values (#7456)

This commit is contained in:
Eve 2026-03-01 10:48:03 -05:00 committed by GitHub
parent f17a787026
commit 688826facd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View File

@ -14,7 +14,9 @@ function normalizeValue(value) {
return value;
}
return utils.isArray(value) ? value.map(normalizeValue) : String(value);
return utils.isArray(value)
? value.map(normalizeValue)
: String(value).replace(/[\r\n]+$/, '');
}
function parseTokens(str) {

View File

@ -83,6 +83,30 @@ describe('AxiosHeaders', function () {
assert.strictEqual(headers.get('x'), '123');
});
it('should trim trailing CRLF from plain object header values', function () {
const headers = new AxiosHeaders();
headers.set({
'cache-control': 'no-cache\r',
expires: '-1\r\n',
});
assert.strictEqual(headers.get('cache-control'), 'no-cache');
assert.strictEqual(headers.get('expires'), '-1');
});
it('should trim trailing CRLF from iterable header values', function () {
const headers = new AxiosHeaders();
headers.set(new Map([
['content-type', 'application/json; charset=utf-8\r'],
['pragma', 'no-cache\r\n'],
]));
assert.strictEqual(headers.get('content-type'), 'application/json; charset=utf-8');
assert.strictEqual(headers.get('pragma'), 'no-cache');
});
it('should support setting multiple header values from an iterable source', function () {
if (nodeMajorVersion < 18) {
this.skip();