-
Notifications
You must be signed in to change notification settings - Fork 53
feat: add support for skew protection #746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@opennextjs/cloudflare": minor | ||
--- | ||
|
||
feat: add support for experimental skew protection |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ pnpm-lock.yaml | |
.vscode/setting.json | ||
test-fixtures | ||
test-snapshots | ||
playwright-report | ||
playwright-report |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import { defineCloudflareConfig } from "@opennextjs/cloudflare"; | ||
import kvIncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/kv-incremental-cache"; | ||
import { defineCloudflareConfig, type OpenNextConfig } from "@opennextjs/cloudflare"; | ||
import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache"; | ||
|
||
export default defineCloudflareConfig({ | ||
incrementalCache: kvIncrementalCache, | ||
enableCacheInterception: true, | ||
}); | ||
export default { | ||
...defineCloudflareConfig({ | ||
incrementalCache: r2IncrementalCache, | ||
}), | ||
cloudflare: { | ||
skewProtection: { | ||
enabled: false, | ||
}, | ||
}, | ||
} satisfies OpenNextConfig; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./cloudflare-context.js"; | ||
export { defineCloudflareConfig, type OpenNextConfig } from "./config.js"; | ||
export { defineCloudflareConfig, getDeploymentId, type OpenNextConfig } from "./config.js"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import path from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
|
||
import type { BuildOptions } from "@opennextjs/aws/build/helper.js"; | ||
import { build } from "esbuild"; | ||
|
||
import type { OpenNextConfig } from "../../../api"; | ||
|
||
export async function compileSkewProtection(options: BuildOptions, config: OpenNextConfig) { | ||
const currentDir = path.join(path.dirname(fileURLToPath(import.meta.url))); | ||
const templatesDir = path.join(currentDir, "../../templates"); | ||
const initPath = path.join(templatesDir, "skew-protection.js"); | ||
|
||
const skewProtectionEnabled = config.cloudflare?.skewProtection?.enabled === true; | ||
|
||
await build({ | ||
entryPoints: [initPath], | ||
outdir: path.join(options.outputDir, "cloudflare"), | ||
bundle: false, | ||
minify: false, | ||
format: "esm", | ||
target: "esnext", | ||
platform: "node", | ||
define: { | ||
__SKEW_PROTECTION_ENABLED__: JSON.stringify(skewProtectionEnabled), | ||
}, | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,41 @@ | ||
import { BuildOptions } from "@opennextjs/aws/build/helper.js"; | ||
|
||
import type { OpenNextConfig } from "../../api/config.js"; | ||
import { DEPLOYMENT_MAPPING_ENV_NAME } from "../templates/skew-protection.js"; | ||
import { getWranglerEnvironmentFlag, runWrangler } from "../utils/run-wrangler.js"; | ||
import { getEnvFromPlatformProxy, quoteShellMeta } from "./helpers.js"; | ||
import { populateCache } from "./populate-cache.js"; | ||
import { getDeploymentMapping } from "./skew-protection.js"; | ||
|
||
export async function deploy( | ||
options: BuildOptions, | ||
config: OpenNextConfig, | ||
deployOptions: { passthroughArgs: string[]; cacheChunkSize?: number } | ||
) { | ||
const envVars = await getEnvFromPlatformProxy({ | ||
// TODO: Pass the configPath, update everywhere applicable | ||
environment: getWranglerEnvironmentFlag(deployOptions.passthroughArgs), | ||
}); | ||
|
||
const deploymentMapping = await getDeploymentMapping(options, config, envVars); | ||
|
||
await populateCache(options, config, { | ||
target: "remote", | ||
environment: getWranglerEnvironmentFlag(deployOptions.passthroughArgs), | ||
cacheChunkSize: deployOptions.cacheChunkSize, | ||
}); | ||
|
||
runWrangler(options, ["deploy", ...deployOptions.passthroughArgs], { logging: "all" }); | ||
runWrangler( | ||
options, | ||
[ | ||
"deploy", | ||
...deployOptions.passthroughArgs, | ||
...(deploymentMapping | ||
? [`--var ${DEPLOYMENT_MAPPING_ENV_NAME}:${quoteShellMeta(JSON.stringify(deploymentMapping))}`] | ||
: []), | ||
], | ||
{ | ||
logging: "all", | ||
} | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { getPlatformProxy, type GetPlatformProxyOptions } from "wrangler"; | ||
|
||
export type WorkerEnvVar = Record<keyof CloudflareEnv, string | undefined>; | ||
|
||
/** | ||
* Return the string env vars from the worker. | ||
* | ||
* @param options Options to pass to `getPlatformProxy`, i.e. to set the environment | ||
* @returns the env vars | ||
*/ | ||
export async function getEnvFromPlatformProxy(options: GetPlatformProxyOptions) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should have only one instance of this, that we spawn at the beginning and that we dispose at the end. It's a bit wasteful to launch multiple workerd process. |
||
const envVars = {} as WorkerEnvVar; | ||
const proxy = await getPlatformProxy<CloudflareEnv>(options); | ||
Object.entries(proxy.env).forEach(([key, value]) => { | ||
if (typeof value === "string") { | ||
envVars[key as keyof CloudflareEnv] = value; | ||
} | ||
}); | ||
await proxy.dispose(); | ||
return envVars; | ||
} | ||
|
||
/** | ||
* Escape shell metacharacters. | ||
* | ||
* When `spawnSync` is invoked with `shell: true`, metacharacters need to be escaped. | ||
* | ||
* Based on https://github.com/ljharb/shell-quote/blob/main/quote.js | ||
* | ||
* @param arg | ||
* @returns escaped arg | ||
*/ | ||
export function quoteShellMeta(arg: string) { | ||
if (/["\s]/.test(arg) && !/'/.test(arg)) { | ||
return `'${arg.replace(/(['\\])/g, "\\$1")}'`; | ||
} | ||
if (/["'\s]/.test(arg)) { | ||
return `"${arg.replace(/(["\\$`!])/g, "\\$1")}"`; | ||
} | ||
return arg.replace(/([A-Za-z]:)?([#!"$&'()*,:;<=>?@[\\\]^`{|}])/g, "$1\\$2"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used this API for debugging (the env contains the build id, deployment id, and deployment mapping) and the response was easier to read when formatted this way.