fix: attach CSRF token to state-changing requests
ci / docker (push) Successful in 5m20s

This commit is contained in:
2026-09-05 17:33:30 +08:00
parent 0dff033a7f
commit 799b522f9b
7 changed files with 391 additions and 5 deletions
+13
View File
@@ -0,0 +1,13 @@
export function csrfHeader(method?: string) {
if (method && !['GET', 'HEAD', 'OPTIONS'].includes(method.toUpperCase())) {
const token = getCookie('dodo_csrf')
if (token) return { 'x-csrf-token': token }
}
return {}
}
export function getCookie(name: string) {
if (typeof document === 'undefined') return ''
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
const match = document.cookie.match(new RegExp(`(?:^|; )${escaped}=([^;]*)`))
return match ? decodeURIComponent(match[1]) : ''
}