-
-
Couldn't load subscription status.
- Fork 672
fix: detect and prevent redirect loops with Client/Pool #4361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| 'use strict' | ||
|
|
||
| const { test, after } = require('node:test') | ||
| const { createServer } = require('node:http') | ||
| const { once } = require('node:events') | ||
| const { tspl } = require('@matteo.collina/tspl') | ||
| const undici = require('../..') | ||
|
|
||
| const { | ||
| interceptors: { redirect } | ||
| } = undici | ||
|
|
||
| test('Client should throw redirect loop error for cross-origin redirect', async (t) => { | ||
| t = tspl(t, { plan: 2 }) | ||
|
|
||
| const serverA = createServer((req, res) => { | ||
| res.writeHead(301, { | ||
| Location: 'http://localhost:9999/target' // Different port = cross-origin | ||
| }) | ||
| res.end() | ||
| }) | ||
|
|
||
| serverA.listen(0) | ||
| after(() => serverA.close()) | ||
| await once(serverA, 'listening') | ||
|
|
||
| const client = new undici.Client(`http://localhost:${serverA.address().port}`).compose( | ||
| redirect({ maxRedirections: 2 }) // Keep low to avoid long waits | ||
| ) | ||
| after(() => client.close()) | ||
|
|
||
| try { | ||
| await client.request({ | ||
| method: 'GET', | ||
| path: '/test' | ||
| }) | ||
| t.fail('Expected error but request succeeded') | ||
| } catch (error) { | ||
| t.ok(error.message.includes('Redirect loop detected'), 'Error message indicates redirect loop') | ||
| t.ok(error.message.includes('Client or Pool'), 'Error message mentions Client or Pool') | ||
| } | ||
|
|
||
| await t.completed | ||
| }) | ||
|
|
||
| test('Pool should throw redirect loop error for cross-origin redirect', async (t) => { | ||
| t = tspl(t, { plan: 2 }) | ||
|
|
||
| const serverA = createServer((req, res) => { | ||
| res.writeHead(301, { | ||
| Location: 'http://localhost:9998/target' // Different port = cross-origin | ||
| }) | ||
| res.end() | ||
| }) | ||
|
|
||
| serverA.listen(0) | ||
| after(() => serverA.close()) | ||
| await once(serverA, 'listening') | ||
|
|
||
| const pool = new undici.Pool(`http://localhost:${serverA.address().port}`).compose( | ||
| redirect({ maxRedirections: 2 }) // Keep low to avoid long waits | ||
| ) | ||
| after(() => pool.close()) | ||
|
|
||
| try { | ||
| await pool.request({ | ||
| method: 'GET', | ||
| path: '/test' | ||
| }) | ||
| t.fail('Expected error but request succeeded') | ||
| } catch (error) { | ||
| t.ok(error.message.includes('Redirect loop detected'), 'Error message indicates redirect loop') | ||
| t.ok(error.message.includes('Client or Pool'), 'Error message mentions Client or Pool') | ||
| } | ||
|
|
||
| await t.completed | ||
| }) | ||
|
|
||
| test('Agent should successfully follow cross-origin redirect', async (t) => { | ||
| t = tspl(t, { plan: 2 }) | ||
|
|
||
| const serverB = createServer((req, res) => { | ||
| res.writeHead(200) | ||
| res.end('Cross-origin redirect success') | ||
| }) | ||
|
|
||
| const serverA = createServer((req, res) => { | ||
| res.writeHead(301, { | ||
| Location: `http://localhost:${serverB.address().port}/success` | ||
| }) | ||
| res.end() | ||
| }) | ||
|
|
||
| serverA.listen(0) | ||
| serverB.listen(0) | ||
| after(() => { | ||
| serverA.close() | ||
| serverB.close() | ||
| }) | ||
|
|
||
| await Promise.all([ | ||
| once(serverA, 'listening'), | ||
| once(serverB, 'listening') | ||
| ]) | ||
|
|
||
| const agent = new undici.Agent().compose( | ||
| redirect({ maxRedirections: 2 }) | ||
| ) | ||
| after(() => agent.close()) | ||
|
|
||
| const response = await agent.request({ | ||
| origin: `http://localhost:${serverA.address().port}`, | ||
| method: 'GET', | ||
| path: '/test' | ||
| }) | ||
|
|
||
| const body = await response.body.text() | ||
|
|
||
| t.strictEqual(response.statusCode, 200, 'Response has 200 status code') | ||
| t.ok(body.includes('Cross-origin redirect success'), 'Response body indicates successful cross-origin redirect') | ||
|
|
||
| await t.completed | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.