Node.js: Check redirect destinations by parsed origin
An application that accepts an absolute return URL can restrict it to an approved HTTPS origin. Parse the URL first, check its components, then use the serialized result. A hostname-looking prefix is insufficient.
Set up
Tested on Linux with Node.js v24.22.1. Use Bash, Node.js on your PATH, a text editor, and the mktemp, rm and rmdir utilities. No package installation or network access is needed. Keep the same terminal through cleanup. Stop if setup fails before saving the source.
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1Run the example
Save as node-redirect-origin-allowlist.mjs:
import assert from 'node:assert/strict';
const allowedOrigins = new Set(['https://app.example']);
function redirectTarget(input) {
if (typeof input !== 'string') throw new TypeError('Expected URL text');
const target = new URL(input);
if (target.protocol !== 'https:' ||
target.username !== '' || target.password !== '' ||
!allowedOrigins.has(target.origin)) {
throw new Error('Redirect destination rejected');
}
return target.href;
}
const cases = [
'https://APP.example:443/account',
'https://app.example.evil.example/account',
'https://user@app.example/account',
'http://app.example/account',
'https://app.example:8443/account',
'blob:https://app.example/id',
];
assert.equal(redirectTarget(cases[0]), 'https://app.example/account');
for (const input of cases.slice(1)) {
assert.throws(() => redirectTarget(input), /rejected/);
}
assert.throws(() => redirectTarget('/account'), TypeError);
assert.throws(() => redirectTarget(null), TypeError);
for (const input of cases) {
try {
console.log(`${input} => ${redirectTarget(input)}`);
} catch {
console.log(`${input} => rejected`);
}
}Run:
node node-redirect-origin-allowlist.mjsObserved output:
https://APP.example:443/account => https://app.example/account
https://app.example.evil.example/account => rejected
https://user@app.example/account => rejected
http://app.example/account => rejected
https://app.example:8443/account => rejected
blob:https://app.example/id => rejectedRead the result
The accepted URL normalizes the hostname’s case and removes the default HTTPS port. The suffix hostname and the nondefault port identify different origins. Credentials are checked separately because an origin does not include username or password.
The protocol check also rejects the blob URL even though it can carry the approved origin. The helper accepts absolute URLs only: a relative path has no supplied base and fails parsing. All addresses use reserved example domains; the script makes no requests.
Apply it carefully
This policy approves every path and query on the allowed origin. It does not check whether a destination requires authorization or itself redirects elsewhere. Add application-specific route rules if that matters, and use the returned serialized URL for the redirect rather than the original input.
Do not reuse this as an SSRF defense for a server-side fetcher: DNS resolution, destination IPs and subsequent redirects require separate controls. See Node’s WHATWG URL reference.
Clean up
Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.
rm -- node-redirect-origin-allowlist.mjs
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirAcknowledgements
This post was created with assistance from AI.