@elumixor/devkit 0.12.11 → 0.12.12
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/package.json +1 -1
- package/src/cloud/env-leak.ts +37 -0
- package/src/deploy-targets/aws.ts +10 -2
package/package.json
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readdir } from "node:fs/promises";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { CloudError } from "./aws.ts";
|
|
5
|
+
import { type Env, parseDotenv } from "./env.ts";
|
|
6
|
+
|
|
7
|
+
const DOTENV_FILES = [".env", ".env.local", ".env.production", ".env.production.local"];
|
|
8
|
+
const TEXT_FILE = /\.(js|mjs|css|html|json|txt|map)$/;
|
|
9
|
+
/** Shorter values (`1`, `true`, a port) appear in any bundle by chance. */
|
|
10
|
+
const MIN_VALUE_LENGTH = 8;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A laptop's `.env` holds development values (a test Clerk key, a local URL). A deploy builds with the
|
|
14
|
+
* stage's variables, but a bundler that reads `.env` files itself can still bake a local value in, and
|
|
15
|
+
* the result is a production site talking to development services. This refuses such a build.
|
|
16
|
+
*/
|
|
17
|
+
export async function assertNoLocalEnvInBuild(staticDir: string, dirs: readonly string[], stageEnv: Env) {
|
|
18
|
+
const leaks = new Map<string, string>();
|
|
19
|
+
for (const dir of dirs)
|
|
20
|
+
for (const name of DOTENV_FILES) {
|
|
21
|
+
const file = join(dir, name);
|
|
22
|
+
if (!existsSync(file)) continue;
|
|
23
|
+
for (const [key, value] of Object.entries(parseDotenv(await Bun.file(file).text())))
|
|
24
|
+
if (key in stageEnv && stageEnv[key] !== value && value.length >= MIN_VALUE_LENGTH) leaks.set(value, key);
|
|
25
|
+
}
|
|
26
|
+
if (!leaks.size || !existsSync(staticDir)) return;
|
|
27
|
+
|
|
28
|
+
for (const entry of await readdir(staticDir, { recursive: true, withFileTypes: true })) {
|
|
29
|
+
if (!entry.isFile() || !TEXT_FILE.test(entry.name)) continue;
|
|
30
|
+
const text = await Bun.file(resolve(entry.parentPath, entry.name)).text();
|
|
31
|
+
for (const [value, key] of leaks)
|
|
32
|
+
if (text.includes(value))
|
|
33
|
+
throw new CloudError(
|
|
34
|
+
`The build has the local .env value of ${key} in ${entry.name}, not the stage's — nothing was uploaded`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { join, resolve } from "node:path";
|
|
2
|
+
import { AWS_OUTPUT, buildWeb } from "../build-targets/web.ts";
|
|
2
3
|
import { readStageEnv } from "../cloud/env.ts";
|
|
4
|
+
import { assertNoLocalEnvInBuild } from "../cloud/env-leak.ts";
|
|
3
5
|
import { STAGE_BRANCHES } from "../cloud/names.ts";
|
|
4
6
|
import { type AwsTargetOptions, projectName, resolveStage, Target } from "../cloud/project.ts";
|
|
5
7
|
import { release } from "../cloud/release.ts";
|
|
@@ -19,8 +21,14 @@ export async function deployAws(options: AwsTargetOptions) {
|
|
|
19
21
|
|
|
20
22
|
// The stage's variables are the build's too, so `VITE_*` values reach the frontend bundle in CI
|
|
21
23
|
// exactly as they do from a laptop's `.env`.
|
|
22
|
-
|
|
24
|
+
const stageEnv = await readStageEnv(target);
|
|
25
|
+
Object.assign(process.env, stageEnv);
|
|
23
26
|
|
|
24
27
|
await buildWeb({ ...options, host: "aws", static: target.isStatic });
|
|
28
|
+
await assertNoLocalEnvInBuild(
|
|
29
|
+
join(root, AWS_OUTPUT, "static"),
|
|
30
|
+
[root, resolve(root, options.frontendDir ?? "apps/frontend")],
|
|
31
|
+
stageEnv,
|
|
32
|
+
);
|
|
25
33
|
await release(target, { root });
|
|
26
34
|
}
|