Skip to content

Commit 945435f

Browse files
authored
fix(node): enforce maxContentLength for data: URLs (#7011)
* fix(node): enforce maxContentLength for data: URLs (pre-decode size check)- CVE-2025-58754 * feat(utils): add estimateDataURLDecodedBytes helper and fix duplicate condition in base64 padding check * feat: add estimateDataURLDecodedBytes helper with tests
1 parent 28e5e30 commit 945435f

4 files changed

Lines changed: 123 additions & 0 deletions

File tree

lib/adapters/http.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import readBlob from "../helpers/readBlob.js";
2525
import ZlibHeaderTransformStream from '../helpers/ZlibHeaderTransformStream.js';
2626
import callbackify from "../helpers/callbackify.js";
2727
import {progressEventReducer, progressEventDecorator, asyncDecorator} from "../helpers/progressEventReducer.js";
28+
import estimateDataURLDecodedBytes from '../helpers/estimateDataURLDecodedBytes.js';
2829

2930
const zlibOptions = {
3031
flush: zlib.constants.Z_SYNC_FLUSH,
@@ -46,6 +47,7 @@ const supportedProtocols = platform.protocols.map(protocol => {
4647
return protocol + ':';
4748
});
4849

50+
4951
const flushOnFinish = (stream, [throttled, flush]) => {
5052
stream
5153
.on('end', flush)
@@ -54,6 +56,7 @@ const flushOnFinish = (stream, [throttled, flush]) => {
5456
return throttled;
5557
}
5658

59+
5760
/**
5861
* If the proxy or config beforeRedirects functions are defined, call them with the options
5962
* object.
@@ -233,6 +236,21 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
233236
const protocol = parsed.protocol || supportedProtocols[0];
234237

235238
if (protocol === 'data:') {
239+
// Apply the same semantics as HTTP: only enforce if a finite, non-negative cap is set.
240+
if (config.maxContentLength > -1) {
241+
// Use the exact string passed to fromDataURI (config.url); fall back to fullPath if needed.
242+
const dataUrl = String(config.url || fullPath || '');
243+
const estimated = estimateDataURLDecodedBytes(dataUrl);
244+
245+
if (estimated > config.maxContentLength) {
246+
return reject(new AxiosError(
247+
'maxContentLength size of ' + config.maxContentLength + ' exceeded',
248+
AxiosError.ERR_BAD_RESPONSE,
249+
config
250+
));
251+
}
252+
}
253+
236254
let convertedData;
237255

238256
if (method !== 'GET') {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Estimate decoded byte length of a data:// URL *without* allocating large buffers.
3+
* - For base64: compute exact decoded size using length and padding;
4+
* handle %XX at the character-count level (no string allocation).
5+
* - For non-base64: use UTF-8 byteLength of the encoded body as a safe upper bound.
6+
*
7+
* @param {string} url
8+
* @returns {number}
9+
*/
10+
export default function estimateDataURLDecodedBytes(url) {
11+
if (!url || typeof url !== 'string') return 0;
12+
if (!url.startsWith('data:')) return 0;
13+
14+
const comma = url.indexOf(',');
15+
if (comma < 0) return 0;
16+
17+
const meta = url.slice(5, comma);
18+
const body = url.slice(comma + 1);
19+
const isBase64 = /;base64/i.test(meta);
20+
21+
if (isBase64) {
22+
let effectiveLen = body.length;
23+
const len = body.length; // cache length
24+
25+
for (let i = 0; i < len; i++) {
26+
if (body.charCodeAt(i) === 37 /* '%' */ && i + 2 < len) {
27+
const a = body.charCodeAt(i + 1);
28+
const b = body.charCodeAt(i + 2);
29+
const isHex =
30+
((a >= 48 && a <= 57) || (a >= 65 && a <= 70) || (a >= 97 && a <= 102)) &&
31+
((b >= 48 && b <= 57) || (b >= 65 && b <= 70) || (b >= 97 && b <= 102));
32+
33+
if (isHex) {
34+
effectiveLen -= 2;
35+
i += 2;
36+
}
37+
}
38+
}
39+
40+
let pad = 0;
41+
let idx = len - 1;
42+
43+
const tailIsPct3D = (j) =>
44+
j >= 2 &&
45+
body.charCodeAt(j - 2) === 37 && // '%'
46+
body.charCodeAt(j - 1) === 51 && // '3'
47+
(body.charCodeAt(j) === 68 || body.charCodeAt(j) === 100); // 'D' or 'd'
48+
49+
if (idx >= 0) {
50+
if (body.charCodeAt(idx) === 61 /* '=' */) {
51+
pad++;
52+
idx--;
53+
} else if (tailIsPct3D(idx)) {
54+
pad++;
55+
idx -= 3;
56+
}
57+
}
58+
59+
if (pad === 1 && idx >= 0) {
60+
if (body.charCodeAt(idx) === 61 /* '=' */) {
61+
pad++;
62+
} else if (tailIsPct3D(idx)) {
63+
pad++;
64+
}
65+
}
66+
67+
const groups = Math.floor(effectiveLen / 4);
68+
const bytes = groups * 3 - (pad || 0);
69+
return bytes > 0 ? bytes : 0;
70+
}
71+
72+
return Buffer.byteLength(body, 'utf8');
73+
}

lib/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,8 @@ const toFiniteNumber = (value, defaultValue) => {
635635
return value != null && Number.isFinite(value = +value) ? value : defaultValue;
636636
}
637637

638+
639+
638640
/**
639641
* If the thing is a FormData object, return true, otherwise return false.
640642
*
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import assert from 'assert';
2+
import estimateDataURLDecodedBytes from '../../../lib/helpers/estimateDataURLDecodedBytes.js';
3+
4+
describe('estimateDataURLDecodedBytes', () => {
5+
it('should return 0 for non-data URLs', () => {
6+
assert.strictEqual(estimateDataURLDecodedBytes('http://example.com'), 0);
7+
});
8+
9+
it('should calculate length for simple non-base64 data URL', () => {
10+
const url = 'data:,Hello';
11+
assert.strictEqual(estimateDataURLDecodedBytes(url), Buffer.byteLength('Hello', 'utf8'));
12+
});
13+
14+
it('should calculate decoded length for base64 data URL', () => {
15+
const str = 'Hello';
16+
const b64 = Buffer.from(str, 'utf8').toString('base64');
17+
const url = `data:text/plain;base64,${b64}`;
18+
assert.strictEqual(estimateDataURLDecodedBytes(url), str.length);
19+
});
20+
21+
it('should handle base64 with = padding', () => {
22+
const url = 'data:text/plain;base64,TQ=='; // "M"
23+
assert.strictEqual(estimateDataURLDecodedBytes(url), 1);
24+
});
25+
26+
it('should handle base64 with %3D padding', () => {
27+
const url = 'data:text/plain;base64,TQ%3D%3D'; // "M"
28+
assert.strictEqual(estimateDataURLDecodedBytes(url), 1);
29+
});
30+
});

0 commit comments

Comments
 (0)