Skip to content

Commit 799ef4e

Browse files
committed
Add barrier guards for CLI injection
1 parent 4ecc78e commit 799ef4e

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

ql/lib/codeql/ruby/security/CommandInjectionQuery.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import ruby
1111
import codeql.ruby.TaintTracking
1212
import CommandInjectionCustomizations::CommandInjection
1313
import codeql.ruby.DataFlow
14+
import codeql.ruby.dataflow.BarrierGuards
1415

1516
/**
1617
* A taint-tracking configuration for reasoning about command-injection vulnerabilities.
@@ -23,4 +24,9 @@ class Configuration extends TaintTracking::Configuration {
2324
override predicate isSink(DataFlow::Node sink) { sink instanceof Sink }
2425

2526
override predicate isSanitizer(DataFlow::Node node) { node instanceof Sanitizer }
27+
28+
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
29+
guard instanceof StringConstCompare or
30+
guard instanceof StringConstArrayInclusionCall
31+
}
2632
}

ql/test/library-tests/frameworks/CommandExecution.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@
6262
Open3.pipeline_r("echo foo")
6363
Open3.pipeline_w("echo foo")
6464
Open3.pipeline_start("echo foo")
65-
Open3.pipeline("echo foo")
65+
Open3.pipeline("echo foo")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
queries/security/cwe-078/CommandInjection.ql
1+
queries/security/cwe-078/CommandInjection.ql

ql/test/query-tests/security/cwe-078/CommandInjection.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@ def create
77
system(cmd)
88
exec(cmd)
99
%x(echo #{cmd})
10+
1011
safe_cmd = Shellwords.escape(cmd)
1112
`echo #{safe_cmd}`
13+
14+
if cmd == "some constant"
15+
`echo #{cmd}`
16+
end
17+
18+
if %w(foo bar).include? cmd
19+
`echo #{cmd}`
20+
end
1221
end
1322

1423
def show
@@ -17,4 +26,4 @@ def show
1726
exec("ls")
1827
%x(ls)
1928
end
20-
end
29+
end

0 commit comments

Comments
 (0)