nuxt-skew-protection 1.5.1 → 1.5.2
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/module.json +1 -1
- package/dist/module.mjs +2 -1
- package/dist/runtime/app/plugins/multi-tab.client.js +2 -1
- package/dist/runtime/server/plugins/html-cache-headers.d.ts +3 -3
- package/dist/runtime/server/plugins/html-cache-headers.js +3 -1
- package/dist/runtime/server/utils/html-cache-policy.d.ts +13 -5
- package/dist/runtime/server/utils/html-cache-policy.js +48 -7
- package/package.json +1 -1
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1117,7 +1117,8 @@ export {}
|
|
|
1117
1117
|
}
|
|
1118
1118
|
{
|
|
1119
1119
|
const onVercel = resolveNitroPreset(nuxt.options.nitro)?.includes("vercel") || process.env.VERCEL_SKEW_PROTECTION_ENABLED === "1";
|
|
1120
|
-
const
|
|
1120
|
+
const publicAssetBaseURLs = (nuxt.options.nitro.publicAssets ?? []).flatMap((asset) => asset && typeof asset.baseURL === "string" ? [asset.baseURL] : []);
|
|
1121
|
+
const caching = cachingRouteRules(nuxt.options.routeRules ?? {}, publicAssetBaseURLs);
|
|
1121
1122
|
if (onVercel) {
|
|
1122
1123
|
if (caching.length) {
|
|
1123
1124
|
logger.warn(`${caching.length} route rule${caching.length > 1 ? "s" : ""} ask a shared cache to store HTML. That cannot work on Vercel. Vercel skew protection sets a \`__vdpl\` cookie on every document. Shared caches skip any response that sets a cookie.`);
|
|
@@ -41,7 +41,7 @@ export default defineNuxtPlugin({
|
|
|
41
41
|
return;
|
|
42
42
|
const channel = new BroadcastChannel(CHANNEL_NAME);
|
|
43
43
|
let receivedFromChannel = false;
|
|
44
|
-
nuxtApp.hooks.hook("app:manifest:update", (manifest) => {
|
|
44
|
+
const stopBroadcasting = nuxtApp.hooks.hook("app:manifest:update", (manifest) => {
|
|
45
45
|
if (receivedFromChannel) {
|
|
46
46
|
receivedFromChannel = false;
|
|
47
47
|
return;
|
|
@@ -57,6 +57,7 @@ export default defineNuxtPlugin({
|
|
|
57
57
|
}
|
|
58
58
|
};
|
|
59
59
|
nuxtApp.hook("app:error", () => {
|
|
60
|
+
stopBroadcasting();
|
|
60
61
|
channel.close();
|
|
61
62
|
});
|
|
62
63
|
}
|
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Runs on `beforeResponse` rather than as middleware, because the answer needs
|
|
5
5
|
* the response: the status decides whether the document is worth keeping, and
|
|
6
|
-
* the app's
|
|
6
|
+
* the app's cache policy is the only signal that it wants one kept.
|
|
7
7
|
* Reading it here also means it does not matter how the app set it, route rule
|
|
8
8
|
* or plugin or handler.
|
|
9
9
|
*
|
|
10
|
-
* A route rule is caught at build time and named there. A
|
|
11
|
-
*
|
|
10
|
+
* A route rule is caught at build time and named there. A cache header set by
|
|
11
|
+
* a handler or a nitro plugin is not, and gets no warning at all. There is
|
|
12
12
|
* nowhere to put one: the cookie middleware only registers outside dev
|
|
13
13
|
* (`module.ts`), so nothing is ever dropped while `nuxt dev` runs, and a
|
|
14
14
|
* per-route warning in production logs is noise for a header the author wrote
|
|
@@ -5,15 +5,17 @@ import {
|
|
|
5
5
|
htmlCacheRequestFromEvent,
|
|
6
6
|
readSetCookies,
|
|
7
7
|
resolveHtmlCachePolicy,
|
|
8
|
+
sharedCacheControlHeader,
|
|
8
9
|
withoutCookie
|
|
9
10
|
} from "../utils/html-cache-policy.js";
|
|
10
11
|
export default defineNitroPlugin((nitroApp) => {
|
|
11
12
|
nitroApp.hooks.hook("beforeResponse", (event) => {
|
|
13
|
+
const cacheControl = sharedCacheControlHeader((name2) => getResponseHeader(event, name2));
|
|
12
14
|
const decision = resolveHtmlCachePolicy(
|
|
13
15
|
htmlCacheRequestFromEvent(event, getHeader),
|
|
14
16
|
{
|
|
15
17
|
status: getResponseStatus(event),
|
|
16
|
-
cacheControl:
|
|
18
|
+
cacheControl: cacheControl?.value
|
|
17
19
|
}
|
|
18
20
|
);
|
|
19
21
|
if (decision._tag !== "shared-cacheable")
|
|
@@ -121,16 +121,23 @@ export interface OverlongRoute {
|
|
|
121
121
|
/** The window came from `max-age` alone, so CDN intent is a guess. */
|
|
122
122
|
fromMaxAgeAlone: boolean;
|
|
123
123
|
}
|
|
124
|
+
declare const SHARED_CACHE_CONTROL_HEADERS: readonly ["cloudflare-cdn-cache-control", "cdn-cache-control", "cache-control"];
|
|
125
|
+
interface SharedCacheControlHeader {
|
|
126
|
+
name: typeof SHARED_CACHE_CONTROL_HEADERS[number];
|
|
127
|
+
value: unknown;
|
|
128
|
+
}
|
|
129
|
+
/** The highest-precedence header that states a shared-cache policy. */
|
|
130
|
+
export declare function sharedCacheControlHeader(getHeader: (name: string) => unknown): SharedCacheControlHeader | null;
|
|
124
131
|
/**
|
|
125
132
|
* Route rules that ask a shared cache to keep a document.
|
|
126
133
|
*
|
|
127
|
-
* Reads the app's own `routeRules` rather than a second copy of them
|
|
128
|
-
*
|
|
129
|
-
*
|
|
134
|
+
* Reads the app's own `routeRules` rather than a second copy of them. Public
|
|
135
|
+
* asset prefixes are excluded because their cached responses are not HTML.
|
|
136
|
+
* Empty means the app never asked for shared HTML caching in its config.
|
|
130
137
|
*/
|
|
131
|
-
export declare function cachingRouteRules(routeRules: Record<string, InspectableRouteRule | undefined
|
|
138
|
+
export declare function cachingRouteRules(routeRules: Record<string, InspectableRouteRule | undefined>, publicAssetBaseURLs?: readonly string[]): OverlongRoute[];
|
|
132
139
|
/** Route rules whose declared window outlives the retained builds. */
|
|
133
|
-
export declare function overlongRouteRules(routeRules: Record<string, InspectableRouteRule | undefined>, ceilingSeconds: number): OverlongRoute[];
|
|
140
|
+
export declare function overlongRouteRules(routeRules: Record<string, InspectableRouteRule | undefined>, ceilingSeconds: number, publicAssetBaseURLs?: readonly string[]): OverlongRoute[];
|
|
134
141
|
/**
|
|
135
142
|
* Reads the request fields the policy needs off an H3 event.
|
|
136
143
|
*
|
|
@@ -210,3 +217,4 @@ export declare function htmlCacheCapability(input: {
|
|
|
210
217
|
maxNumberOfVersions: number;
|
|
211
218
|
assetRecovery: boolean;
|
|
212
219
|
}): HtmlCacheCapability | null;
|
|
220
|
+
export {};
|
|
@@ -54,11 +54,49 @@ export function skewCacheCeilingSeconds(retentionDays) {
|
|
|
54
54
|
return 0;
|
|
55
55
|
return Math.max(0, Math.floor(retentionDays * 24 * 60 * 60));
|
|
56
56
|
}
|
|
57
|
+
const SHARED_CACHE_CONTROL_HEADERS = [
|
|
58
|
+
"cloudflare-cdn-cache-control",
|
|
59
|
+
"cdn-cache-control",
|
|
60
|
+
"cache-control"
|
|
61
|
+
];
|
|
62
|
+
const NON_HTML_ROUTE_PREFIXES = [
|
|
63
|
+
"/.well-known",
|
|
64
|
+
"/api",
|
|
65
|
+
"/assets",
|
|
66
|
+
"/fonts",
|
|
67
|
+
"/images",
|
|
68
|
+
"/mcp"
|
|
69
|
+
];
|
|
70
|
+
const NON_HTML_EXTENSION_RE = /\.(?:avif|css|csv|gif|ico|jpe?g|js|json|map|md|mjs|pdf|png|svg|txt|webmanifest|webp|woff2?|xml)(?:$|[?*])/i;
|
|
71
|
+
const HTML_CONTENT_TYPE_RE = /^(?:application\/xhtml\+xml|text\/html)(?:\s*;|$)/i;
|
|
72
|
+
export function sharedCacheControlHeader(getHeader) {
|
|
73
|
+
for (const name of SHARED_CACHE_CONTROL_HEADERS) {
|
|
74
|
+
const value = getHeader(name);
|
|
75
|
+
if (value !== void 0)
|
|
76
|
+
return { name, value };
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
function isHtmlCapableRoute(route, rule, publicAssetPrefixes) {
|
|
81
|
+
const contentType = rule.headers && Object.entries(rule.headers).find(([name]) => name.toLowerCase() === "content-type")?.[1];
|
|
82
|
+
if (typeof contentType === "string")
|
|
83
|
+
return HTML_CONTENT_TYPE_RE.test(contentType);
|
|
84
|
+
if (route.startsWith("/_") || NON_HTML_EXTENSION_RE.test(route))
|
|
85
|
+
return false;
|
|
86
|
+
if (NON_HTML_ROUTE_PREFIXES.some((prefix) => route === prefix || route.startsWith(`${prefix}/`)))
|
|
87
|
+
return false;
|
|
88
|
+
return !publicAssetPrefixes.some((prefix) => route === prefix || route.startsWith(`${prefix}/`));
|
|
89
|
+
}
|
|
57
90
|
function ruleWindow(rule) {
|
|
58
|
-
const header = rule.headers && Object.entries(rule.headers).find(([
|
|
59
|
-
const fromHeader = sharedCacheSeconds(header);
|
|
60
|
-
if (fromHeader !== null)
|
|
61
|
-
return {
|
|
91
|
+
const header = sharedCacheControlHeader((name) => rule.headers && Object.entries(rule.headers).find(([candidate]) => candidate.toLowerCase() === name)?.[1]);
|
|
92
|
+
const fromHeader = sharedCacheSeconds(header?.value);
|
|
93
|
+
if (fromHeader !== null) {
|
|
94
|
+
return {
|
|
95
|
+
seconds: fromHeader,
|
|
96
|
+
source: "cache-control",
|
|
97
|
+
fromMaxAgeAlone: header?.name === "cache-control" && sharedWindowFromMaxAgeAlone(header.value)
|
|
98
|
+
};
|
|
99
|
+
}
|
|
62
100
|
const cache = rule.cache;
|
|
63
101
|
if (cache && typeof cache === "object" && typeof cache.maxAge === "number") {
|
|
64
102
|
const stale = typeof cache.staleMaxAge === "number" ? cache.staleMaxAge : 0;
|
|
@@ -81,19 +119,22 @@ function ruleWindow(rule) {
|
|
|
81
119
|
}
|
|
82
120
|
return null;
|
|
83
121
|
}
|
|
84
|
-
export function cachingRouteRules(routeRules) {
|
|
122
|
+
export function cachingRouteRules(routeRules, publicAssetBaseURLs = []) {
|
|
85
123
|
const caching = [];
|
|
124
|
+
const publicAssetPrefixes = publicAssetBaseURLs.map((baseURL) => `/${baseURL}`.replace(/^\/{2,}/, "/").replace(/\/+$/, "")).filter((prefix) => prefix !== "" && prefix !== "/");
|
|
86
125
|
for (const [route, rule] of Object.entries(routeRules ?? {})) {
|
|
87
126
|
if (!rule || typeof rule !== "object")
|
|
88
127
|
continue;
|
|
128
|
+
if (!isHtmlCapableRoute(route, rule, publicAssetPrefixes))
|
|
129
|
+
continue;
|
|
89
130
|
const window = ruleWindow(rule);
|
|
90
131
|
if (window)
|
|
91
132
|
caching.push({ route, seconds: window.seconds, source: window.source, fromMaxAgeAlone: window.fromMaxAgeAlone });
|
|
92
133
|
}
|
|
93
134
|
return caching;
|
|
94
135
|
}
|
|
95
|
-
export function overlongRouteRules(routeRules, ceilingSeconds) {
|
|
96
|
-
return cachingRouteRules(routeRules).filter((route) => route.seconds > ceilingSeconds);
|
|
136
|
+
export function overlongRouteRules(routeRules, ceilingSeconds, publicAssetBaseURLs = []) {
|
|
137
|
+
return cachingRouteRules(routeRules, publicAssetBaseURLs).filter((route) => route.seconds > ceilingSeconds);
|
|
97
138
|
}
|
|
98
139
|
export function htmlCacheRequestFromEvent(event, getHeader) {
|
|
99
140
|
return {
|