Django Cloudflare 代理 "CSRF Verification Failed"

Django Cloudflare Proxy "CSRF Verification Failed"

我正在尝试通过 Workers 通过 Cloudflare 代理我的 Django 应用程序。

设置是这样的:

example.com/app/* 转发到我的 Django 站点 ~其他的~转发到我的 Webflow 网站

到目前为止,该部分正在运行!但是,我无法让 Django 接受 CSRF 验证。

这里是请求的 headers(出于身份目的已清理)

{
    'HTTP_HOST': 'fluent-spring.uc.r.appspot.com',
    'HTTP_X_FORWARDED_FOR': '70.192.78.2',
    'HTTP_X_FORWARDED_PROTO': 'https',
    'HTTP_FORWARDED': 'for="172.70.0.123";proto=https',
    'HTTP_CF_WORKER': 'example.com',
    'HTTP_UPGRADE_INSECURE_REQUESTS': '1',
    'HTTP_CF_RAY': '6d2a89b3435e8c3b-EWR',
    'HTTP_CF_VISITOR': '{"scheme":"https"}',
    'HTTP_CF_EW_VIA': '15',
    'HTTP_CDN_LOOP': 'cloudflare; subreqs=1',
    'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.9',
    'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'HTTP_CACHE_CONTROL': 'no-cache',
    'HTTP_REFERER': 'https://fluent-spring.uc.r.appspot.com/',
    'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36',
    'HTTP_CF_CONNECTING_IP': '70.19.78.2',
    'HTTP_ORIGIN': 'https://www.example.com',
    'HTTP_PRAGMA': 'no-cache',
    'HTTP_SEC_CH_UA': '" Not;A Brand";v="99", "Google Chrome";v="97", "Chromium";v="97"',
    'HTTP_SEC_CH_UA_MOBILE': '?0',
    'HTTP_SEC_CH_UA_PLATFORM': '"macOS"',
    'HTTP_SEC_FETCH_DEST': 'document',
    'HTTP_SEC_FETCH_MODE': 'navigate',
    'HTTP_SEC_FETCH_SITE': 'same-origin',
    'HTTP_SEC_FETCH_USER': '?1',
    'HTTP_X_CLOUD_TRACE_CONTEXT': '959632cd27b84e7aad1a5e3c71f1d8a3/18242229191417730943',
    'HTTP_COOKIE': 'csrftoken=GHjnkrOrhave8EJ1eayWxxaSZiaxu5JJcJAaI1dmzc5Tdnb9T1YwaXvYUDr5ZQ83',
    'HTTP_X_APPENGINE_CITYLATLONG': '40.735657,-74.172367',
    'HTTP_X_APPENGINE_COUNTRY': 'US',
    'HTTP_X_APPENGINE_CITY': 'newark',
    'HTTP_X_APPENGINE_REGION': 'nj',
    'HTTP_TRACEPARENT': '00-959632cd27b84e7aad1a5e3c71f1d8a3-fd296acc51b7177f-00',
    'HTTP_X_APPENGINE_TIMEOUT_MS': '599998',
    'HTTP_X_APPENGINE_HTTPS': 'on',
    'HTTP_X_APPENGINE_USER_IP': '172.70.230.1',
    'HTTP_X_APPENGINE_API_TICKET': 'ChBkODIxOGU1YjRmMWE5NDlmGhMI2KyFxePK9QIVY049Ah0P8gjM',
    'HTTP_ACCEPT_ENCODING': 'gzip',
    'HTTP_X_APPENGINE_REQUEST_LOG_ID': '61eecfb100ff02c818a28bb9f40001737e666c75656e742d737072696e672d3333303332310001323032323031323474313035373136000100',
    'HTTP_X_APPENGINE_DEFAULT_VERSION_HOSTNAME': 'fluent-spring-.uc.r.appspot.com'
}

我在日志中得到的错误是

Forbidden (CSRF token missing or incorrect.): /app/sadmin/login/

屏幕上是:

CSRF verification failed. Request aborted.

我 settings.py 中的 CSRF 设置是:

CSRF_TRUSTED_ORIGINS = [
    "www.example.com",
    "example.com",
    "kevin-dot-fluent-spring.uc.r.appspot.com",
    "fluent-spring.uc.r.appspot.com",
    "localhost",
    "https://www.example.com",
    "https://example.com",
]

如果你已经安装了django-cors-headers那么从文档中-

Configure the middleware’s behaviour in your Django settings. You must set at least one of three following settings:

  1. CORS_ALLOWED_ORIGINS
  2. CORS_ALLOWED_ORIGIN_REGEXES
  3. CORS_ALLOW_ALL_ORIGINS

因此您可以尝试将 CSRF_TRUSTED_ORIGINS 更改为这三个 (CORS_ALLOWED_ORIGINS) 中的任何一个。

我的项目有CORS_ORIGIN_WHITELIST,现在我也得找这个。

该错误最终出现在 Cloudflare 代理工作者代码中--

正确的 Cloudflare 代理工作者是

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
  let url = new URL(request.url);
  // where we're proxying to
  url.hostname = "fluent-spring.uc.r.appspot.com";
  const newRequest = new Request(url.href, request);
  newRequest.headers.set("Referer", url.href);
  return await fetch(newRequest);
}