Skip to content

Commit 10a227c

Browse files
committed
feat: support skew protection
1 parent 2ee7716 commit 10a227c

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/build/plugin-context.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ export class PluginContext {
207207
return join(this.edgeFunctionsDir, EDGE_HANDLER_NAME)
208208
}
209209

210+
/** Absolute path to the skew protection config */
211+
get skewProtectionConfigPath(): string {
212+
return this.resolveFromPackagePath('.netlify/v1/skew-protection.json')
213+
}
214+
210215
constructor(options: NetlifyPluginOptions) {
211216
this.constants = options.constants
212217
this.featureFlags = options.featureFlags

src/build/skew-protection.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { mkdir, writeFile } from 'node:fs/promises'
2+
import { dirname } from 'node:path'
3+
4+
import { PluginContext } from './plugin-context.js'
5+
6+
export const setSkewProtection = async (ctx: PluginContext) => {
7+
if (!process.env.DEPLOY_ID || process.env.DEPLOY_ID === '0') {
8+
return
9+
}
10+
11+
console.log('[Next Runtime] Setting up Next.js Skew Protection')
12+
13+
process.env.NEXT_DEPLOYMENT_ID = process.env.DEPLOY_ID
14+
15+
await mkdir(dirname(ctx.skewProtectionConfigPath), {
16+
recursive: true,
17+
})
18+
await writeFile(
19+
ctx.skewProtectionConfigPath,
20+
JSON.stringify(
21+
{
22+
patterns: ['.*'],
23+
sources: [
24+
{
25+
type: 'cookie',
26+
name: '__vdpl',
27+
},
28+
{
29+
type: 'header',
30+
name: 'X-Deployment-Id',
31+
},
32+
{
33+
type: 'query',
34+
name: 'dpl',
35+
},
36+
],
37+
},
38+
null,
39+
2,
40+
),
41+
)
42+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { clearStaleEdgeHandlers, createEdgeHandlers } from './build/functions/ed
1818
import { clearStaleServerHandlers, createServerHandler } from './build/functions/server.js'
1919
import { setImageConfig } from './build/image-cdn.js'
2020
import { PluginContext } from './build/plugin-context.js'
21+
import { setSkewProtection } from './build/skew-protection.js'
2122
import {
2223
verifyAdvancedAPIRoutes,
2324
verifyNetlifyFormsWorkaround,
@@ -62,6 +63,7 @@ export const onPreBuild = async (options: NetlifyPluginOptions) => {
6263
} else {
6364
await restoreBuildCache(ctx)
6465
}
66+
await setSkewProtection(ctx)
6567
})
6668
}
6769

0 commit comments

Comments
 (0)