Skip to content

Commit 2f268b3

Browse files
committed
Ruby: improve non-constant-kernel-open, freeze called on constant
1 parent 0a6bb3f commit 2f268b3

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,33 @@
1818
import codeql.ruby.security.KernelOpenQuery
1919
import codeql.ruby.AST
2020
import codeql.ruby.ApiGraphs
21+
import codeql.ruby.DataFlow
2122

2223
from AmbiguousPathCall call
2324
where
2425
call.getNumberOfArguments() > 0 and
25-
not hasConstantPrefix(call.getPathArgument().getALocalSource().asExpr().getExpr()) and
26+
not hasConstantPrefix(call.getPathArgument()) and
2627
not call.getPathArgument().getALocalSource() =
2728
API::getTopLevelMember("File").getAMethodCall("join")
2829
select call,
2930
"Call to " + call.getName() + " with a non-constant value. Consider replacing it with " +
3031
call.getReplacement() + "."
3132

32-
predicate hasConstantPrefix(Expr e) {
33+
predicate hasConstantPrefix(DataFlow::Node node) {
34+
hasConstantPrefix(node.getALocalSource())
35+
or
3336
// if it's a format string, then the first argument is not a constant string
34-
e.(StringlikeLiteral).getComponent(0) instanceof StringTextComponent
37+
node.asExpr().getExpr().(StringlikeLiteral).getComponent(0) instanceof StringTextComponent
3538
or
3639
// it is not a constant string argument
37-
exists(e.getConstantValue())
40+
exists(node.asExpr().getExpr().getConstantValue())
3841
or
3942
// not a concatenation that starts with a constant string
40-
hasConstantPrefix(e.(AddExpr).getLeftOperand())
43+
exists(DataFlow::ExprNode prefix |
44+
node.asExpr().getExpr().(AddExpr).getLeftOperand() = prefix.asExpr().getExpr() and
45+
hasConstantPrefix(prefix)
46+
)
47+
or
48+
// is a .freeze call on a constant string
49+
node.asExpr().getExpr().(ConstantReadAccess).getValue().(MethodCall).getMethodName() = "freeze"
4150
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
| NonConstantKernelOpen.rb:6:5:6:14 | call to open | Call to Kernel.open with a non-constant value. Consider replacing it with File.open. |
2-
| NonConstantKernelOpen.rb:7:5:7:17 | call to read | Call to IO.read with a non-constant value. Consider replacing it with File.read. |
3-
| NonConstantKernelOpen.rb:8:5:8:18 | call to write | Call to IO.write with a non-constant value. Consider replacing it with File.write. |
4-
| NonConstantKernelOpen.rb:9:5:9:20 | call to binread | Call to IO.binread with a non-constant value. Consider replacing it with File.binread. |
5-
| NonConstantKernelOpen.rb:10:5:10:21 | call to binwrite | Call to IO.binwrite with a non-constant value. Consider replacing it with File.binwrite. |
6-
| NonConstantKernelOpen.rb:11:5:11:20 | call to foreach | Call to IO.foreach with a non-constant value. Consider replacing it with File.foreach. |
7-
| NonConstantKernelOpen.rb:12:5:12:22 | call to readlines | Call to IO.readlines with a non-constant value. Consider replacing it with File.readlines. |
8-
| NonConstantKernelOpen.rb:13:5:13:18 | call to open | Call to URI.open with a non-constant value. Consider replacing it with URI(<uri>).open. |
9-
| NonConstantKernelOpen.rb:17:5:17:21 | call to open | Call to Kernel.open with a non-constant value. Consider replacing it with File.open. |
10-
| NonConstantKernelOpen.rb:27:5:27:33 | call to open | Call to Kernel.open with a non-constant value. Consider replacing it with File.open. |
11-
| NonConstantKernelOpen.rb:41:5:41:14 | call to open | Call to Kernel.open with a non-constant value. Consider replacing it with File.open. |
1+
| NonConstantKernelOpen.rb:7:5:7:14 | call to open | Call to Kernel.open with a non-constant value. Consider replacing it with File.open. |
2+
| NonConstantKernelOpen.rb:8:5:8:17 | call to read | Call to IO.read with a non-constant value. Consider replacing it with File.read. |
3+
| NonConstantKernelOpen.rb:9:5:9:18 | call to write | Call to IO.write with a non-constant value. Consider replacing it with File.write. |
4+
| NonConstantKernelOpen.rb:10:5:10:20 | call to binread | Call to IO.binread with a non-constant value. Consider replacing it with File.binread. |
5+
| NonConstantKernelOpen.rb:11:5:11:21 | call to binwrite | Call to IO.binwrite with a non-constant value. Consider replacing it with File.binwrite. |
6+
| NonConstantKernelOpen.rb:12:5:12:20 | call to foreach | Call to IO.foreach with a non-constant value. Consider replacing it with File.foreach. |
7+
| NonConstantKernelOpen.rb:13:5:13:22 | call to readlines | Call to IO.readlines with a non-constant value. Consider replacing it with File.readlines. |
8+
| NonConstantKernelOpen.rb:14:5:14:18 | call to open | Call to URI.open with a non-constant value. Consider replacing it with URI(<uri>).open. |
9+
| NonConstantKernelOpen.rb:18:5:18:21 | call to open | Call to Kernel.open with a non-constant value. Consider replacing it with File.open. |
10+
| NonConstantKernelOpen.rb:28:5:28:33 | call to open | Call to Kernel.open with a non-constant value. Consider replacing it with File.open. |
11+
| NonConstantKernelOpen.rb:46:5:46:14 | call to open | Call to Kernel.open with a non-constant value. Consider replacing it with File.open. |

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class UsersController < ActionController::Base
22
CONSTANT = "constant"
3+
CONSTANT_WITH_FREEZE = "constant-with-freeze".freeze
34

45
def create
56
file = params[:file]
@@ -36,6 +37,10 @@ def create
3637

3738
IO.read(CONSTANT + file) # GOOD
3839

40+
IO.read(CONSTANT_WITH_FREEZE) # GOOD
41+
42+
IO.read(CONSTANT_WITH_FREEZE + file) # GOOD
43+
3944
open.where(external: false) # GOOD - an open method is called withoout arguments
4045

4146
open(file) # BAD - sanity check to verify that file was not mistakenly marked as sanitized

0 commit comments

Comments
 (0)