Skip to content

Commit 009e0e1

Browse files
committed
Don't consider arguments with %T as logger call components
1 parent f173305 commit 009e0e1

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

go/ql/lib/semmle/go/Concepts.qll

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,19 @@ module RegexpReplaceFunction {
355355
* extend `LoggerCall::Range` instead.
356356
*/
357357
class LoggerCall extends DataFlow::Node instanceof LoggerCall::Range {
358-
/** Gets a node that is a part of the logged message. */
359-
DataFlow::Node getAMessageComponent() { result = super.getAMessageComponent() }
358+
/**
359+
* Gets a node that is a part of the logged message.
360+
*
361+
* Exclude components corresponding to the format specifier "%T" as this
362+
* prints the type of the argument, which is not considered vulnerable.
363+
*/
364+
DataFlow::Node getAMessageComponent() {
365+
result = super.getAMessageComponent() and
366+
not exists(string formatSpecifier |
367+
formatSpecifier.regexpMatch("%[^%]*T") and
368+
result = this.(StringOps::Formatting::StringFormatCall).getOperand(_, formatSpecifier)
369+
)
370+
}
360371
}
361372

362373
/** Provides a class for modeling new logging APIs. */

go/ql/test/library-tests/semmle/go/concepts/LoggerCall/glog.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func glogTest() {
3131
glog.Warningln(text) // $ logger=text
3232

3333
// components corresponding to the format specifier "%T" are not considered vulnerable
34-
glog.Errorf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
35-
glog.Exitf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
36-
glog.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
37-
glog.Infof("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
38-
glog.Warningf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
34+
glog.Errorf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
35+
glog.Exitf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
36+
glog.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
37+
glog.Infof("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
38+
glog.Warningf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
3939

4040
klog.Error(text) // $ logger=text
4141
klog.ErrorDepth(0, text) // $ logger=text
@@ -59,9 +59,9 @@ func glogTest() {
5959
klog.Warningln(text) // $ logger=text
6060

6161
// components corresponding to the format specifier "%T" are not considered vulnerable
62-
klog.Errorf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
63-
klog.Exitf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
64-
klog.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
65-
klog.Infof("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
66-
klog.Warningf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
62+
klog.Errorf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
63+
klog.Exitf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
64+
klog.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
65+
klog.Infof("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
66+
klog.Warningf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
6767
}

go/ql/test/library-tests/semmle/go/concepts/LoggerCall/logrus.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ func logrusCalls() {
3434
logrus.FatalFn(fn) // $ logger=fn
3535

3636
// components corresponding to the format specifier "%T" are not considered vulnerable
37-
logrus.Infof("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
38-
logrus.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
37+
logrus.Infof("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
38+
logrus.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
3939
}

go/ql/test/library-tests/semmle/go/concepts/LoggerCall/stdlib.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ func stdlib() {
1818
logger.Println(text) // $ logger=text
1919

2020
// components corresponding to the format specifier "%T" are not considered vulnerable
21-
logger.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
22-
logger.Panicf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
23-
logger.Printf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
21+
logger.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
22+
logger.Panicf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
23+
logger.Printf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
2424

2525
log.SetPrefix("prefix: ")
2626
log.Fatal(text) // $ logger=text
@@ -34,7 +34,7 @@ func stdlib() {
3434
log.Println(text) // $ logger=text
3535

3636
// components corresponding to the format specifier "%T" are not considered vulnerable
37-
log.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
38-
log.Panicf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
39-
log.Printf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text SPURIOUS: logger=v
37+
log.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
38+
log.Panicf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
39+
log.Printf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text
4040
}

0 commit comments

Comments
 (0)