@griddo/ax 12.18.0 → 12.18.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/config/env.js +25 -34
- package/config/lib/bundleSecrets.js +159 -0
- package/config/lib/publicEnv.js +44 -0
- package/package.json +4 -4
- package/scripts/build.js +5 -0
- package/scripts/check-bundle-secrets.js +109 -0
- package/src/__tests__/helpers/bundleSecrets.test.ts +261 -0
- package/src/__tests__/helpers/bundleSecretsLiterals.test.ts +35 -0
- package/src/__tests__/helpers/checkBundleSecrets.test.ts +253 -0
- package/src/__tests__/helpers/clientEnvironment.test.ts +135 -0
- package/src/__tests__/helpers/publicEnv.test.ts +96 -0
- package/src/__tests__/helpers/publicEnvUsage.test.ts +105 -0
- package/src/components/Fields/Wysiwyg/config.tsx +1 -1
- package/src/modules/Login/index.tsx +1 -1
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
|
|
3
|
+
const require = createRequire(import.meta.url);
|
|
4
|
+
const { findSecretLeaks, KNOWN_LITERAL_KEYS } = require("../../../config/lib/bundleSecrets.js") as {
|
|
5
|
+
findSecretLeaks: (input: {
|
|
6
|
+
files: { name: string; content: string }[];
|
|
7
|
+
env: Record<string, string | undefined>;
|
|
8
|
+
allowedKeys: readonly string[];
|
|
9
|
+
knownLiterals: readonly string[];
|
|
10
|
+
}) => { kind: string; name: string; file: string }[];
|
|
11
|
+
KNOWN_LITERAL_KEYS: readonly string[];
|
|
12
|
+
};
|
|
13
|
+
const { PUBLIC_ENV_KEYS } = require("../../../config/lib/publicEnv.js") as { PUBLIC_ENV_KEYS: readonly string[] };
|
|
14
|
+
|
|
15
|
+
/** Valor de `n` caracteres, alfanumérico y sin parecerse a ningún nombre de variable. */
|
|
16
|
+
const value = (n: number, seed = "Zq7wK") => seed.repeat(Math.ceil(n / seed.length)).slice(0, n);
|
|
17
|
+
|
|
18
|
+
const scan = (env: Record<string, string | undefined>, content: string, file = "main.js") =>
|
|
19
|
+
findSecretLeaks({
|
|
20
|
+
files: [{ name: file, content }],
|
|
21
|
+
env,
|
|
22
|
+
allowedKeys: PUBLIC_ENV_KEYS,
|
|
23
|
+
knownLiterals: KNOWN_LITERAL_KEYS,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("[UC-AX-bundle-public-config] findSecretLeaks · valores de variables de entorno", () => {
|
|
27
|
+
it.each([
|
|
28
|
+
["GRIDDO_NPM_TOKEN", 40, true],
|
|
29
|
+
["GRIDDO_SLACKCH", 60, true],
|
|
30
|
+
["botPassword", 16, true],
|
|
31
|
+
["developerKey", 36, true],
|
|
32
|
+
["GRIDDO_NPM_TOKEN", 15, false],
|
|
33
|
+
["MONKEY_NAME", 40, false],
|
|
34
|
+
])("AC3 · %s con valor de %i caracteres en el bundle → falla: %s", (name, length, fails) => {
|
|
35
|
+
const v = value(length);
|
|
36
|
+
|
|
37
|
+
const leaks = scan({ [name]: v }, `var a="${v}";`);
|
|
38
|
+
|
|
39
|
+
expect(leaks.length > 0).toBe(fails);
|
|
40
|
+
if (fails) expect(leaks).toEqual([{ kind: "env-value", name, file: "main.js" }]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it.each([
|
|
44
|
+
["MY_KEY_X", true],
|
|
45
|
+
["KEY", true],
|
|
46
|
+
["my_token", true],
|
|
47
|
+
["GRIDDO_PASSW", true],
|
|
48
|
+
["WEBHOOK_URL", true],
|
|
49
|
+
["SOME_CREDENTIAL", true],
|
|
50
|
+
["APP_SECRET", true],
|
|
51
|
+
["MONKEY", false],
|
|
52
|
+
["TOKENIZER", false],
|
|
53
|
+
["DONKEY_KONG_X", false],
|
|
54
|
+
])("AC3 · el nombre %s se compara por segmentos y sin distinguir mayúsculas → falla: %s", (name, fails) => {
|
|
55
|
+
const v = value(40);
|
|
56
|
+
|
|
57
|
+
const leaks = scan({ [name]: v }, `var a="${v}";`);
|
|
58
|
+
|
|
59
|
+
expect(leaks.length > 0).toBe(fails);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it.each([
|
|
63
|
+
["+ / =", "Zq7+wK/Zq7=wK"],
|
|
64
|
+
[". * ( )", "Zq7.wK*Zq7(wK)Zq7"],
|
|
65
|
+
])("AC3 · un secreto con %s se detecta tal cual (búsqueda literal, no regex)", (_c, base) => {
|
|
66
|
+
const v = base.repeat(4).slice(0, 40);
|
|
67
|
+
|
|
68
|
+
const leaks = scan({ API_SECRET: v }, `var a="${v}";`);
|
|
69
|
+
|
|
70
|
+
expect(leaks).toHaveLength(1);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("AC3 · un secreto con comillas y barra se detecta escapado como JSON", () => {
|
|
74
|
+
const v = `Zq7"wK\\Zq7"wK\\Zq7"wK\\Zq7"wK\\Zq7"wK`.slice(0, 40);
|
|
75
|
+
const escaped = JSON.stringify(v).slice(1, -1);
|
|
76
|
+
expect(escaped).not.toBe(v);
|
|
77
|
+
|
|
78
|
+
const leaks = scan({ API_SECRET: v }, `var a="${escaped}";`);
|
|
79
|
+
|
|
80
|
+
expect(leaks).toHaveLength(1);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("AC3 · la búsqueda no interpreta el valor como expresión regular", () => {
|
|
84
|
+
const v = "Zq7.*Zq7.*Zq7.*Zq7.*";
|
|
85
|
+
|
|
86
|
+
const leaks = scan({ API_SECRET: v }, "var a='Zq7XXXXZq7XXXXZq7XXXX';");
|
|
87
|
+
|
|
88
|
+
expect(leaks).toEqual([]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it.each(["QodanaToken", "apiKey", "githubToken", "myApiKey", "slackWebhook"])(
|
|
92
|
+
"AC3 · el nombre en camelCase %s se parte en palabras y se reconoce como secreto",
|
|
93
|
+
(name) => {
|
|
94
|
+
const v = value(40);
|
|
95
|
+
|
|
96
|
+
expect(scan({ [name]: v }, `var a="${v}";`)).toEqual([{ kind: "env-value", name, file: "main.js" }]);
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
it.each(["Monkey", "turkeyName", "keyboard", "Tokenizer"])(
|
|
101
|
+
"AC3 · el nombre %s no contiene una palabra sensible aunque contenga sus letras",
|
|
102
|
+
(name) => {
|
|
103
|
+
const v = value(40);
|
|
104
|
+
|
|
105
|
+
expect(scan({ [name]: v }, `var a="${v}";`)).toEqual([]);
|
|
106
|
+
},
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
it("AC3 · un secreto con caracteres no ASCII se detecta cuando el minificador lo escribe como \\uXXXX", () => {
|
|
110
|
+
const v = "Zq7wKéñ€Zq7wKéñ€Zq7wKéñ€Zq7wKéñ€";
|
|
111
|
+
const escaped = [...v]
|
|
112
|
+
.map((c) => (c.charCodeAt(0) > 127 ? `\\u${c.charCodeAt(0).toString(16).padStart(4, "0")}` : c))
|
|
113
|
+
.join("");
|
|
114
|
+
expect(escaped).not.toBe(v);
|
|
115
|
+
|
|
116
|
+
expect(scan({ API_SECRET: v }, `var a="${escaped}";`)).toHaveLength(1);
|
|
117
|
+
expect(
|
|
118
|
+
scan(
|
|
119
|
+
{ API_SECRET: v },
|
|
120
|
+
`var a="${escaped.toUpperCase().replaceAll("\\U", "\\u").replaceAll("ZQ7WK", "Zq7wK")}";`,
|
|
121
|
+
),
|
|
122
|
+
).toHaveLength(1);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("AC3 · una variable fuera de la lista cuyo valor es el de una variable pública no es una fuga", () => {
|
|
126
|
+
const v = value(40);
|
|
127
|
+
|
|
128
|
+
expect(scan({ GRIDDO_FROALA_KEY: v, REACT_APP_FROALA_KEY: v }, `var a="${v}";`)).toEqual([]);
|
|
129
|
+
expect(
|
|
130
|
+
scan({ GRIDDO_FROALA_KEY: v, REACT_APP_FROALA_KEY: value(40, "Pw9xJ") }, `var a="${value(40, "Pw9xJ")}";`),
|
|
131
|
+
).toHaveLength(1);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("AC3 · una variable de la lista pública con nombre de clave no hace fallar", () => {
|
|
135
|
+
const v = value(40);
|
|
136
|
+
|
|
137
|
+
const leaks = scan({ GRIDDO_FROALA_KEY: v }, `var a="${v}";`);
|
|
138
|
+
|
|
139
|
+
expect(leaks).toEqual([]);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("AC3 · cualquier GRIDDO_/REACT_APP_ fuera de la lista con valor de 16+ cuenta como secreto, sea cual sea su nombre", () => {
|
|
143
|
+
const v = value(20);
|
|
144
|
+
|
|
145
|
+
expect(scan({ griddo_whatever: v }, `"${v}"`)).toHaveLength(1);
|
|
146
|
+
expect(scan({ REACT_APP_OTHER: v }, `"${v}"`)).toHaveLength(1);
|
|
147
|
+
expect(scan({ GRIDDO_ID_ENABLED: v }, `"${v}"`)).toEqual([]);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("AC3 · ignora variables sin valor, vacías o solo presentes en el entorno (no en el bundle)", () => {
|
|
151
|
+
const v = value(40);
|
|
152
|
+
|
|
153
|
+
expect(scan({ API_SECRET: undefined, OTHER_TOKEN: "" }, `"${v}"`)).toEqual([]);
|
|
154
|
+
expect(scan({ API_SECRET: v }, "var a=1;")).toEqual([]);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it.each(["main.js", "main.js.map", "index.html"])("AC3 · informa del fichero %s donde aparece el valor", (file) => {
|
|
158
|
+
const v = value(40);
|
|
159
|
+
|
|
160
|
+
const leaks = scan({ API_SECRET: v }, `x ${v} y`, file);
|
|
161
|
+
|
|
162
|
+
expect(leaks).toEqual([{ kind: "env-value", name: "API_SECRET", file }]);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("AC5 · los hallazgos nunca contienen el valor, ni siquiera parcial", () => {
|
|
166
|
+
const v = value(40, "Pw9xJ");
|
|
167
|
+
|
|
168
|
+
const leaks = scan({ API_SECRET: v }, `x ${v} y`);
|
|
169
|
+
|
|
170
|
+
const dump = JSON.stringify(leaks);
|
|
171
|
+
expect(dump).not.toContain(v);
|
|
172
|
+
expect(dump).not.toContain(v.slice(0, 8));
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("AC3 · no repite el mismo hallazgo si el valor aparece varias veces en un fichero", () => {
|
|
176
|
+
const v = value(40);
|
|
177
|
+
|
|
178
|
+
expect(scan({ API_SECRET: v }, `${v} ${v} ${v}`)).toHaveLength(1);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe("[UC-AX-bundle-public-config] findSecretLeaks · tokens de registro y claves de entorno", () => {
|
|
183
|
+
const npm = (n: number) => `npm_${value(n, "aB3dE")}`;
|
|
184
|
+
|
|
185
|
+
it("AC4 · un token npm_ + 36 alfanuméricos hace fallar", () => {
|
|
186
|
+
const leaks = scan({}, `var t="${npm(36)}";`);
|
|
187
|
+
|
|
188
|
+
expect(leaks).toEqual([{ kind: "npm-token", name: "npm_token", file: "main.js" }]);
|
|
189
|
+
expect(JSON.stringify(leaks)).not.toContain(npm(36));
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it.each([
|
|
193
|
+
[35, false],
|
|
194
|
+
[36, true],
|
|
195
|
+
[37, true],
|
|
196
|
+
])("AC4 · npm_ seguido de %i caracteres → falla: %s", (n, fails) => {
|
|
197
|
+
expect(scan({}, `var t="${npm(n)}";`).length > 0).toBe(fails);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it.each([
|
|
201
|
+
["GRIDDO_botPassword", true],
|
|
202
|
+
["griddo_npm_token", true],
|
|
203
|
+
["GRIDDO_NPM_TOKEN", true],
|
|
204
|
+
["REACT_APP_SECRET", true],
|
|
205
|
+
["GRIDDO_API_URL", false],
|
|
206
|
+
["GRIDDO_ID", false],
|
|
207
|
+
["GRIDDO_ID_ENABLED", false],
|
|
208
|
+
["REACT_APP_INSTANCE", false],
|
|
209
|
+
])("AC4 · la clave %s en el bundle → falla: %s", (key, fails) => {
|
|
210
|
+
const leaks = scan({}, `var a=e.${key};`);
|
|
211
|
+
|
|
212
|
+
expect(leaks.length > 0).toBe(fails);
|
|
213
|
+
if (fails) expect(leaks).toEqual([{ kind: "key", name: key, file: "main.js" }]);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("AC4 · un identificador que webpack genera a partir de la ruta del módulo no es una clave de entorno", () => {
|
|
217
|
+
const id = "griddo_node_modules_pnpm_babel_runtime_helpers_esm_objectSpread2_js__WEBPACK_IMPORTED_MODULE_0__";
|
|
218
|
+
|
|
219
|
+
expect(scan({}, `var a=${id};`)).toEqual([]);
|
|
220
|
+
expect(scan({}, `var a=${id}; GRIDDO_botPassword`)).toHaveLength(1);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("AC4 · los literales conocidos incluyen los del iframe, los contextos de core y los de styled-components", () => {
|
|
224
|
+
expect(KNOWN_LITERAL_KEYS).toEqual(
|
|
225
|
+
expect.arrayContaining([
|
|
226
|
+
"GRIDDO_ID",
|
|
227
|
+
"GRIDDO_ID_ENABLED",
|
|
228
|
+
"GRIDDO_SITE_CONTEXT__",
|
|
229
|
+
"REACT_APP_SC_ATTR",
|
|
230
|
+
"REACT_APP_SC_DISABLE_SPEEDY",
|
|
231
|
+
"griddo_config",
|
|
232
|
+
]),
|
|
233
|
+
);
|
|
234
|
+
expect(KNOWN_LITERAL_KEYS.some((k) => /token|secret|password|passw|webhook|credential/i.test(k))).toBe(false);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("AC4 · allowedKeys y knownLiterals se respetan tal como llegan", () => {
|
|
238
|
+
const leaks = findSecretLeaks({
|
|
239
|
+
files: [{ name: "a.js", content: "GRIDDO_X GRIDDO_Y" }],
|
|
240
|
+
env: {},
|
|
241
|
+
allowedKeys: ["GRIDDO_X"],
|
|
242
|
+
knownLiterals: [],
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
expect(leaks).toEqual([{ kind: "key", name: "GRIDDO_Y", file: "a.js" }]);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("AC4 · informa de cada fichero afectado por separado", () => {
|
|
249
|
+
const leaks = findSecretLeaks({
|
|
250
|
+
files: [
|
|
251
|
+
{ name: "a.js", content: "GRIDDO_BAD" },
|
|
252
|
+
{ name: "b.js.map", content: "GRIDDO_BAD" },
|
|
253
|
+
],
|
|
254
|
+
env: {},
|
|
255
|
+
allowedKeys: [],
|
|
256
|
+
knownLiterals: [],
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
expect(leaks.map((l) => l.file)).toEqual(["a.js", "b.js.map"]);
|
|
260
|
+
});
|
|
261
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
const { KNOWN_LITERAL_KEYS } = require("../../../config/lib/bundleSecrets.js") as {
|
|
7
|
+
KNOWN_LITERAL_KEYS: readonly string[];
|
|
8
|
+
};
|
|
9
|
+
const { PUBLIC_ENV_KEYS } = require("../../../config/lib/publicEnv.js") as { PUBLIC_ENV_KEYS: readonly string[] };
|
|
10
|
+
|
|
11
|
+
const SRC_ROOT = resolve(__dirname, "../..");
|
|
12
|
+
|
|
13
|
+
const listSources = (dir: string): string[] =>
|
|
14
|
+
readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
|
15
|
+
const full = join(dir, entry.name);
|
|
16
|
+
if (entry.isDirectory()) return entry.name === "__tests__" ? [] : listSources(full);
|
|
17
|
+
return /\.(tsx?|jsx?|json|html)$/.test(entry.name) ? [full] : [];
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe("[UC-AX-bundle-public-config] literales de src/ frente al guard", () => {
|
|
21
|
+
it("AC4 · toda clave literal GRIDDO_/REACT_APP_ de src/ está en la lista pública o entre los literales conocidos", () => {
|
|
22
|
+
const allowed = new Set([...PUBLIC_ENV_KEYS, ...KNOWN_LITERAL_KEYS]);
|
|
23
|
+
const files = listSources(SRC_ROOT);
|
|
24
|
+
const offenders: string[] = [];
|
|
25
|
+
|
|
26
|
+
for (const file of files) {
|
|
27
|
+
for (const match of readFileSync(file, "utf8").matchAll(/(?:GRIDDO|REACT_APP)_[A-Za-z0-9_]+/gi)) {
|
|
28
|
+
if (!allowed.has(match[0])) offenders.push(`${file}: ${match[0]}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
expect(files.length).toBeGreaterThan(100);
|
|
33
|
+
expect(offenders).toEqual([]);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import {
|
|
3
|
+
chmodSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
mkdtempSync,
|
|
7
|
+
readdirSync,
|
|
8
|
+
readFileSync,
|
|
9
|
+
rmSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from "node:fs";
|
|
12
|
+
import { createRequire } from "node:module";
|
|
13
|
+
import { tmpdir } from "node:os";
|
|
14
|
+
import { join, resolve } from "node:path";
|
|
15
|
+
|
|
16
|
+
const require = createRequire(import.meta.url);
|
|
17
|
+
const { runBundleGuard, guardBuildOutput } = require("../../../scripts/check-bundle-secrets.js") as {
|
|
18
|
+
runBundleGuard: (opts: {
|
|
19
|
+
buildDir: string;
|
|
20
|
+
env: Record<string, string | undefined>;
|
|
21
|
+
out: (line: string) => void;
|
|
22
|
+
err: (line: string) => void;
|
|
23
|
+
}) => number;
|
|
24
|
+
guardBuildOutput: (opts: {
|
|
25
|
+
buildDir: string;
|
|
26
|
+
env: Record<string, string | undefined>;
|
|
27
|
+
exit: (code: number) => void;
|
|
28
|
+
out: (line: string) => void;
|
|
29
|
+
err: (line: string) => void;
|
|
30
|
+
}) => void;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const PACKAGE_ROOT = resolve(__dirname, "../../..");
|
|
34
|
+
const SCRIPT = join(PACKAGE_ROOT, "scripts/check-bundle-secrets.js");
|
|
35
|
+
const SECRET = "Pw9xJ".repeat(8);
|
|
36
|
+
|
|
37
|
+
let dir: string;
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
dir = mkdtempSync(join(tmpdir(), "bundle-guard-"));
|
|
40
|
+
});
|
|
41
|
+
afterEach(() => {
|
|
42
|
+
rmSync(dir, { recursive: true, force: true });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
/** Entorno mínimo del proceso hijo: el secreto y el directorio de salida de la prueba. */
|
|
46
|
+
const childEnv = () =>
|
|
47
|
+
({
|
|
48
|
+
PATH: process.env.PATH ?? "",
|
|
49
|
+
NODE_ENV: "production",
|
|
50
|
+
BUILD_PATH: dir,
|
|
51
|
+
GRIDDO_NPM_TOKEN: SECRET,
|
|
52
|
+
}) as unknown as NodeJS.ProcessEnv;
|
|
53
|
+
|
|
54
|
+
const write = (rel: string, content: string) => {
|
|
55
|
+
const full = join(dir, rel);
|
|
56
|
+
mkdirSync(join(full, ".."), { recursive: true });
|
|
57
|
+
writeFileSync(full, content);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const run = (env: Record<string, string | undefined> = { GRIDDO_NPM_TOKEN: SECRET }, buildDir = dir) => {
|
|
61
|
+
const lines: string[] = [];
|
|
62
|
+
const code = runBundleGuard({ buildDir, env, out: (l) => lines.push(l), err: (l) => lines.push(l) });
|
|
63
|
+
return { code, output: lines.join("\n") };
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
describe("[UC-AX-bundle-public-config] check-bundle-secrets · fallo y limpieza", () => {
|
|
67
|
+
it.each(["main.js", "main.js.map", "index.html", "static/js/chunk.js"])(
|
|
68
|
+
"AC3 · un secreto en %s hace fallar",
|
|
69
|
+
(file) => {
|
|
70
|
+
write(file, `x ${SECRET} y`);
|
|
71
|
+
|
|
72
|
+
expect(run().code).toBe(1);
|
|
73
|
+
},
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
it("AC5 · al fallar vacía la salida (el directorio sigue existiendo) y nombra variable y fichero, no el valor", () => {
|
|
77
|
+
write("main.js", `x ${SECRET} y`);
|
|
78
|
+
write("static/css/app.css", "a{}");
|
|
79
|
+
|
|
80
|
+
const { code, output } = run();
|
|
81
|
+
|
|
82
|
+
expect(code).toBe(1);
|
|
83
|
+
expect(existsSync(dir)).toBe(true);
|
|
84
|
+
expect(readdirSync(dir)).toEqual([]);
|
|
85
|
+
expect(output).toContain("GRIDDO_NPM_TOKEN");
|
|
86
|
+
expect(output).toContain("main.js");
|
|
87
|
+
expect(output).not.toContain(SECRET);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("AC5 · ningún canal (stdout, stderr, excepción) revela el valor, ni parcialmente", () => {
|
|
91
|
+
write("main.js", `x ${SECRET} y`);
|
|
92
|
+
|
|
93
|
+
const res = spawnSync(process.execPath, [SCRIPT], {
|
|
94
|
+
cwd: PACKAGE_ROOT,
|
|
95
|
+
env: childEnv(),
|
|
96
|
+
encoding: "utf8",
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const all = `${res.stdout}${res.stderr}`;
|
|
100
|
+
expect(res.status).toBe(1);
|
|
101
|
+
expect(all).toContain("GRIDDO_NPM_TOKEN");
|
|
102
|
+
expect(all).not.toContain(SECRET);
|
|
103
|
+
expect(all).not.toContain(SECRET.slice(0, 10));
|
|
104
|
+
expect(readdirSync(dir)).toEqual([]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("AC3 · el script escanea el directorio de BUILD_PATH, no el predeterminado", () => {
|
|
108
|
+
write("main.js", `x ${SECRET} y`);
|
|
109
|
+
|
|
110
|
+
const res = spawnSync(process.execPath, [SCRIPT], {
|
|
111
|
+
cwd: PACKAGE_ROOT,
|
|
112
|
+
env: childEnv(),
|
|
113
|
+
encoding: "utf8",
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
expect(res.status).toBe(1);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("AC5 · un token npm_ en el bundle se informa sin imprimirlo", () => {
|
|
120
|
+
const token = `npm_${"aB3dE".repeat(8).slice(0, 36)}`;
|
|
121
|
+
write("main.js", `var t="${token}"`);
|
|
122
|
+
|
|
123
|
+
const { code, output } = run({});
|
|
124
|
+
|
|
125
|
+
expect(code).toBe(1);
|
|
126
|
+
expect(output).not.toContain(token);
|
|
127
|
+
expect(output).toContain("main.js");
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe("[UC-AX-bundle-public-config] check-bundle-secrets · falla cerrado", () => {
|
|
132
|
+
it("AC6 · un directorio de salida inexistente falla con un mensaje claro", () => {
|
|
133
|
+
const { code, output } = run({}, join(dir, "no-existe"));
|
|
134
|
+
|
|
135
|
+
expect(code).toBe(1);
|
|
136
|
+
expect(output).toContain("no-existe");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("AC6 · un fichero ilegible falla con un mensaje que lo nombra", () => {
|
|
140
|
+
write("main.js", "ok");
|
|
141
|
+
chmodSync(join(dir, "main.js"), 0o000);
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
const { code, output } = run({});
|
|
145
|
+
|
|
146
|
+
expect(code).toBe(1);
|
|
147
|
+
expect(output).toContain("main.js");
|
|
148
|
+
} finally {
|
|
149
|
+
chmodSync(join(dir, "main.js"), 0o644);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe("[UC-AX-bundle-public-config] check-bundle-secrets · bundle limpio", () => {
|
|
155
|
+
it("AC8 · un build sin mapas de fuentes y sin secretos termina bien", () => {
|
|
156
|
+
write("main.js", "var a=1;");
|
|
157
|
+
write("index.html", "<html></html>");
|
|
158
|
+
|
|
159
|
+
expect(run().code).toBe(0);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("AC8 · un bundle limpio conserva sus ficheros", () => {
|
|
163
|
+
write("main.js", "var a=1;");
|
|
164
|
+
write("static/js/chunk.js.map", "{}");
|
|
165
|
+
write("static/css/app.css", "a{}");
|
|
166
|
+
|
|
167
|
+
const { code } = run();
|
|
168
|
+
|
|
169
|
+
expect(code).toBe(0);
|
|
170
|
+
expect(readFileSync(join(dir, "main.js"), "utf8")).toBe("var a=1;");
|
|
171
|
+
expect(existsSync(join(dir, "static/js/chunk.js.map"))).toBe(true);
|
|
172
|
+
expect(existsSync(join(dir, "static/css/app.css"))).toBe(true);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe("[UC-AX-bundle-public-config] check-bundle-secrets · ficheros que se escanean", () => {
|
|
177
|
+
it.each([
|
|
178
|
+
"static/css/app.css",
|
|
179
|
+
"static/config.json",
|
|
180
|
+
"static/notes.txt",
|
|
181
|
+
"static/page.html",
|
|
182
|
+
"static/mod.mjs",
|
|
183
|
+
"LICENSE",
|
|
184
|
+
])("AC3 · un secreto en %s (fichero copiado a la salida) hace fallar", (file) => {
|
|
185
|
+
write(file, `x ${SECRET} y`);
|
|
186
|
+
|
|
187
|
+
expect(run().code).toBe(1);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it.each(["static/img/logo.png", "static/fonts/a.woff2", "static/img/photo.jpg"])(
|
|
191
|
+
"AC3 · el binario %s no se escanea",
|
|
192
|
+
(file) => {
|
|
193
|
+
write(file, `x ${SECRET} y`);
|
|
194
|
+
|
|
195
|
+
expect(run().code).toBe(0);
|
|
196
|
+
},
|
|
197
|
+
);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
describe("[UC-AX-bundle-public-config] guardBuildOutput · cableado en el build", () => {
|
|
201
|
+
it("AC7 · propaga el código de salida del guard cuando hay un secreto", () => {
|
|
202
|
+
write("main.js", `x ${SECRET} y`);
|
|
203
|
+
const exit = vi.fn();
|
|
204
|
+
|
|
205
|
+
guardBuildOutput({ buildDir: dir, env: { GRIDDO_NPM_TOKEN: SECRET }, exit, out: () => {}, err: () => {} });
|
|
206
|
+
|
|
207
|
+
expect(exit).toHaveBeenCalledTimes(1);
|
|
208
|
+
expect(exit).toHaveBeenCalledWith(1);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("AC7 · con un bundle limpio no sale", () => {
|
|
212
|
+
write("main.js", "var a=1;");
|
|
213
|
+
const exit = vi.fn();
|
|
214
|
+
|
|
215
|
+
guardBuildOutput({ buildDir: dir, env: {}, exit, out: () => {}, err: () => {} });
|
|
216
|
+
|
|
217
|
+
expect(exit).not.toHaveBeenCalled();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
describe("scripts/build.js", () => {
|
|
221
|
+
const source = readFileSync(join(PACKAGE_ROOT, "scripts/build.js"), "utf8");
|
|
222
|
+
const between = (from: string, to: string) => {
|
|
223
|
+
const start = source.indexOf(from);
|
|
224
|
+
const end = source.indexOf(to, start);
|
|
225
|
+
expect(start).toBeGreaterThan(-1);
|
|
226
|
+
expect(end).toBeGreaterThan(start);
|
|
227
|
+
return source.slice(start, end);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
it("AC7 · importa el guard del script de comprobación", () => {
|
|
231
|
+
expect(source).toMatch(/require\("\.\/check-bundle-secrets"\)/);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("AC7 · corre el guard cuando webpack ha terminado de emitir, antes de dar el build por bueno", () => {
|
|
235
|
+
const success = between("({ stats, previousFileSizes, warnings }) => {", "(err) => {");
|
|
236
|
+
|
|
237
|
+
expect(success).toContain("guardBuildOutput()");
|
|
238
|
+
expect(success.indexOf("guardBuildOutput()")).toBeLessThan(success.indexOf("Compiled successfully"));
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("AC7 · también corre con TSC_COMPILE_ON_ERROR=true (no es un bypass)", () => {
|
|
242
|
+
const tolerated = between("if (tscCompileOnError) {", "} else {");
|
|
243
|
+
|
|
244
|
+
expect(tolerated).toContain("guardBuildOutput()");
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("AC7 · el guard no corre antes de build(): copyStaticAssets() se ejecuta antes que webpack", () => {
|
|
248
|
+
const beforeBuild = source.slice(0, source.indexOf("return build(previousFileSizes)"));
|
|
249
|
+
|
|
250
|
+
expect(beforeBuild).not.toContain("guardBuildOutput()");
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
|
|
3
|
+
const require = createRequire(import.meta.url);
|
|
4
|
+
const getClientEnvironment = require("../../../config/env.js") as (publicUrl: string) => {
|
|
5
|
+
raw: Record<string, unknown>;
|
|
6
|
+
stringified: { "process.env": Record<string, string | undefined> };
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const PUBLIC_VALUES: Record<string, string> = {
|
|
10
|
+
GRIDDO_API_URL: "https://api.test",
|
|
11
|
+
GRIDDO_PUBLIC_API_URL: "https://public.test",
|
|
12
|
+
GRIDDO_REACT_APP_INSTANCE: "inst-a",
|
|
13
|
+
GRIDDO_FROALA_KEY: "froala",
|
|
14
|
+
GRIDDO_SITE_TITLE: "Title",
|
|
15
|
+
REACT_APP_API_ENDPOINT: "https://legacy.test",
|
|
16
|
+
REACT_APP_PUBLIC_API_ENDPOINT: "https://legacy-public.test",
|
|
17
|
+
REACT_APP_INSTANCE: "inst-b",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** Claves que incorpora la propia compilación, no leídas de la lista blanca. */
|
|
21
|
+
const BUILT_IN_KEYS = [
|
|
22
|
+
"NODE_ENV",
|
|
23
|
+
"PUBLIC_URL",
|
|
24
|
+
"WDS_SOCKET_HOST",
|
|
25
|
+
"WDS_SOCKET_PATH",
|
|
26
|
+
"WDS_SOCKET_PORT",
|
|
27
|
+
"FAST_REFRESH",
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
const stubPublicEnv = (values: Record<string, string> = PUBLIC_VALUES) => {
|
|
31
|
+
for (const key of Object.keys(PUBLIC_VALUES)) {
|
|
32
|
+
vi.stubEnv(key, values[key] as string);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const stubUnset = (...keys: string[]) => {
|
|
37
|
+
for (const key of keys) {
|
|
38
|
+
vi.stubEnv(key, undefined);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
describe("[UC-AX-bundle-public-config] getClientEnvironment", () => {
|
|
43
|
+
it.each(["GRIDDO_NPM_TOKEN", "GRIDDO_botPassword", "GRIDDO_SLACKCH", "griddo_npm_token", "REACT_APP_OTHER"])(
|
|
44
|
+
"AC1 · la variable %s fuera de la lista no se incrusta ni en bruto ni serializada",
|
|
45
|
+
(variable) => {
|
|
46
|
+
vi.stubEnv(variable, "valor-secreto");
|
|
47
|
+
|
|
48
|
+
const { raw, stringified } = getClientEnvironment("");
|
|
49
|
+
|
|
50
|
+
expect(Object.keys(raw)).not.toContain(variable);
|
|
51
|
+
expect(Object.keys(stringified["process.env"])).not.toContain(variable);
|
|
52
|
+
expect(JSON.stringify(stringified)).not.toContain("valor-secreto");
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
it("AC1 · las 8 variables de la lista definidas se incrustan junto a las incorporadas por la compilación", () => {
|
|
57
|
+
stubPublicEnv();
|
|
58
|
+
|
|
59
|
+
const { raw, stringified } = getClientEnvironment("/pub");
|
|
60
|
+
|
|
61
|
+
const expected = [...Object.keys(PUBLIC_VALUES), ...BUILT_IN_KEYS].sort();
|
|
62
|
+
expect(Object.keys(raw).sort()).toEqual(expected);
|
|
63
|
+
expect(Object.keys(stringified["process.env"]).sort()).toEqual(expected);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("AC1 · las 8 variables públicas están en el entorno en bruto y en el serializado", () => {
|
|
67
|
+
stubPublicEnv();
|
|
68
|
+
|
|
69
|
+
const { raw, stringified } = getClientEnvironment("");
|
|
70
|
+
|
|
71
|
+
for (const [key, value] of Object.entries(PUBLIC_VALUES)) {
|
|
72
|
+
expect(raw[key]).toBe(value);
|
|
73
|
+
expect(stringified["process.env"][key]).toBe(JSON.stringify(value));
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("AC1 · una variable de la lista sin definir no se incrusta", () => {
|
|
78
|
+
stubPublicEnv();
|
|
79
|
+
stubUnset("GRIDDO_API_URL");
|
|
80
|
+
|
|
81
|
+
const { raw, stringified } = getClientEnvironment("");
|
|
82
|
+
|
|
83
|
+
expect(Object.keys(raw)).not.toContain("GRIDDO_API_URL");
|
|
84
|
+
expect(Object.keys(stringified["process.env"])).not.toContain("GRIDDO_API_URL");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("AC1 · una variable de la lista con valor vacío se incrusta con valor vacío", () => {
|
|
88
|
+
stubPublicEnv({ ...PUBLIC_VALUES, GRIDDO_API_URL: "" });
|
|
89
|
+
|
|
90
|
+
const { raw, stringified } = getClientEnvironment("");
|
|
91
|
+
|
|
92
|
+
expect(raw.GRIDDO_API_URL).toBe("");
|
|
93
|
+
expect(stringified["process.env"].GRIDDO_API_URL).toBe('""');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("AC1 · el objeto de entorno completo solo expone valores públicos", () => {
|
|
97
|
+
stubPublicEnv();
|
|
98
|
+
vi.stubEnv("GRIDDO_NPM_TOKEN", "npm_secreto");
|
|
99
|
+
|
|
100
|
+
const { stringified } = getClientEnvironment("");
|
|
101
|
+
|
|
102
|
+
const keys = Object.keys(stringified["process.env"]);
|
|
103
|
+
expect(keys.every((key) => key in PUBLIC_VALUES || BUILT_IN_KEYS.includes(key))).toBe(true);
|
|
104
|
+
expect(keys).not.toContain("GRIDDO_NPM_TOKEN");
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("AC1 · `process.env` sigue siendo un objeto serializado para DefinePlugin", () => {
|
|
108
|
+
stubPublicEnv();
|
|
109
|
+
|
|
110
|
+
const { stringified } = getClientEnvironment("/pub");
|
|
111
|
+
|
|
112
|
+
expect(Object.keys(stringified)).toEqual(["process.env"]);
|
|
113
|
+
expect(typeof stringified["process.env"]).toBe("object");
|
|
114
|
+
expect(stringified["process.env"].PUBLIC_URL).toBe('"/pub"');
|
|
115
|
+
expect(stringified["process.env"].NODE_ENV).toBe(JSON.stringify(process.env.NODE_ENV));
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("AC1 · se mantienen NODE_ENV, PUBLIC_URL, WDS_SOCKET_* y FAST_REFRESH", () => {
|
|
119
|
+
vi.stubEnv("WDS_SOCKET_HOST", "h");
|
|
120
|
+
vi.stubEnv("WDS_SOCKET_PATH", "/p");
|
|
121
|
+
vi.stubEnv("WDS_SOCKET_PORT", "1234");
|
|
122
|
+
vi.stubEnv("FAST_REFRESH", "false");
|
|
123
|
+
|
|
124
|
+
const { raw } = getClientEnvironment("/pub");
|
|
125
|
+
|
|
126
|
+
expect(raw).toMatchObject({
|
|
127
|
+
NODE_ENV: process.env.NODE_ENV,
|
|
128
|
+
PUBLIC_URL: "/pub",
|
|
129
|
+
WDS_SOCKET_HOST: "h",
|
|
130
|
+
WDS_SOCKET_PATH: "/p",
|
|
131
|
+
WDS_SOCKET_PORT: "1234",
|
|
132
|
+
FAST_REFRESH: false,
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
});
|