refactor: type escaped OS strings and fix the final audit findings

Escaped config strings (ghostty_os::osbytes) kept reaching OS
boundaries undecoded, and decoding was occasionally applied to plain
text. The root cause was that both were plain `String`s. Now:
- `EncodedOs` is a newtype (no Display/AsRef<Path>) that decodes only
  via to_os/to_path/to_bytes/to_wide; plain text enters via
  from_plain_text. OS helpers (env, home, xdg, passwd, resources dir,
  path expansion), config::Path, WorkingDirectory, theme/file_load/edit
  paths, terminal pwd, OpenUrl/Pwd actions and termio cwd use it, so the
  compiler forces decoding at every boundary. os::open takes &OsStr.
  Config output is unchanged.

Fixes all rows of docs/rewrite/notes/review-rv5-codex.md (final-Z7.md):
screen-file copy; regex/OSC 8 URLs no longer decoded (regression);
lossless Windows WTF-8/WTF-16 for args, env and CreateProcessW; Windows
resources dir/TERMINFO; color OSC request lists and X11 color lookup no
longer allocate globally (C zero-allocation test extended); macOS Mach
handler resumes all threads before KERN_FAILURE (hang regression) and
SIGABRT minidumps go through the exception thread; plus open-config,
background images, crash dir, ssh cache, exe dir/PATH, fontconfig
filenames, file links, kitty TMPDIR, .hushlogin and benchmark paths.
The typing also surfaced and fixed gtk-custom-css, bell audio, bash
HISTFILE, OSC 52 writes and the C config getters.

Test Plan:
- cargo test --workspace (3376 passed)
- just check-macos (both), just check-windows, just check-wasm
- diff <(ghostty-rs +show-config --default --docs) \
  <(ghostty +show-config --default --docs)
This commit is contained in:
ddidderr committed 2026-10-12 10:06:07 +02:00
1 parent 0f898d3e41
commit 8f3e206d9f
92 files changed
+2967 -992

No files matched your search

+20 -12
View File
@@ -1,5 +1,5 @@
use std::{
ffi::{OsStr, OsString},
ffi::OsStr,
io,
process::{Command, Stdio},
};
@@ -18,9 +18,10 @@ pub enum OpenUrlKind {
/// Open a URL in the default handling application (via `xdg-open`).
///
/// `url` is the core's "config string" form (see [`crate::osbytes`]): a
/// non-UTF-8 path is carried in an escaped form and is decoded to the real
/// path bytes here, right before it is handed to the opener.
/// `url` is the exact OS string to hand to the opener. Callers holding an
/// [`EncodedOs`](crate::osbytes::EncodedOs) decode it explicitly
/// (`to_os`); plain text (a regex link, an OSC 8 URI) is wrapped with
/// `EncodedOs::from_plain_text` first, so it is never decoded by mistake.
///
/// Any output on stderr is logged as a warning in the application logs.
/// Output on stdout is ignored.
@@ -29,9 +30,8 @@ pub enum OpenUrlKind {
/// some portable way to open URLs. If you are implementing an
/// apprt for Ghostty, you should consider doing something special-cased
/// for your platform.
pub fn open(kind: OpenUrlKind, url: &str) -> io::Result<()> {
let url: OsString = crate::osbytes::config_str_to_os(url);
let mut cmd = open_command(kind, &url)?;
pub fn open(kind: OpenUrlKind, url: &OsStr) -> io::Result<()> {
let mut cmd = open_command(kind, url)?;
// Pipe stdout/stderr so we can collect output from the command.
cmd.stdin(Stdio::null())
@@ -141,12 +141,20 @@ mod tests {
assert_eq!(macos_open_args(OpenUrlKind::Unknown, u), ["u"]);
}
/// The opener receives decoded bytes for encoded paths and the text
/// unchanged for plain URLs (even when they contain the escape char).
#[test]
fn encoded_non_utf8_path_is_decoded_for_the_opener() {
fn encoded_paths_decode_but_plain_urls_do_not() {
use std::os::unix::ffi::OsStrExt as _;
let enc = crate::osbytes::bytes_to_config_string(b"/tmp/a\xffb");
let os = crate::osbytes::config_str_to_os(&enc);
assert_eq!(os.as_bytes(), b"/tmp/a\xffb");
use crate::osbytes::EncodedOs;
let path = EncodedOs::from_bytes(b"/tmp/a\xffb");
assert_eq!(path.to_os().as_bytes(), b"/tmp/a\xffb");
// U+FDD0 U+00FF in an ordinary URL must stay two chars, not byte FF.
let url = "https://x/\u{fdd0}\u{ff}";
let plain = EncodedOs::from_plain_text(url);
assert_eq!(plain.to_os().as_bytes(), url.as_bytes());
}
#[cfg(not(target_vendor = "apple"))]
@@ -155,7 +163,7 @@ mod tests {
// With an empty PATH, xdg-open can't be found; Zig detects this
// synchronously and so do we.
temp_env::with_var("PATH", Some("/nonexistent-dir"), || {
let err = open(OpenUrlKind::Unknown, "https://example.com").unwrap_err();
let err = open(OpenUrlKind::Unknown, OsStr::new("https://example.com")).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::NotFound);
});
}