diff --git a/pkg/devcontainer/sshtunnel/command_error.go b/pkg/devcontainer/sshtunnel/command_error.go new file mode 100644 index 0000000000..277cb340c5 --- /dev/null +++ b/pkg/devcontainer/sshtunnel/command_error.go @@ -0,0 +1,28 @@ +package sshtunnel + +import "github.com/devsy-org/devsy/pkg/log" + +var _ log.LogSummaryProvider = (*agentCommandFailure)(nil) + +const agentCommandFailedPrefix = "run agent command failed: " + +// agentCommandFailure pairs a command failure with the redacted stderr tail +// the streamer already logged. Error includes the tail for CLI consumers; +// LogSummary omits it so log sites do not replay it. +type agentCommandFailure struct { + cause error + diagnostics string +} + +func (e *agentCommandFailure) Error() string { + if e.diagnostics == "" { + return e.LogSummary() + } + return e.LogSummary() + "\n" + e.diagnostics +} + +func (e *agentCommandFailure) Unwrap() error { return e.cause } + +func (e *agentCommandFailure) LogSummary() string { + return agentCommandFailedPrefix + e.cause.Error() +} diff --git a/pkg/devcontainer/sshtunnel/command_error_test.go b/pkg/devcontainer/sshtunnel/command_error_test.go new file mode 100644 index 0000000000..dcc71e86e6 --- /dev/null +++ b/pkg/devcontainer/sshtunnel/command_error_test.go @@ -0,0 +1,78 @@ +package sshtunnel + +import ( + "context" + "errors" + "fmt" + "testing" + + "github.com/devsy-org/devsy/pkg/log" + devssh "github.com/devsy-org/devsy/pkg/ssh" + "github.com/stretchr/testify/suite" +) + +const failureDiagnostics = `{"level":"debug","ts":"2026-10-13T22:41:48.610Z","msg":"credentials server started"} +{"kind":"error","outcome":"error","code":"UNKNOWN","message":"command failed with exit code 1"}` + +var errSentinelCause = errors.New("sentinel cause") + +type AgentCommandFailureTestSuite struct { + suite.Suite +} + +func TestAgentCommandFailureTestSuite(t *testing.T) { + suite.Run(t, new(AgentCommandFailureTestSuite)) +} + +func (s *AgentCommandFailureTestSuite) TestErrorKeepsDiagnosticsAndLogSummaryOmitsThem() { + cause := errors.New("exit status 1") + + withDiagnostics := &agentCommandFailure{cause: cause, diagnostics: failureDiagnostics} + s.Equal( + fmt.Errorf("run agent command failed: %w\n%s", cause, failureDiagnostics).Error(), + withDiagnostics.Error(), + ) + s.Same(cause, withDiagnostics.Unwrap()) + s.Equal("run agent command failed: exit status 1", withDiagnostics.LogSummary()) + + empty := &agentCommandFailure{cause: cause} + s.Equal("run agent command failed: exit status 1", empty.Error()) + s.Equal(empty.Error(), empty.LogSummary()) +} + +func (s *AgentCommandFailureTestSuite) TestWrappedFailureMatchesCauseAndType() { + exitErr := &devssh.ExitError{ExitCode: 1} + for _, tc := range []struct { + name string + cause error + target error + }{ + {"context canceled", context.Canceled, context.Canceled}, + {"sentinel", errSentinelCause, errSentinelCause}, + {"ssh exit error", exitErr, exitErr}, + } { + s.Run(tc.name, func() { + failure := &agentCommandFailure{cause: tc.cause, diagnostics: failureDiagnostics} + wrapped := fmt.Errorf("outer: %w", fmt.Errorf("middle: %w", failure)) + + s.ErrorIs(wrapped, tc.target) + s.ErrorIs(failure, tc.target) + + var asFailure *agentCommandFailure + s.Require().ErrorAs(wrapped, &asFailure) + s.Same(failure, asFailure) + + s.Equal(failure.LogSummary(), log.ErrorForLog(wrapped)) + }) + } + + var exit *devssh.ExitError + failure := &agentCommandFailure{cause: fmt.Errorf("wrap: %w", exitErr)} + s.Require().ErrorAs(fmt.Errorf("outer: %w", failure), &exit) + s.Equal(1, exit.ExitCode) +} + +func (s *AgentCommandFailureTestSuite) TestErrorForLogOmitsDiagnostics() { + failure := &agentCommandFailure{cause: errSentinelCause, diagnostics: failureDiagnostics} + s.Equal("run agent command failed: sentinel cause", log.ErrorForLog(failure)) +} diff --git a/pkg/devcontainer/sshtunnel/sshtunnel.go b/pkg/devcontainer/sshtunnel/sshtunnel.go index 9ea49e8f3d..25f725567b 100644 --- a/pkg/devcontainer/sshtunnel/sshtunnel.go +++ b/pkg/devcontainer/sshtunnel/sshtunnel.go @@ -247,11 +247,11 @@ func runCommandInSSHTunnel(ctx context.Context, p sshCommandParams, stdin, stdou Stderr: streamer, }) if err != nil { - _ = streamer.Close() - if out := streamer.ErrorOutput(); out != "" { - return fmt.Errorf("run agent command failed: %w\n%s", err, out) + _ = streamer.Close() // drain before reading ErrorOutput + return &agentCommandFailure{ + cause: err, + diagnostics: streamer.ErrorOutput(), } - return fmt.Errorf("run agent command failed: %w", err) } return nil } diff --git a/pkg/log/error_value.go b/pkg/log/error_value.go new file mode 100644 index 0000000000..fc1a7cf2d7 --- /dev/null +++ b/pkg/log/error_value.go @@ -0,0 +1,48 @@ +package log + +import ( + "errors" + "strings" + "unicode/utf8" +) + +const ( + errorSummaryMaxRunes = 2048 + truncatedErrorSummarySuffix = "...[truncated]" +) + +// LogSummaryProvider is implemented by errors whose Error text carries data +// that must not be replayed into a log record, such as a diagnostic tail that +// was already emitted separately. +type LogSummaryProvider interface { + LogSummary() string +} + +// ErrorForLog returns a log-safe description of err, capped at 2048 runes +// including the truncation suffix. The first LogSummaryProvider found while +// unwrapping err supplies the summary; otherwise err.Error() is used. +func ErrorForLog(err error) string { + if err == nil { + return "" + } + summary := err.Error() + var provider LogSummaryProvider + if errors.As(err, &provider) { + summary = provider.LogSummary() + } + return truncateErrorSummary(summary) +} + +func truncateErrorSummary(summary string) string { + summary = strings.ToValidUTF8(summary, "\uFFFD") + if utf8.RuneCountInString(summary) <= errorSummaryMaxRunes { + return summary + } + keep := errorSummaryMaxRunes - utf8.RuneCountInString(truncatedErrorSummarySuffix) + cutoff := 0 + for range keep { + _, width := utf8.DecodeRuneInString(summary[cutoff:]) + cutoff += width + } + return summary[:cutoff] + truncatedErrorSummarySuffix +} diff --git a/pkg/log/error_value_test.go b/pkg/log/error_value_test.go new file mode 100644 index 0000000000..34286fee71 --- /dev/null +++ b/pkg/log/error_value_test.go @@ -0,0 +1,103 @@ +package log + +import ( + "errors" + "fmt" + "strings" + "testing" + "unicode/utf8" + + "github.com/stretchr/testify/suite" +) + +type summaryError struct { + full string + summary string +} + +func (e *summaryError) Error() string { return e.full } +func (e *summaryError) LogSummary() string { return e.summary } + +type ErrorForLogTestSuite struct { + suite.Suite +} + +func TestErrorForLogTestSuite(t *testing.T) { + suite.Run(t, new(ErrorForLogTestSuite)) +} + +func (s *ErrorForLogTestSuite) TestNilAndEmpty() { + s.Equal("", ErrorForLog(nil)) + s.Empty(ErrorForLog(errors.New(""))) +} + +func (s *ErrorForLogTestSuite) TestWrappedTypedFallbackAndMultiline() { + typed := &summaryError{ + full: "cause\n{\"level\":\"debug\",\"msg\":\"a\"}\n{\"level\":\"debug\",\"msg\":\"b\"}", + summary: "cause", + } + wrapped := fmt.Errorf("outer: %w", fmt.Errorf("middle: %w", typed)) + s.Equal("cause", ErrorForLog(typed)) + s.Equal("cause", ErrorForLog(wrapped), "provider summary wins over wrapper text") + + multiline := errors.New("first\nsecond\r\nthird ") + s.Equal("first\nsecond\r\nthird ", ErrorForLog(multiline), "no trimming or first-line cut") +} + +func (s *ErrorForLogTestSuite) TestTypedSummaryIsBounded() { + typed := &summaryError{full: "x", summary: strings.Repeat("a", 5000)} + got := ErrorForLog(fmt.Errorf("wrap: %w", typed)) + s.Equal(errorSummaryMaxRunes, utf8.RuneCountInString(got)) + s.True(strings.HasSuffix(got, truncatedErrorSummarySuffix)) +} + +func (s *ErrorForLogTestSuite) TestTruncationBoundaries() { + suffixRunes := utf8.RuneCountInString(truncatedErrorSummarySuffix) + for _, tc := range []struct { + name string + runes int + truncated bool + }{ + {"2047", errorSummaryMaxRunes - 1, false}, + {"2048", errorSummaryMaxRunes, false}, + {"2049", errorSummaryMaxRunes + 1, true}, + } { + s.Run(tc.name, func() { + input := strings.Repeat("a", tc.runes) + got := truncateErrorSummary(input) + if !tc.truncated { + s.Equal(input, got) + return + } + s.Equal(errorSummaryMaxRunes, utf8.RuneCountInString(got)) + s.Equal( + strings.Repeat("a", errorSummaryMaxRunes-suffixRunes)+truncatedErrorSummarySuffix, + got, + ) + }) + } +} + +func (s *ErrorForLogTestSuite) TestTruncationIsRuneSafe() { + input := strings.Repeat("é世🙂", 1000) // mixed 2/3/4-byte runes + got := truncateErrorSummary(input) + s.True(utf8.ValidString(got)) + s.Equal(errorSummaryMaxRunes, utf8.RuneCountInString(got)) + prefix := strings.TrimSuffix(got, truncatedErrorSummarySuffix) + s.Equal(errorSummaryMaxRunes-utf8.RuneCountInString(truncatedErrorSummarySuffix), + utf8.RuneCountInString(prefix)) + s.True(strings.HasPrefix(input, prefix)) +} + +func (s *ErrorForLogTestSuite) TestInvalidUTF8IsNormalizedOnlyInSummary() { + raw := "bad\xff\xfebytes" + err := errors.New(raw) + got := ErrorForLog(err) + s.Equal("bad\uFFFDbytes", got) + s.Equal(raw, err.Error(), "original error text is not mutated") + + // Each invalid run becomes one replacement rune before counting. + long := truncateErrorSummary(strings.Repeat("a\xff", 3000)) + s.True(utf8.ValidString(long)) + s.Equal(errorSummaryMaxRunes, utf8.RuneCountInString(long)) +} diff --git a/pkg/log/logfmt.go b/pkg/log/logfmt.go index 756f4b1e4b..5e89108879 100644 --- a/pkg/log/logfmt.go +++ b/pkg/log/logfmt.go @@ -5,6 +5,7 @@ import ( "sort" "strings" "time" + "unicode" "go.uber.org/zap" "go.uber.org/zap/buffer" @@ -79,12 +80,16 @@ func (e *logfmtEncoder) EncodeEntry( } func quoteLogfmt(s string) string { - if s == "" || strings.ContainsAny(s, " \t\n\"=") { + if s == "" || strings.IndexFunc(s, needsLogfmtQuote) >= 0 { return fmt.Sprintf("%q", s) } return s } +func needsLogfmtQuote(r rune) bool { + return unicode.IsSpace(r) || unicode.IsControl(r) || r == '"' || r == '=' || r == '\\' +} + // Required interface methods - delegate to buffer for simple types. func (e *logfmtEncoder) AddArray(key string, arr zapcore.ArrayMarshaler) error { return nil } func (e *logfmtEncoder) AddObject(key string, obj zapcore.ObjectMarshaler) error { return nil } diff --git a/pkg/log/logfmt_test.go b/pkg/log/logfmt_test.go new file mode 100644 index 0000000000..cfac41035c --- /dev/null +++ b/pkg/log/logfmt_test.go @@ -0,0 +1,62 @@ +package log + +import ( + "strconv" + "strings" + "testing" + + "github.com/stretchr/testify/suite" + "go.uber.org/zap" +) + +type LogfmtQuotingSuite struct{ suite.Suite } + +func TestLogfmtQuotingSuite(t *testing.T) { suite.Run(t, new(LogfmtQuotingSuite)) } + +func (s *LogfmtQuotingSuite) TestSafeTokenStaysBare() { + s.Equal("plain-token_1.2/x:y", quoteLogfmt("plain-token_1.2/x:y")) +} + +func (s *LogfmtQuotingSuite) TestControlQuoting() { + for name, value := range map[string]string{ + "empty": "", + "CR only": "a\rb", + "LF": "a\nb", + "space": "a b", + "tab": "a\tb", + "vertical tab": "a\vb", + "form feed": "a\fb", + "NUL": "a\x00b", + "DEL": "a\x7fb", + "C1 NEL": "a\u0085b", + "line separator": "a\u2028b", + "para separator": "a\u2029b", + "no-break space": "a\u00a0b", + "backslash": `a\b`, + "quote": `a"b`, + "equals": "a=b", + "ANSI escape": "a\x1b[31mb", + } { + s.Run(name, func() { + quoted := quoteLogfmt(value) + s.True(strings.HasPrefix(quoted, `"`) && strings.HasSuffix(quoted, `"`), quoted) + s.NotContains(quoted, "\n") + s.NotContains(quoted, "\r") + unquoted, err := strconv.Unquote(quoted) + s.Require().NoError(err) + s.Equal(value, unquoted) + }) + } +} + +func (s *LogfmtQuotingSuite) TestEncodedRecordKeepsLayoutAndOneLine() { + enc := newLogfmtEncoder() + buf, err := enc.EncodeEntry(testEntry("cr\ronly"), []zap.Field{zap.String("b", "x\ry"), zap.String("a", "ok")}) + s.Require().NoError(err) + out := buf.String() + buf.Free() + + s.Equal(1, strings.Count(out, "\n")) + s.NotContains(out, "\r") + s.Equal(`level=debug ts=2026-10-13T22:41:50Z msg="cr\ronly" a=ok b="x\ry"`+"\n", out) +} diff --git a/pkg/log/logger.go b/pkg/log/logger.go index e91870da34..7175469f47 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -37,7 +37,9 @@ type Config struct { // Init configures the global logger. Called once in root command PersistentPreRunE. func Init(cfg Config) { - cfg.Redactor = secrets.Combine(cfg.Redactor, secrets.NewEnvironmentRedactor(os.Environ())) + combined := secrets.Combine(cfg.Redactor, secrets.NewEnvironmentRedactor(os.Environ())) + // Writers redact serialized records, so secrets must match in escaped form too. + cfg.Redactor = secrets.WithLogEscapes(combined) level := resolveLevel(cfg) encoder := resolveEncoder(cfg.Format) stderrWriter := zapcore.Lock(&redactingWriter{next: os.Stderr, redactor: cfg.Redactor}) @@ -191,9 +193,9 @@ func resolveEncoder(format string) zapcore.Encoder { default: // "text" - use console encoder, with color if stderr is a terminal if colorEnabled() { - return zapcore.NewConsoleEncoder(colorEncoderConfig()) + return newOneLineTextEncoder(zapcore.NewConsoleEncoder(colorEncoderConfig())) } - return zapcore.NewConsoleEncoder(plainEncoderConfig()) + return newOneLineTextEncoder(zapcore.NewConsoleEncoder(plainEncoderConfig())) } } diff --git a/pkg/log/serialized_record_test.go b/pkg/log/serialized_record_test.go new file mode 100644 index 0000000000..1ca20cba57 --- /dev/null +++ b/pkg/log/serialized_record_test.go @@ -0,0 +1,206 @@ +package log + +import ( + "encoding/json" + "errors" + "io" + "os" + "strconv" + "strings" + "testing" + + "github.com/devsy-org/devsy/pkg/secrets" + "github.com/stretchr/testify/suite" + "go.uber.org/zap" +) + +// The logger is package-global, so these tests must not run in parallel. +type SerializedRecordSuite struct{ suite.Suite } + +func TestSerializedRecordSuite(t *testing.T) { suite.Run(t, new(SerializedRecordSuite)) } + +func (s *SerializedRecordSuite) SetupTest() { + s.T().Setenv("NO_COLOR", "1") + s.T().Cleanup(func() { sugar.Store(zap.NewNop().Sugar()) }) +} + +// capture returns what emit wrote to the AddSink sink and to stderr. +func (s *SerializedRecordSuite) capture(cfg Config, emit func()) (sink, stderr string) { + reader, writer, err := os.Pipe() + s.Require().NoError(err) + previous := os.Stderr + os.Stderr = writer + defer func() { os.Stderr = previous }() + + cfg.Verbosity = 2 + Init(cfg) + var sinkBuf syncBuffer + remove := AddSink(&sinkBuf) + defer remove() + + emit() + _ = Sync() // fsync on a pipe fails with EBADF; the write itself is synchronous + os.Stderr = previous + s.Require().NoError(writer.Close()) + data, err := io.ReadAll(reader) + s.Require().NoError(err) + s.Require().NoError(reader.Close()) + return sinkBuf.String(), string(data) +} + +func (s *SerializedRecordSuite) TestTextMultilineErrorIsOnePhysicalRecord() { + tunnelErr := errors.New( + "run agent command failed: exit status 1\n" + + `{"level":"debug","ts":"2026-10-13T22:41:48.610Z","msg":"incoming client connection: path=/"}` + "\n" + + `{"level":"debug","ts":"2026-10-13T22:41:48.610Z","msg":"credentials server started"}` + "\n" + + `{"kind":"error","outcome":"error","code":"UNKNOWN","message":"command failed with exit code 1"}`, + ) + + sink, stderr := s.capture(Config{Format: "text"}, func() { + Debugf( + "pipe bridge completed: tunnel_err=%v handler_err=%v parent_err=%v pair_err=%v", + tunnelErr, nil, nil, errors.New("context canceled"), + ) + }) + + for name, out := range map[string]string{"sink": sink, "stderr": stderr} { + s.Equal(1, strings.Count(out, "\n"), "%s must be one physical record: %q", name, out) + s.True(strings.HasSuffix(out, "\n"), name) + s.Contains(out, "\tDEBUG\tpipe bridge completed: tunnel_err=run agent command failed: exit status 1\\n{") + s.Contains(out, "pair_err=context canceled") + } + s.Equal(stderr, sink) +} + +func (s *SerializedRecordSuite) TestEveryFormatEmitsOneRecordPerEvent() { + const message = "alpha\nbeta\r\ngamma\rdelta\u2028epsilon\u0085zeta\v\f\x00\x7f\x01 end" + for _, format := range []string{"text", "json", "logfmt"} { + s.Run(format, func() { + sink, stderr := s.capture(Config{Format: format}, func() { + Infof("%s", message) + Infow("fields", "detail", message) + }) + s.Equal(sink, stderr) + lines := strings.Split(strings.TrimSuffix(sink, "\n"), "\n") + s.Require().Len(lines, 2, "two events, two physical lines: %q", sink) + s.NotContains(sink, "\r") + s.assertRecordContent(format, lines, message) + }) + } +} + +func (s *SerializedRecordSuite) assertRecordContent(format string, lines []string, message string) { + switch format { + case "json": + for i, line := range lines { + var decoded map[string]any + s.Require().NoError(json.Unmarshal([]byte(line), &decoded)) + if i == 0 { + s.Equal(message, decoded["msg"], "JSON must round-trip the logical message") + } else { + s.Equal(message, decoded["detail"]) + } + } + case "logfmt": + s.Contains(lines[0], " msg=") + quoted := lines[0][strings.Index(lines[0], " msg=")+len(" msg="):] + unquoted, err := strconv.Unquote(quoted) + s.Require().NoError(err) + s.Equal(message, unquoted) + default: + s.Contains(lines[0], `alpha\nbeta\r\ngamma\rdelta\u2028epsilon\u0085zeta\v\f\x00\x7f\x01 end`) + s.Contains(lines[1], `alpha\nbeta`) + } +} + +func (s *SerializedRecordSuite) TestConcurrentEventsStayOnePhysicalRecordEach() { + sink, _ := s.capture(Config{Format: "text"}, func() { + done := make(chan struct{}) + for i := range 8 { + go func() { + defer func() { done <- struct{}{} }() + for j := range 25 { + Infof("worker %d event %d\nsecond line", i, j) + } + }() + } + for range 8 { + <-done + } + }) + + lines := strings.Split(strings.TrimSuffix(sink, "\n"), "\n") + s.Len(lines, 200) + for _, line := range lines { + s.Contains(line, `\nsecond line`) + } +} + +const redactionSecret = "line1\nli\"ne2\r\\end\x00\v\f\x01\u2028" + +// Encoders rewrite control characters before the writers redact, so escaped +// forms of the secret must be masked too. +func (s *SerializedRecordSuite) TestRedactionAcrossFormats() { + for _, format := range []string{"text", "json", "logfmt"} { + s.Run(format, func() { + sink, stderr := s.capture(Config{ + Format: format, + Redactor: secrets.NewRedactor([]string{"TOKEN=" + redactionSecret}), + }, func() { + Infof("token=%s", redactionSecret) + Infow("with field", "credential", redactionSecret) + Errorf("%v", errors.New(redactionSecret)) + }) + + s.Equal(sink, stderr, "stderr and AddSink must agree") + s.Contains(sink, "***") + s.assertNoSecretFragments(sink) + s.Equal(3, strings.Count(sink, "\n")) + }) + } +} + +func (s *SerializedRecordSuite) assertNoSecretFragments(out string) { + for _, fragment := range []string{ + "line1", "li\"ne2", `li\"ne2`, "ne2", "end", "\x00", "\\x00", "\\u0000", + "\u2028", `\u2028`, "\\x01", "\\u0001", + } { + s.NotContains(out, fragment, "secret fragment %q leaked", fragment) + } +} + +func (s *SerializedRecordSuite) TestRedactionCoversEveryControlByte() { + chars := make([]string, 0, 0x80+3) + for b := range 0x80 { + chars = append(chars, string(rune(b))) + } + chars = append(chars, "\u0085", "\u2028", "\u2029", "\xff") + + for _, format := range []string{"text", "json", "logfmt"} { + for _, char := range chars { + secret := "SECRETHEAD" + char + "SECRETTAIL" + sink, _ := s.capture(Config{ + Format: format, + Redactor: secrets.NewRedactor([]string{"TOKEN=" + secret}), + }, func() { + Infof("m=%s", secret) + Infow("f", "k", secret) + }) + s.NotContains(sink, "SECRETHEAD", "format %s char %q leaked head: %q", format, char, sink) + s.NotContains(sink, "SECRETTAIL", "format %s char %q leaked tail: %q", format, char, sink) + s.Contains(sink, "***", "format %s char %q", format, char) + } + } +} + +func (s *SerializedRecordSuite) TestEnvironmentSecretWithLineBreakIsMasked() { + s.T().Setenv("DEVSY_LOG_TEST_SECRET", "env\nsecret-846297") + + sink, stderr := s.capture(Config{Format: "text"}, func() { + Infof("value=%s", "env\nsecret-846297") + }) + + s.Equal(sink, stderr) + s.NotContains(sink, "secret-846297") + s.Contains(sink, "value=***") +} diff --git a/pkg/log/text_record_encoder.go b/pkg/log/text_record_encoder.go new file mode 100644 index 0000000000..4d060cb656 --- /dev/null +++ b/pkg/log/text_record_encoder.go @@ -0,0 +1,130 @@ +package log + +import ( + "fmt" + + zapbuffer "go.uber.org/zap/buffer" + "go.uber.org/zap/zapcore" +) + +var textRecordPool = zapbuffer.NewPool() + +// oneLineTextEncoder wraps a console encoder so every entry is a single +// physical line: one terminating LF, with line breaks and terminal control +// characters elsewhere in the record escaped as text. +// +// ESC is left untouched on purpose: the color level encoder emits ANSI +// sequences and the serialized buffer cannot distinguish them from payload. +type oneLineTextEncoder struct{ zapcore.Encoder } + +func newOneLineTextEncoder(inner zapcore.Encoder) zapcore.Encoder { + return &oneLineTextEncoder{Encoder: inner} +} + +// Clone wraps a clone of the inner encoder, which holds the With fields. +func (e *oneLineTextEncoder) Clone() zapcore.Encoder { + return newOneLineTextEncoder(e.Encoder.Clone()) +} + +func (e *oneLineTextEncoder) EncodeEntry( + entry zapcore.Entry, + fields []zapcore.Field, +) (*zapbuffer.Buffer, error) { + original, err := e.Encoder.EncodeEntry(entry, fields) + if err != nil { + if original != nil { + original.Free() + } + return nil, err + } + + body := original.Bytes() + hasFinalLF := len(body) > 0 && body[len(body)-1] == '\n' + if hasFinalLF { + body = body[:len(body)-1] + } + if hasFinalLF && !needsRecordEscaping(body) { + return original, nil + } + + // original is freed exactly once, after body has been copied out of it; + // ownership of normalized passes to the caller. + normalized := textRecordPool.Get() + appendEscapedRecordBody(normalized, body) + normalized.AppendByte('\n') + original.Free() + return normalized, nil +} + +func forbiddenASCII(b byte) bool { + return (b < 0x20 && b != '\t' && b != 0x1b) || b == 0x7f +} + +func asciiRecordEscape(b byte) string { + switch b { + case '\n': + return `\n` + case '\r': + return `\r` + case '\v': + return `\v` + case '\f': + return `\f` + case 0: + return `\x00` + } + if forbiddenASCII(b) { + return fmt.Sprintf(`\x%02x`, b) + } + return "" +} + +// unicodeRecordEscape matches U+0085, U+2028 and U+2029 at the start of b. +func unicodeRecordEscape(b []byte) (int, string) { + if len(b) >= 2 && b[0] == 0xc2 && b[1] == 0x85 { + return 2, `\u0085` + } + if len(b) >= 3 && b[0] == 0xe2 && b[1] == 0x80 { + switch b[2] { + case 0xa8: + return 3, `\u2028` + case 0xa9: + return 3, `\u2029` + } + } + return 0, "" +} + +func recordEscapeAt(b []byte) (int, string) { + if value := asciiRecordEscape(b[0]); value != "" { + return 1, value + } + return unicodeRecordEscape(b) +} + +func needsRecordEscaping(body []byte) bool { + for i := range body { + if forbiddenASCII(body[i]) { + return true + } + if body[i] >= 0xc2 { + if width, _ := unicodeRecordEscape(body[i:]); width > 0 { + return true + } + } + } + return false +} + +func appendEscapedRecordBody(dst *zapbuffer.Buffer, body []byte) { + for i := 0; i < len(body); { + width, replacement := recordEscapeAt(body[i:]) + if width > 0 { + dst.AppendString(replacement) + i += width + continue + } + dst.AppendByte(body[i]) + i++ + } +} diff --git a/pkg/log/text_record_encoder_test.go b/pkg/log/text_record_encoder_test.go new file mode 100644 index 0000000000..3b5e85f63d --- /dev/null +++ b/pkg/log/text_record_encoder_test.go @@ -0,0 +1,164 @@ +package log + +import ( + "errors" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/suite" + "go.uber.org/zap" + zapbuffer "go.uber.org/zap/buffer" + "go.uber.org/zap/zapcore" +) + +type TextRecordEncoderSuite struct{ suite.Suite } + +func TestTextRecordEncoderSuite(t *testing.T) { suite.Run(t, new(TextRecordEncoderSuite)) } + +var fixedEntryTime = time.Date(2026, 10, 13, 22, 41, 50, 98_000_000, time.UTC) + +func testEntry(message string) zapcore.Entry { + return zapcore.Entry{Level: zapcore.DebugLevel, Time: fixedEntryTime, Message: message} +} + +func (s *TextRecordEncoderSuite) encode( + enc zapcore.Encoder, + entry zapcore.Entry, + fields ...zapcore.Field, +) string { + buf, err := enc.EncodeEntry(entry, fields) + s.Require().NoError(err) + out := buf.String() + buf.Free() + return out +} + +func (s *TextRecordEncoderSuite) plainEncoder() zapcore.Encoder { + return newOneLineTextEncoder(zapcore.NewConsoleEncoder(plainEncoderConfig())) +} + +// requireOneRecord asserts out is one LF-terminated line and returns it without the LF. +func (s *TextRecordEncoderSuite) requireOneRecord(out string) string { + s.Require().True(strings.HasSuffix(out, "\n"), "record must end in LF: %q", out) + body := strings.TrimSuffix(out, "\n") + s.Equal(1, strings.Count(out, "\n"), "exactly one LF: %q", out) + for _, r := range body { + s.False(r == '\r' || r == '\v' || r == '\f' || r == 0 || r == 0x7f || + r == 0x85 || r == 0x2028 || r == 0x2029, "forbidden rune %U in %q", r, out) + } + return body +} + +func (s *TextRecordEncoderSuite) TestEscapesPhysicalLineBreaks() { + out := s.encode(s.plainEncoder(), testEntry("first\nsecond\rthird\r\nfourth")) + + body := s.requireOneRecord(out) + s.Equal( + "2026-10-13T22:41:50.098Z\tDEBUG\tfirst\\nsecond\\rthird\\r\\nfourth", + body, + ) +} + +func (s *TextRecordEncoderSuite) TestEscapesControlsAndUnicodeSeparators() { + out := s.encode( + s.plainEncoder(), + testEntry("a\x00b\vc\fd\x7fe\x01f\u0085g\u2028h\u2029i héllo \t|"), + ) + + body := s.requireOneRecord(out) + s.Contains( + body, + `a\x00b\vc\fd\x7fe\x01f\u0085g\u2028h\u2029i héllo `+"\t|", + ) +} + +func (s *TextRecordEncoderSuite) TestStackAndFields() { + entry := testEntry("msg") + entry.Stack = "frame1\nframe2" + + out := s.encode(s.plainEncoder(), entry, zap.String("detail", "x\ny")) + + body := s.requireOneRecord(out) + s.Contains(body, `frame1\nframe2`) + s.Contains(body, `"detail": "x\ny"`) +} + +func (s *TextRecordEncoderSuite) TestCloneKeepsWithFieldsIsolated() { + base := s.plainEncoder() + derived := base.Clone() + zap.String("child", "a\nb").AddTo(derived) + + derivedOut := s.requireOneRecord(s.encode(derived, testEntry("msg"))) + baseOut := s.requireOneRecord(s.encode(base, testEntry("msg"))) + + s.Contains(derivedOut, `"child": "a\nb"`) + s.NotContains(baseOut, "child") +} + +func (s *TextRecordEncoderSuite) TestCleanRecordMatchesInnerEncoder() { + inner := zapcore.NewConsoleEncoder(plainEncoderConfig()) + wrapped := newOneLineTextEncoder(zapcore.NewConsoleEncoder(plainEncoderConfig())) + entry := testEntry("ordinary message with unicode héllo") + + s.Equal(s.encode(inner, entry), s.encode(wrapped, entry)) +} + +func (s *TextRecordEncoderSuite) TestColorEncoderPreservesANSIAndEscapesPayload() { + enc := newOneLineTextEncoder(zapcore.NewConsoleEncoder(colorEncoderConfig())) + + out := s.encode(enc, testEntry("one\ntwo")) + + s.Equal(1, strings.Count(out, "\n")) + s.True(strings.HasSuffix(out, `one\ntwo`+"\n")) + s.Contains(out, "\x1b[") + s.Contains(out, "\x1b[0m") +} + +// fakeEncoder returns canned inner output. +type fakeEncoder struct { + zapcore.Encoder + pool zapbuffer.Pool + out *string + err error +} + +func (f *fakeEncoder) EncodeEntry(zapcore.Entry, []zapcore.Field) (*zapbuffer.Buffer, error) { + buf := f.pool.Get() + if f.out != nil { + _, _ = buf.WriteString(*f.out) + } + return buf, f.err +} + +func (s *TextRecordEncoderSuite) TestTerminatorNormalization() { + tests := []struct{ name, inner, want string }{ + {"surplus final LF", "hello\n\n", "hello\\n\n"}, + {"missing final LF", "hello", "hello\n"}, + {"empty", "", "\n"}, + {"only LF", "\n", "\n"}, + {"CRLF terminator", "hello\r\n", "hello\\r\n"}, + } + for _, tt := range tests { + s.Run(tt.name, func() { + inner := tt.inner + enc := newOneLineTextEncoder(&fakeEncoder{pool: zapbuffer.NewPool(), out: &inner}) + s.Equal(tt.want, s.encode(enc, testEntry("ignored"))) + }) + } +} + +func (s *TextRecordEncoderSuite) TestInnerErrorIsReturned() { + boom := errors.New("boom") + enc := newOneLineTextEncoder(&fakeEncoder{pool: zapbuffer.NewPool(), err: boom}) + buf, err := enc.EncodeEntry(testEntry("x"), nil) + s.Nil(buf) + s.ErrorIs(err, boom) +} + +func (s *TextRecordEncoderSuite) TestInvalidAndTruncatedUTF8DoNotPanic() { + out := s.encode(s.plainEncoder(), testEntry("bad\xff\xc2 tail\xe2\x80\n\xe2")) + + s.Equal(1, strings.Count(out, "\n")) + s.Contains(out, "bad\xff\xc2 tail\xe2\x80\\n\xe2") +} diff --git a/pkg/secrets/redact.go b/pkg/secrets/redact.go index e50880f246..f95f7bb7ce 100644 --- a/pkg/secrets/redact.go +++ b/pkg/secrets/redact.go @@ -1,11 +1,14 @@ package secrets import ( + "encoding/json" "fmt" "regexp" "sort" + "strconv" "strings" "sync" + "unicode/utf8" ) const redactMask = "***" @@ -69,6 +72,110 @@ func Combine(redactors ...*Redactor) *Redactor { return NewRedactor(entries) } +// logTextEscaper mirrors the escapes oneLineTextEncoder applies in pkg/log. +// It is duplicated here because pkg/log imports this package. +var logTextEscaper = newLogTextEscaper() + +func newLogTextEscaper() *strings.Replacer { + pairs := []string{ + "\n", `\n`, + "\r", `\r`, + "\v", `\v`, + "\f", `\f`, + "\x00", `\x00`, + "\x7f", `\x7f`, + "\u0085", `\u0085`, + "\u2028", `\u2028`, + "\u2029", `\u2029`, + } + for b := byte(1); b < 0x20; b++ { + switch b { + case '\t', 0x1b, '\n', '\r', '\v', '\f': + continue + } + pairs = append(pairs, string([]byte{b}), fmt.Sprintf(`\x%02x`, b)) + } + return strings.NewReplacer(pairs...) +} + +// WithLogEscapes returns a redactor that also masks every value of r in the +// escaped forms the log encoders serialize it in (strconv, JSON, zap JSON and +// text-record escapes). Redaction runs on serialized records, so raw values +// alone would miss secrets containing quotes or control characters. A nil r +// yields a redactor with only format-based masking. +func WithLogEscapes(r *Redactor) *Redactor { + if r == nil { + return NewRedactor(nil) + } + entries := make([]string, 0, len(r.values)*5) + for i, original := range r.values { + for j, variant := range logEscapeVariants(original) { + entries = append(entries, fmt.Sprintf("DEVSY_LOG_VALUE_%d_%d=%s", i, j, variant)) + } + } + return NewRedactor(entries) +} + +func logEscapeVariants(value string) []string { + quoted := strconv.Quote(value) + encoded, _ := json.Marshal(value) // cannot fail for a string + zapJSON := zapJSONEscape(value) + return uniqueNonemptyStrings([]string{ + value, + quoted[1 : len(quoted)-1], + logTextEscaper.Replace(value), + string(encoded[1 : len(encoded)-1]), + zapJSON, + // Text fields are zap JSON, then escaped again as a text record. + logTextEscaper.Replace(zapJSON), + }) +} + +// zapJSONEscape reproduces zap's JSON string escaping, which differs from +// encoding/json for \b, \f and U+2028/U+2029. +func zapJSONEscape(value string) string { + var b strings.Builder + b.Grow(len(value)) + for i := 0; i < len(value); { + r, width := utf8.DecodeRuneInString(value[i:]) + switch { + case r == utf8.RuneError && width == 1: + b.WriteString(`\ufffd`) + case r == '\\' || r == '"': + b.WriteByte('\\') + b.WriteRune(r) + case r == '\n': + b.WriteString(`\n`) + case r == '\r': + b.WriteString(`\r`) + case r == '\t': + b.WriteString(`\t`) + case r < 0x20: + fmt.Fprintf(&b, `\u%04x`, r) + default: + b.WriteString(value[i : i+width]) + } + i += width + } + return b.String() +} + +func uniqueNonemptyStrings(values []string) []string { + seen := make(map[string]struct{}, len(values)) + result := make([]string, 0, len(values)) + for _, value := range values { + if value == "" { + continue + } + if _, ok := seen[value]; ok { + continue + } + seen[value] = struct{}{} + result = append(result, value) + } + return result +} + // NewEnvironmentRedactor protects values from environment variables that are // conventionally credential-bearing, while leaving ordinary environment // values available in diagnostics. diff --git a/pkg/secrets/redact_test.go b/pkg/secrets/redact_test.go index f2d5b2d568..76f4a0de23 100644 --- a/pkg/secrets/redact_test.go +++ b/pkg/secrets/redact_test.go @@ -1,10 +1,13 @@ package secrets_test import ( + "encoding/json" + "strconv" "strings" "testing" "github.com/devsy-org/devsy/pkg/secrets" + "github.com/stretchr/testify/suite" ) func TestRedactor_MasksValues(t *testing.T) { @@ -147,3 +150,81 @@ func TestStreamingRedactor_MasksCredentialsSplitInsideFormatPrefix(t *testing.T) }) } } + +type LogEscapeSuite struct{ suite.Suite } + +func TestLogEscapeSuite(t *testing.T) { suite.Run(t, new(LogEscapeSuite)) } + +func (s *LogEscapeSuite) TestNilAndEmptyKeepFormatMasking() { + for _, base := range []*secrets.Redactor{nil, secrets.NewRedactor(nil)} { + r := secrets.WithLogEscapes(base) + s.Equal("plain", r.Redact("plain")) + s.Equal("https://***@host", r.Redact("https://user:pw@host")) + } +} + +func (s *LogEscapeSuite) TestMasksRawQuotedJSONAndTextForms() { + const value = "a\nb\r\"c\"\\d\x00\x0b\x0c\u2028" + r := secrets.WithLogEscapes(secrets.NewRedactor([]string{"TOKEN=" + value})) + + quoted := strings.Trim(strconv.Quote(value), `"`) + encoded, err := json.Marshal(value) + s.Require().NoError(err) + jsonBody := strings.Trim(string(encoded), `"`) + zapBody := `a\nb\r\"c\"\\d\u0000\u000b\u000c` + "\u2028" + textBody := `a\nb\r"c"\d\x00\v\f\u2028` + textFieldBody := `a\nb\r\"c\"\\d\u0000\u000b\u000c\u2028` + + for name, form := range map[string]string{ + "raw": value, "strconv": quoted, "json": jsonBody, "zapJSON": zapBody, "text": textBody, + "textField": textFieldBody, + } { + s.Equal("[***]", r.Redact("["+form+"]"), name) + } +} + +func (s *LogEscapeSuite) TestDoesNotMutateInputAndKeepsRawValues() { + original := secrets.NewRedactor([]string{"TOKEN=a\nb"}) + derived := secrets.WithLogEscapes(original) + + s.Equal("***", derived.Redact("a\nb")) + s.Equal(`a\nb`, original.Redact(`a\nb`), "input redactor must not learn escaped forms") + s.Equal("***", original.Redact("a\nb")) +} + +func (s *LogEscapeSuite) TestSafeValueHasSingleVariantAndOrdinaryTextUntouched() { + r := secrets.WithLogEscapes(secrets.NewRedactor([]string{"TOKEN=alnum42"})) + + s.Equal("*** and other-value", r.Redact("alnum42 and other-value")) +} + +func (s *LogEscapeSuite) TestValueWithEqualsSurvives() { + r := secrets.WithLogEscapes(secrets.NewRedactor([]string{"CONN=user=admin;pw=a\nb"})) + + s.Equal("[***] [***]", r.Redact("[user=admin;pw=a\nb] [user=admin;pw=a\\nb]")) +} + +func (s *LogEscapeSuite) TestLogEscapeVariantsPreserveLongestFirst() { + r := secrets.WithLogEscapes(secrets.NewRedactor([]string{"A=sec", "B=sec\nret"})) + + input := "x sec\nret " + `sec\nret` + " sec y" + s.Equal("x *** *** *** y", r.Redact(input)) +} + +func (s *LogEscapeSuite) TestSplitWritesNeverLeakSerializedVariants() { + r := secrets.WithLogEscapes(secrets.NewRedactor([]string{"A=sec", "B=sec\nret\"x"})) + for _, input := range []string{ + "ordinary " + `sec\nret\"x` + " tail", + "ordinary sec\nret\"x tail", + "ordinary " + `sec\nret"x` + " sec", + } { + want := r.Redact(input) + s.NotContains(want, "ret") + for split := 0; split <= len(input); split++ { + stream := secrets.NewStreamingRedactor(r) + got := stream.RedactChunk(input[:split]) + stream.RedactChunk(input[split:]) + + stream.Flush() + s.Equal(want, got, "input %q split %d", input, split) + } + } +} diff --git a/pkg/tunnel/pipebridge.go b/pkg/tunnel/pipebridge.go index dff7df33fd..82e99ceb02 100644 --- a/pkg/tunnel/pipebridge.go +++ b/pkg/tunnel/pipebridge.go @@ -103,11 +103,11 @@ func (pb *PipeBridge) RunPair( // side to be abandoned after joinTimeout). tunnelErr, handlerErr := iojoin.Join(tunnelSide, handlerSide, joinTimeout, stop) log.Debugf( - "pipe bridge completed: tunnel_err=%v handler_err=%v parent_err=%v pair_err=%v", - tunnelErr, - handlerErr, - ctx.Err(), - pairCtx.Err(), + "pipe bridge completed: tunnel_err=%s handler_err=%s parent_err=%s pair_err=%s", + log.ErrorForLog(tunnelErr), + log.ErrorForLog(handlerErr), + log.ErrorForLog(ctx.Err()), + log.ErrorForLog(pairCtx.Err()), ) return ClassifyTunnelErrors(tunnelErr, handlerErr) } diff --git a/pkg/tunnel/pipebridge_test.go b/pkg/tunnel/pipebridge_test.go index f1366ff2ad..d7b0ff9fa5 100644 --- a/pkg/tunnel/pipebridge_test.go +++ b/pkg/tunnel/pipebridge_test.go @@ -3,13 +3,18 @@ package tunnel import ( "context" "errors" + "fmt" "io" "os" "sync/atomic" "testing" "time" + "github.com/devsy-org/devsy/pkg/log" "github.com/devsy-org/devsy/pkg/util/goleaktest" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/zap/zapcore" ) func TestMain(m *testing.M) { @@ -403,3 +408,53 @@ func TestRunPairStopsBothSidesWhenParentContextCancelledBeforeEitherReturns(t *t t.Fatal("RunPair hung after parent ctx cancellation with both sides blocked in Read") } } + +// summarizedFailure stands in for sshtunnel's agentCommandFailure, which +// pkg/tunnel cannot import. +type summarizedFailure struct { + cause error + summary string + tail string +} + +func (e *summarizedFailure) Error() string { return e.summary + "\n" + e.tail } +func (e *summarizedFailure) Unwrap() error { return e.cause } +func (e *summarizedFailure) LogSummary() string { return e.summary } + +// Not parallel: it replaces the package-global logger. +func TestRunPairCompletionDoesNotReplayCapturedJSON(t *testing.T) { + logs := log.InitTestObserved(t, zapcore.DebugLevel) + + pb, err := NewPipeBridge() + require.NoError(t, err) + defer pb.Close() + + cause := errors.New("exit status 1") + const ( + summary = "run agent command failed: exit status 1" + line1 = `{"level":"debug","ts":"2026-10-13T22:41:48.610Z","msg":"credentials server started"}` + line2 = `{"kind":"error","outcome":"error","code":"UNKNOWN","message":"command failed"}` + ) + failure := &summarizedFailure{cause: cause, summary: summary, tail: line1 + "\n" + line2} + + tunnelFn := func(_ context.Context, _ *os.File, _ *os.File) error { + return fmt.Errorf("outer: %w", failure) + } + handler := func(_ context.Context, stdout *os.File, _ *os.File) error { + _, err := stdout.Read(make([]byte, 1)) + return err + } + + runErr := pb.RunPair(context.Background(), tunnelFn, handler) + require.Error(t, runErr) + require.ErrorIs(t, runErr, cause) + assert.Contains(t, runErr.Error(), line1, "returned error keeps the full text") + + completed := logs.FilterMessageSnippet("pipe bridge completed").All() + require.Len(t, completed, 1) + entry := completed[0] + assert.Contains(t, entry.Message, "tunnel_err="+summary+" ") + assert.NotContains(t, entry.Message, `{"level"`) + assert.NotContains(t, entry.Message, "\n") + assert.Contains(t, entry.Message, "handler_err=EOF") +}