From a40415c38bf785db5ce7fecad83a5c4fcc376a5b Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 13 Oct 2026 17:35:18 -0600 Subject: [PATCH 1/3] fix(log): serialize each text log event as one physical record --- pkg/log/logfmt.go | 7 +- pkg/log/logfmt_test.go | 62 +++++++ pkg/log/logger.go | 9 +- pkg/log/serialized_record_test.go | 244 ++++++++++++++++++++++++++++ pkg/log/text_record_encoder.go | 139 ++++++++++++++++ pkg/log/text_record_encoder_test.go | 216 ++++++++++++++++++++++++ pkg/secrets/redact.go | 109 +++++++++++++ pkg/secrets/redact_test.go | 81 +++++++++ 8 files changed, 863 insertions(+), 4 deletions(-) create mode 100644 pkg/log/logfmt_test.go create mode 100644 pkg/log/serialized_record_test.go create mode 100644 pkg/log/text_record_encoder.go create mode 100644 pkg/log/text_record_encoder_test.go 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..91ace69ecd 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -37,7 +37,10 @@ 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())) + // Redaction runs on the serialized record, so it must also know the escaped + // forms the encoders produce for values containing quotes or control bytes. + cfg.Redactor = secrets.WithLogEscapes(combined) level := resolveLevel(cfg) encoder := resolveEncoder(cfg.Format) stderrWriter := zapcore.Lock(&redactingWriter{next: os.Stderr, redactor: cfg.Redactor}) @@ -191,9 +194,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..743e69e178 --- /dev/null +++ b/pkg/log/serialized_record_test.go @@ -0,0 +1,244 @@ +package log + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "os" + "strconv" + "strings" + "testing" + + "github.com/devsy-org/devsy/pkg/secrets" + "github.com/stretchr/testify/suite" + "go.uber.org/zap" +) + +// SerializedRecordSuite exercises real Init + AddSink serialization. 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 runs emit against a freshly initialised logger and returns the bytes +// written to both the additive sink and 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) +} + +// TestTextMultilineBridgeCompletionIsOnePhysicalRecord reproduces the CI +// symptom: one logical event whose error text contains raw remote log lines +// must not produce unprefixed continuation lines. +func (s *SerializedRecordSuite) TestTextMultilineBridgeCompletionIsOnePhysicalRecord() { + 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.True(strings.HasSuffix(sink, "\n")) + s.NotContains(sink, "\r") + s.Equal(2, strings.Count(sink, "\n")) + 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) TestTextWithFieldsAndChildLoggerShareFraming() { + sink, _ := s.capture(Config{Format: "text"}, func() { + child := Underlying().With(zap.String("scope", "a\nb")).Sugar() + child.Infow("child event", "k", "v\rw") + Infof("parent") + }) + + lines := strings.Split(strings.TrimSuffix(sink, "\n"), "\n") + s.Require().Len(lines, 2, "%q", sink) + s.Contains(lines[0], `"scope": "a\nb"`) + s.Contains(lines[0], `"k": "v\rw"`) + s.Contains(lines[1], "parent") +} + +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" + +// TestTextRecordRedactionAcrossFormats is the mandatory post-serialization +// redaction gate: the encoders rewrite control characters before the writers +// redact, so the escaped forms must be masked too. +func (s *SerializedRecordSuite) TestTextRecordRedactionAcrossFormats() { + 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) + } +} + +// TestRedactionCoversEverySerializedByte checks each ASCII byte and the +// Unicode separators embedded in a secret across all formats and both field +// and message positions, pinning the observed encodings. +func (s *SerializedRecordSuite) TestRedactionCoversEverySerializedByte() { + 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, "***", fmt.Sprintf("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=***") +} + +func (s *SerializedRecordSuite) TestOrdinaryValuesAreNotRedactedByEscapeVariants() { + sink, _ := s.capture(Config{ + Format: "text", + Redactor: secrets.NewRedactor([]string{"TOKEN=alnumsecret42"}), + }, func() { + Infof("plain value only: quote=%q", "ordinary") + }) + + s.Contains(sink, `plain value only: quote="ordinary"`) + s.NotContains(sink, "***") +} diff --git a/pkg/log/text_record_encoder.go b/pkg/log/text_record_encoder.go new file mode 100644 index 0000000000..53c6ed0ab6 --- /dev/null +++ b/pkg/log/text_record_encoder.go @@ -0,0 +1,139 @@ +package log + +import ( + "errors" + "fmt" + + zapbuffer "go.uber.org/zap/buffer" + "go.uber.org/zap/zapcore" +) + +var textRecordPool = zapbuffer.NewPool() + +// oneLineTextEncoder wraps a console encoder so every encoded entry is exactly +// one physical record: a single terminating LF, with line breaks and terminal +// control characters inside the record escaped as text. Escaping covers the +// whole serialized record (message, fields, stack) because it is applied to +// the inner encoder's output. +// +// ESC is deliberately left untouched: the color level encoder emits ANSI +// sequences and the serialized buffer does not distinguish formatter bytes from +// payload bytes. Payload ANSI injection is therefore out of scope here. +type oneLineTextEncoder struct{ zapcore.Encoder } + +func newOneLineTextEncoder(inner zapcore.Encoder) zapcore.Encoder { + return &oneLineTextEncoder{Encoder: inner} +} + +// Clone must wrap a clone of the inner encoder because zap calls it in +// Core.With and the inner encoder holds the accumulated 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 + } + if original == nil { + return nil, errors.New("text encoder returned nil buffer") + } + + body, hasFinalLF := splitEncodedRecord(original.Bytes()) + if hasFinalLF && !needsRecordEscaping(body) { + return original, nil + } + + normalized := textRecordPool.Get() + appendEscapedRecordBody(normalized, body) + normalized.AppendByte('\n') + original.Free() + return normalized, nil +} + +// splitEncodedRecord separates the single final LF terminator from the body. +func splitEncodedRecord(b []byte) ([]byte, bool) { + if len(b) > 0 && b[len(b)-1] == '\n' { + return b[:len(b)-1], true + } + return b, false +} + +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 "" +} + +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..94f3da7061 --- /dev/null +++ b/pkg/log/text_record_encoder_test.go @@ -0,0 +1,216 @@ +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 the acceptance invariant for a text record. +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) TestPreservesLiteralBackslashN() { + literal := s.requireOneRecord(s.encode(s.plainEncoder(), testEntry(`literal \n`))) + actual := s.requireOneRecord(s.encode(s.plainEncoder(), testEntry("literal \n"))) + + s.True(strings.HasSuffix(literal, `literal \n`)) + s.True(strings.HasSuffix(actual, `literal \n`)) +} + +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") + _, wrapped := derived.(*oneLineTextEncoder) + s.True(wrapped, "Clone must keep the wrapper") +} + +func (s *TextRecordEncoderSuite) TestFastPathMatchesInnerEncoder() { + 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)) + + run := func(enc zapcore.Encoder) float64 { + return testing.AllocsPerRun(50, func() { + buf, err := enc.EncodeEntry(entry, nil) + s.Require().NoError(err) + buf.Free() + }) + } + s.LessOrEqual(run(wrapped), run(inner), "fast path must not allocate beyond the inner encoder") +} + +func (s *TextRecordEncoderSuite) TestColorEncoderPreservesANSIAndFramesRecord() { + 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[", "formatter color sequence must survive escaping") + s.Contains(out, "\x1b[0m") +} + +func (s *TextRecordEncoderSuite) TestNoColorSelectsPlainWrappedEncoder() { + s.T().Setenv("NO_COLOR", "1") + + enc := resolveEncoder("text") + out := s.encode(enc, testEntry("one\ntwo")) + + _, wrapped := enc.(*oneLineTextEncoder) + s.True(wrapped) + s.NotContains(out, "\x1b") + s.requireOneRecord(out) +} + +func (s *TextRecordEncoderSuite) TestResolveEncoderWrapsTextOnly() { + s.T().Setenv("NO_COLOR", "") + for _, format := range []string{"text", "", "other"} { + _, wrapped := resolveEncoder(format).(*oneLineTextEncoder) + s.True(wrapped, "format %q", format) + } + for _, format := range []string{"json", "logfmt"} { + _, wrapped := resolveEncoder(format).(*oneLineTextEncoder) + s.False(wrapped, "format %q", format) + } +} + +// fakeEncoder returns canned inner output to exercise terminator handling. +type fakeEncoder struct { + zapcore.Encoder + pool zapbuffer.Pool + out *string + err error + nils bool +} + +func (f *fakeEncoder) EncodeEntry(zapcore.Entry, []zapcore.Field) (*zapbuffer.Buffer, error) { + if f.nils { + return nil, nil + } + 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) TestInnerErrorAndNilBuffer() { + 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) + + enc = newOneLineTextEncoder(&fakeEncoder{nils: true}) + buf, err = enc.EncodeEntry(testEntry("x"), nil) + s.Nil(buf) + s.Error(err) +} + +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..428f357062 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,112 @@ func Combine(redactors ...*Redactor) *Redactor { return NewRedactor(entries) } +// logTextEscaper mirrors the record-framing escapes the text log encoder +// applies to a serialized record body. It lives here, rather than being +// imported, because pkg/log depends on 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 masks every value known by r in the +// forms the application log encoders can serialize it: the raw value, its +// strconv-quoted form (logfmt), its JSON-escaped forms (json encoder and text +// fields) and its text-record-escaped forms. r is not modified and the raw +// values stay registered. 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) + result := []string{value, quoted[1 : len(quoted)-1], logTextEscaper.Replace(value)} + if encoded, err := json.Marshal(value); err == nil { + result = append(result, string(encoded[1:len(encoded)-1])) + } + zapJSON := zapJSONEscape(value) + // The text encoder renders fields as zap JSON and then escapes the record, + // so U+0085/U+2028/U+2029/DEL inside that JSON are rewritten again. + return uniqueNonemptyStrings(append(result, zapJSON, logTextEscaper.Replace(zapJSON))) +} + +// zapJSONEscape reproduces how zap's JSON encoder escapes a string value. It +// differs from encoding/json for \b, \f and U+2028/U+2029, so it is a +// distinct serialized form of the same secret. +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() +} + +// uniqueNonemptyStrings drops empty values and duplicates, keeping first-seen +// order. +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) + } + } +} From 6014cc8c53bf5774263345cfbe4aa4774626ecb9 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 13 Oct 2026 17:35:19 -0600 Subject: [PATCH 2/3] fix(tunnel): summarize SSH command failures in bridge debug log --- pkg/devcontainer/sshtunnel/command_error.go | 37 +++++++ .../sshtunnel/command_error_test.go | 98 +++++++++++++++++ pkg/devcontainer/sshtunnel/sshtunnel.go | 8 +- pkg/log/error_value.go | 52 +++++++++ pkg/log/error_value_test.go | 104 ++++++++++++++++++ pkg/tunnel/pipebridge.go | 10 +- pkg/tunnel/pipebridge_test.go | 67 +++++++++++ 7 files changed, 367 insertions(+), 9 deletions(-) create mode 100644 pkg/devcontainer/sshtunnel/command_error.go create mode 100644 pkg/devcontainer/sshtunnel/command_error_test.go create mode 100644 pkg/log/error_value.go create mode 100644 pkg/log/error_value_test.go diff --git a/pkg/devcontainer/sshtunnel/command_error.go b/pkg/devcontainer/sshtunnel/command_error.go new file mode 100644 index 0000000000..e1574bbe21 --- /dev/null +++ b/pkg/devcontainer/sshtunnel/command_error.go @@ -0,0 +1,37 @@ +package sshtunnel + +import "github.com/devsy-org/devsy/pkg/log" + +var _ log.LogSummaryProvider = (*agentCommandFailure)(nil) + +const agentCommandFailedPrefix = "run agent command failed: " + +// agentCommandFailure separates the cause of an agent command failure from the +// redacted diagnostic tail captured from its stderr. Error preserves the legacy +// "cause\ndiagnostics" text for CLI and status consumers; LogSummary omits the +// tail so log sites do not replay records the streamer already emitted. +type agentCommandFailure struct { + // cause is always non-nil: the error returned by devssh.RunSession. + cause error + // diagnostics is redacted and bounded by the JSONLogStreamer. + diagnostics string +} + +func (e *agentCommandFailure) Error() string { + base := agentCommandFailedPrefix + e.cause.Error() + if e.diagnostics == "" { + return base + } + return base + "\n" + e.diagnostics +} + +func (e *agentCommandFailure) Unwrap() error { return e.cause } + +// LogSummary implements log.LogSummaryProvider without the diagnostic tail. +func (e *agentCommandFailure) LogSummary() string { + return agentCommandFailedPrefix + e.cause.Error() +} + +// Diagnostics returns the redacted, bounded stderr tail. It is exposed by +// behavior so callers can errors.As into an interface; never write it to a log. +func (e *agentCommandFailure) Diagnostics() string { return e.diagnostics } diff --git a/pkg/devcontainer/sshtunnel/command_error_test.go b/pkg/devcontainer/sshtunnel/command_error_test.go new file mode 100644 index 0000000000..8b16d66187 --- /dev/null +++ b/pkg/devcontainer/sshtunnel/command_error_test.go @@ -0,0 +1,98 @@ +package sshtunnel + +import ( + "context" + "errors" + "fmt" + "strings" + "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 diagnosticsProvider interface { + Diagnostics() string +} + +type AgentCommandFailureTestSuite struct { + suite.Suite +} + +func TestAgentCommandFailureTestSuite(t *testing.T) { + suite.Run(t, new(AgentCommandFailureTestSuite)) +} + +func (s *AgentCommandFailureTestSuite) TestAgentCommandFailureCompatibility() { + 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()) + s.NotContains(withDiagnostics.LogSummary(), "\n") + s.NotContains(withDiagnostics.LogSummary(), `{"level"`) + s.Equal(failureDiagnostics, withDiagnostics.Diagnostics()) + + empty := &agentCommandFailure{cause: cause} + s.Equal(fmt.Errorf("run agent command failed: %w", cause).Error(), empty.Error()) + s.Equal("run agent command failed: exit status 1", empty.Error()) + s.Equal(empty.Error(), empty.LogSummary()) + s.Empty(empty.Diagnostics()) +} + +func (s *AgentCommandFailureTestSuite) TestWrappedMatching() { + 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) + + var provider log.LogSummaryProvider + s.Require().ErrorAs(wrapped, &provider) + s.Equal(failure.LogSummary(), provider.LogSummary()) + + var diag diagnosticsProvider + s.Require().ErrorAs(wrapped, &diag) + s.Equal(failureDiagnostics, diag.Diagnostics()) + + 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} + got := log.ErrorForLog(failure) + s.Equal("run agent command failed: sentinel cause", got) + s.False(strings.Contains(got, "credentials server started")) +} 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..469a5cd6e5 --- /dev/null +++ b/pkg/log/error_value.go @@ -0,0 +1,52 @@ +package log + +import ( + "errors" + "strings" + "unicode/utf8" +) + +const ( + // errorSummaryMaxRunes bounds an operational log summary, including the + // truncation marker. + errorSummaryMaxRunes = 2048 + truncatedErrorSummarySuffix = "…[truncated]" +) + +// LogSummaryProvider is implemented by errors whose Error text carries data +// that must not be replayed into an application log record (for example a +// captured diagnostic tail that was already emitted separately). +type LogSummaryProvider interface { + LogSummary() string +} + +// ErrorForLog returns a bounded, log-safe description of err. The first +// LogSummaryProvider found while unwrapping err supplies the summary; +// otherwise err.Error() is used. The result is capped at 2,048 runes +// including the truncation marker. It never mutates err and never trims +// whitespace: embedded line breaks are escaped later by the text encoder. +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 + } + available := errorSummaryMaxRunes - utf8.RuneCountInString(truncatedErrorSummarySuffix) + cutoff := 0 + for range available { + _, 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..9badd1a899 --- /dev/null +++ b/pkg/log/error_value_test.go @@ -0,0 +1,104 @@ +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) // 3000 runes, mixed 2/3/4-byte + 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") + + // Normalization happens before the rune count; each invalid run collapses + // to a single replacement rune, so interleave valid runes to exceed the cap. + long := truncateErrorSummary(strings.Repeat("a\xff", 3000)) + s.True(utf8.ValidString(long)) + s.Equal(errorSummaryMaxRunes, utf8.RuneCountInString(long)) +} 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..7aed3ec404 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,65 @@ func TestRunPairStopsBothSidesWhenParentContextCancelledBeforeEitherReturns(t *t t.Fatal("RunPair hung after parent ctx cancellation with both sides blocked in Read") } } + +// summarizedFailure mimics a typed error whose Error text carries a captured +// diagnostic tail that log sites must not replay. It is defined locally because +// pkg/tunnel cannot import pkg/devcontainer/sshtunnel. +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 } + +var _ log.LogSummaryProvider = (*summarizedFailure)(nil) + +// Serial on purpose: it replaces the package-global logger, so it must not run +// in parallel with other tests that log. +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) + } + // Reading the closed bridge side yields EOF, so ClassifyTunnelErrors reports + // the tunnel error. + 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) + var provider log.LogSummaryProvider + require.ErrorAs(t, runErr, &provider) + assert.Equal(t, failure.Error(), errors.Unwrap(errors.Unwrap(runErr)).Error(), + "returned error keeps the full original text") + + completed := logs.FilterMessageSnippet("pipe bridge completed").All() + require.Len(t, completed, 1) + entry := completed[0] + assert.Equal(t, zapcore.DebugLevel, entry.Level) + assert.Contains(t, entry.Message, "tunnel_err="+summary+" ") + assert.NotContains(t, entry.Message, `{"level"`) + assert.NotContains(t, entry.Message, `{"kind"`) + assert.NotContains(t, entry.Message, "credentials server started") + assert.NotContains(t, entry.Message, "\n") + assert.Contains(t, entry.Message, "handler_err=EOF") +} From 0c45e070a143ffec802d02faf474c9d4fe7d39a2 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 13 Oct 2026 17:43:05 -0600 Subject: [PATCH 3/3] refactor(log): trim comments and use ASCII ellipsis --- pkg/devcontainer/sshtunnel/command_error.go | 21 ++---- .../sshtunnel/command_error_test.go | 26 +------- pkg/log/error_value.go | 20 +++--- pkg/log/error_value_test.go | 5 +- pkg/log/logger.go | 3 +- pkg/log/serialized_record_test.go | 54 +++------------- pkg/log/text_record_encoder.go | 37 ++++------- pkg/log/text_record_encoder_test.go | 64 ++----------------- pkg/secrets/redact.go | 40 ++++++------ pkg/tunnel/pipebridge_test.go | 20 ++---- 10 files changed, 71 insertions(+), 219 deletions(-) diff --git a/pkg/devcontainer/sshtunnel/command_error.go b/pkg/devcontainer/sshtunnel/command_error.go index e1574bbe21..277cb340c5 100644 --- a/pkg/devcontainer/sshtunnel/command_error.go +++ b/pkg/devcontainer/sshtunnel/command_error.go @@ -6,32 +6,23 @@ var _ log.LogSummaryProvider = (*agentCommandFailure)(nil) const agentCommandFailedPrefix = "run agent command failed: " -// agentCommandFailure separates the cause of an agent command failure from the -// redacted diagnostic tail captured from its stderr. Error preserves the legacy -// "cause\ndiagnostics" text for CLI and status consumers; LogSummary omits the -// tail so log sites do not replay records the streamer already emitted. +// 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 is always non-nil: the error returned by devssh.RunSession. - cause error - // diagnostics is redacted and bounded by the JSONLogStreamer. + cause error diagnostics string } func (e *agentCommandFailure) Error() string { - base := agentCommandFailedPrefix + e.cause.Error() if e.diagnostics == "" { - return base + return e.LogSummary() } - return base + "\n" + e.diagnostics + return e.LogSummary() + "\n" + e.diagnostics } func (e *agentCommandFailure) Unwrap() error { return e.cause } -// LogSummary implements log.LogSummaryProvider without the diagnostic tail. func (e *agentCommandFailure) LogSummary() string { return agentCommandFailedPrefix + e.cause.Error() } - -// Diagnostics returns the redacted, bounded stderr tail. It is exposed by -// behavior so callers can errors.As into an interface; never write it to a log. -func (e *agentCommandFailure) Diagnostics() string { return e.diagnostics } diff --git a/pkg/devcontainer/sshtunnel/command_error_test.go b/pkg/devcontainer/sshtunnel/command_error_test.go index 8b16d66187..dcc71e86e6 100644 --- a/pkg/devcontainer/sshtunnel/command_error_test.go +++ b/pkg/devcontainer/sshtunnel/command_error_test.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "strings" "testing" "github.com/devsy-org/devsy/pkg/log" @@ -17,10 +16,6 @@ const failureDiagnostics = `{"level":"debug","ts":"2026-10-13T22:41:48.610Z","ms var errSentinelCause = errors.New("sentinel cause") -type diagnosticsProvider interface { - Diagnostics() string -} - type AgentCommandFailureTestSuite struct { suite.Suite } @@ -29,7 +24,7 @@ func TestAgentCommandFailureTestSuite(t *testing.T) { suite.Run(t, new(AgentCommandFailureTestSuite)) } -func (s *AgentCommandFailureTestSuite) TestAgentCommandFailureCompatibility() { +func (s *AgentCommandFailureTestSuite) TestErrorKeepsDiagnosticsAndLogSummaryOmitsThem() { cause := errors.New("exit status 1") withDiagnostics := &agentCommandFailure{cause: cause, diagnostics: failureDiagnostics} @@ -39,18 +34,13 @@ func (s *AgentCommandFailureTestSuite) TestAgentCommandFailureCompatibility() { ) s.Same(cause, withDiagnostics.Unwrap()) s.Equal("run agent command failed: exit status 1", withDiagnostics.LogSummary()) - s.NotContains(withDiagnostics.LogSummary(), "\n") - s.NotContains(withDiagnostics.LogSummary(), `{"level"`) - s.Equal(failureDiagnostics, withDiagnostics.Diagnostics()) empty := &agentCommandFailure{cause: cause} - s.Equal(fmt.Errorf("run agent command failed: %w", cause).Error(), empty.Error()) s.Equal("run agent command failed: exit status 1", empty.Error()) s.Equal(empty.Error(), empty.LogSummary()) - s.Empty(empty.Diagnostics()) } -func (s *AgentCommandFailureTestSuite) TestWrappedMatching() { +func (s *AgentCommandFailureTestSuite) TestWrappedFailureMatchesCauseAndType() { exitErr := &devssh.ExitError{ExitCode: 1} for _, tc := range []struct { name string @@ -72,14 +62,6 @@ func (s *AgentCommandFailureTestSuite) TestWrappedMatching() { s.Require().ErrorAs(wrapped, &asFailure) s.Same(failure, asFailure) - var provider log.LogSummaryProvider - s.Require().ErrorAs(wrapped, &provider) - s.Equal(failure.LogSummary(), provider.LogSummary()) - - var diag diagnosticsProvider - s.Require().ErrorAs(wrapped, &diag) - s.Equal(failureDiagnostics, diag.Diagnostics()) - s.Equal(failure.LogSummary(), log.ErrorForLog(wrapped)) }) } @@ -92,7 +74,5 @@ func (s *AgentCommandFailureTestSuite) TestWrappedMatching() { func (s *AgentCommandFailureTestSuite) TestErrorForLogOmitsDiagnostics() { failure := &agentCommandFailure{cause: errSentinelCause, diagnostics: failureDiagnostics} - got := log.ErrorForLog(failure) - s.Equal("run agent command failed: sentinel cause", got) - s.False(strings.Contains(got, "credentials server started")) + s.Equal("run agent command failed: sentinel cause", log.ErrorForLog(failure)) } diff --git a/pkg/log/error_value.go b/pkg/log/error_value.go index 469a5cd6e5..fc1a7cf2d7 100644 --- a/pkg/log/error_value.go +++ b/pkg/log/error_value.go @@ -7,24 +7,20 @@ import ( ) const ( - // errorSummaryMaxRunes bounds an operational log summary, including the - // truncation marker. errorSummaryMaxRunes = 2048 - truncatedErrorSummarySuffix = "…[truncated]" + truncatedErrorSummarySuffix = "...[truncated]" ) // LogSummaryProvider is implemented by errors whose Error text carries data -// that must not be replayed into an application log record (for example a -// captured diagnostic tail that was already emitted separately). +// 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 bounded, log-safe description of err. The first -// LogSummaryProvider found while unwrapping err supplies the summary; -// otherwise err.Error() is used. The result is capped at 2,048 runes -// including the truncation marker. It never mutates err and never trims -// whitespace: embedded line breaks are escaped later by the text encoder. +// 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 "" @@ -42,9 +38,9 @@ func truncateErrorSummary(summary string) string { if utf8.RuneCountInString(summary) <= errorSummaryMaxRunes { return summary } - available := errorSummaryMaxRunes - utf8.RuneCountInString(truncatedErrorSummarySuffix) + keep := errorSummaryMaxRunes - utf8.RuneCountInString(truncatedErrorSummarySuffix) cutoff := 0 - for range available { + for range keep { _, width := utf8.DecodeRuneInString(summary[cutoff:]) cutoff += width } diff --git a/pkg/log/error_value_test.go b/pkg/log/error_value_test.go index 9badd1a899..34286fee71 100644 --- a/pkg/log/error_value_test.go +++ b/pkg/log/error_value_test.go @@ -79,7 +79,7 @@ func (s *ErrorForLogTestSuite) TestTruncationBoundaries() { } func (s *ErrorForLogTestSuite) TestTruncationIsRuneSafe() { - input := strings.Repeat("é世🙂", 1000) // 3000 runes, mixed 2/3/4-byte + input := strings.Repeat("é世🙂", 1000) // mixed 2/3/4-byte runes got := truncateErrorSummary(input) s.True(utf8.ValidString(got)) s.Equal(errorSummaryMaxRunes, utf8.RuneCountInString(got)) @@ -96,8 +96,7 @@ func (s *ErrorForLogTestSuite) TestInvalidUTF8IsNormalizedOnlyInSummary() { s.Equal("bad\uFFFDbytes", got) s.Equal(raw, err.Error(), "original error text is not mutated") - // Normalization happens before the rune count; each invalid run collapses - // to a single replacement rune, so interleave valid runes to exceed the cap. + // 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/logger.go b/pkg/log/logger.go index 91ace69ecd..7175469f47 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -38,8 +38,7 @@ type Config struct { // Init configures the global logger. Called once in root command PersistentPreRunE. func Init(cfg Config) { combined := secrets.Combine(cfg.Redactor, secrets.NewEnvironmentRedactor(os.Environ())) - // Redaction runs on the serialized record, so it must also know the escaped - // forms the encoders produce for values containing quotes or control bytes. + // Writers redact serialized records, so secrets must match in escaped form too. cfg.Redactor = secrets.WithLogEscapes(combined) level := resolveLevel(cfg) encoder := resolveEncoder(cfg.Format) diff --git a/pkg/log/serialized_record_test.go b/pkg/log/serialized_record_test.go index 743e69e178..1ca20cba57 100644 --- a/pkg/log/serialized_record_test.go +++ b/pkg/log/serialized_record_test.go @@ -3,7 +3,6 @@ package log import ( "encoding/json" "errors" - "fmt" "io" "os" "strconv" @@ -15,8 +14,7 @@ import ( "go.uber.org/zap" ) -// SerializedRecordSuite exercises real Init + AddSink serialization. The -// logger is package-global, so these tests must not run in parallel. +// 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)) } @@ -26,8 +24,7 @@ func (s *SerializedRecordSuite) SetupTest() { s.T().Cleanup(func() { sugar.Store(zap.NewNop().Sugar()) }) } -// capture runs emit against a freshly initialised logger and returns the bytes -// written to both the additive sink and stderr. +// 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) @@ -51,10 +48,7 @@ func (s *SerializedRecordSuite) capture(cfg Config, emit func()) (sink, stderr s return sinkBuf.String(), string(data) } -// TestTextMultilineBridgeCompletionIsOnePhysicalRecord reproduces the CI -// symptom: one logical event whose error text contains raw remote log lines -// must not produce unprefixed continuation lines. -func (s *SerializedRecordSuite) TestTextMultilineBridgeCompletionIsOnePhysicalRecord() { +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" + @@ -89,9 +83,7 @@ func (s *SerializedRecordSuite) TestEveryFormatEmitsOneRecordPerEvent() { 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.True(strings.HasSuffix(sink, "\n")) s.NotContains(sink, "\r") - s.Equal(2, strings.Count(sink, "\n")) s.assertRecordContent(format, lines, message) }) } @@ -121,20 +113,6 @@ func (s *SerializedRecordSuite) assertRecordContent(format string, lines []strin } } -func (s *SerializedRecordSuite) TestTextWithFieldsAndChildLoggerShareFraming() { - sink, _ := s.capture(Config{Format: "text"}, func() { - child := Underlying().With(zap.String("scope", "a\nb")).Sugar() - child.Infow("child event", "k", "v\rw") - Infof("parent") - }) - - lines := strings.Split(strings.TrimSuffix(sink, "\n"), "\n") - s.Require().Len(lines, 2, "%q", sink) - s.Contains(lines[0], `"scope": "a\nb"`) - s.Contains(lines[0], `"k": "v\rw"`) - s.Contains(lines[1], "parent") -} - func (s *SerializedRecordSuite) TestConcurrentEventsStayOnePhysicalRecordEach() { sink, _ := s.capture(Config{Format: "text"}, func() { done := make(chan struct{}) @@ -160,10 +138,9 @@ func (s *SerializedRecordSuite) TestConcurrentEventsStayOnePhysicalRecordEach() const redactionSecret = "line1\nli\"ne2\r\\end\x00\v\f\x01\u2028" -// TestTextRecordRedactionAcrossFormats is the mandatory post-serialization -// redaction gate: the encoders rewrite control characters before the writers -// redact, so the escaped forms must be masked too. -func (s *SerializedRecordSuite) TestTextRecordRedactionAcrossFormats() { +// 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{ @@ -192,10 +169,7 @@ func (s *SerializedRecordSuite) assertNoSecretFragments(out string) { } } -// TestRedactionCoversEverySerializedByte checks each ASCII byte and the -// Unicode separators embedded in a secret across all formats and both field -// and message positions, pinning the observed encodings. -func (s *SerializedRecordSuite) TestRedactionCoversEverySerializedByte() { +func (s *SerializedRecordSuite) TestRedactionCoversEveryControlByte() { chars := make([]string, 0, 0x80+3) for b := range 0x80 { chars = append(chars, string(rune(b))) @@ -214,7 +188,7 @@ func (s *SerializedRecordSuite) TestRedactionCoversEverySerializedByte() { }) 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, "***", fmt.Sprintf("format %s char %q", format, char)) + s.Contains(sink, "***", "format %s char %q", format, char) } } } @@ -230,15 +204,3 @@ func (s *SerializedRecordSuite) TestEnvironmentSecretWithLineBreakIsMasked() { s.NotContains(sink, "secret-846297") s.Contains(sink, "value=***") } - -func (s *SerializedRecordSuite) TestOrdinaryValuesAreNotRedactedByEscapeVariants() { - sink, _ := s.capture(Config{ - Format: "text", - Redactor: secrets.NewRedactor([]string{"TOKEN=alnumsecret42"}), - }, func() { - Infof("plain value only: quote=%q", "ordinary") - }) - - s.Contains(sink, `plain value only: quote="ordinary"`) - s.NotContains(sink, "***") -} diff --git a/pkg/log/text_record_encoder.go b/pkg/log/text_record_encoder.go index 53c6ed0ab6..4d060cb656 100644 --- a/pkg/log/text_record_encoder.go +++ b/pkg/log/text_record_encoder.go @@ -1,7 +1,6 @@ package log import ( - "errors" "fmt" zapbuffer "go.uber.org/zap/buffer" @@ -10,23 +9,19 @@ import ( var textRecordPool = zapbuffer.NewPool() -// oneLineTextEncoder wraps a console encoder so every encoded entry is exactly -// one physical record: a single terminating LF, with line breaks and terminal -// control characters inside the record escaped as text. Escaping covers the -// whole serialized record (message, fields, stack) because it is applied to -// the inner encoder's output. +// 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 deliberately left untouched: the color level encoder emits ANSI -// sequences and the serialized buffer does not distinguish formatter bytes from -// payload bytes. Payload ANSI injection is therefore out of scope here. +// 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 must wrap a clone of the inner encoder because zap calls it in -// Core.With and the inner encoder holds the accumulated With fields. +// Clone wraps a clone of the inner encoder, which holds the With fields. func (e *oneLineTextEncoder) Clone() zapcore.Encoder { return newOneLineTextEncoder(e.Encoder.Clone()) } @@ -42,15 +37,18 @@ func (e *oneLineTextEncoder) EncodeEntry( } return nil, err } - if original == nil { - return nil, errors.New("text encoder returned nil buffer") - } - body, hasFinalLF := splitEncodedRecord(original.Bytes()) + 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') @@ -58,14 +56,6 @@ func (e *oneLineTextEncoder) EncodeEntry( return normalized, nil } -// splitEncodedRecord separates the single final LF terminator from the body. -func splitEncodedRecord(b []byte) ([]byte, bool) { - if len(b) > 0 && b[len(b)-1] == '\n' { - return b[:len(b)-1], true - } - return b, false -} - func forbiddenASCII(b byte) bool { return (b < 0x20 && b != '\t' && b != 0x1b) || b == 0x7f } @@ -89,6 +79,7 @@ func asciiRecordEscape(b byte) string { 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` diff --git a/pkg/log/text_record_encoder_test.go b/pkg/log/text_record_encoder_test.go index 94f3da7061..3b5e85f63d 100644 --- a/pkg/log/text_record_encoder_test.go +++ b/pkg/log/text_record_encoder_test.go @@ -38,7 +38,7 @@ func (s *TextRecordEncoderSuite) plainEncoder() zapcore.Encoder { return newOneLineTextEncoder(zapcore.NewConsoleEncoder(plainEncoderConfig())) } -// requireOneRecord asserts the acceptance invariant for a text record. +// 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") @@ -73,14 +73,6 @@ func (s *TextRecordEncoderSuite) TestEscapesControlsAndUnicodeSeparators() { ) } -func (s *TextRecordEncoderSuite) TestPreservesLiteralBackslashN() { - literal := s.requireOneRecord(s.encode(s.plainEncoder(), testEntry(`literal \n`))) - actual := s.requireOneRecord(s.encode(s.plainEncoder(), testEntry("literal \n"))) - - s.True(strings.HasSuffix(literal, `literal \n`)) - s.True(strings.HasSuffix(actual, `literal \n`)) -} - func (s *TextRecordEncoderSuite) TestStackAndFields() { entry := testEntry("msg") entry.Stack = "frame1\nframe2" @@ -102,75 +94,36 @@ func (s *TextRecordEncoderSuite) TestCloneKeepsWithFieldsIsolated() { s.Contains(derivedOut, `"child": "a\nb"`) s.NotContains(baseOut, "child") - _, wrapped := derived.(*oneLineTextEncoder) - s.True(wrapped, "Clone must keep the wrapper") } -func (s *TextRecordEncoderSuite) TestFastPathMatchesInnerEncoder() { +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)) - - run := func(enc zapcore.Encoder) float64 { - return testing.AllocsPerRun(50, func() { - buf, err := enc.EncodeEntry(entry, nil) - s.Require().NoError(err) - buf.Free() - }) - } - s.LessOrEqual(run(wrapped), run(inner), "fast path must not allocate beyond the inner encoder") } -func (s *TextRecordEncoderSuite) TestColorEncoderPreservesANSIAndFramesRecord() { +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[", "formatter color sequence must survive escaping") + s.Contains(out, "\x1b[") s.Contains(out, "\x1b[0m") } -func (s *TextRecordEncoderSuite) TestNoColorSelectsPlainWrappedEncoder() { - s.T().Setenv("NO_COLOR", "1") - - enc := resolveEncoder("text") - out := s.encode(enc, testEntry("one\ntwo")) - - _, wrapped := enc.(*oneLineTextEncoder) - s.True(wrapped) - s.NotContains(out, "\x1b") - s.requireOneRecord(out) -} - -func (s *TextRecordEncoderSuite) TestResolveEncoderWrapsTextOnly() { - s.T().Setenv("NO_COLOR", "") - for _, format := range []string{"text", "", "other"} { - _, wrapped := resolveEncoder(format).(*oneLineTextEncoder) - s.True(wrapped, "format %q", format) - } - for _, format := range []string{"json", "logfmt"} { - _, wrapped := resolveEncoder(format).(*oneLineTextEncoder) - s.False(wrapped, "format %q", format) - } -} - -// fakeEncoder returns canned inner output to exercise terminator handling. +// fakeEncoder returns canned inner output. type fakeEncoder struct { zapcore.Encoder pool zapbuffer.Pool out *string err error - nils bool } func (f *fakeEncoder) EncodeEntry(zapcore.Entry, []zapcore.Field) (*zapbuffer.Buffer, error) { - if f.nils { - return nil, nil - } buf := f.pool.Get() if f.out != nil { _, _ = buf.WriteString(*f.out) @@ -195,17 +148,12 @@ func (s *TextRecordEncoderSuite) TestTerminatorNormalization() { } } -func (s *TextRecordEncoderSuite) TestInnerErrorAndNilBuffer() { +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) - - enc = newOneLineTextEncoder(&fakeEncoder{nils: true}) - buf, err = enc.EncodeEntry(testEntry("x"), nil) - s.Nil(buf) - s.Error(err) } func (s *TextRecordEncoderSuite) TestInvalidAndTruncatedUTF8DoNotPanic() { diff --git a/pkg/secrets/redact.go b/pkg/secrets/redact.go index 428f357062..f95f7bb7ce 100644 --- a/pkg/secrets/redact.go +++ b/pkg/secrets/redact.go @@ -72,9 +72,8 @@ func Combine(redactors ...*Redactor) *Redactor { return NewRedactor(entries) } -// logTextEscaper mirrors the record-framing escapes the text log encoder -// applies to a serialized record body. It lives here, rather than being -// imported, because pkg/log depends on this package. +// 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 { @@ -99,12 +98,11 @@ func newLogTextEscaper() *strings.Replacer { return strings.NewReplacer(pairs...) } -// WithLogEscapes returns a redactor that masks every value known by r in the -// forms the application log encoders can serialize it: the raw value, its -// strconv-quoted form (logfmt), its JSON-escaped forms (json encoder and text -// fields) and its text-record-escaped forms. r is not modified and the raw -// values stay registered. A nil r yields a redactor with only format-based -// masking. +// 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) @@ -120,19 +118,21 @@ func WithLogEscapes(r *Redactor) *Redactor { func logEscapeVariants(value string) []string { quoted := strconv.Quote(value) - result := []string{value, quoted[1 : len(quoted)-1], logTextEscaper.Replace(value)} - if encoded, err := json.Marshal(value); err == nil { - result = append(result, string(encoded[1:len(encoded)-1])) - } + encoded, _ := json.Marshal(value) // cannot fail for a string zapJSON := zapJSONEscape(value) - // The text encoder renders fields as zap JSON and then escapes the record, - // so U+0085/U+2028/U+2029/DEL inside that JSON are rewritten again. - return uniqueNonemptyStrings(append(result, zapJSON, logTextEscaper.Replace(zapJSON))) + 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 how zap's JSON encoder escapes a string value. It -// differs from encoding/json for \b, \f and U+2028/U+2029, so it is a -// distinct serialized form of the same secret. +// 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)) @@ -160,8 +160,6 @@ func zapJSONEscape(value string) string { return b.String() } -// uniqueNonemptyStrings drops empty values and duplicates, keeping first-seen -// order. func uniqueNonemptyStrings(values []string) []string { seen := make(map[string]struct{}, len(values)) result := make([]string, 0, len(values)) diff --git a/pkg/tunnel/pipebridge_test.go b/pkg/tunnel/pipebridge_test.go index 7aed3ec404..d7b0ff9fa5 100644 --- a/pkg/tunnel/pipebridge_test.go +++ b/pkg/tunnel/pipebridge_test.go @@ -409,9 +409,8 @@ func TestRunPairStopsBothSidesWhenParentContextCancelledBeforeEitherReturns(t *t } } -// summarizedFailure mimics a typed error whose Error text carries a captured -// diagnostic tail that log sites must not replay. It is defined locally because -// pkg/tunnel cannot import pkg/devcontainer/sshtunnel. +// summarizedFailure stands in for sshtunnel's agentCommandFailure, which +// pkg/tunnel cannot import. type summarizedFailure struct { cause error summary string @@ -422,10 +421,7 @@ func (e *summarizedFailure) Error() string { return e.summary + "\n" + e.ta func (e *summarizedFailure) Unwrap() error { return e.cause } func (e *summarizedFailure) LogSummary() string { return e.summary } -var _ log.LogSummaryProvider = (*summarizedFailure)(nil) - -// Serial on purpose: it replaces the package-global logger, so it must not run -// in parallel with other tests that log. +// Not parallel: it replaces the package-global logger. func TestRunPairCompletionDoesNotReplayCapturedJSON(t *testing.T) { logs := log.InitTestObserved(t, zapcore.DebugLevel) @@ -444,8 +440,6 @@ func TestRunPairCompletionDoesNotReplayCapturedJSON(t *testing.T) { tunnelFn := func(_ context.Context, _ *os.File, _ *os.File) error { return fmt.Errorf("outer: %w", failure) } - // Reading the closed bridge side yields EOF, so ClassifyTunnelErrors reports - // the tunnel error. handler := func(_ context.Context, stdout *os.File, _ *os.File) error { _, err := stdout.Read(make([]byte, 1)) return err @@ -454,19 +448,13 @@ func TestRunPairCompletionDoesNotReplayCapturedJSON(t *testing.T) { runErr := pb.RunPair(context.Background(), tunnelFn, handler) require.Error(t, runErr) require.ErrorIs(t, runErr, cause) - var provider log.LogSummaryProvider - require.ErrorAs(t, runErr, &provider) - assert.Equal(t, failure.Error(), errors.Unwrap(errors.Unwrap(runErr)).Error(), - "returned error keeps the full original text") + 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.Equal(t, zapcore.DebugLevel, entry.Level) assert.Contains(t, entry.Message, "tunnel_err="+summary+" ") assert.NotContains(t, entry.Message, `{"level"`) - assert.NotContains(t, entry.Message, `{"kind"`) - assert.NotContains(t, entry.Message, "credentials server started") assert.NotContains(t, entry.Message, "\n") assert.Contains(t, entry.Message, "handler_err=EOF") }