File tree Expand file tree Collapse file tree 2 files changed +20
-6
lines changed
src/queries/security/cwe-078
test/query-tests/security/cwe-078/NonConstantKernelOpen Expand file tree Collapse file tree 2 files changed +20
-6
lines changed Original file line number Diff line number Diff line change 16
16
*/
17
17
18
18
import codeql.ruby.security.KernelOpenQuery
19
- import codeql.ruby.ast.Literal
19
+ import codeql.ruby.AST
20
+ import codeql.ruby.ApiGraphs
20
21
21
22
from AmbiguousPathCall call
22
23
where
23
- // there is not a constant string argument
24
- not exists ( call .getPathArgument ( ) .getConstantValue ( ) ) and
25
- // if it's a format string, then the first argument is not a constant string
26
- not call .getPathArgument ( ) .getALocalSource ( ) .asExpr ( ) .getExpr ( ) .( StringLiteral ) .getComponent ( 0 )
27
- instanceof StringTextComponent
24
+ not hasConstantPrefix ( call .getPathArgument ( ) .getALocalSource ( ) .asExpr ( ) .getExpr ( ) ) and
25
+ not call .getPathArgument ( ) .getALocalSource ( ) =
26
+ API:: getTopLevelMember ( "File" ) .getAMethodCall ( "join" )
28
27
select call ,
29
28
"Call to " + call .getName ( ) + " with a non-constant value. Consider replacing it with " +
30
29
call .getReplacement ( ) + "."
30
+
31
+ predicate hasConstantPrefix ( Expr e ) {
32
+ // if it's a format string, then the first argument is not a constant string
33
+ e .( StringlikeLiteral ) .getComponent ( 0 ) instanceof StringTextComponent
34
+ or
35
+ // it is not a constant string argument
36
+ exists ( e .getConstantValue ( ) )
37
+ or
38
+ // not a concatenation that starts with a constant string
39
+ hasConstantPrefix ( e .( AddExpr ) .getLeftOperand ( ) )
40
+ }
Original file line number Diff line number Diff line change @@ -25,5 +25,9 @@ def create
25
25
Kernel . open ( "#{ this_is } bad" ) # BAD
26
26
27
27
open ( "| #{ this_is_an_explicit_command } foo bar" ) # GOOD
28
+
29
+ IO . foreach ( "|" + EnvUtil . rubybin + " -e 'puts :foo; puts :bar; puts :baz'" ) { |x | a << x } # GOOD
30
+
31
+ IO . write ( File . join ( "foo" , "bar.txt" ) , "bar" ) # GOOD
28
32
end
29
33
end
You can’t perform that action at this time.
0 commit comments