Skip to content

Commit 64c3c72

Browse files
feat: support skew protection token (#7796)
* feat: support skew protection token * chore: remove claude file * refactor: move options to `getRunBuildOptions`
1 parent d619104 commit 64c3c72

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/commands/deploy/deploy.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -680,29 +680,36 @@ const handleBuild = async ({
680680
currentDir,
681681
defaultConfig,
682682
deployHandler,
683+
deployId,
683684
options,
684685
packagePath,
686+
skewProtectionToken,
685687
}: {
686688
cachedConfig: CachedConfig
687689
currentDir: string
688690
defaultConfig?: DefaultConfig | undefined
689691
deployHandler?: PatchedHandlerType<OnPostBuild> | undefined
692+
deployId?: string
690693
options: DeployOptionValues
691694
packagePath: string | undefined
695+
skewProtectionToken?: string
692696
}) => {
693697
if (!options.build) {
694698
return {}
695699
}
696700
const [token] = await getToken()
697701
const resolvedOptions = await getRunBuildOptions({
698702
cachedConfig,
703+
currentDir,
699704
defaultConfig,
705+
deployHandler,
706+
deployId,
707+
options,
700708
packagePath,
709+
skewProtectionToken,
701710
token,
702-
options,
703-
currentDir,
704-
deployHandler,
705711
})
712+
706713
const { configMutations, exitCode, newConfig, logs } = await runBuild(resolvedOptions)
707714
// Without this, the deploy command fails silently
708715
if (exitCode !== 0) {
@@ -1112,13 +1119,7 @@ export const deploy = async (options: DeployOptionValues, command: BaseCommand)
11121119
source_zip_filename?: string
11131120
}
11141121
const deployId = deployMetadata.id || ''
1115-
const deployUrl = deployMetadata.deploy_ssl_url || deployMetadata.deploy_url || ''
1116-
1117-
command.netlify.cachedConfig.env.DEPLOY_ID = { sources: ['internal'], value: deployId }
1118-
command.netlify.cachedConfig.env.DEPLOY_URL = { sources: ['internal'], value: deployUrl }
1119-
1120-
process.env.DEPLOY_ID = deployId
1121-
process.env.DEPLOY_URL = deployUrl
1122+
const skewProtectionToken = deployMetadata.skew_protection_token
11221123

11231124
if (
11241125
options.uploadSourceZip &&
@@ -1157,6 +1158,8 @@ export const deploy = async (options: DeployOptionValues, command: BaseCommand)
11571158

11581159
return {}
11591160
},
1161+
deployId,
1162+
skewProtectionToken,
11601163
})
11611164
} catch (error) {
11621165
// The build has failed, so let's cancel the deploy we created.

src/lib/build.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,20 @@ export const getRunBuildOptions = async ({
132132
currentDir,
133133
defaultConfig,
134134
deployHandler,
135+
deployId,
135136
options: { context, cwd, debug, dry, json, offline, silent },
136137
packagePath,
138+
skewProtectionToken,
137139
token,
138140
}: {
139141
cachedConfig: CachedConfig
140142
currentDir: string
141143
defaultConfig?: undefined | DefaultConfig
142144
deployHandler?: PatchedHandlerType<OnPostBuild>
145+
deployId?: string
143146
options: OptionValues
144147
packagePath?: string
148+
skewProtectionToken?: string
145149
token?: null | string
146150
}): Promise<RunBuildOptions> => {
147151
const eventHandlers: { onEnd: EventHandler<OnEnd>; onPostBuild?: EventHandler<OnPostBuild> } = {
@@ -170,6 +174,7 @@ export const getRunBuildOptions = async ({
170174
return {
171175
cachedConfig,
172176
defaultConfig: defaultConfig ?? {},
177+
deployId,
173178
siteId: cachedConfig.siteInfo.id,
174179
accountId: cachedConfig.siteInfo.account_id,
175180
packagePath,
@@ -191,6 +196,7 @@ export const getRunBuildOptions = async ({
191196
// @ts-expect-error(serhalp) -- TODO(serhalp): Upstream the type fixes above into @netlify/build
192197
eventHandlers,
193198
edgeFunctionsBootstrapURL: await getBootstrapURL(),
199+
skewProtectionToken,
194200
}
195201
}
196202

tests/integration/commands/deploy/deploy.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,16 @@ describe.skipIf(process.env.NETLIFY_TEST_DISABLE_LIVE === 'true').concurrent('co
388388
name: 'log-env',
389389
plugin: {
390390
async onPreBuild() {
391-
const { DEPLOY_ID, DEPLOY_URL } = require('process').env
391+
const { DEPLOY_ID, DEPLOY_URL, NETLIFY_SKEW_PROTECTION_TOKEN } = require('process').env
392392
console.log(`DEPLOY_ID_PREBUILD: ${DEPLOY_ID}`)
393393
console.log(`DEPLOY_URL_PREBUILD: ${DEPLOY_URL}`)
394+
console.log(`NETLIFY_SKEW_PROTECTION_TOKEN_PREBUILD: ${NETLIFY_SKEW_PROTECTION_TOKEN}`)
394395
},
395396
async onSuccess() {
396-
const { DEPLOY_ID, DEPLOY_URL } = require('process').env
397+
const { DEPLOY_ID, DEPLOY_URL, NETLIFY_SKEW_PROTECTION_TOKEN } = require('process').env
397398
console.log(`DEPLOY_ID: ${DEPLOY_ID}`)
398399
console.log(`DEPLOY_URL: ${DEPLOY_URL}`)
400+
console.log(`NETLIFY_SKEW_PROTECTION_TOKEN: ${NETLIFY_SKEW_PROTECTION_TOKEN}`)
399401
},
400402
},
401403
})
@@ -424,15 +426,20 @@ describe.skipIf(process.env.NETLIFY_TEST_DISABLE_LIVE === 'true').concurrent('co
424426
t.expect(output).toContain('Netlify Build completed in')
425427
const [, deployIdPreBuild] = output.match(/DEPLOY_ID_PREBUILD: (\w+)/) ?? []
426428
const [, deployURLPreBuild] = output.match(/DEPLOY_URL_PREBUILD: (.+)/) ?? []
429+
const [, skewProtectionTokenPreBuild] = output.match(/NETLIFY_SKEW_PROTECTION_TOKEN_PREBUILD: (.+)/) ?? []
427430
const [, deployId] = output.match(/DEPLOY_ID: (\w+)/) ?? []
428431
const [, deployURL] = output.match(/DEPLOY_URL: (.+)/) ?? []
432+
const [, skewProtectionToken] = output.match(/NETLIFY_SKEW_PROTECTION_TOKEN: (.+)/) ?? []
429433

430434
t.expect(deployIdPreBuild).toBeTruthy()
431435
t.expect(deployIdPreBuild).not.toEqual('0')
432436
t.expect(deployURLPreBuild).toContain(`https://${deployIdPreBuild}--`)
433437
t.expect(deployId).toEqual(deployIdPreBuild)
434438
t.expect(deployURL).toEqual(deployURLPreBuild)
435439

440+
t.expect(skewProtectionTokenPreBuild).toEqual(skewProtectionToken)
441+
t.expect(skewProtectionToken).toBeTruthy()
442+
436443
await validateContent({ siteUrl: deployURL, path: '', content: rootContent })
437444
await validateContent({ siteUrl: deployURL, path: '/edge-function', content: 'Hello from edge function' })
438445
await validateContent({ siteUrl: deployURL, path: '/function', content: 'Hello from function' })

0 commit comments

Comments
 (0)