bun-plugin-stx 0.2.301 → 0.2.302

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/serve.d.ts CHANGED
@@ -19,6 +19,18 @@ export declare function extractServerScriptsFromTemplate(content: string): {
19
19
  * reject the very form they are about to submit.
20
20
  */
21
21
  export declare function csrfTokenToMint(req: Request, cookies: Record<string, string>): string | null;
22
+ /**
23
+ * Whether the finished response is a page, the only thing a minted token is for.
24
+ *
25
+ * The mint is decided before the render, from the request, and the request
26
+ * cannot say reliably what it is asking for: browsers send a wildcard (any
27
+ * type) in the Accept of a stylesheet, a script and an image alike, so every asset a first-time
28
+ * visitor loaded went out with its own `Set-Cookie: X-CSRF-Token`. That made
29
+ * each one a response no shared cache will store - behind Cloudflare a whole
30
+ * site's assets came back `cf-cache-status: BYPASS` - and handed out a fresh
31
+ * token per asset while the page had embedded another. The response knows.
32
+ */
33
+ export declare function responseTakesCsrfCookie(response: Response): boolean;
22
34
  /** The `Set-Cookie` value for a minted token. Readable by script, as double-submit requires. */
23
35
  export declare function csrfCookieHeader(token: string, secure: boolean): string;
24
36
  export declare function buildDynamicRouteRegexes(fileRouteBase: string): RegExp[];
package/dist/serve.js CHANGED
@@ -8865,6 +8865,10 @@ function csrfTokenToMint(req, cookies) {
8865
8865
  crypto.getRandomValues(bytes);
8866
8866
  return [...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("");
8867
8867
  }
8868
+ function responseTakesCsrfCookie(response) {
8869
+ const type = ((response.headers.get("content-type") || "").split(";")[0] ?? "").trim().toLowerCase();
8870
+ return type === "text/html" || type === "application/xhtml+xml";
8871
+ }
8868
8872
  function csrfCookieHeader(token, secure) {
8869
8873
  return [
8870
8874
  `${CSRF_COOKIE}=${token}`,
@@ -10982,7 +10986,7 @@ ${trailingPageScripts.join(`
10982
10986
  }
10983
10987
  })();
10984
10988
  let _finalResp = await applyI18nToResponse(_i18nResp, i18nLocale ?? (i18nConfig?.defaultLocale ?? "en"), path);
10985
- if (mintedCsrfToken) {
10989
+ if (mintedCsrfToken && responseTakesCsrfCookie(_finalResp)) {
10986
10990
  try {
10987
10991
  _finalResp.headers.append("Set-Cookie", csrfCookieHeader(mintedCsrfToken, req.url.startsWith("https://")));
10988
10992
  } catch {
@@ -11105,6 +11109,7 @@ export {
11105
11109
  sharedScriptCacheControl,
11106
11110
  serve,
11107
11111
  routeSpecificity,
11112
+ responseTakesCsrfCookie,
11108
11113
  resolveServeOpenUrl,
11109
11114
  render404Page,
11110
11115
  publicFileExists,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-plugin-stx",
3
3
  "type": "module",
4
- "version": "0.2.301",
4
+ "version": "0.2.302",
5
5
  "description": "A Bun plugin that allows for using Laravel Blade-like syntax.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",