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 1616 */
1717
1818import codeql.ruby.security.KernelOpenQuery
19- import codeql.ruby.ast.Literal
19+ import codeql.ruby.AST
20+ import codeql.ruby.ApiGraphs
2021
2122from AmbiguousPathCall call
2223where
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" )
2827select call ,
2928 "Call to " + call .getName ( ) + " with a non-constant value. Consider replacing it with " +
3029 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
2525 Kernel . open ( "#{ this_is } bad" ) # BAD
2626
2727 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
2832 end
2933end
You can’t perform that action at this time.
0 commit comments