refactor(config): parse config values from raw bytes like Zig
Zig parses config values (cli/args.zig, Config.zig field parsers, config/string.zig) from raw byte slices, and each field type decides whether it requires UTF-8 (InvalidValue otherwise) or stores bytes. The port escape-encoded raw bytes into Strings at the line reader and let fields parse the escaped text, so fields could observe encoding artifacts; successive audits kept finding such leaks (RV5-RV8). Field parsers now receive the original bytes: text fields validate UTF-8 where Zig does, byte/path fields store bytes (EncodedOs/OsString). This resolves all 9 RV8 findings (docs/rewrite/notes/review-rv8-codex.md), each with a regression test: Windows path conversion fails with BadPathName instead of substituting U+FFFD; Windows process launch uses strict UTF-8 like Zig's Command; the WTF-8 decoder accepts paired- surrogate encodings like Zig; literal U+FDD0 Unicode key triggers; selection-word-chars and clipboard-codepoint-map reject invalid UTF-8; font style / codepoint-map family names use original bytes; C title actions forward bytes; D-Bus new-window/Flatpak conversions match Zig. Design, sweep and validation: docs/rewrite/notes/final-bytes-refactor.md. Also moves the bundled iTerm2-Color-Schemes license out of resources/themes/ so +list-themes no longer lists it as a theme (output is now byte-identical to Ghostty 1.3.1). Done by Codex (gpt-6-astra), reviewed and verified by the orchestrator. Test Plan: - cargo test --workspace --no-fail-fast (3428 passed, exit 0) - just check-macos (both), just check-windows, just check-wasm - +show-config --default --docs, +list-keybinds, +list-actions --docs, +list-themes --plain compared with /usr/bin/ghostty
This commit is contained in:
1 parent
37a1646611
commit
672e8a4ae1
100 files changed
+2310
-1054
No files matched your search
@@ -16,7 +16,7 @@
|
||||
//! ```
|
||||
|
||||
use ghostty_apprt::ipc::{Error, NewWindow, Target};
|
||||
use gtk::prelude::*;
|
||||
use gtk::glib;
|
||||
|
||||
use super::dbus::DBus;
|
||||
|
||||
@@ -35,9 +35,78 @@ pub fn new_window(target: &Target, value: &NewWindow) -> Result<bool, Error> {
|
||||
// parameter is an array of strings that contain the arguments. They
|
||||
// will be sent to the main Ghostty instance and interpreted as CLI
|
||||
// arguments.
|
||||
dbus.add_parameter(&arguments.to_variant());
|
||||
dbus.add_parameter(&arguments_variant(arguments));
|
||||
}
|
||||
|
||||
dbus.send()?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Zig constructs each GVariant string from serialized bytes including its NUL.
|
||||
/// Keep valid wire bytes unchanged. Zig marks these strings trusted, causing
|
||||
/// GIO to abort when serializing invalid UTF-8 or an interior NUL. Fail the same
|
||||
/// way explicitly: an untrusted GVariant would silently substitute an empty
|
||||
/// string, while a malformed trusted variant would violate Rust GLib's contract.
|
||||
fn arguments_variant(arguments: &[Vec<u8>]) -> glib::Variant {
|
||||
glib::Variant::array_from_iter_with_type(
|
||||
glib::VariantTy::STRING,
|
||||
arguments.iter().map(|arg| {
|
||||
if arg.contains(&0) || std::str::from_utf8(arg).is_err() {
|
||||
log::error!("invalid D-Bus string in new-window arguments");
|
||||
std::process::abort();
|
||||
}
|
||||
let mut bytes = arg.clone();
|
||||
bytes.push(0);
|
||||
glib::Variant::from_bytes_with_type(
|
||||
&glib::Bytes::from_owned(bytes),
|
||||
glib::VariantTy::STRING,
|
||||
)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![allow(clippy::unwrap_used)]
|
||||
use super::*;
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn invalid_dbus_strings_fail_without_substitution() {
|
||||
use std::os::unix::process::ExitStatusExt;
|
||||
const CHILD: &str = "GHOSTTY_TEST_INVALID_DBUS_VALUE";
|
||||
if let Ok(kind) = std::env::var(CHILD) {
|
||||
let value: &[u8] = if kind == "utf8" {
|
||||
b"--title=bad\xff"
|
||||
} else {
|
||||
b"--title=bad\0tail"
|
||||
};
|
||||
arguments_variant(&[value.to_vec()]);
|
||||
panic!("invalid D-Bus argument must abort like Zig/GIO");
|
||||
}
|
||||
for kind in ["utf8", "nul"] {
|
||||
// Disable core dumps in the child without changing this test process.
|
||||
let result = std::process::Command::new("/bin/sh")
|
||||
.args(["-c", "ulimit -c 0; exec \"$@\"", "ghostty-dbus-test"])
|
||||
.arg(std::env::current_exe().unwrap())
|
||||
.args([
|
||||
"--exact",
|
||||
"ipc::new_window::tests::invalid_dbus_strings_fail_without_substitution",
|
||||
])
|
||||
.env(CHILD, kind)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert_eq!(result.status.signal(), Some(libc::SIGABRT));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arguments_are_plain_dbus_strings_not_escape_encoded() {
|
||||
let argument = "--working-directory=/tmp/\u{fdd0}é";
|
||||
let variant = arguments_variant(&[argument.as_bytes().to_vec()]);
|
||||
assert_eq!(variant.get::<Vec<String>>().unwrap(), [argument]);
|
||||
assert_eq!(
|
||||
variant.child_value(0).data(),
|
||||
[argument.as_bytes(), b"\0"].concat()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user