-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
a5aed99
commit 06a6b0e
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
}) | ||
} | ||
} |