Skip to content
Open
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
8 changes: 4 additions & 4 deletions libs/lume/src/Commands/SSH.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ struct SSH: AsyncParsableCommand {
timeout: TimeInterval(timeout)
)

if !result.output.isEmpty {
print(result.output)
if !result.outputData.isEmpty {
FileHandle.standardOutput.write(result.outputData)
}

if result.exitCode != 0 {
Expand All @@ -129,8 +129,8 @@ struct SSH: AsyncParsableCommand {
timeout: TimeInterval(timeout)
)

if !result.output.isEmpty {
print(result.output)
if !result.outputData.isEmpty {
FileHandle.standardOutput.write(result.outputData)
}

if result.exitCode != 0 {
Expand Down
16 changes: 12 additions & 4 deletions libs/lume/src/SSH/SSHClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import Foundation
/// Result of an SSH command execution
public struct SSHResult: Sendable {
public let exitCode: Int32
public let outputData: Data
public let output: String

public init(exitCode: Int32, output: String) {
self.exitCode = exitCode
self.outputData = Data(output.utf8)
self.output = output
}

public init(exitCode: Int32, outputData: Data) {
self.exitCode = exitCode
self.outputData = outputData
self.output = String(decoding: outputData, as: UTF8.self)
}
}

/// SSH client using SwiftNIO SSH for typed, versioned API
Expand Down Expand Up @@ -310,13 +318,13 @@ private final class CommandExecHandler: ChannelDuplexHandler, @unchecked Sendabl
// Complete when we have exit status (some servers close channel before sending exit status)
if let status = exitStatus {
resultPromise = nil
let output = outputBuffer.readString(length: outputBuffer.readableBytes) ?? ""
promise.succeed(SSHResult(exitCode: status, output: output))
let outputData = outputBuffer.readData(length: outputBuffer.readableBytes) ?? Data()
promise.succeed(SSHResult(exitCode: status, outputData: outputData))
} else if channelClosed {
// Channel closed without exit status - assume success with exit code 0
resultPromise = nil
let output = outputBuffer.readString(length: outputBuffer.readableBytes) ?? ""
promise.succeed(SSHResult(exitCode: 0, output: output))
let outputData = outputBuffer.readData(length: outputBuffer.readableBytes) ?? Data()
promise.succeed(SSHResult(exitCode: 0, outputData: outputData))
}
}

Expand Down
8 changes: 5 additions & 3 deletions libs/lume/src/SSH/SystemSSHClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public final class SystemSSHClient: Sendable {

let stdoutData = stdoutPipe.fileHandleForReading.readDataToEndOfFile()
let stderrData = stderrPipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: stdoutData, encoding: .utf8) ?? ""
let errorOutput = String(data: stderrData, encoding: .utf8) ?? ""

// Filter out SSH warnings from stderr (known_hosts, etc.)
Expand All @@ -73,11 +72,14 @@ public final class SystemSSHClient: Sendable {
}
.joined(separator: "\n")

let combinedOutput = filteredError.isEmpty ? output : output + filteredError
var outputData = stdoutData
if !filteredError.isEmpty, let errorData = filteredError.data(using: .utf8) {
outputData.append(errorData)
}
Comment on lines +75 to +78
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Do not append stderr bytes into outputData.

This reintroduces stream corruption for binary stdout whenever non-filtered stderr exists, because outputData is no longer raw stdout-only bytes.

Suggested change
-        var outputData = stdoutData
-        if !filteredError.isEmpty, let errorData = filteredError.data(using: .utf8) {
-            outputData.append(errorData)
-        }
+        let outputData = stdoutData
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var outputData = stdoutData
if !filteredError.isEmpty, let errorData = filteredError.data(using: .utf8) {
outputData.append(errorData)
}
let outputData = stdoutData
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@libs/lume/src/SSH/SystemSSHClient.swift` around lines 75 - 78, The code
currently appends filtered stderr text bytes into outputData (the stdout buffer)
which corrupts binary stdout; revert this by removing the append of errorData
into outputData so outputData remains exactly stdoutData, and instead preserve
filteredError (or errorData) in its own variable/return value if callers need
stderr separately; update the logic around outputData, stdoutData, filteredError
in SystemSSHClient.swift (the outputData/filteredError handling) so stdout bytes
are never mutated with stderr bytes.


return SSHResult(
exitCode: process.terminationStatus,
output: combinedOutput
outputData: outputData
)
}

Expand Down
15 changes: 15 additions & 0 deletions libs/lume/tests/SSHResultTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation
import Testing

@testable import lume

@Test("SSHResult preserves raw output bytes alongside text view")
func testSSHResultPreservesRawOutputBytes() {
let bytes = Data([0x00, 0xff, 0x41, 0x80, 0x42])
let result = SSHResult(exitCode: 0, outputData: bytes)

#expect(result.outputData == bytes)
#expect(result.output.contains("A"))
#expect(result.output.contains("B"))
}