|
| 1 | +export function throwIfPotentialCSRFAttack( |
| 2 | + headers: Headers, |
| 3 | + allowedActionOrigins: string[] | undefined, |
| 4 | +) { |
| 5 | + let originHeader = headers.get("origin"); |
| 6 | + let originDomain = |
| 7 | + typeof originHeader === "string" && originHeader !== "null" |
| 8 | + ? new URL(originHeader).host |
| 9 | + : originHeader; |
| 10 | + let host = parseHostHeader(headers); |
| 11 | + |
| 12 | + if (originDomain && (!host || originDomain !== host.value)) { |
| 13 | + if (!isAllowedOrigin(originDomain, allowedActionOrigins)) { |
| 14 | + if (host) { |
| 15 | + // This seems to be an CSRF attack. We should not proceed with the action. |
| 16 | + throw new Error( |
| 17 | + `${host.type} header does not match \`origin\` header from a forwarded ` + |
| 18 | + `action request. Aborting the action.`, |
| 19 | + ); |
| 20 | + } else { |
| 21 | + // This is an attack. We should not proceed with the action. |
| 22 | + throw new Error( |
| 23 | + "`x-forwarded-host` or `host` headers are not provided. One of these " + |
| 24 | + "is needed to compare the `origin` header from a forwarded action " + |
| 25 | + "request. Aborting the action.", |
| 26 | + ); |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Implementation of micromatch by Next.js https://github.com/vercel/next.js/blob/ea927b583d24f42e538001bf13370e38c91d17bf/packages/next/src/server/app-render/csrf-protection.ts#L6 |
| 33 | +function matchWildcardDomain(domain: string, pattern: string) { |
| 34 | + const domainParts = domain.split("."); |
| 35 | + const patternParts = pattern.split("."); |
| 36 | + |
| 37 | + if (patternParts.length < 1) { |
| 38 | + // pattern is empty and therefore invalid to match against |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + if (domainParts.length < patternParts.length) { |
| 43 | + // domain has too few segments and thus cannot match |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + // Prevent wildcards from matching entire domains (e.g. '**' or '*.com') |
| 48 | + // This ensures wildcards can only match subdomains, not the main domain |
| 49 | + if ( |
| 50 | + patternParts.length === 1 && |
| 51 | + (patternParts[0] === "*" || patternParts[0] === "**") |
| 52 | + ) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + while (patternParts.length) { |
| 57 | + const patternPart = patternParts.pop(); |
| 58 | + const domainPart = domainParts.pop(); |
| 59 | + |
| 60 | + switch (patternPart) { |
| 61 | + case "": { |
| 62 | + // invalid pattern. pattern segments must be non empty |
| 63 | + return false; |
| 64 | + } |
| 65 | + case "*": { |
| 66 | + // wildcard matches anything so we continue if the domain part is non-empty |
| 67 | + if (domainPart) { |
| 68 | + continue; |
| 69 | + } else { |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + case "**": { |
| 74 | + // if this is not the last item in the pattern the pattern is invalid |
| 75 | + if (patternParts.length > 0) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + // recursive wildcard matches anything so we terminate here if the domain part is non empty |
| 79 | + return domainPart !== undefined; |
| 80 | + } |
| 81 | + case undefined: |
| 82 | + default: { |
| 83 | + if (domainPart !== patternPart) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // We exhausted the pattern. If we also exhausted the domain we have a match |
| 91 | + return domainParts.length === 0; |
| 92 | +} |
| 93 | + |
| 94 | +function isAllowedOrigin( |
| 95 | + originDomain: string, |
| 96 | + allowedActionOrigins: string[] | undefined = [], |
| 97 | +) { |
| 98 | + return allowedActionOrigins.some( |
| 99 | + (allowedOrigin) => |
| 100 | + allowedOrigin && |
| 101 | + (allowedOrigin === originDomain || |
| 102 | + matchWildcardDomain(originDomain, allowedOrigin)), |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +function parseHostHeader(headers: Headers) { |
| 107 | + let forwardedHostHeader = headers.get("x-forwarded-host"); |
| 108 | + let forwardedHostValue = forwardedHostHeader?.split(",")[0]?.trim(); |
| 109 | + let hostHeader = headers.get("host"); |
| 110 | + |
| 111 | + return forwardedHostValue |
| 112 | + ? { |
| 113 | + type: "x-forwarded-host", |
| 114 | + value: forwardedHostValue, |
| 115 | + } |
| 116 | + : hostHeader |
| 117 | + ? { |
| 118 | + type: "host", |
| 119 | + value: hostHeader, |
| 120 | + } |
| 121 | + : undefined; |
| 122 | +} |
0 commit comments