Skip to content

Commit

Permalink
Patch utf8 handling
Browse files Browse the repository at this point in the history
Osquery sometimes mis-encodes utf8 data osquery/osquery#5288

This is a broad attempt to repair log files that exhibit that issue. This runs against the entire log file. Hopefully, there isn’t going to be a case where it misfires.

Fixes: #445
  • Loading branch information
directionless committed Jan 20, 2021
1 parent a5aed99 commit 06a6b0e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/service/publish_logs.go
Expand Up @@ -152,7 +152,8 @@ func MakePublishLogsEndpoint(svc KolideService) endpoint.Endpoint {
func (e Endpoints) PublishLogs(ctx context.Context, nodeKey string, logType logger.LogType, logs []string) (string, string, bool, error) {
newCtx, cancel := context.WithTimeout(ctx, requestTimeout)
defer cancel()
request := logCollection{NodeKey: nodeKey, LogType: logType, Logs: logs}

request := logCollection{NodeKey: nodeKey, LogType: logType, Logs: patchOsqueryEmojiHandlingArray(logs)}
response, err := e.PublishLogsEndpoint(newCtx, request)
if err != nil {
return "", "", false, err
Expand Down
35 changes: 35 additions & 0 deletions pkg/service/util.go
@@ -0,0 +1,35 @@
package service

import (
"encoding/hex"
"strings"
)

// patchOsqueryEmojiHandling repairs utf8 data in the logs. See:
//
// https://github.com/kolide/launcher/issues/445
// https://github.com/facebook/osquery/issues/5288
func patchOsqueryEmojiHandling(in string) string {
if !strings.Contains(in, `\x`) {
return in
}

out := strings.Replace(in, `\x`, ``, -1)
outBytes, err := hex.DecodeString(out)
if err != nil {
return in
}

return string(outBytes)
}

// patchOsqueryEmojiHandlingArray calls patchOsqueryEmojiHandling across an array
func patchOsqueryEmojiHandlingArray(logs []string) []string {
out := make([]string, len(logs))

for i, in := range logs {
out[i] = patchOsqueryEmojiHandling(in)
}

return out
}
61 changes: 61 additions & 0 deletions pkg/service/util_test.go
@@ -0,0 +1,61 @@
package service

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPatchOsqueryEmojiHandling(t *testing.T) {
t.Parallel()

var tests = []struct {
in string
out string
}{
{
in: `\xF0\x9F\x9A\xB2`,
out: `🚲`,
},
{
in: `\xFNOCANDOBUDDY`,
out: `\xFNOCANDOBUDDY`,
},
{
in: `a normal string`,
out: `a normal string`,
},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("Input: %s", tt.in), func(t *testing.T) {
assert.Equal(t, tt.out, patchOsqueryEmojiHandling(tt.in))
})
}
}

func TestPatchOsqueryEmojiHandlingArray(t *testing.T) {
t.Parallel()

var tests = []struct {
in []string
out []string
}{
{
in: []string{},
out: []string{},
},
{
in: []string{`\xFNOCANDOBUDDY`, `a normal string`, `\xF0\x9F\x9A\xB2`},
out: []string{`\xFNOCANDOBUDDY`, `a normal string`, `🚲`},
},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("Input: %s", tt.in), func(t *testing.T) {
require.Equal(t, tt.out, patchOsqueryEmojiHandlingArray(tt.in))
})
}
}

0 comments on commit 06a6b0e

Please sign in to comment.