fix: support URL object as config.url input#10866
Conversation
Fixes axios#6546 When passing a URL object (e.g., new URL(...)) to axios methods like axios.get(url, { params: {...} }), the buildURL function would crash with 'url.indexOf is not a function' because it assumed url was always a string. This fix converts URL objects to strings before processing.
- Move URL object coercion from buildURL to Axios._request so that all downstream consumers (buildFullPath, combineURLs, adapters) see a string. This fixes the crash when using URL object with baseURL (combineURLs calls .replace() on the URL object). - Remove the broken buildURL unit test that tested buildURL(url, null) without actually exercising URL coercion (buildURL early-returns when !params). - Add e2e tests via the HTTP adapter for URL object with params and URL object without params (no crash).
There was a problem hiding this comment.
4 issues found across 7 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="lib/helpers/buildURL.js">
<violation number="1" location="lib/helpers/buildURL.js:32">
P3: Custom agent: **Flag AI Slop and Fabricated Changes**
Comment references a non-existent `./unsafe/buildURL` path, which is misleading and looks like fabricated change text.</violation>
<violation number="2" location="lib/helpers/buildURL.js:34">
P2: URL coercion is now too narrow; non-native or cross-realm URL-like objects can reach string methods and crash.</violation>
<violation number="3" location="lib/helpers/buildURL.js:34">
P2: `url instanceof URL` can throw when `URL` exists but is not a callable constructor.</violation>
</file>
<file name="index.d.cts">
<violation number="1" location="index.d.cts:476">
P1: Broadening `url` to `string | URL` leaks into internal interceptor config, but the runtime only normalizes before interceptors run. An interceptor can reintroduce a `URL` object and send it downstream unnormalized.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
|
|
||
| interface AxiosRequestConfig<D = any> { | ||
| url?: string; | ||
| url?: string | URL; |
There was a problem hiding this comment.
P1: Broadening url to string | URL leaks into internal interceptor config, but the runtime only normalizes before interceptors run. An interceptor can reintroduce a URL object and send it downstream unnormalized.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At index.d.cts, line 476:
<comment>Broadening `url` to `string | URL` leaks into internal interceptor config, but the runtime only normalizes before interceptors run. An interceptor can reintroduce a `URL` object and send it downstream unnormalized.</comment>
<file context>
@@ -473,7 +473,7 @@ declare namespace axios {
interface AxiosRequestConfig<D = any> {
- url?: string;
+ url?: string | URL;
method?: Method | string;
baseURL?: string;
</file context>
6fbaf0f to
c84d3e7
Compare
|
@cubic-dev-ai please review this one again! |
@jasonsaayman I have started the AI code review. It will take a few minutes to complete. |
|
@jasonsaayman
is equal to |
|
Thanks @DigitalBrainJS, this one is on me I maybe went to heavily handed with review suggestions. I have reverted this, and we can think about URL object support, maybe in a future release. |
Summary
Fixes the crash when passing a URL object as the URL input to axios.
Problem
When calling
axios.get(new URL('http://example.com/foo'))oraxios.get(new URL('/foo', 'http://example.com'))with abaseURL, axios crashed because:isAbsoluteURLreturnedfalsefor URL objects (typeof check)combineURLscalled.replace()on the URL object which doesn't existbuildFullPathpassed the raw URL object tocombineURLsFix
buildURLtoAxios._requestbeforemergeConfig. This ensures all downstream consumers (buildFullPath,combineURLs, adapters) see a string.buildURL(url, null)that never exercised URL coercion (it early-returns before the coercion code)Review feedback
Addresses @jasonsaayman's feedback:
Axios.jsso it covers all crash paths, not justbuildURLSummary by cubic
Fixes crashes when passing a
URLobject toaxiosby normalizingconfig.urlto a string across the whole pipeline and widening types tostring | URL. Covers interceptor-modified URLs, adds abuildURLsafeguard, and updates docs.Description
Summary of changes
config.urlwhen it is aURLinAxios._request,dispatchRequest(pre/post transforms), andgetUri.buildURLto coerce URL-like inputs for direct callers.string | URLfor all method overloads andAxiosRequestConfig.url.axios(url[, config]).URLbeing a constructor; uses a tag-based check.Reasoning
URLobjects (e.g.,.replace,.indexOf), causing crashes with or withoutbaseURLand when interceptors setconfig.url.Additional context
buildFullPath,combineURLs, transforms, adapters) see a string.Docs
/docs/to noteconfig.urlsupportsstring | URLand include a short example mirroring the README.Testing
URLinputs (with existing query +params, and without params).buildURLwithURLand URL-like objects.axios(urlObj, config),getUri({ url: URL }).config.urlto aURL.URLis not a constructor.Semantic version impact
Written for commit e23b357. Summary will update on new commits.