Skip to content

Commit 997d300

Browse files
wip zap tests
1 parent 9ad152f commit 997d300

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+10332
-5
lines changed

go/ql/lib/semmle/go/security/LogInjectionCustomizations.qll

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,33 @@ module LogInjection {
8484
)
8585
}
8686
}
87-
}
87+
88+
/**
89+
* Returns true if `t` is a zap encoder type that is considered safe.
90+
*
91+
* We intentionally whitelist *only* JSONEncoder.
92+
* Other encoders may not escape newline characters and therefore
93+
* must NOT be treated as sanitizers.
94+
*/
95+
private predicate isSafeZapEncoder(Type t) {
96+
exists(Type zapEncoder |
97+
// Matches go.uber.org/zap/zapcore.JSONEncoder
98+
zapEncoder.hasQualifiedName("go.uber.org/zap/zapcore", "JSONEncoder") and
99+
t = zapEncoder
100+
)
101+
}
102+
103+
/**
104+
* Zap encoder sanitizer class.
105+
*
106+
* This extends the Sanitizer class used by the go/log-injection query.
107+
*/
108+
class ZapEncoderSanitizer extends Sanitizer {
109+
ZapEncoderSanitizer() {
110+
exists(Type t |
111+
this.getType() = t and
112+
isSafeZapEncoder(t)
113+
)
114+
}
115+
}
116+
}

go/ql/src/experimental/CWE-117/LogSanitizer.qll renamed to go/ql/lib/semmle/go/security/LogInjectionCustomizations/ZapEncoderSanitizer.qll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/**
2-
* LogSanitizer.qll
3-
*
4-
* Predicates to identify sanitizer functions and zap encoder-like types.
5-
* Template: adjust whitelist entries as needed.
2+
* Provides a taint tracking configuration for zap encoders that are known to remove or escape
3+
* newline characters, thus mitigating log injection (CWE-117).
64
*/
75

86
import go

go/ql/test/experimental/CWE-117-ZapEncoder/LogInjection.expected

Whitespace-only changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"go.uber.org/zap"
5+
"go.uber.org/zap/zapcore"
6+
"os"
7+
)
8+
9+
func LogWithSafeZapEncoder() {
10+
unsafeInput := os.Getenv("UNTRUSTED") // treat this as “source”
11+
12+
// Create a safe JSON encoder (that we whitelist)
13+
encoderCfg := zap.NewProductionEncoderConfig()
14+
jsonEncoder := zapcore.NewJSONEncoder(encoderCfg)
15+
16+
// Build logger using that encoder
17+
core := zapcore.NewCore(jsonEncoder, zapcore.AddSync(os.Stdout), zapcore.DebugLevel)
18+
logger := zap.New(core)
19+
20+
logger.Info("user input", zap.String("data", unsafeInput))
21+
}
22+
23+
func LogWithUnsafeZapEncoder() {
24+
unsafeInput := os.Getenv("UNTRUSTED") // source
25+
26+
// Suppose a “custom” encoder that does *not* sanitize newline
27+
// For test purposes, just use console encoder but pretend it’s unsafe
28+
encoderCfg := zap.NewProductionEncoderConfig()
29+
consoleEncoder := zapcore.NewConsoleEncoder(encoderCfg)
30+
31+
core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), zapcore.DebugLevel)
32+
logger := zap.New(core)
33+
34+
logger.Info("user input", zap.String("data", unsafeInput))
35+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
query: experimental/CWE-117-ZapEncoder/LogInjection.ql
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module example/zaploginjection
2+
3+
go 1.25.4
4+
5+
require go.uber.org/zap v1.27.1
6+
7+
require go.uber.org/multierr v1.10.0 // indirect

0 commit comments

Comments
 (0)