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: enforce CLI numeric ranges in config files and schema

Tw93afc73ec77216
diff --git a/bin/helpers/config-file.ts b/bin/helpers/config-file.ts
index 7897734b..39197de4 100644
--- a/bin/helpers/config-file.ts
+++ b/bin/helpers/config-file.ts
@@ -23,6 +23,16 @@ const EXTRA_STRING_KEYS = new Set(['name', 'title', 'identifier']);
type ExpectedType = 'string' | 'number' | 'boolean' | 'string[]';
+// Numeric fields share the CLI flag ranges (see cli-program.ts validators),
+// so a config file cannot smuggle a value the same flag would reject.
+const NUMBER_RANGES: Record<string, { min: number; max?: number }> = {
+ width: { min: 0 },
+ height: { min: 0 },
+ minWidth: { min: 0 },
+ minHeight: { min: 0 },
+ zoom: { min: 50, max: 200 },
+};
+
function expectedTypeFor(key: string): ExpectedType | null {
if (key === 'inject') return 'string[]';
if (key === 'hideOnClose') return 'boolean';
@@ -114,6 +124,26 @@ export async function loadConfigFile(
},
);
}
+ if (typeof value === 'number') {
+ const range = NUMBER_RANGES[key];
+ const min = range?.min ?? 0;
+ const max = range?.max;
+ if (
+ !Number.isFinite(value) ||
+ value < min ||
+ (max !== undefined && value > max)
+ ) {
+ const bounds = max !== undefined ? `${min}-${max}` : `>= ${min}`;
+ throw new PakeError(
+ `Config field "${key}" must be a finite number (${bounds}).`,
+ {
+ code: 'INVALID_INPUT',
+ hint: 'See schema/pake.schema.json for field ranges.',
+ },
+ );
+ }
+ }
+
if (!expected && (typeof value === 'object' || value === null)) {
throw new PakeError(
`Config field "${key}" must be a string, number, or boolean.`,
diff --git a/schema/pake.schema.json b/schema/pake.schema.json
index d957ba99..d2a8ffd7 100644
--- a/schema/pake.schema.json
+++ b/schema/pake.schema.json
@@ -34,12 +34,14 @@
"width": {
"type": "number",
"description": "Window width in pixels.",
- "default": 1200
+ "default": 1200,
+ "minimum": 0
},
"height": {
"type": "number",
"description": "Window height in pixels.",
- "default": 780
+ "default": 780,
+ "minimum": 0
},
"useLocalFile": {
"type": "boolean",
@@ -68,7 +70,9 @@
},
"inject": {
"type": "array",
- "items": { "type": "string" },
+ "items": {
+ "type": "string"
+ },
"description": "Local CSS/JS file paths injected into the page.",
"default": []
},
@@ -203,17 +207,21 @@
"zoom": {
"type": "number",
"description": "Initial page zoom level (50-200).",
- "default": 100
+ "default": 100,
+ "minimum": 50,
+ "maximum": 200
},
"minWidth": {
"type": "number",
"description": "Minimum window width (0 = no limit).",
- "default": 0
+ "default": 0,
+ "minimum": 0
},
"minHeight": {
"type": "number",
"description": "Minimum window height (0 = no limit).",
- "default": 0
+ "default": 0,
+ "minimum": 0
},
"ignoreCertificateErrors": {
"type": "boolean",
diff --git a/tests/unit/config-file.test.ts b/tests/unit/config-file.test.ts
index 72dd1dd4..ddcc8222 100644
--- a/tests/unit/config-file.test.ts
+++ b/tests/unit/config-file.test.ts
@@ -122,6 +122,27 @@ describe('loadConfigFile', () => {
);
});
+ it('rejects numbers outside the CLI flag ranges', async () => {
+ const zoomPath = await writeConfig({ zoom: 1000 }, 'zoom.json');
+ await expect(loadConfigFile(zoomPath, validKeys)).rejects.toThrow(
+ /"zoom" must be a finite number \(50-200\)/,
+ );
+ const widthPath = await writeConfig({ width: -5 }, 'negwidth.json');
+ await expect(loadConfigFile(widthPath, validKeys)).rejects.toMatchObject({
+ code: 'INVALID_INPUT',
+ });
+ const okPath = await writeConfig({ zoom: 100, width: 800 }, 'ok.json');
+ const loaded = await loadConfigFile(okPath, validKeys);
+ expect(loaded.options).toEqual({ zoom: 100, width: 800 });
+ });
+
+ it('schema ranges match the loader ranges', () => {
+ expect(schema.properties.zoom).toMatchObject({ minimum: 50, maximum: 200 });
+ for (const key of ['width', 'height', 'minWidth', 'minHeight'] as const) {
+ expect(schema.properties[key]).toMatchObject({ minimum: 0 });
+ }
+ });
+
it('rejects wrong value types naming the expected type', async () => {
const configPath = await writeConfig({ width: 'wide' }, 'badtype.json');
const rejection = expect(loadConfigFile(configPath, validKeys)).rejects;