Skip to content

Commit 32fc489

Browse files
authored
fix: malformed http urls (#11000)
1 parent b40ce49 commit 32fc489

29 files changed

Lines changed: 1640 additions & 131 deletions

‎PRE_RELEASE_CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
## Bug Fixes
1010

11+
- **URL Validation:** Reject malformed `http:` and `https:` URLs that omit `//` before adapter URL normalization, returning `ERR_INVALID_URL` instead of silently normalizing invalid input. (**#10900**, closes **#7315**)
1112
- **Types:** Add the missing readonly `name: 'CanceledError'` declaration to CommonJS `CanceledError` typings to match the ESM declarations. (**#10922**)
1213
- **Config Merge:** Added `transitional.validateStatusUndefinedResolves` (default `true`) so applications can opt into treating explicit `validateStatus: undefined` like an omitted option by setting it to `false`. `validateStatus: null` still accepts every response status. (**#10899**, closes **#6688**)
1314

‎PRE_RELEASE_DOCS.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ Do not store raw diffs or line-number-only instructions here; prefer stable sect
2020

2121
## Unreleased
2222

23+
### malformed HTTP URL rejection
24+
25+
- **Change:** Note that malformed `http:` and `https:` URLs missing `//` are rejected before adapter normalization.
26+
- **Source:** `PRE_RELEASE_CHANGELOG.md` Bug Fixes, #10900, closes #7315.
27+
- **Status:** Skipped.
28+
- **Docs targets:** None beyond release notes.
29+
- **Required content:** No API documentation update is needed because this changes handling for invalid URL input without adding or changing request config, types, or public APIs. The release note should mention that axios now throws `AxiosError` with `ERR_INVALID_URL` for malformed HTTP(S) URLs such as `https:example.com` or `http:/example.com` instead of allowing platform URL normalization.
30+
- **Examples:** None.
31+
- **Notes:** Treat as a bug/security-hardening release note, not a request-config documentation change.
32+
2333
### sensitiveHeaders request config
2434

2535
- **Change:** Document the Node.js `sensitiveHeaders` request config option for stripping custom secret headers from cross-origin redirects.

‎lib/adapters/fetch.js‎

Lines changed: 113 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,28 @@ const factory = (env) => {
234234

235235
let requestContentLength;
236236

237+
// AxiosError we raise while the request body is being streamed. Captured
238+
// by identity so the catch block can surface it directly, regardless of
239+
// how the runtime wraps the resulting fetch rejection (undici exposes it
240+
// as `err.cause`; some browsers drop the original error entirely).
241+
let pendingBodyError = null;
242+
243+
const maxBodyLengthError = () =>
244+
new AxiosError(
245+
'Request body larger than maxBodyLength limit',
246+
AxiosError.ERR_BAD_REQUEST,
247+
config,
248+
request
249+
);
250+
237251
try {
238252
// HTTP basic authentication
239253
let auth = undefined;
240254
const configAuth = own('auth');
241255

242256
if (configAuth) {
243-
const username = configAuth.username || '';
244-
const password = configAuth.password || '';
257+
const username = utils.getSafeProp(configAuth, 'username') || '';
258+
const password = utils.getSafeProp(configAuth, 'password') || '';
245259
auth = {
246260
username,
247261
password
@@ -290,53 +304,96 @@ const factory = (env) => {
290304
}
291305
}
292306

293-
// Enforce maxBodyLength against the outbound request body before dispatch.
294-
// Mirrors http.js behavior (ERR_BAD_REQUEST / 'Request body larger than
295-
// maxBodyLength limit'). Skip when the body length cannot be determined
296-
// (e.g. a live ReadableStream supplied by the caller).
307+
// Enforce maxBodyLength against known-size bodies before dispatch using
308+
// the body's *actual* size — never a caller-declared Content-Length,
309+
// which could under-report to slip an oversized body past the check.
310+
// Unknown-size streams return undefined here and are counted per-chunk
311+
// below as fetch consumes them.
297312
if (hasMaxBodyLength && method !== 'get' && method !== 'head') {
298-
const outboundLength = await resolveBodyLength(headers, data);
299-
if (
300-
typeof outboundLength === 'number' &&
301-
isFinite(outboundLength) &&
302-
outboundLength > maxBodyLength
303-
) {
304-
throw new AxiosError(
305-
'Request body larger than maxBodyLength limit',
306-
AxiosError.ERR_BAD_REQUEST,
307-
config,
308-
request
309-
);
313+
const outboundLength = await getBodyLength(data);
314+
if (typeof outboundLength === 'number' && isFinite(outboundLength)) {
315+
requestContentLength = outboundLength;
316+
if (outboundLength > maxBodyLength) {
317+
throw maxBodyLengthError();
318+
}
310319
}
311320
}
312321

322+
// A streamed body under maxBodyLength must be counted as fetch consumes
323+
// it; its size is never trusted from a caller-declared Content-Length.
324+
const mustEnforceStreamBody =
325+
hasMaxBodyLength && (utils.isReadableStream(data) || utils.isStream(data));
326+
327+
const trackRequestStream = (stream, onProgress, flush) =>
328+
trackStream(
329+
stream,
330+
DEFAULT_CHUNK_SIZE,
331+
(loadedBytes) => {
332+
if (hasMaxBodyLength && loadedBytes > maxBodyLength) {
333+
throw (pendingBodyError = maxBodyLengthError());
334+
}
335+
onProgress && onProgress(loadedBytes);
336+
},
337+
flush
338+
);
339+
313340
if (
314-
onUploadProgress &&
315341
supportsRequestStream &&
316342
method !== 'get' &&
317343
method !== 'head' &&
318-
(requestContentLength = await resolveBodyLength(headers, data)) !== 0
344+
(onUploadProgress || mustEnforceStreamBody)
319345
) {
320-
let _request = new Request(url, {
321-
method: 'POST',
322-
body: data,
323-
duplex: 'half',
324-
});
346+
requestContentLength =
347+
requestContentLength == null ? await resolveBodyLength(headers, data) : requestContentLength;
348+
349+
// A declared length of 0 is only trusted to skip the wrap when we are
350+
// not enforcing a stream limit (which must not rely on that header).
351+
if (requestContentLength !== 0 || mustEnforceStreamBody) {
352+
let _request = new Request(url, {
353+
method: 'POST',
354+
body: data,
355+
duplex: 'half',
356+
});
325357

326-
let contentTypeHeader;
358+
let contentTypeHeader;
327359

328-
if (utils.isFormData(data) && (contentTypeHeader = _request.headers.get('content-type'))) {
329-
headers.setContentType(contentTypeHeader);
330-
}
360+
if (utils.isFormData(data) && (contentTypeHeader = _request.headers.get('content-type'))) {
361+
headers.setContentType(contentTypeHeader);
362+
}
331363

332-
if (_request.body) {
333-
const [onProgress, flush] = progressEventDecorator(
334-
requestContentLength,
335-
progressEventReducer(asyncDecorator(onUploadProgress))
336-
);
364+
if (_request.body) {
365+
const [onProgress, flush] =
366+
(onUploadProgress &&
367+
progressEventDecorator(
368+
requestContentLength,
369+
progressEventReducer(asyncDecorator(onUploadProgress))
370+
)) ||
371+
[];
337372

338-
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, onProgress, flush);
373+
data = trackRequestStream(_request.body, onProgress, flush);
374+
}
339375
}
376+
} else if (
377+
mustEnforceStreamBody &&
378+
!isRequestSupported &&
379+
isReadableStreamSupported &&
380+
method !== 'get' &&
381+
method !== 'head'
382+
) {
383+
data = trackRequestStream(data);
384+
} else if (
385+
mustEnforceStreamBody &&
386+
isRequestSupported &&
387+
!supportsRequestStream &&
388+
method !== 'get' &&
389+
method !== 'head'
390+
) {
391+
throw new AxiosError(
392+
'Stream request bodies are not supported by the current fetch implementation',
393+
AxiosError.ERR_NOT_SUPPORT,
394+
config,
395+
request
396+
);
340397
}
341398

342399
if (!utils.isString(withCredentials)) {
@@ -379,10 +436,12 @@ const factory = (env) => {
379436
? _fetch(request, fetchOptions)
380437
: _fetch(url, resolvedOptions));
381438

439+
const responseHeaders = AxiosHeaders.from(response.headers);
440+
382441
// Cheap pre-check: if the server honestly declares a content-length that
383442
// already exceeds the cap, reject before we start streaming.
384443
if (hasMaxContentLength) {
385-
const declaredLength = utils.toFiniteNumber(response.headers.get('content-length'));
444+
const declaredLength = utils.toFiniteNumber(responseHeaders.getContentLength());
386445
if (declaredLength != null && declaredLength > maxContentLength) {
387446
throw new AxiosError(
388447
'maxContentLength size of ' + maxContentLength + ' exceeded',
@@ -407,7 +466,7 @@ const factory = (env) => {
407466
options[prop] = response[prop];
408467
});
409468

410-
const responseContentLength = utils.toFiniteNumber(response.headers.get('content-length'));
469+
const responseContentLength = utils.toFiniteNumber(responseHeaders.getContentLength());
411470

412471
const [onProgress, flush] =
413472
(onDownloadProgress &&
@@ -502,6 +561,23 @@ const factory = (env) => {
502561
throw canceledError;
503562
}
504563

564+
// Surface a maxBodyLength violation we raised while the request body was
565+
// being streamed. Matching by identity (rather than reading
566+
// `err.cause.isAxiosError`) keeps the error deterministic across runtimes
567+
// and avoids both prototype-pollution reads and mis-attributing a foreign
568+
// AxiosError that merely happened to land in `err.cause`.
569+
if (pendingBodyError) {
570+
request && !pendingBodyError.request && (pendingBodyError.request = request);
571+
throw pendingBodyError;
572+
}
573+
574+
// Re-throw AxiosErrors we raised synchronously (data: URL / content-length
575+
// pre-checks, response size enforcement) without re-wrapping them.
576+
if (err instanceof AxiosError) {
577+
request && !err.request && (err.request = request);
578+
throw err;
579+
}
580+
505581
if (err && err.name === 'TypeError' && /Load failed|fetch/i.test(err.message)) {
506582
throw Object.assign(
507583
new AxiosError(

0 commit comments

Comments
 (0)