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:
parent
750ecf5e51
commit
8a05f508c8
8 changed files with 150 additions and 51 deletions
|
|
@ -36,11 +36,46 @@ pub enum Kind {
|
|||
},
|
||||
/// `true` or `false`.
|
||||
Bool,
|
||||
/// Any string of at most `max_len` bytes, such as a file path.
|
||||
/// Any string of at most `max_len` bytes without NUL.
|
||||
Str {
|
||||
/// Longest accepted value in bytes.
|
||||
max_len: usize,
|
||||
},
|
||||
/// A file path, or empty for "detect it": a string checked by
|
||||
/// [`check_path`], the one validator for `settings.set` and the
|
||||
/// settings dialog (which shows its message).
|
||||
Path {
|
||||
/// Longest accepted value in bytes.
|
||||
max_len: usize,
|
||||
},
|
||||
}
|
||||
|
||||
/// Checks a path setting's text: at most `max_len` bytes, no control
|
||||
/// characters (U+0000–U+001F, U+007F), not wrapped in quotes (as copied
|
||||
/// from a shell or the file manager) and no spaces at either end. Values
|
||||
/// are refused, never trimmed, so what is stored is what was typed.
|
||||
///
|
||||
/// # Errors
|
||||
/// What to change, as a sentence the dialog shows under the field.
|
||||
pub fn check_path(text: &str, max_len: usize) -> Result<(), String> {
|
||||
if text.len() > max_len {
|
||||
return Err(format!("Enter at most {max_len} bytes"));
|
||||
}
|
||||
if text.chars().any(|c| c < ' ' || c == '\u{7f}') {
|
||||
return Err("Remove tabs, line breaks and other control characters".into());
|
||||
}
|
||||
let trimmed = text.trim();
|
||||
let quoted = trimmed.len() >= 2
|
||||
&& ['"', '\'']
|
||||
.iter()
|
||||
.any(|q| trimmed.starts_with(*q) && trimmed.ends_with(*q));
|
||||
if quoted {
|
||||
return Err("Remove the quotes around the path".into());
|
||||
}
|
||||
if trimmed != text {
|
||||
return Err("Remove the spaces at the start or end of the path".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The default value of a setting.
|
||||
|
|
@ -121,6 +156,12 @@ impl Def {
|
|||
self.key
|
||||
)),
|
||||
},
|
||||
Kind::Path { max_len } => match value.as_str() {
|
||||
Some(s) => {
|
||||
check_path(s, max_len).map_err(|reason| format!("{}: {reason}", self.key))
|
||||
}
|
||||
None => Err(format!("{} must be a path string, got {value}", self.key)),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -130,7 +171,7 @@ impl Def {
|
|||
Kind::Enum(values) => ("enum", Some(values.to_vec()), None, None),
|
||||
Kind::Int { min, max } => ("int", None, Some(min), Some(max)),
|
||||
Kind::Bool => ("bool", None, None, None),
|
||||
Kind::Str { .. } => ("string", None, None, None),
|
||||
Kind::Str { .. } | Kind::Path { .. } => ("string", None, None, None),
|
||||
};
|
||||
SettingInfo {
|
||||
key: self.key,
|
||||
|
|
@ -139,6 +180,7 @@ impl Def {
|
|||
enum_values,
|
||||
min,
|
||||
max,
|
||||
path: matches!(self.kind, Kind::Path { .. }),
|
||||
label_key: self.label_key,
|
||||
description_key: self.description_key,
|
||||
section: self.section,
|
||||
|
|
@ -166,6 +208,10 @@ pub struct SettingInfo {
|
|||
/// Largest value of an int.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub max: Option<i64>,
|
||||
/// Whether a string is a file path ([`Kind::Path`]): the dialog adds
|
||||
/// "Browse…" and shows [`check_path`]'s refusals.
|
||||
#[serde(skip_serializing_if = "std::ops::Not::not")]
|
||||
pub path: bool,
|
||||
/// Catalog key of the label.
|
||||
pub label_key: &'static str,
|
||||
/// Catalog key of the description.
|
||||
|
|
@ -202,7 +248,7 @@ pub const DEFS: &[Def] = &[
|
|||
},
|
||||
Def {
|
||||
key: "php.executable",
|
||||
kind: Kind::Str { max_len: 4096 },
|
||||
kind: Kind::Path { max_len: 4096 },
|
||||
default: DefaultValue::Str(""),
|
||||
label_key: "settings.php.executable.label",
|
||||
description_key: "settings.php.executable.description",
|
||||
|
|
@ -210,7 +256,7 @@ pub const DEFS: &[Def] = &[
|
|||
},
|
||||
Def {
|
||||
key: "phpactor.path",
|
||||
kind: Kind::Str { max_len: 4096 },
|
||||
kind: Kind::Path { max_len: 4096 },
|
||||
default: DefaultValue::Str(""),
|
||||
label_key: "settings.phpactor.path.label",
|
||||
description_key: "settings.phpactor.path.description",
|
||||
|
|
@ -277,7 +323,7 @@ pub const DEFS: &[Def] = &[
|
|||
},
|
||||
Def {
|
||||
key: "git.executable",
|
||||
kind: Kind::Str { max_len: 4096 },
|
||||
kind: Kind::Path { max_len: 4096 },
|
||||
default: DefaultValue::Str(""),
|
||||
label_key: "settings.git.executable.label",
|
||||
description_key: "settings.git.executable.description",
|
||||
|
|
@ -651,6 +697,44 @@ mod tests {
|
|||
assert_eq!(s.get("editor.fontSize"), Some(json!(14)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn path_settings_refuse_quotes_padding_and_control_characters() {
|
||||
let mut s = Settings::in_memory();
|
||||
for key in ["php.executable", "phpactor.path", "git.executable"] {
|
||||
for good in ["", r"C:\php\php.exe", "/opt/my php/php", "'"] {
|
||||
assert!(s.set(key, json!(good)).is_ok(), "{key} {good:?}");
|
||||
}
|
||||
for (bad, reason) in [
|
||||
(" php", "spaces"),
|
||||
("php ", "spaces"),
|
||||
(r#""C:\Program Files\php\php.exe""#, "quotes"),
|
||||
("'/opt/php'", "quotes"),
|
||||
(r#" "/opt/php" "#, "quotes"),
|
||||
("/opt/php\t", "control"),
|
||||
("/opt/\u{7f}php", "control"),
|
||||
] {
|
||||
let Err(SetError::Invalid(message)) = s.set(key, json!(bad)) else {
|
||||
panic!("{key} accepted {bad:?}");
|
||||
};
|
||||
assert!(message.starts_with(&format!("{key}: ")), "{message}");
|
||||
assert!(message.contains(reason), "{message}");
|
||||
}
|
||||
assert!(s.set(key, json!("x".repeat(4097))).is_err());
|
||||
assert!(s.set(key, json!(1)).is_err());
|
||||
assert!(DEFS.iter().any(|d| d.key == key && d.info().path));
|
||||
}
|
||||
assert!(!DEFS.iter().any(|d| d.key == "editor.fontSize" && d.info().path));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stored_padded_path_counts_as_unset() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
fs::write(dir.path().join(FILE_NAME), r#"{"php.executable": " php"}"#).unwrap();
|
||||
let (s, problem) = Settings::load(dir.path());
|
||||
assert!(problem.is_none());
|
||||
assert_eq!(s.get("php.executable"), Some(json!("")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn autosave_defaults_and_validation() {
|
||||
let mut s = Settings::in_memory();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue