Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: reject a bad windowsToolchain in config files too

Tw937a13977940f9
diff --git a/bin/helpers/config-file.ts b/bin/helpers/config-file.ts
index 448709f9..b5244314 100644
--- a/bin/helpers/config-file.ts
+++ b/bin/helpers/config-file.ts
@@ -36,6 +36,14 @@ const NUMBER_RANGES: Record<string, NumberRange> = {
zoom: { min: 50, max: 200, integer: true },
};
+// Fields whose CLI flag restricts the value set (see the .choices() calls in
+// cli-program.ts), so a config file cannot smuggle a value the same flag
+// would reject. Without this, an unknown value falls through to the builder's
+// own fallback and silently produces the default behavior.
+const ENUM_VALUES: Record<string, readonly string[]> = {
+ windowsToolchain: ['msvc', 'gnu'],
+};
+
function expectedTypeFor(key: string): ExpectedType | null {
if (key === 'inject') return 'string[]';
if (key === 'hideOnClose') return 'boolean';
@@ -127,6 +135,17 @@ export async function loadConfigFile(
},
);
}
+ const allowed = ENUM_VALUES[key];
+ if (allowed && !allowed.includes(value as string)) {
+ throw new PakeError(
+ `Config field "${key}" must be one of: ${allowed.join(', ')}.`,
+ {
+ code: 'INVALID_INPUT',
+ hint: 'See schema/pake.schema.json for allowed values.',
+ },
+ );
+ }
+
if (typeof value === 'number') {
const range = NUMBER_RANGES[key];
const min = range?.min ?? 0;
diff --git a/dist/cli.js b/dist/cli.js
index 7b0e7afb..5b329495 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -3763,6 +3763,13 @@ const NUMBER_RANGES = {
minHeight: { min: 0 },
zoom: { min: 50, max: 200, integer: true },
};
+// Fields whose CLI flag restricts the value set (see the .choices() calls in
+// cli-program.ts), so a config file cannot smuggle a value the same flag
+// would reject. Without this, an unknown value falls through to the builder's
+// own fallback and silently produces the default behavior.
+const ENUM_VALUES = {
+ windowsToolchain: ['msvc', 'gnu'],
+};
function expectedTypeFor(key) {
if (key === 'inject')
return 'string[]';
@@ -3840,6 +3847,13 @@ async function loadConfigFile(configPath, validKeys) {
hint: 'See schema/pake.schema.json for field types.',
});
}
+ const allowed = ENUM_VALUES[key];
+ if (allowed && !allowed.includes(value)) {
+ throw new PakeError(`Config field "${key}" must be one of: ${allowed.join(', ')}.`, {
+ code: 'INVALID_INPUT',
+ hint: 'See schema/pake.schema.json for allowed values.',
+ });
+ }
if (typeof value === 'number') {
const range = NUMBER_RANGES[key];
const min = range?.min ?? 0;
diff --git a/tests/unit/config-file.test.ts b/tests/unit/config-file.test.ts
index 2110acf5..bb52775c 100644
--- a/tests/unit/config-file.test.ts
+++ b/tests/unit/config-file.test.ts
@@ -114,6 +114,39 @@ describe('loadConfigFile', () => {
});
});
+ it('rejects a windowsToolchain value the CLI flag would reject', async () => {
+ // --windows-toolchain is a .choices() flag; without a matching check here a
+ // typo would load fine and silently fall back to the msvc default.
+ const configPath = await writeConfig(
+ { windowsToolchain: 'clang' },
+ 'toolchain-typo.json',
+ );
+ await expect(loadConfigFile(configPath, validKeys)).rejects.toThrow(
+ /must be one of: msvc, gnu/,
+ );
+ });
+
+ it('rejects a non-string windowsToolchain', async () => {
+ const configPath = await writeConfig(
+ { windowsToolchain: 123 },
+ 'toolchain-number.json',
+ );
+ await expect(loadConfigFile(configPath, validKeys)).rejects.toMatchObject({
+ code: 'INVALID_INPUT',
+ });
+ });
+
+ it('keeps both valid windowsToolchain values', async () => {
+ for (const value of ['msvc', 'gnu']) {
+ const configPath = await writeConfig(
+ { windowsToolchain: value },
+ `toolchain-${value}.json`,
+ );
+ const loaded = await loadConfigFile(configPath, validKeys);
+ expect(loaded.options.windowsToolchain).toBe(value);
+ }
+ });
+
it('rejects unknown fields naming the field', async () => {
const configPath = await writeConfig({ nmae: 'typo' }, 'typo.json');
await expect(loadConfigFile(configPath, validKeys)).rejects.toThrow(