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 fractional zoom in config files (#1380)

Suliman Abdulrazzaqc6a9617c491c
diff --git a/bin/helpers/config-file.ts b/bin/helpers/config-file.ts
index 39197de4..448709f9 100644
--- a/bin/helpers/config-file.ts
+++ b/bin/helpers/config-file.ts
@@ -23,14 +23,17 @@ const EXTRA_STRING_KEYS = new Set(['name', 'title', 'identifier']);
type ExpectedType = 'string' | 'number' | 'boolean' | 'string[]';
+type NumberRange = { min: number; max?: number; integer?: boolean };
+
// 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 }> = {
+// Like --zoom, zoom must be an integer: pake.json stores it as a Rust u32.
+const NUMBER_RANGES: Record<string, NumberRange> = {
width: { min: 0 },
height: { min: 0 },
minWidth: { min: 0 },
minHeight: { min: 0 },
- zoom: { min: 50, max: 200 },
+ zoom: { min: 50, max: 200, integer: true },
};
function expectedTypeFor(key: string): ExpectedType | null {
@@ -128,14 +131,17 @@ export async function loadConfigFile(
const range = NUMBER_RANGES[key];
const min = range?.min ?? 0;
const max = range?.max;
+ const integer = range?.integer === true;
if (
!Number.isFinite(value) ||
+ (integer && !Number.isInteger(value)) ||
value < min ||
(max !== undefined && value > max)
) {
const bounds = max !== undefined ? `${min}-${max}` : `>= ${min}`;
+ const kind = integer ? 'an integer' : 'a finite number';
throw new PakeError(
- `Config field "${key}" must be a finite number (${bounds}).`,
+ `Config field "${key}" must be ${kind} (${bounds}).`,
{
code: 'INVALID_INPUT',
hint: 'See schema/pake.schema.json for field ranges.',
diff --git a/dist/cli.js b/dist/cli.js
index 37d449fa..d1889efe 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -3668,12 +3668,13 @@ const REJECTED_KEYS = new Set(['config', 'json', 'version']);
const EXTRA_STRING_KEYS = new Set(['name', 'title', 'identifier']);
// 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.
+// Like --zoom, zoom must be an integer: pake.json stores it as a Rust u32.
const NUMBER_RANGES = {
width: { min: 0 },
height: { min: 0 },
minWidth: { min: 0 },
minHeight: { min: 0 },
- zoom: { min: 50, max: 200 },
+ zoom: { min: 50, max: 200, integer: true },
};
function expectedTypeFor(key) {
if (key === 'inject')
@@ -3756,11 +3757,14 @@ async function loadConfigFile(configPath, validKeys) {
const range = NUMBER_RANGES[key];
const min = range?.min ?? 0;
const max = range?.max;
+ const integer = range?.integer === true;
if (!Number.isFinite(value) ||
+ (integer && !Number.isInteger(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}).`, {
+ const kind = integer ? 'an integer' : 'a finite number';
+ throw new PakeError(`Config field "${key}" must be ${kind} (${bounds}).`, {
code: 'INVALID_INPUT',
hint: 'See schema/pake.schema.json for field ranges.',
});
diff --git a/schema/pake.schema.json b/schema/pake.schema.json
index ff50c0e8..1c8c4286 100644
--- a/schema/pake.schema.json
+++ b/schema/pake.schema.json
@@ -215,7 +215,7 @@
"default": "en-US"
},
"zoom": {
- "type": "number",
+ "type": "integer",
"description": "Initial page zoom level (50-200).",
"default": 100,
"minimum": 50,
diff --git a/tests/unit/config-file.test.ts b/tests/unit/config-file.test.ts
index d3326a54..2110acf5 100644
--- a/tests/unit/config-file.test.ts
+++ b/tests/unit/config-file.test.ts
@@ -131,7 +131,7 @@ 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\)/,
+ /"zoom" must be an integer \(50-200\)/,
);
const widthPath = await writeConfig({ width: -5 }, 'negwidth.json');
await expect(loadConfigFile(widthPath, validKeys)).rejects.toMatchObject({
@@ -142,8 +142,28 @@ describe('loadConfigFile', () => {
expect(loaded.options).toEqual({ zoom: 100, width: 800 });
});
+ it('rejects fractional zoom like the --zoom flag', async () => {
+ // pake.json stores zoom as a Rust u32, so 99.5 would build an app that
+ // cannot parse its own config at launch (#1274).
+ const zoomPath = await writeConfig({ zoom: 99.5 }, 'fraction-zoom.json');
+ await expect(loadConfigFile(zoomPath, validKeys)).rejects.toMatchObject({
+ code: 'INVALID_INPUT',
+ message: 'Config field "zoom" must be an integer (50-200).',
+ });
+ const widthPath = await writeConfig(
+ { width: 800.5 },
+ 'fraction-width.json',
+ );
+ const loaded = await loadConfigFile(widthPath, validKeys);
+ expect(loaded.options).toEqual({ width: 800.5 });
+ });
+
it('schema ranges match the loader ranges', () => {
- expect(schema.properties.zoom).toMatchObject({ minimum: 50, maximum: 200 });
+ expect(schema.properties.zoom).toMatchObject({
+ type: 'integer',
+ minimum: 50,
+ maximum: 200,
+ });
for (const key of ['width', 'height', 'minWidth', 'minHeight'] as const) {
expect(schema.properties[key]).toMatchObject({ minimum: 0 });
}