Skip to content

The swiss army knife of lossless video/audio editing

Don't abort startup on malformed --settings-json

Claudeaa4cc8a35a6e
diff --git a/docs/cli.md b/docs/cli.md
index 5cd31e40..d3484874 100644
--- a/docs/cli.md
+++ b/docs/cli.md
@@ -29,7 +29,7 @@ LosslessCut file1.mp4 file2.mkv
## Override settings (experimental) 🧪
-See [available settings](https://github.com/mifi/lossless-cut/blob/master/src/main/configStore.ts). Note that this is subject to change in newer versions. Settings are validated, and if any key or value is invalid, none of them are applied (see the log for details). You may use JSON or JSON5. Example:
+See [available settings](https://github.com/mifi/lossless-cut/blob/master/src/main/configStore.ts). Note that this is subject to change in newer versions. Settings are validated, and if the JSON or any key or value is invalid, none of them are applied (see the log for details). You may use JSON or JSON5. Example:
```bash
LosslessCut --settings-json '{captureFormat:"jpeg", "keyframeCut":true}'
```
diff --git a/src/main/util.test.ts b/src/main/util.test.ts
index 30903de9..45acd64c 100644
--- a/src/main/util.test.ts
+++ b/src/main/util.test.ts
@@ -41,4 +41,8 @@ describe('parseSettingsJson', () => {
expect(parseSettingsJson('{captureFormat:"gif"}').success).toBe(false);
expect(parseSettingsJson('{language:"xx"}').success).toBe(false);
});
+
+ test('rejects invalid JSON5 without throwing', () => {
+ expect(parseSettingsJson('{captureFormat:').success).toBe(false);
+ });
});
diff --git a/src/main/util.ts b/src/main/util.ts
index daa08300..24c1303a 100644
--- a/src/main/util.ts
+++ b/src/main/util.ts
@@ -2,6 +2,7 @@ import { access, constants } from 'node:fs/promises';
import os from 'node:os';
import { isAbsolute, resolve } from 'node:path';
import JSON5 from 'json5';
+import { z } from 'zod';
import { configSchema, openFilesActionArgsSchema } from '../common/types.js';
@@ -34,4 +35,13 @@ export function resolveCliKeyboardActionArgs(action: string, args: unknown[], wo
}
// `--settings-json` may contain any subset of the config, but only known keys with valid values
-export const parseSettingsJson = (settingsJson: string) => configSchema.partial().strict().safeParse(JSON5.parse(settingsJson));
+const settingsJsonSchema = z.string().transform((str, ctx) => {
+ try {
+ return JSON5.parse(str);
+ } catch (err) {
+ ctx.addIssue({ code: 'custom', message: err instanceof Error ? err.message : String(err) });
+ return z.NEVER;
+ }
+}).pipe(configSchema.partial().strict());
+
+export const parseSettingsJson = (settingsJson: string) => settingsJsonSchema.safeParse(settingsJson);