F1: settings.set refuses quoted, padded and control-character paths

Path settings get Kind::Path, checked by check_path in the engine: the one
validator for settings.set from agents, the CLI and the dialog. settings.list
marks them with path: true; the dialog uses that flag for Browse and shows
the engine's reason (userMessage drops the invalid-params prefix).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-11-02 19:02:08 +01:00
parent 750ecf5e51
commit 8a05f508c8
8 changed files with 150 additions and 51 deletions

View file

@ -5,8 +5,9 @@
*
* The rules work on any string setting `settings.list` reports, so a new
* one (such as `git.executable`) gets the control without changes here.
* The engine still validates every value; these checks only catch mistakes
* early and say how to fix them.
* Path settings are checked only by the engine (`check_path` in
* `crates/engine/src/settings.rs`), the one validator shared with
* `settings.set` from agents and the CLI; the dialog shows its reason.
*/
import { t } from "../../i18n";
import type { SettingDef } from "../../stores/settings";
@ -14,9 +15,6 @@ import type { SettingDef } from "../../stores/settings";
/** Longest value in UTF-8 bytes; the engine's limit for string settings. */
export const MAX_STRING_BYTES = 4096;
/** Endings of a key's last segment that mark a path setting. */
const PATH_ENDINGS = ["path", "executable"];
/**
* Whether a text has control characters (U+0000–U+001F, U+007F), which no
* single-line setting needs.
@ -34,18 +32,13 @@ function hasControlChars(text: string): boolean {
}
/**
* Whether a setting holds a file path: a string setting whose key ends in
* `path` or `executable` (`phpactor.path`, `php.executable`,
* `git.executable`, `x.pharPath`).
* Whether a setting holds a file path: a string setting the engine marks
* with `path` (`php.executable`, `phpactor.path`, `git.executable`).
*
* @param def - the setting.
*/
export function isPathSetting(def: SettingDef): boolean {
if (def.type !== "string") {
return false;
}
const last = (def.key.split(".").at(-1) ?? "").toLowerCase();
return PATH_ENDINGS.some((ending) => last.endsWith(ending));
return def.type === "string" && def.path;
}
/**
@ -60,32 +53,24 @@ export function placeholderOf(def: SettingDef): string {
}
/**
* Checks a typed value before it goes to `settings.set`.
*
* - Longer than {@link MAX_STRING_BYTES} UTF-8 bytes, or with control
* characters: refused for every string setting.
* - Paths: refused when wrapped in quotes (as copied from a shell or the
* file manager) or with spaces at either end.
* Checks a typed value before it goes to `settings.set`: longer than
* {@link MAX_STRING_BYTES} UTF-8 bytes, or with control characters, is
* refused. Paths are left to the engine, which refuses the same and
* quoted or padded paths too.
*
* @param def - the setting.
* @param text - the typed value.
* @returns the error text from the catalog, or null when the value may be sent.
*/
export function checkString(def: SettingDef, text: string): string | null {
if (isPathSetting(def)) {
return null;
}
if (new TextEncoder().encode(text).length > MAX_STRING_BYTES) {
return t("settings.error.stringTooLong", { max: MAX_STRING_BYTES });
}
if (hasControlChars(text)) {
return t("settings.error.stringControl");
}
if (!isPathSetting(def)) {
return null;
}
if (/^\s*(["']).*\1\s*$/.test(text) && text.trim().length >= 2) {
return t("settings.error.pathQuoted");
}
if (text !== text.trim()) {
return t("settings.error.pathSpaces");
}
return null;
}