Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hot-jokes-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Improve error message when Origin header is invalid
17 changes: 13 additions & 4 deletions packages/react-router/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ export function throwIfPotentialCSRFAttack(
allowedActionOrigins: string[] | undefined,
) {
let originHeader = headers.get("origin");
let originDomain =
typeof originHeader === "string" && originHeader !== "null"
? new URL(originHeader).host
: originHeader;
let originDomain: string | null = null;

try {
originDomain =
typeof originHeader === "string" && originHeader !== "null"
? new URL(originHeader).host
: originHeader;
} catch {
// error parsing the Origin header as a valid URL
Comment thread
brophdawg11 marked this conversation as resolved.
Outdated
throw new Error(
`provided \`origin\` header is not a valid URL. Aborting the action.`,
Comment thread
brophdawg11 marked this conversation as resolved.
Outdated
);
}
let host = parseHostHeader(headers);

if (originDomain && (!host || originDomain !== host.value)) {
Expand Down