|
18 | 18 | import codeql.ruby.security.KernelOpenQuery |
19 | 19 | import codeql.ruby.AST |
20 | 20 | import codeql.ruby.ApiGraphs |
| 21 | +import codeql.ruby.DataFlow |
21 | 22 |
|
22 | 23 | from AmbiguousPathCall call |
23 | 24 | where |
24 | | - not hasConstantPrefix(call.getPathArgument().getALocalSource().asExpr().getExpr()) and |
| 25 | + call.getNumberOfArguments() > 0 and |
| 26 | + not hasConstantPrefix(call.getPathArgument()) and |
25 | 27 | not call.getPathArgument().getALocalSource() = |
26 | 28 | API::getTopLevelMember("File").getAMethodCall("join") |
27 | 29 | select call, |
28 | 30 | "Call to " + call.getName() + " with a non-constant value. Consider replacing it with " + |
29 | 31 | call.getReplacement() + "." |
30 | 32 |
|
31 | | -predicate hasConstantPrefix(Expr e) { |
| 33 | +predicate hasConstantPrefix(DataFlow::Node node) { |
| 34 | + hasConstantPrefix(node.getALocalSource()) |
| 35 | + or |
32 | 36 | // if it's a format string, then the first argument is not a constant string |
33 | | - e.(StringlikeLiteral).getComponent(0) instanceof StringTextComponent |
| 37 | + node.asExpr().getExpr().(StringlikeLiteral).getComponent(0) instanceof StringTextComponent |
34 | 38 | or |
35 | 39 | // it is not a constant string argument |
36 | | - exists(e.getConstantValue()) |
| 40 | + exists(node.getConstantValue()) |
37 | 41 | or |
38 | 42 | // not a concatenation that starts with a constant string |
39 | | - 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 | + exists(DataFlow::CallNode call | node = call and call.getMethodName() = "freeze" | |
| 50 | + hasConstantPrefix(call.getReceiver()) |
| 51 | + ) |
| 52 | + or |
| 53 | + // is a constant read of a constant string |
| 54 | + exists(DataFlow::Node constant | |
| 55 | + constant.asExpr().getExpr() = node.asExpr().getExpr().(ConstantReadAccess).getValue() and |
| 56 | + hasConstantPrefix(constant) |
| 57 | + ) |
40 | 58 | } |
0 commit comments