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 an invalid --internal-url-regex before the build (#1445)

Andrew Chend7409bd6b04d
diff --git a/bin/cli.ts b/bin/cli.ts
index 09687d31..2233ab3f 100644
--- a/bin/cli.ts
+++ b/bin/cli.ts
@@ -17,6 +17,7 @@ import {
validateUrlInput,
validateDownloadDirInput,
validateInjectInput,
+ validateInternalUrlRegexInput,
} from './utils/validate';
import {
ERROR_EXIT_CODES,
@@ -132,6 +133,7 @@ program.action(async (urlArg: string, options: PakeCliOptions) => {
}
validateDownloadDirInput(options.downloadDir);
+ validateInternalUrlRegexInput(options.internalUrlRegex);
options.inject = validateInjectInput(options.inject);
if (!url) {
diff --git a/bin/utils/validate.ts b/bin/utils/validate.ts
index 6add393b..fa78e792 100644
--- a/bin/utils/validate.ts
+++ b/bin/utils/validate.ts
@@ -70,6 +70,23 @@ export function validateDownloadDirInput(value: string): string {
return value;
}
+// The injected link policy compiles this with `new RegExp` and, when that
+// throws, only logs it and falls back to same-root-domain matching, so a typo
+// would ship an app that silently ignores the option.
+export function validateInternalUrlRegexInput(value: string): string {
+ if (value === '') return value;
+ try {
+ new RegExp(value);
+ } catch (error) {
+ const detail = error instanceof Error ? error.message : String(error);
+ throw new PakeError(`Invalid internal URL regex: ${detail}`, {
+ code: 'INVALID_INPUT',
+ hint: 'Pass a JavaScript regular expression to --internal-url-regex, or list domains with --safe-domain.',
+ });
+ }
+ return value;
+}
+
const INJECT_EXTENSIONS = new Set(['.css', '.js']);
// Shared by --inject and the config "inject" field, so a bad file fails before
diff --git a/dist/cli.js b/dist/cli.js
index b5d2f796..0c4f5068 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -1050,6 +1050,24 @@ function validateDownloadDirInput(value) {
}
return value;
}
+// The injected link policy compiles this with `new RegExp` and, when that
+// throws, only logs it and falls back to same-root-domain matching, so a typo
+// would ship an app that silently ignores the option.
+function validateInternalUrlRegexInput(value) {
+ if (value === '')
+ return value;
+ try {
+ new RegExp(value);
+ }
+ catch (error) {
+ const detail = error instanceof Error ? error.message : String(error);
+ throw new PakeError(`Invalid internal URL regex: ${detail}`, {
+ code: 'INVALID_INPUT',
+ hint: 'Pass a JavaScript regular expression to --internal-url-regex, or list domains with --safe-domain.',
+ });
+ }
+ return value;
+}
const INJECT_EXTENSIONS = new Set(['.css', '.js']);
// Shared by --inject and the config "inject" field, so a bad file fails before
// anything is built. The flag has split its value on commas already; a config
@@ -4358,6 +4376,7 @@ program.action(async (urlArg, options) => {
}
}
validateDownloadDirInput(options.downloadDir);
+ validateInternalUrlRegexInput(options.internalUrlRegex);
options.inject = validateInjectInput(options.inject);
if (!url) {
if (jsonMode) {
diff --git a/tests/unit/validate-internal-url-regex-input.test.ts b/tests/unit/validate-internal-url-regex-input.test.ts
new file mode 100644
index 00000000..c2132276
--- /dev/null
+++ b/tests/unit/validate-internal-url-regex-input.test.ts
@@ -0,0 +1,23 @@
+import { describe, expect, it } from 'vitest';
+import { validateInternalUrlRegexInput } from '@/utils/validate';
+import { safeDomainsToRegex } from '@/utils/url';
+
+describe('validateInternalUrlRegexInput', () => {
+ it('accepts an empty value and valid patterns', () => {
+ expect(validateInternalUrlRegexInput('')).toBe('');
+ expect(
+ validateInternalUrlRegexInput('^https://(www\\.)?example\\.com'),
+ ).toBe('^https://(www\\.)?example\\.com');
+ const fromSafeDomain = safeDomainsToRegex('example.com,sso.example.org');
+ expect(validateInternalUrlRegexInput(fromSafeDomain)).toBe(fromSafeDomain);
+ });
+
+ it.each(['[', '(abc', '*.example.com', 'a{2,1}'])(
+ 'rejects %s, which the app could not compile',
+ (value) => {
+ expect(() => validateInternalUrlRegexInput(value)).toThrow(
+ expect.objectContaining({ code: 'INVALID_INPUT' }),
+ );
+ },
+ );
+});