Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
17017b25
Unverified
Commit
17017b25
authored
Jan 30, 2023
by
Martin Holst Swende
Committed by
GitHub
Jan 30, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
log: better sanitation (#26556)
parent
3ff3d07e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
6 deletions
+78
-6
format.go
log/format.go
+32
-6
format_test.go
log/format_test.go
+46
-0
No files found.
log/format.go
View file @
17017b25
...
...
@@ -86,6 +86,7 @@ type TerminalStringer interface {
// [DBUG] [May 16 20:58:45] remove route ns=haproxy addr=127.0.0.1:50002
func
TerminalFormat
(
usecolor
bool
)
Format
{
return
FormatFunc
(
func
(
r
*
Record
)
[]
byte
{
msg
:=
escapeMessage
(
r
.
Msg
)
var
color
=
0
if
usecolor
{
switch
r
.
Lvl
{
...
...
@@ -122,19 +123,19 @@ func TerminalFormat(usecolor bool) Format {
// Assemble and print the log heading
if
color
>
0
{
fmt
.
Fprintf
(
b
,
"
\x1b
[%dm%s
\x1b
[0m[%s|%s]%s %s "
,
color
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
location
,
padding
,
r
.
M
sg
)
fmt
.
Fprintf
(
b
,
"
\x1b
[%dm%s
\x1b
[0m[%s|%s]%s %s "
,
color
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
location
,
padding
,
m
sg
)
}
else
{
fmt
.
Fprintf
(
b
,
"%s[%s|%s]%s %s "
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
location
,
padding
,
r
.
M
sg
)
fmt
.
Fprintf
(
b
,
"%s[%s|%s]%s %s "
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
location
,
padding
,
m
sg
)
}
}
else
{
if
color
>
0
{
fmt
.
Fprintf
(
b
,
"
\x1b
[%dm%s
\x1b
[0m[%s] %s "
,
color
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
r
.
M
sg
)
fmt
.
Fprintf
(
b
,
"
\x1b
[%dm%s
\x1b
[0m[%s] %s "
,
color
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
m
sg
)
}
else
{
fmt
.
Fprintf
(
b
,
"%s[%s] %s "
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
r
.
M
sg
)
fmt
.
Fprintf
(
b
,
"%s[%s] %s "
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
m
sg
)
}
}
// try to justify the log output for short messages
length
:=
utf8
.
RuneCountInString
(
r
.
M
sg
)
length
:=
utf8
.
RuneCountInString
(
m
sg
)
if
len
(
r
.
Ctx
)
>
0
&&
length
<
termMsgJust
{
b
.
Write
(
bytes
.
Repeat
([]
byte
{
' '
},
termMsgJust
-
length
))
}
...
...
@@ -167,6 +168,8 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) {
v
:=
formatLogfmtValue
(
ctx
[
i
+
1
],
term
)
if
!
ok
{
k
,
v
=
errorKey
,
formatLogfmtValue
(
k
,
term
)
}
else
{
k
=
escapeString
(
k
)
}
// XXX: we should probably check that all of your key bytes aren't invalid
...
...
@@ -471,7 +474,7 @@ func formatLogfmtBigInt(n *big.Int) string {
func
escapeString
(
s
string
)
string
{
needsQuoting
:=
false
for
_
,
r
:=
range
s
{
// We quote everything below " (0x
34
) and above~ (0x7E), plus equal-sign
// We quote everything below " (0x
22
) and above~ (0x7E), plus equal-sign
if
r
<=
'"'
||
r
>
'~'
||
r
==
'='
{
needsQuoting
=
true
break
...
...
@@ -482,3 +485,26 @@ func escapeString(s string) string {
}
return
strconv
.
Quote
(
s
)
}
// escapeMessage checks if the provided string needs escaping/quoting, similarly
// to escapeString. The difference is that this method is more lenient: it allows
// for spaces and linebreaks to occur without needing quoting.
func
escapeMessage
(
s
string
)
string
{
needsQuoting
:=
false
for
_
,
r
:=
range
s
{
// Carriage return and Line feed are ok
if
r
==
0xa
||
r
==
0xd
{
continue
}
// We quote everything below <space> (0x20) and above~ (0x7E),
// plus equal-sign
if
r
<
' '
||
r
>
'~'
||
r
==
'='
{
needsQuoting
=
true
break
}
}
if
!
needsQuoting
{
return
s
}
return
strconv
.
Quote
(
s
)
}
log/format_test.go
View file @
17017b25
package
log
import
(
"fmt"
"math"
"math/big"
"math/rand"
"strings"
"testing"
)
...
...
@@ -93,3 +95,47 @@ func BenchmarkPrettyUint64Logfmt(b *testing.B) {
sink
=
FormatLogfmtUint64
(
rand
.
Uint64
())
}
}
func
TestSanitation
(
t
*
testing
.
T
)
{
msg
:=
"
\u001b
[1G
\u001b
[K
\u001b
[1A"
msg2
:=
"
\u001b
\u0000
"
msg3
:=
"NiceMessage"
msg4
:=
"Space Message"
msg5
:=
"Enter
\n
Message"
for
i
,
tt
:=
range
[]
struct
{
msg
string
want
string
}{
{
msg
:
msg
,
want
:
fmt
.
Sprintf
(
"] %q %q=%q
\n
"
,
msg
,
msg
,
msg
),
},
{
msg
:
msg2
,
want
:
fmt
.
Sprintf
(
"] %q %q=%q
\n
"
,
msg2
,
msg2
,
msg2
),
},
{
msg
:
msg3
,
want
:
fmt
.
Sprintf
(
"] %s %s=%s
\n
"
,
msg3
,
msg3
,
msg3
),
},
{
msg
:
msg4
,
want
:
fmt
.
Sprintf
(
"] %s %q=%q
\n
"
,
msg4
,
msg4
,
msg4
),
},
{
msg
:
msg5
,
want
:
fmt
.
Sprintf
(
"] %s %q=%q
\n
"
,
msg5
,
msg5
,
msg5
),
},
}
{
var
(
logger
=
New
()
out
=
new
(
strings
.
Builder
)
)
logger
.
SetHandler
(
LvlFilterHandler
(
LvlInfo
,
StreamHandler
(
out
,
TerminalFormat
(
false
))))
logger
.
Info
(
tt
.
msg
,
tt
.
msg
,
tt
.
msg
)
if
have
:=
out
.
String
()[
24
:
];
tt
.
want
!=
have
{
t
.
Fatalf
(
"test %d: want / have:
\n
%v
\n
%v"
,
i
,
tt
.
want
,
have
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment