From 6e51bf150bf6e5fb85974b551ae9a3779347c9ab Mon Sep 17 00:00:00 2001 From: arzafran Date: Sun, 15 Nov 2026 12:09:27 -0300 Subject: [PATCH 1/3] refactor: move terminal PATH composition into TerminalPathEnvironment The app's startup PATH and the CLI's tmux shim PATH now share one helper compiled into both targets. No behavior change. --- CLI/CLI+TmuxCompat.swift | 9 +-------- GhosttyTabs.xcodeproj/project.pbxproj | 6 ++++++ Sources/TerminalPathEnvironment.swift | 28 +++++++++++++++++++++++++++ Sources/TerminalSurface.swift | 5 ++--- 4 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 Sources/TerminalPathEnvironment.swift diff --git a/CLI/CLI+TmuxCompat.swift b/CLI/CLI+TmuxCompat.swift index 50e47de4..9d16212e 100644 --- a/CLI/CLI+TmuxCompat.swift +++ b/CLI/CLI+TmuxCompat.swift @@ -1057,14 +1057,7 @@ extension ProgramaCLI { } private func prependPathEntries(_ newEntries: [String], to currentPath: String?) -> String { - var ordered: [String] = [] - var seen: Set = [] - for entry in newEntries + (currentPath?.split(separator: ":").map(String.init) ?? []) where !entry.isEmpty { - if seen.insert(entry).inserted { - ordered.append(entry) - } - } - return ordered.joined(separator: ":") + TerminalPathEnvironment.prependingUniqueEntries(newEntries, to: currentPath) } struct TmuxCompatFocusedContext { diff --git a/GhosttyTabs.xcodeproj/project.pbxproj b/GhosttyTabs.xcodeproj/project.pbxproj index 6917aed6..ba49194f 100644 --- a/GhosttyTabs.xcodeproj/project.pbxproj +++ b/GhosttyTabs.xcodeproj/project.pbxproj @@ -93,6 +93,8 @@ A1D000000000000000000007 /* CLI+Handoff.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1D000000000000000000006 /* CLI+Handoff.swift */; }; A1D000000000000000000005 /* PaneHandoffFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1D000000000000000000003 /* PaneHandoffFormatter.swift */; }; A1D000000000000000000004 /* PaneHandoffFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1D000000000000000000003 /* PaneHandoffFormatter.swift */; }; + TPEV00000000000000000002 /* TerminalPathEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = TPEV00000000000000000001 /* TerminalPathEnvironment.swift */; }; + TPEV00000000000000000003 /* TerminalPathEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = TPEV00000000000000000001 /* TerminalPathEnvironment.swift */; }; A1D000000000000000000002 /* TerminalController+SurfacePaste.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1D000000000000000000001 /* TerminalController+SurfacePaste.swift */; }; B6F200000000000000000002 /* TerminalController+SurfaceConditionalSend.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6F200000000000000000001 /* TerminalController+SurfaceConditionalSend.swift */; }; A2CV0000000000000000000A /* CommandPaletteView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2CV0000000000000000000B /* CommandPaletteView.swift */; }; @@ -575,6 +577,7 @@ A1D000000000000000000008 /* CommandPaletteView+PaneHandoff.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CommandPaletteView+PaneHandoff.swift"; sourceTree = ""; }; A1D000000000000000000006 /* CLI+Handoff.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CLI+Handoff.swift"; sourceTree = ""; }; A1D000000000000000000003 /* PaneHandoffFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaneHandoffFormatter.swift; sourceTree = ""; }; + TPEV00000000000000000001 /* TerminalPathEnvironment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminalPathEnvironment.swift; sourceTree = ""; }; A1D000000000000000000001 /* TerminalController+SurfacePaste.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TerminalController+SurfacePaste.swift"; sourceTree = ""; }; B6F200000000000000000001 /* TerminalController+SurfaceConditionalSend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TerminalController+SurfaceConditionalSend.swift"; sourceTree = ""; }; A2CV0000000000000000000B /* CommandPaletteView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandPaletteView.swift; sourceTree = ""; }; @@ -1036,6 +1039,7 @@ NRPA00010 /* SettingsView.swift */, THTM0001 /* TerminalThemeStore.swift */, A1D000000000000000000003 /* PaneHandoffFormatter.swift */, + TPEV00000000000000000001 /* TerminalPathEnvironment.swift */, REND0001A1B2C3D4E5F60719 /* RendererRealization.swift */, A5FF0017 /* SettingDefinition.swift */, A5001012 /* ContentView.swift */, @@ -1656,6 +1660,7 @@ NRPA00009 /* SettingsView.swift in Sources */, THTM0002 /* TerminalThemeStore.swift in Sources */, A1D000000000000000000004 /* PaneHandoffFormatter.swift in Sources */, + TPEV00000000000000000002 /* TerminalPathEnvironment.swift in Sources */, REND0002A1B2C3D4E5F60719 /* RendererRealization.swift in Sources */, A5FF0007 /* SettingDefinition.swift in Sources */, A5001002 /* ContentView.swift in Sources */, @@ -2008,6 +2013,7 @@ B9000037A1B2C3D4E5F60719 /* CLI+Themes.swift in Sources */, THTM0003 /* TerminalThemeStore.swift in Sources */, A1D000000000000000000005 /* PaneHandoffFormatter.swift in Sources */, + TPEV00000000000000000003 /* TerminalPathEnvironment.swift in Sources */, B9000039A1B2C3D4E5F60719 /* CLI+Tree.swift in Sources */, 7B3F9A10C2D4E5F6A7B8000A /* CLI+Ports.swift in Sources */, WTST000005 /* CLI+Storage.swift in Sources */, diff --git a/Sources/TerminalPathEnvironment.swift b/Sources/TerminalPathEnvironment.swift new file mode 100644 index 00000000..c13ddea0 --- /dev/null +++ b/Sources/TerminalPathEnvironment.swift @@ -0,0 +1,28 @@ +import Foundation + +/// Composes the PATH values Programa exports to terminals and child processes. +/// Compiled into both the app and the `programa` CLI. +enum TerminalPathEnvironment { + /// The PATH to export when `directory` must lead `currentPath`, or nil + /// when `currentPath` already contains it and can be left alone. + static func startupPath(prependingDirectory directory: String, to currentPath: String) -> String? { + if currentPath.split(separator: ":").contains(Substring(directory)) { + return nil + } + let separator = currentPath.isEmpty ? "" : ":" + return "\(directory)\(separator)\(currentPath)" + } + + /// Prepends `newEntries` to `currentPath`, keeping the first occurrence + /// of each entry and dropping empty ones. + static func prependingUniqueEntries(_ newEntries: [String], to currentPath: String?) -> String { + var ordered: [String] = [] + var seen: Set = [] + for entry in newEntries + (currentPath?.split(separator: ":").map(String.init) ?? []) where !entry.isEmpty { + if seen.insert(entry).inserted { + ordered.append(entry) + } + } + return ordered.joined(separator: ":") + } +} diff --git a/Sources/TerminalSurface.swift b/Sources/TerminalSurface.swift index d1fa6733..a1fe83d1 100644 --- a/Sources/TerminalSurface.swift +++ b/Sources/TerminalSurface.swift @@ -1426,9 +1426,8 @@ final class TerminalSurface: Identifiable, ObservableObject { ?? getenv("PATH").map { String(cString: $0) } ?? ProcessInfo.processInfo.environment["PATH"] ?? "" - if !currentPath.split(separator: ":").contains(Substring(cliBinPath)) { - let separator = currentPath.isEmpty ? "" : ":" - setManagedEnvironmentValue("PATH", "\(cliBinPath)\(separator)\(currentPath)") + if let path = TerminalPathEnvironment.startupPath(prependingDirectory: cliBinPath, to: currentPath) { + setManagedEnvironmentValue("PATH", path) } } From 465d9e52ee9db884391588b0d2bdf0c80d617274 Mon Sep 17 00:00:00 2001 From: arzafran Date: Sun, 15 Nov 2026 12:09:49 -0300 Subject: [PATCH 2/3] test: terminal PATH must drop malformed inherited components An inherited PATH entry with invalid bytes decodes as U+FFFD and control characters, and Programa re-exports it to every new terminal and tmux shim. These tests fail until the PATH helper filters such entries. --- programaTests/GhosttyConfigTests.swift | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/programaTests/GhosttyConfigTests.swift b/programaTests/GhosttyConfigTests.swift index fd1914f8..06ab62fd 100644 --- a/programaTests/GhosttyConfigTests.swift +++ b/programaTests/GhosttyConfigTests.swift @@ -1099,6 +1099,53 @@ final class GhosttyTerminalStartupEnvironmentTests: XCTestCase { XCTAssertEqual(merged["COLORTERM"], TerminalSurface.managedColorTerm) XCTAssertEqual(merged["TERM_PROGRAM"], TerminalSurface.managedTerminalProgram) } + + // Foundation and String(cString:) decode malformed inherited PATH bytes + // as U+FFFD; re-exporting them hands strict tools an invalid PATH. + private let malformedPathComponent = "\u{FFFD}u[\u{FFFD}\u{0008}`\u{FFFD}-\u{FFFD}(\u{FFFD}" + + func testStartupPathDropsMalformedComponentsWhenPrependingCLIBin() { + XCTAssertEqual( + TerminalPathEnvironment.startupPath( + prependingDirectory: "/Applications/Programa.app/Contents/Resources/bin", + to: "/usr/bin:\(malformedPathComponent):/bin" + ), + "/Applications/Programa.app/Contents/Resources/bin:/usr/bin:/bin" + ) + } + + func testStartupPathSanitizesPathThatAlreadyContainsCLIBin() { + let cliBin = "/Applications/Programa.app/Contents/Resources/bin" + XCTAssertEqual( + TerminalPathEnvironment.startupPath( + prependingDirectory: cliBin, + to: "\(cliBin):/usr/bin:\(malformedPathComponent)" + ), + "\(cliBin):/usr/bin" + ) + } + + func testStartupPathLeavesCleanPathContainingCLIBinAlone() { + let cliBin = "/Applications/Programa.app/Contents/Resources/bin" + XCTAssertNil( + TerminalPathEnvironment.startupPath(prependingDirectory: cliBin, to: "/usr/bin:\(cliBin)::/bin") + ) + XCTAssertEqual( + TerminalPathEnvironment.startupPath(prependingDirectory: cliBin, to: "/usr/bin::/bin"), + "\(cliBin):/usr/bin::/bin" + ) + XCTAssertEqual(TerminalPathEnvironment.startupPath(prependingDirectory: cliBin, to: ""), cliBin) + } + + func testPrependingUniqueEntriesDropsMalformedComponents() { + XCTAssertEqual( + TerminalPathEnvironment.prependingUniqueEntries( + ["/tmp/programa-tmux-shim"], + to: "/usr/bin:\(malformedPathComponent):/usr/bin:/bin" + ), + "/tmp/programa-tmux-shim:/usr/bin:/bin" + ) + } } final class TitlebarDoubleClickPreferenceTests: XCTestCase { From ea3196fa545ad5e17c2ceb9b04473dae7b4aab60 Mon Sep 17 00:00:00 2001 From: arzafran Date: Sun, 15 Nov 2026 12:10:26 -0300 Subject: [PATCH 3/3] fix: drop malformed PATH entries before exporting them to terminals An inherited PATH entry with invalid bytes decodes as U+FFFD, and Programa passed that text on to every new terminal and to the tmux shim PATH, so strict tools saw a corrupted PATH. The PATH helper now drops entries that carry U+FFFD or control characters, and the startup PATH is rewritten even when it already contains the bundled CLI directory. --- Sources/TerminalPathEnvironment.swift | 32 +++++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/Sources/TerminalPathEnvironment.swift b/Sources/TerminalPathEnvironment.swift index c13ddea0..7db2f39b 100644 --- a/Sources/TerminalPathEnvironment.swift +++ b/Sources/TerminalPathEnvironment.swift @@ -3,26 +3,44 @@ import Foundation /// Composes the PATH values Programa exports to terminals and child processes. /// Compiled into both the app and the `programa` CLI. enum TerminalPathEnvironment { + /// The components of `path`, minus any that cannot be re-exported intact. + /// + /// Foundation and `String(cString:)` decode malformed inherited bytes as + /// U+FFFD, so such a component no longer names the directory it came + /// from, and strict tools reject the PATH that carries it. Components with + /// control characters are dropped for the same reason. Empty components + /// are kept, since they mean the current directory. + static func components(from path: String) -> [String] { + path.split(separator: ":", omittingEmptySubsequences: false) + .map(String.init) + .filter { !$0.unicodeScalars.contains(where: isMalformedScalar) } + } + /// The PATH to export when `directory` must lead `currentPath`, or nil - /// when `currentPath` already contains it and can be left alone. + /// when `currentPath` already contains it and has nothing to drop. static func startupPath(prependingDirectory directory: String, to currentPath: String) -> String? { - if currentPath.split(separator: ":").contains(Substring(directory)) { - return nil + let entries = components(from: currentPath) + let sanitizedPath = entries.joined(separator: ":") + if entries.contains(directory) { + return sanitizedPath == currentPath ? nil : sanitizedPath } - let separator = currentPath.isEmpty ? "" : ":" - return "\(directory)\(separator)\(currentPath)" + return sanitizedPath.isEmpty ? directory : "\(directory):\(sanitizedPath)" } /// Prepends `newEntries` to `currentPath`, keeping the first occurrence - /// of each entry and dropping empty ones. + /// of each entry and dropping empty and malformed ones. static func prependingUniqueEntries(_ newEntries: [String], to currentPath: String?) -> String { var ordered: [String] = [] var seen: Set = [] - for entry in newEntries + (currentPath?.split(separator: ":").map(String.init) ?? []) where !entry.isEmpty { + for entry in newEntries + components(from: currentPath ?? "") where !entry.isEmpty { if seen.insert(entry).inserted { ordered.append(entry) } } return ordered.joined(separator: ":") } + + private static func isMalformedScalar(_ scalar: Unicode.Scalar) -> Bool { + scalar == "\u{FFFD}" || CharacterSet.controlCharacters.contains(scalar) + } }