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;
}

View file

@ -301,8 +301,6 @@
"settings.error.browseFailed": "The file picker could not open: {message}",
"settings.error.int": "Enter a whole number",
"settings.error.intRange": "Enter a whole number from {min} to {max}",
"settings.error.pathQuoted": "Remove the quotes around the path",
"settings.error.pathSpaces": "Remove the spaces at the start or end of the path",
"settings.error.stringControl": "Remove tabs, line breaks and other control characters",
"settings.error.stringTooLong": "Enter at most {max} bytes",
"settings.git.executable.description": "Path to the git program (2.30 or newer) that Lutra runs for version control. Leave empty to use git from PATH.",

View file

@ -254,6 +254,17 @@ export interface EngineError {
message: string;
}
/**
* The part of an engine error's message meant for a person: the reason
* without the engine's `invalid params for <command>: ` prefix, which
* only repeats the command the caller just ran.
*
* @param error - the error.
*/
export function userMessage(error: EngineError): string {
return error.code === "invalid_params" ? error.message.replace(/^invalid params for [^:\s]+: /, "") : error.message;
}
/** Outcome of an engine call. */
export type RunResult<T> = { ok: true; value: T } | { ok: false; error: EngineError };

View file

@ -19,7 +19,7 @@
import { defineStore } from "pinia";
import { reactive, ref } from "vue";
import { hasMessage, t } from "../i18n";
import { onEngineEvent, run, type EngineEventPayload } from "../ipc";
import { onEngineEvent, run, userMessage, type EngineError, type EngineEventPayload } from "../ipc";
import { setTokenOverride } from "../theme";
/** A setting value: settings are scalars. Values from the engine are unchecked. */
@ -48,6 +48,8 @@ export interface SettingDef {
descriptionKey: string;
/** Section id; shown with `settings.section.<id>`. */
section: string;
/** Whether a string setting holds a file path (the engine's `path` flag). */
path: boolean;
}
/** A section of the dialog with its settings, in engine order. */
@ -118,6 +120,7 @@ function toSettingDef(raw: unknown): SettingDef | null {
labelKey,
descriptionKey,
section,
path: type === "string" && entry.path === true,
};
}
@ -330,7 +333,8 @@ export const useSettingsStore = defineStore("settings", () => {
}
/**
* Stores the outcome of `settings.set` or `settings.reset`.
* Stores the outcome of `settings.set` or `settings.reset`. A refusal is
* kept as its reason, without the engine's command and key prefixes.
*
* @param key - the setting.
* @param result - the command's outcome.
@ -338,10 +342,11 @@ export const useSettingsStore = defineStore("settings", () => {
*/
function settle(
key: string,
result: { ok: true; value: unknown } | { ok: false; error: { message: string } },
result: { ok: true; value: unknown } | { ok: false; error: EngineError },
): boolean {
if (!result.ok) {
errors[key] = clip(result.error.message);
const message = userMessage(result.error);
errors[key] = clip(message.startsWith(`${key}: `) ? message.slice(key.length + 2) : message);
return false;
}
Reflect.deleteProperty(errors, key);