@@ -46,16 +46,33 @@ private class LineBreaksLogInjectionSanitizer extends LogInjectionSanitizer {
46
46
}
47
47
}
48
48
49
+ private predicate stringMethodAccess (
50
+ MethodAccess ma , CompileTimeConstantExpr arg0 , CompileTimeConstantExpr arg1
51
+ ) {
52
+ ma .getMethod ( ) .getDeclaringType ( ) instanceof TypeString and
53
+ arg0 = ma .getArgument ( 0 ) and
54
+ arg1 = ma .getArgument ( 1 )
55
+ }
56
+
57
+ private predicate stringMethodArgument ( CompileTimeConstantExpr arg ) {
58
+ stringMethodAccess ( _, arg , _) or stringMethodAccess ( _, _, arg )
59
+ }
60
+
61
+ bindingset [ match]
62
+ pragma [ inline_late]
63
+ private predicate stringMethodArgumentValueMatches ( CompileTimeConstantExpr const , string match ) {
64
+ stringMethodArgument ( const ) and
65
+ const .getStringValue ( ) .matches ( match )
66
+ }
67
+
49
68
/**
50
69
* Holds if the return value of `ma` is sanitized against log injection attacks
51
70
* by removing line breaks from it.
52
71
*/
53
72
private predicate logInjectionSanitizer ( MethodAccess ma ) {
54
73
exists ( CompileTimeConstantExpr target , CompileTimeConstantExpr replacement |
55
- ma .getMethod ( ) .getDeclaringType ( ) instanceof TypeString and
56
- target = ma .getArgument ( 0 ) and
57
- replacement = ma .getArgument ( 1 ) and
58
- not replacement .getStringValue ( ) .matches ( [ "%\n%" , "%\r%" ] )
74
+ stringMethodAccess ( ma , target , replacement ) and
75
+ not stringMethodArgumentValueMatches ( replacement , [ "%\n%" , "%\r%" ] )
59
76
|
60
77
ma .getMethod ( ) .hasName ( "replace" ) and
61
78
not replacement .getIntValue ( ) = [ 10 , 13 ] and
@@ -68,7 +85,7 @@ private predicate logInjectionSanitizer(MethodAccess ma) {
68
85
(
69
86
// Replace anything not in an allow list
70
87
target .getStringValue ( ) .matches ( "[^%]" ) and
71
- not target . getStringValue ( ) . matches ( "%" + [ "\n" , "\r" , "\\n" , "\\r" , "\\R" ] + "%" )
88
+ not stringMethodArgumentValueMatches ( target , "%" + [ "\n" , "\r" , "\\n" , "\\r" , "\\R" ] + "%" )
72
89
or
73
90
// Replace line breaks
74
91
target .getStringValue ( ) = [ "\n" , "\r" , "\\n" , "\\r" , "\\R" ]
0 commit comments