Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions CLI/CMUXCLI+SSHCommandSupport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Foundation

extension CMUXCLI {
internal func openSSHLocalCommandValue(shellScript: String?) -> String? {
guard let shellScript else { return nil }
let trimmed = shellScript.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return nil }
return openSSHCommandOptionValue(posixShellCommand(trimmed))
}

internal func openSSHRemoteCommandValue(shellScript: String) -> String {
openSSHCommandOptionValue(posixShellCommand(shellScript))
}

internal func posixShellCommand(_ shellScript: String) -> String {
"/bin/sh -c " + shellQuote(shellScript)
}

internal func openSSHCommandOptionValue(_ command: String) -> String {
command.replacingOccurrences(of: "%", with: "%%")
}

/// Joins self-delimiting POSIX shell snippets with one space; this is not a general shell combiner.
internal func combinedLocalShellScript(_ parts: [String?]) -> String? {
let filtered = parts.compactMap { raw -> String? in
guard let raw else { return nil }
let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? nil : trimmed
}
guard !filtered.isEmpty else { return nil }
return filtered.joined(separator: " ")
}
}
66 changes: 26 additions & 40 deletions CLI/cmux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4758,19 +4758,19 @@ struct CMUXCLI {
target: sshOptions.displayDestination,
relayPort: sshOptions.remoteRelayPort
)
let combinedLocalCommand = combinedLocalShellCommand([
let combinedLocalCommandScript = combinedLocalShellScript([
deferredRemoteReconnectCommand,
sshConnectionTimingCommand,
])
let configuredForegroundAuthToken = deferredRemoteReconnectCommand == nil ? nil : deferredRemoteReconnectToken
let startupInitialSSHCommand = buildSSHCommandText(
sshOptions,
localCommand: combinedLocalCommand
localCommandScript: combinedLocalCommandScript
)
let startupRemoteTerminalSSHCommand = buildSSHCommandText(
sshOptions,
remoteBootstrapScript: remoteTerminalBootstrapScript,
localCommand: combinedLocalCommand
localCommandScript: combinedLocalCommandScript
)
let initialSSHStartupCommand: String
let remoteTerminalSSHStartupCommand: String
Expand All @@ -4780,14 +4780,14 @@ struct CMUXCLI {
remoteBootstrapScript: remoteTerminalBootstrapScript,
shellFeatures: shellFeaturesValue,
remoteRelayPort: sshOptions.remoteRelayPort,
localCommand: combinedLocalCommand
localCommandScript: combinedLocalCommandScript
)
remoteTerminalSSHStartupCommand = buildReusableBootstrapSSHStartupCommand(
options: sshOptions,
remoteBootstrapScript: remoteTerminalBootstrapScript,
shellFeatures: shellFeaturesValue,
remoteRelayPort: sshOptions.remoteRelayPort,
localCommand: combinedLocalCommand
localCommandScript: combinedLocalCommandScript
)
} else {
initialSSHStartupCommand = try buildSSHStartupCommand(
Expand Down Expand Up @@ -5046,12 +5046,12 @@ struct CMUXCLI {
func buildSSHCommandText(
_ options: SSHCommandOptions,
remoteBootstrapScript: String? = nil,
localCommand: String? = nil
localCommandScript: String? = nil
) -> String {
buildSSHCommandArguments(
options,
remoteBootstrapScript: remoteBootstrapScript,
localCommand: localCommand
localCommandScript: localCommandScript
)
.map(shellQuote)
.joined(separator: " ")
Expand All @@ -5060,16 +5060,16 @@ struct CMUXCLI {
private func buildSSHCommandArguments(
_ options: SSHCommandOptions,
remoteBootstrapScript: String? = nil,
localCommand: String? = nil
localCommandScript: String? = nil
) -> [String] {
var parts = baseSSHArguments(options, localCommand: localCommand)
var parts = baseSSHArguments(options, localCommandScript: localCommandScript)
let trimmedRemoteBootstrap = remoteBootstrapScript?
.trimmingCharacters(in: .whitespacesAndNewlines)

if options.extraArguments.isEmpty {
if let trimmedRemoteBootstrap, !trimmedRemoteBootstrap.isEmpty {
let remoteCommand = sshPercentEscapedRemoteCommand(
encodedRemoteBootstrapCommand(
let remoteCommand = openSSHRemoteCommandValue(
shellScript: encodedRemoteBootstrapCommand(
trimmedRemoteBootstrap,
remoteRelayPort: options.remoteRelayPort
)
Expand All @@ -5092,12 +5092,12 @@ struct CMUXCLI {
remoteBootstrapScript: String,
shellFeatures: String,
remoteRelayPort: Int,
localCommand: String? = nil
localCommandScript: String? = nil
) throws -> String {
let commandSnippet = buildSSHBootstrapCommandSnippet(
options: options,
remoteBootstrapScript: remoteBootstrapScript,
localCommand: localCommand
localCommandScript: localCommandScript
)
return try buildSSHStartupCommand(
sshCommand: commandSnippet,
Expand All @@ -5112,12 +5112,12 @@ struct CMUXCLI {
remoteBootstrapScript: String,
shellFeatures: String,
remoteRelayPort: Int,
localCommand: String? = nil
localCommandScript: String? = nil
) -> String {
let commandSnippet = buildSSHBootstrapCommandSnippet(
options: options,
remoteBootstrapScript: remoteBootstrapScript,
localCommand: localCommand
localCommandScript: localCommandScript
)
return buildReusableSSHStartupCommand(
sshCommand: commandSnippet,
Expand All @@ -5130,17 +5130,17 @@ struct CMUXCLI {
private func buildSSHBootstrapCommandSnippet(
options: SSHCommandOptions,
remoteBootstrapScript: String,
localCommand: String? = nil
localCommandScript: String? = nil
) -> String {
let encodedBootstrapScript = Data(remoteBootstrapScript.utf8).base64EncodedString()
let installSSHPrefix = baseSSHArguments(options, localCommand: localCommand).map(shellQuote).joined(separator: " ")
let installSSHPrefix = baseSSHArguments(options, localCommandScript: localCommandScript).map(shellQuote).joined(separator: " ")
let sessionSSHPrefix = baseSSHArguments(options).map(shellQuote).joined(separator: " ")
let remoteCommandTemplate = sshPercentEscapedRemoteCommand(
stagedRemoteBootstrapCommandShell(
let remoteCommandTemplate = openSSHRemoteCommandValue(
shellScript: stagedRemoteBootstrapCommandShell(
remoteRelayPort: options.remoteRelayPort
)
)
let remoteBootstrapInstallCommand = "/bin/sh -c " + shellQuote(
let remoteBootstrapInstallCommand = posixShellCommand(
remoteBootstrapInstallShell(remoteRelayPort: options.remoteRelayPort)
)
var lines: [String] = [
Expand Down Expand Up @@ -5441,7 +5441,7 @@ struct CMUXCLI {
shellFeatures: shellFeatures,
terminfoSource: terminfoSource
)
return "/bin/sh -c \(shellQuote(script))"
return posixShellCommand(script)
}

private func interactiveRemoteTerminalSetupLines(terminfoSource: String?) -> [String] {
Expand Down Expand Up @@ -5519,7 +5519,7 @@ struct CMUXCLI {
]
}

private func baseSSHArguments(_ options: SSHCommandOptions, localCommand: String? = nil) -> [String] {
private func baseSSHArguments(_ options: SSHCommandOptions, localCommandScript: String? = nil) -> [String] {
let effectiveSSHOptions = effectiveSSHOptions(
options.sshOptions,
remoteRelayPort: options.remoteRelayPort
Expand Down Expand Up @@ -5549,8 +5549,7 @@ struct CMUXCLI {
for option in effectiveSSHOptions {
parts += ["-o", option]
}
if let localCommand, !localCommand.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
let escapedLocalCommand = localCommand.replacingOccurrences(of: "%", with: "%%")
if let escapedLocalCommand = openSSHLocalCommandValue(shellScript: localCommandScript) {
parts += ["-o", "PermitLocalCommand=yes"]
parts += ["-o", "LocalCommand=\(escapedLocalCommand)"]
}
Expand Down Expand Up @@ -5636,10 +5635,6 @@ struct CMUXCLI {
return lines.joined(separator: "\n")
}

func sshPercentEscapedRemoteCommand(_ remoteCommand: String) -> String {
remoteCommand.replacingOccurrences(of: "%", with: "%%")
}

func buildSSHStartupCommand(
sshCommand: String,
shellFeatures: String,
Expand Down Expand Up @@ -6668,7 +6663,8 @@ struct CMUXCLI {
.replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "\"", with: "\\\"")
return [
"cmux_ssh_log_path=\"$(tr -d '\\r\\n' < /tmp/cmux-last-debug-log-path 2>/dev/null || true)\";",
"cmux_ssh_log_path=\"\";",
"if [ -r /tmp/cmux-last-debug-log-path ]; then cmux_ssh_log_path=\"$(tr -d '\\r\\n' < /tmp/cmux-last-debug-log-path 2>/dev/null || true)\"; fi;",
"if [ -n \"$cmux_ssh_log_path\" ]; then",
"cmux_ssh_ts=\"$(python3 -c 'from datetime import datetime, timezone; print(datetime.now(timezone.utc).isoformat(timespec=\"milliseconds\").replace(\"+00:00\", \"Z\"))' 2>/dev/null || date -u +%Y-%m-%dT%H:%M:%SZ)\";",
"printf '%s [cmux-cli] cli.ssh.handshake target=\(escapedTarget) relayPort=\(relayPort) stage=ssh.connected workspace=%s surface=%s\\n' \"$cmux_ssh_ts\" \"${CMUX_WORKSPACE_ID:-nil}\" \"${CMUX_SURFACE_ID:-nil}\" >> \"$cmux_ssh_log_path\";",
Expand All @@ -6677,16 +6673,6 @@ struct CMUXCLI {
].joined(separator: " ")
}

private func combinedLocalShellCommand(_ parts: [String?]) -> String? {
let filtered = parts.compactMap { raw -> String? in
guard let raw else { return nil }
let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? nil : trimmed
}
guard !filtered.isEmpty else { return nil }
return filtered.joined(separator: " ")
}

private func shouldDeferRemoteReconnect(in options: [String]) -> Bool {
guard !hasSSHOptionKey(options, key: "LocalCommand"),
!hasSSHOptionKey(options, key: "PermitLocalCommand") else {
Expand Down Expand Up @@ -6731,7 +6717,7 @@ struct CMUXCLI {
return trimmed
}

private func shellQuote(_ value: String) -> String {
func shellQuote(_ value: String) -> String {
Comment thread
austinywang marked this conversation as resolved.
Resolved
let safePattern = "^[A-Za-z0-9_@%+=:,./-]+$"
if value.range(of: safePattern, options: .regularExpression) != nil {
return value
Expand Down
8 changes: 8 additions & 0 deletions GhosttyTabs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
B9000002A1B2C3D4E5F60719 /* cmux.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9000001A1B2C3D4E5F60719 /* cmux.swift */; };
C510C1E00000000000000002 /* SocketOperationTelemetry.swift in Sources */ = {isa = PBXBuildFile; fileRef = C510C1E00000000000000001 /* SocketOperationTelemetry.swift */; };
B9000034A1B2C3D4E5F60719 /* cmux_open.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9000030A1B2C3D4E5F60719 /* cmux_open.swift */; };
B9000041A1B2C3D4E5F60719 /* CMUXCLI+SSHCommandSupport.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9000040A1B2C3D4E5F60719 /* CMUXCLI+SSHCommandSupport.swift */; };
D7AB0000000000000000000D /* CMUXCLI+MoveTabToNewWorkspace.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7AB0000000000000000000E /* CMUXCLI+MoveTabToNewWorkspace.swift */; };
B900000BA1B2C3D4E5F60719 /* cmux in Copy CLI */ = {isa = PBXBuildFile; fileRef = B9000004A1B2C3D4E5F60719 /* cmux */; };
B900002EA1B2C3D4E5F60719 /* CMUXCLI+Themes.swift in Sources */ = {isa = PBXBuildFile; fileRef = B900002CA1B2C3D4E5F60719 /* CMUXCLI+Themes.swift */; };
Expand Down Expand Up @@ -290,6 +291,7 @@
F6000000A1B2C3D4E5F60718 /* AppDelegateShortcutRoutingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6000001A1B2C3D4E5F60718 /* AppDelegateShortcutRoutingTests.swift */; };
F6001000A1B2C3D4E5F60718 /* ShortcutUnbindingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6001001A1B2C3D4E5F60718 /* ShortcutUnbindingTests.swift */; };
F6100000A1B2C3D4E5F60718 /* WorkspaceRemoteConnectionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6100001A1B2C3D4E5F60718 /* WorkspaceRemoteConnectionTests.swift */; };
F6120000A1B2C3D4E5F60718 /* WorkspaceSSHFishShellTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6120001A1B2C3D4E5F60718 /* WorkspaceSSHFishShellTests.swift */; };
F7000000A1B2C3D4E5F60718 /* WorkspaceContentViewVisibilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7000001A1B2C3D4E5F60718 /* WorkspaceContentViewVisibilityTests.swift */; };
F8000000A1B2C3D4E5F60718 /* SocketControlPasswordStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8000001A1B2C3D4E5F60718 /* SocketControlPasswordStoreTests.swift */; };
F9000000A1B2C3D4E5F60718 /* GhosttyEnsureFocusWindowActivationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9000001A1B2C3D4E5F60718 /* GhosttyEnsureFocusWindowActivationTests.swift */; };
Expand Down Expand Up @@ -603,6 +605,7 @@
B9000001A1B2C3D4E5F60719 /* cmux.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = cmux.swift; sourceTree = "<group>"; };
C510C1E00000000000000001 /* SocketOperationTelemetry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketOperationTelemetry.swift; sourceTree = "<group>"; };
B9000030A1B2C3D4E5F60719 /* cmux_open.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = cmux_open.swift; sourceTree = "<group>"; };
B9000040A1B2C3D4E5F60719 /* CMUXCLI+SSHCommandSupport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CMUXCLI+SSHCommandSupport.swift"; sourceTree = "<group>"; };
D7AB0000000000000000000E /* CMUXCLI+MoveTabToNewWorkspace.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CMUXCLI+MoveTabToNewWorkspace.swift"; sourceTree = "<group>"; };
B9000004A1B2C3D4E5F60719 /* cmux */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = cmux; sourceTree = BUILT_PRODUCTS_DIR; };
B900002CA1B2C3D4E5F60719 /* CMUXCLI+Themes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CMUXCLI+Themes.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -660,6 +663,7 @@
E3309A0A /* AppDelegateEqualizeSplitsShortcutTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegateEqualizeSplitsShortcutTests.swift; sourceTree = "<group>"; };
F6001001A1B2C3D4E5F60718 /* ShortcutUnbindingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutUnbindingTests.swift; sourceTree = "<group>"; };
F6100001A1B2C3D4E5F60718 /* WorkspaceRemoteConnectionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorkspaceRemoteConnectionTests.swift; sourceTree = "<group>"; };
F6120001A1B2C3D4E5F60718 /* WorkspaceSSHFishShellTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorkspaceSSHFishShellTests.swift; sourceTree = "<group>"; };
F7000001A1B2C3D4E5F60718 /* WorkspaceContentViewVisibilityTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorkspaceContentViewVisibilityTests.swift; sourceTree = "<group>"; };
F8000001A1B2C3D4E5F60718 /* SocketControlPasswordStoreTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketControlPasswordStoreTests.swift; sourceTree = "<group>"; };
F9000001A1B2C3D4E5F60718 /* GhosttyEnsureFocusWindowActivationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GhosttyEnsureFocusWindowActivationTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1018,6 +1022,7 @@
B9000001A1B2C3D4E5F60719 /* cmux.swift */,
C510C1E00000000000000001 /* SocketOperationTelemetry.swift */,
B9000030A1B2C3D4E5F60719 /* cmux_open.swift */,
B9000040A1B2C3D4E5F60719 /* CMUXCLI+SSHCommandSupport.swift */,
D7AB0000000000000000000E /* CMUXCLI+MoveTabToNewWorkspace.swift */,
B9000031A1B2C3D4E5F60719 /* CMUXCLI+DocsSettings.swift */,
B900002DA1B2C3D4E5F60719 /* CMUXCLI+ThemeSupport.swift */,
Expand All @@ -1044,6 +1049,7 @@
E3309A0A /* AppDelegateEqualizeSplitsShortcutTests.swift */,
F6001001A1B2C3D4E5F60718 /* ShortcutUnbindingTests.swift */,
F6100001A1B2C3D4E5F60718 /* WorkspaceRemoteConnectionTests.swift */,
F6120001A1B2C3D4E5F60718 /* WorkspaceSSHFishShellTests.swift */,
F7000001A1B2C3D4E5F60718 /* WorkspaceContentViewVisibilityTests.swift */,
F8000001A1B2C3D4E5F60718 /* SocketControlPasswordStoreTests.swift */,
F9000001A1B2C3D4E5F60718 /* GhosttyEnsureFocusWindowActivationTests.swift */,
Expand Down Expand Up @@ -1522,6 +1528,7 @@
B9000002A1B2C3D4E5F60719 /* cmux.swift in Sources */,
C510C1E00000000000000002 /* SocketOperationTelemetry.swift in Sources */,
B9000034A1B2C3D4E5F60719 /* cmux_open.swift in Sources */,
B9000041A1B2C3D4E5F60719 /* CMUXCLI+SSHCommandSupport.swift in Sources */,
D7AB0000000000000000000D /* CMUXCLI+MoveTabToNewWorkspace.swift in Sources */,
B9000035A1B2C3D4E5F60719 /* CMUXCLI+DocsSettings.swift in Sources */,
B900002FA1B2C3D4E5F60719 /* CMUXCLI+ThemeSupport.swift in Sources */,
Expand Down Expand Up @@ -1589,6 +1596,7 @@
E3309A09 /* AppDelegateEqualizeSplitsShortcutTests.swift in Sources */,
F6001000A1B2C3D4E5F60718 /* ShortcutUnbindingTests.swift in Sources */,
F6100000A1B2C3D4E5F60718 /* WorkspaceRemoteConnectionTests.swift in Sources */,
F6120000A1B2C3D4E5F60718 /* WorkspaceSSHFishShellTests.swift in Sources */,
F7000000A1B2C3D4E5F60718 /* WorkspaceContentViewVisibilityTests.swift in Sources */,
F8000000A1B2C3D4E5F60718 /* SocketControlPasswordStoreTests.swift in Sources */,
F9000000A1B2C3D4E5F60718 /* GhosttyEnsureFocusWindowActivationTests.swift in Sources */,
Expand Down
1 change: 0 additions & 1 deletion Sources/GhosttyTerminalAppearance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,3 @@ enum GhosttyNotificationKey {
static let selectionBackground = "ghostty.selectionBackground"
static let selectionForeground = "ghostty.selectionForeground"
}

1 change: 0 additions & 1 deletion Sources/WorkspaceAppearanceResolution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,3 @@ extension WorkspaceContentView {
].joined(separator: "|")
}
}

1 change: 0 additions & 1 deletion cmuxTests/GhosttyNotificationDispatcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,3 @@ final class GhosttyDefaultBackgroundNotificationDispatcherTests: XCTestCase {
return -1
}
}

1 change: 0 additions & 1 deletion cmuxTests/WorkspaceAppearanceConfigResolutionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,3 @@ final class WorkspaceAppearanceConfigResolutionTests: XCTestCase {
XCTAssertEqual(resolved.backgroundColor.hexString(), "#272822")
}
}

Loading