Skip to content

Commit 7dcb813

Browse files
committed
remove two more claseses of FPs in rb/non-constant-kernel-open
1 parent 6b1865d commit 7dcb813

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

ruby/ql/src/queries/security/cwe-078/NonConstantKernelOpen.ql

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,25 @@
1616
*/
1717

1818
import codeql.ruby.security.KernelOpenQuery
19-
import codeql.ruby.ast.Literal
19+
import codeql.ruby.AST
20+
import codeql.ruby.ApiGraphs
2021

2122
from AmbiguousPathCall call
2223
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")
2827
select 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+
}

ruby/ql/test/query-tests/security/cwe-078/NonConstantKernelOpen/NonConstantKernelOpen.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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
2933
end

0 commit comments

Comments
 (0)