dashboard / erock/pico / fix(pssh): normalize line ending with pty #118 rss

open · opened on 2026-02-26T03:15:38Z by erock
Help
checkout latest patchset:
ssh pr.pico.sh print pr-118 | git am -3
checkout any patchset in a patch request:
ssh pr.pico.sh print ps-X | git am -3
add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add 118
add review to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add --review 118
accept PR:
ssh pr.pico.sh pr accept 118
close PR:
ssh pr.pico.sh pr close 118
Timeline Patchsets

Patchset ps-210

fix(pssh): normalize line ending with pty

Eric Bower
2026-02-26T03:13:25Z
Back to top
+27 -0 pkg/pssh/server.go link
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
diff --git a/pkg/pssh/server.go b/pkg/pssh/server.go
index 31446f2..31a4301 100644
--- a/pkg/pssh/server.go
+++ b/pkg/pssh/server.go
@@ -1,6 +1,7 @@
 package pssh
 
 import (
+	"bytes"
 	"context"
 	"crypto/ed25519"
 	"crypto/rand"
@@ -185,6 +186,32 @@ func (s *SSHServerConnSession) Pty() (*Pty, <-chan Window, bool) {
 	return s.pty, s.winch, true
 }
 
+// Write overrides the embedded Channel's Write to normalize line endings when PTY is allocated.
+func (s *SSHServerConnSession) Write(p []byte) (n int, err error) {
+	s.mu.Lock()
+	hasPty := s.pty != nil
+	s.mu.Unlock()
+
+	if !hasPty {
+		// No PTY, write as-is
+		return s.Channel.Write(p)
+	}
+
+	// When PTY is active, normalize line endings like a real terminal would.
+	// Replace \n with \r\n, but avoid double \r\n.
+	normalized := bytes.ReplaceAll(p, []byte{'\n'}, []byte{'\r', '\n'})
+	normalized = bytes.ReplaceAll(normalized, []byte{'\r', '\r', '\n'}, []byte{'\r', '\n'})
+
+	// Write the normalized data
+	written, err := s.Channel.Write(normalized)
+
+	// Return the count based on original data length, not normalized
+	if written > len(p) {
+		written = len(p)
+	}
+	return written, err
+}
+
 var _ context.Context = &SSHServerConnSession{}
 
 func (sc *SSHServerConn) Handle(chans <-chan ssh.NewChannel, reqs <-chan *ssh.Request) error {