Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
54b82bb0
Commit
54b82bb0
authored
1 year ago
by
Jano Hendriks
Browse files
Options
Downloads
Patches
Plain Diff
Hide byte arrays when sanitising json strings
parent
7a677b68
Branches
Branches containing commit
Tags
v1.169.0
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
logs/logs.go
+17
-18
17 additions, 18 deletions
logs/logs.go
string_utils/string_utils.go
+31
-1
31 additions, 1 deletion
string_utils/string_utils.go
with
48 additions
and
19 deletions
logs/logs.go
+
17
−
18
View file @
54b82bb0
...
...
@@ -30,33 +30,32 @@ var build string
var
raygunClient
*
raygun4go
.
Client
// Password filtering
var
passwordRegex
=
regexp
.
MustCompile
(
`(?i:\\?"password\\?"\s*:\s*\\?"(.*)\\?",).*`
)
var
passwordRegex
=
regexp
.
MustCompile
(
`(?i:\\?"password\\?"\s*:\s*\\?"(.*)\\?").*`
)
var
byteArrayRegex
=
regexp
.
MustCompile
(
`(?i:\\?"(?i:[\w]*)byte(?i:[\w]*)\\?"\s*:\s*\[([\d\s,]+)*\])`
)
func
SanitiseLogs
(
logString
string
)
string
{
var
isValidJsonString
bool
isValidJsonString
,
logString
=
string_utils
.
PrettyJSON
(
logString
)
if
!
isValidJsonString
{
return
logString
}
logString
=
MaskByteArraysInJsonString
(
logString
)
logString
=
MaskPasswordsInJsonString
(
logString
)
return
logString
}
// MaskPasswordsInJsonString takes a string and
, if
it
is
a JSON string, sanitises all the password. In order for the
//
regex to work correctly we need to prettify the JSON, so the function always returns a formatted JSON string.
// MaskPasswordsInJsonString takes a string and
san
itis
es all the instances of fields named password.
//
E.g. "{"password": "xyz123"}" will become "{"password": "***"}"
func
MaskPasswordsInJsonString
(
jsonString
string
)
string
{
var
isValidJsonString
bool
isValidJsonString
,
jsonString
=
string_utils
.
PrettyJSON
(
jsonString
)
if
!
isValidJsonString
{
return
jsonString
}
if
passwordRegex
.
MatchString
(
jsonString
)
{
result
:=
passwordRegex
.
FindAllStringSubmatch
(
jsonString
,
-
1
)
for
_
,
match
:=
range
result
{
if
len
(
match
)
>
1
{
jsonString
=
strings
.
ReplaceAll
(
jsonString
,
match
[
1
],
"***"
)
}
}
return
string_utils
.
ReplaceAllRegexStringSubmatch
(
passwordRegex
,
jsonString
,
"***"
)
}
return
jsonString
// MaskByteArraysInJsonString takes a string and truncates all the instances of number array fields have the word
// "byte" in the name. E.g. {"file_bytes": [123,68,103]} will become "{"file_bytes": [...]}"
func
MaskByteArraysInJsonString
(
jsonString
string
)
string
{
return
string_utils
.
ReplaceAllRegexStringSubmatch
(
byteArrayRegex
,
jsonString
,
"..."
)
}
func
SanitiseFields
(
fields
map
[
string
]
interface
{})
map
[
string
]
interface
{}
{
...
...
This diff is collapsed.
Click to expand it.
string_utils/string_utils.go
+
31
−
1
View file @
54b82bb0
...
...
@@ -16,7 +16,14 @@ import (
"golang.org/x/text/unicode/norm"
)
const
snakeCasePattern
=
`[a-z]([a-z0-9_]*[a-z0-9])*`
const
(
snakeCasePattern
=
`[a-z]([a-z0-9_]*[a-z0-9])*`
regexIndexMatchStart
=
0
regexIndexMatchEnd
=
1
regexIndexSubmatchStart
=
2
regexIndexSubmatchEnd
=
3
)
var
snakeCaseRegex
=
regexp
.
MustCompile
(
"^"
+
snakeCasePattern
+
"$"
)
...
...
@@ -49,6 +56,29 @@ func ReplaceCaseInsensitive(string, toReplace, replaceWith string) string {
return
regex
.
ReplaceAllString
(
string
,
replaceWith
)
}
// ReplaceAllRegexStringSubmatch finds the submatches for a regular expression that has a single capturing group and
// replaces all the submatches (i.e. the part that matches the capturing group) with replaceWith.
// E.g. the regular expression re = a(x*)b captures any number of x's that are between an a and b.
// ReplaceAllRegexStringSubmatch(re, "-axxb-ab-axb-x-ax-xb-ba-", "?") will result in "-a?b-a?b-a?b-x-ax-xb-ba-"
func
ReplaceAllRegexStringSubmatch
(
re
*
regexp
.
Regexp
,
s
string
,
replaceWith
string
)
string
{
result
:=
""
lastIndex
:=
0
for
_
,
v
:=
range
re
.
FindAllSubmatchIndex
([]
byte
(
s
),
-
1
)
{
if
len
(
v
)
==
regexIndexSubmatchEnd
+
1
{
// One submatch - replace the submatch with replaceWith
result
+=
s
[
lastIndex
:
v
[
regexIndexSubmatchStart
]]
+
replaceWith
+
s
[
v
[
regexIndexSubmatchEnd
]
:
v
[
regexIndexMatchEnd
]]
lastIndex
=
v
[
regexIndexMatchEnd
]
}
else
{
// A normal match with no submatch - don't replace anything (this should not really happen)
result
+=
s
[
lastIndex
:
v
[
regexIndexMatchEnd
]]
}
lastIndex
=
v
[
regexIndexMatchEnd
]
}
return
result
+
s
[
lastIndex
:
]
}
// TrimQuotes - trims quotes from a string (ie: "foo" will return foo)
func
TrimQuotes
(
stringToTrim
string
)
string
{
if
len
(
stringToTrim
)
>=
2
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment