(request: Request)
| 163 | * cross-site POST; this is the belt to that pair of braces. |
| 164 | */ |
| 165 | export function isSameOriginPost(request: Request): boolean { |
| 166 | const origin = request.headers.get('origin'); |
| 167 | if (!origin) return true; // Absent on some legitimate non-browser clients (curl). |
| 168 | // "null" is what Chromium sends on a SAME-origin form submit when the page |
| 169 | // carries our own `Referrer-Policy: no-referrer` header — an unattributed |
| 170 | // origin, not a foreign one. Treat it like an absent header: rejecting it |
| 171 | // locks every Chromium user out of the login form, while an attacker who |
| 172 | // knows the password never needed a browser in the first place. |
| 173 | if (origin === 'null') return true; |
| 174 | try { |
| 175 | return new URL(origin).host === new URL(request.url).host; |
| 176 | } catch { |
| 177 | return false; |
| 178 | } |
| 179 | } |
no test coverage detected