Commit b10aef1
Eric Bower
·
2026-02-25 22:13:25 -0500 EST
parent 1f6cc61
fix(pssh): normalize line ending with pty
1 files changed,
+27,
-0
+27,
-0
| ... | ... | @@ -1,6 +1,7 @@ | |
| 1 | 1 | package pssh | |
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | + | "bytes" | |
| 4 | 5 | "context" | |
| 5 | 6 | "crypto/ed25519" | |
| 6 | 7 | "crypto/rand" |
| ... | ... | @@ -185,6 +186,32 @@ func (s *SSHServerConnSession) Pty() (*Pty, <-chan Window, bool) { | |
| 185 | 186 | return s.pty, s.winch, true | |
| 186 | 187 | } | |
| 187 | 188 | ||
| 189 | + | // Write overrides the embedded Channel's Write to normalize line endings when PTY is allocated. | |
| 190 | + | func (s *SSHServerConnSession) Write(p []byte) (n int, err error) { | |
| 191 | + | s.mu.Lock() | |
| 192 | + | hasPty := s.pty != nil | |
| 193 | + | s.mu.Unlock() | |
| 194 | + | ||
| 195 | + | if !hasPty { | |
| 196 | + | // No PTY, write as-is | |
| 197 | + | return s.Channel.Write(p) | |
| 198 | + | } | |
| 199 | + | ||
| 200 | + | // When PTY is active, normalize line endings like a real terminal would. | |
| 201 | + | // Replace \n with \r\n, but avoid double \r\n. | |
| 202 | + | normalized := bytes.ReplaceAll(p, []byte{'\n'}, []byte{'\r', '\n'}) | |
| 203 | + | normalized = bytes.ReplaceAll(normalized, []byte{'\r', '\r', '\n'}, []byte{'\r', '\n'}) | |
| 204 | + | ||
| 205 | + | // Write the normalized data | |
| 206 | + | written, err := s.Channel.Write(normalized) | |
| 207 | + | ||
| 208 | + | // Return the count based on original data length, not normalized | |
| 209 | + | if written > len(p) { | |
| 210 | + | written = len(p) | |
| 211 | + | } | |
| 212 | + | return written, err | |
| 213 | + | } | |
| 214 | + | ||
| 188 | 215 | var _ context.Context = &SSHServerConnSession{} | |
| 189 | 216 | ||
| 190 | 217 | func (sc *SSHServerConn) Handle(chans <-chan ssh.NewChannel, reqs <-chan *ssh.Request) error { |