Skip to content

Commit

Permalink
chore(tmc): disable log syncing for non-utf8 output.
Browse files Browse the repository at this point in the history
Signed-off-by: Tiago Natel <t.nateldemoura@gmail.com>
  • Loading branch information
i4ki committed Oct 20, 2023
1 parent 42ae2ea commit 41dccb2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cloud/log_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"sync"
"time"
"unicode/utf8"

"github.com/rs/zerolog/log"
"github.com/terramate-io/terramate/errors"
Expand Down Expand Up @@ -68,6 +69,7 @@ func (s *LogSyncer) NewBuffer(channel LogChannel, out io.Writer) io.Writer {
go func() {
defer s.wg.Done()
linenum := int64(1)
syncDisabled := false

var pending []byte
errs := errors.L()
Expand All @@ -86,6 +88,16 @@ func (s *LogSyncer) NewBuffer(channel LogChannel, out io.Writer) io.Writer {
errs.Append(errors.E(err, "writing to terminal"))
}

if syncDisabled {
continue
}

if !utf8.Valid(line) {
syncDisabled = true
errs.Append(errors.E("skipping sync of non-utf8 (%s) output", channel.String()))
continue
}

t := time.Now().UTC()
s.in <- &DeploymentLog{
Channel: channel,
Expand Down
27 changes: 27 additions & 0 deletions cloud/log_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,33 @@ func TestCloudLogSyncer(t *testing.T) {
},
},
},
{
name: "disable sync in case non-utf8 output is detected",
writes: []write{
{channel: cloud.StdoutLogChannel, data: []byte("valid\n")},
{channel: cloud.StdoutLogChannel, data: []byte{0x80, 1, 2, 3, 4, '\n'}},
{channel: cloud.StdoutLogChannel, data: []byte("another valid")},
},
want: want{
output: map[cloud.LogChannel][]byte{
cloud.StdoutLogChannel: {
'v', 'a', 'l', 'i', 'd', '\n',
0x80, 1, 2, 3, 4, '\n',
'a', 'n', 'o', 't', 'h', 'e', 'r', ' ', 'v', 'a', 'l', 'i', 'd',
},
},
batches: []cloud.DeploymentLogs{
{
// sync is disabled at first non-utf8 sequence.
{
Line: 1,
Channel: cloud.StdoutLogChannel,
Message: "valid",
},
},
},
},
},
} {
tc := tc
tc.validate(t)
Expand Down

0 comments on commit 41dccb2

Please sign in to comment.