Skip to content

fix: replace invalid utf-8 sequences in agent logs #13436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: replace invalid UTF-8 with ❌, add regression
Signed-off-by: Spike Curtis <spike@coder.com>
  • Loading branch information
spikecurtis committed Jun 7, 2024
commit 91aff2a0b8093a628e62a41215b0e90ce89d46f7
2 changes: 1 addition & 1 deletion codersdk/agentsdk/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func ProtoFromLog(log Log) (*proto.Log, error) {
}
return &proto.Log{
CreatedAt: timestamppb.New(log.CreatedAt),
Output: strings.ToValidUTF8(log.Output, ""),
Output: strings.ToValidUTF8(log.Output, ""),
Level: proto.Log_Level(lvl),
}, nil
}
Expand Down
45 changes: 45 additions & 0 deletions codersdk/agentsdk/logs_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,51 @@ func TestLogSender_SkipHugeLog(t *testing.T) {
require.ErrorIs(t, err, context.Canceled)
}

func TestLogSender_InvalidUTF8(t *testing.T) {
t.Parallel()
testCtx := testutil.Context(t, testutil.WaitShort)
ctx, cancel := context.WithCancel(testCtx)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
fDest := newFakeLogDest()
uut := NewLogSender(logger)

t0 := dbtime.Now()
ls1 := uuid.UUID{0x11}

uut.Enqueue(ls1,
Log{
CreatedAt: t0,
Output: "test log 0, src 1\xc3\x28",
Level: codersdk.LogLevelInfo,
},
Log{
CreatedAt: t0,
Output: "test log 1, src 1",
Level: codersdk.LogLevelInfo,
})

loopErr := make(chan error, 1)
go func() {
err := uut.SendLoop(ctx, fDest)
loopErr <- err
}()

req := testutil.RequireRecvCtx(ctx, t, fDest.reqs)
require.NotNil(t, req)
require.Len(t, req.Logs, 2, "it should sanitize invalid UTF-8, but still send")
// the 0xc3, 0x28 is an invalid 2-byte sequence in UTF-8. The sanitizer replaces 0xc3 with ❌, and then
// interprets 0x28 as a 1-byte sequence "("
require.Equal(t, "test log 0, src 1❌(", req.Logs[0].GetOutput())
require.Equal(t, proto.Log_INFO, req.Logs[0].GetLevel())
require.Equal(t, "test log 1, src 1", req.Logs[1].GetOutput())
require.Equal(t, proto.Log_INFO, req.Logs[1].GetLevel())
testutil.RequireSendCtx(ctx, t, fDest.resps, &proto.BatchCreateLogsResponse{})

cancel()
err := testutil.RequireRecvCtx(testCtx, t, loopErr)
require.ErrorIs(t, err, context.Canceled)
}

func TestLogSender_Batch(t *testing.T) {
t.Parallel()
testCtx := testutil.Context(t, testutil.WaitShort)
Expand Down
Loading