From caa3497913b731fbae957e6e88edd82da7930111 Mon Sep 17 00:00:00 2001 From: Dave Cardwell Date: Sun, 26 Apr 2026 12:14:23 -0400 Subject: [PATCH] fix: missing beforeRedirect requestDetails argument (#6241) --- index.d.cts | 3 ++- index.d.ts | 7 ++++++- lib/adapters/http.js | 4 ++-- tests/unit/adapters/http.test.js | 31 +++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/index.d.cts b/index.d.cts index 0293479a..be5c214f 100644 --- a/index.d.cts +++ b/index.d.cts @@ -495,7 +495,8 @@ declare namespace axios { maxRate?: number | [MaxUploadRate, MaxDownloadRate]; beforeRedirect?: ( options: Record, - responseDetails: { headers: Record; statusCode: HttpStatusCode } + responseDetails: { headers: Record; statusCode: HttpStatusCode }, + requestDetails: { headers: Record; url: string; method: string }, ) => void; socketPath?: string | null; allowedSocketPaths?: string | string[] | null; diff --git a/index.d.ts b/index.d.ts index dec57647..151d8615 100644 --- a/index.d.ts +++ b/index.d.ts @@ -394,7 +394,12 @@ export interface AxiosRequestConfig { responseDetails: { headers: Record; statusCode: HttpStatusCode; - } + }, + requestDetails: { + headers: Record; + url: string; + method: string; + }, ) => void; socketPath?: string | null; allowedSocketPaths?: string | string[] | null; diff --git a/lib/adapters/http.js b/lib/adapters/http.js index 89ffb204..f59364aa 100755 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -176,12 +176,12 @@ const http2Sessions = new Http2Sessions(); * * @returns {Object} */ -function dispatchBeforeRedirect(options, responseDetails) { +function dispatchBeforeRedirect(options, responseDetails, requestDetails) { if (options.beforeRedirects.proxy) { options.beforeRedirects.proxy(options); } if (options.beforeRedirects.config) { - options.beforeRedirects.config(options, responseDetails); + options.beforeRedirects.config(options, responseDetails, requestDetails); } } diff --git a/tests/unit/adapters/http.test.js b/tests/unit/adapters/http.test.js index ecca6573..5027d0af 100644 --- a/tests/unit/adapters/http.test.js +++ b/tests/unit/adapters/http.test.js @@ -385,6 +385,37 @@ describe('supports http with nodejs', () => { } }); + it('should pass requestDetails to beforeRedirect with the original URL', async () => { + const server = await startHTTPServer( + (req, res) => { + res.setHeader('Location', '/foo'); + res.statusCode = 302; + res.end(); + }, + { port: SERVER_PORT } + ); + + const originalUrl = `http://localhost:${server.address().port}/bar`; + let capturedUrl; + + try { + await axios.get(originalUrl, { + maxRedirects: 3, + beforeRedirect: (options, responseDetails, requestDetails) => { + if (options.path === '/foo' && responseDetails.headers.location === '/foo') { + capturedUrl = requestDetails.url; + throw new Error('Provided path is not allowed'); + } + }, + }); + } catch (error) { + assert.strictEqual(error.message, 'Redirected request failed: Provided path is not allowed'); + assert.strictEqual(capturedUrl, originalUrl); + } finally { + await stopHTTPServer(server); + } + }); + it('should support beforeRedirect and proxy with redirect', async () => { let requestCount = 0; let proxyUseCount = 0;