diff --git a/lib/core/AxiosHeaders.js b/lib/core/AxiosHeaders.js index 36f85122..38a26a40 100644 --- a/lib/core/AxiosHeaders.js +++ b/lib/core/AxiosHeaders.js @@ -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) { diff --git a/test/unit/core/AxiosHeaders.js b/test/unit/core/AxiosHeaders.js index b5c1c955..625e9ee9 100644 --- a/test/unit/core/AxiosHeaders.js +++ b/test/unit/core/AxiosHeaders.js @@ -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();