Skip to content

Commit 99b9078

Browse files
committed
add .shellescape as a sanitizer for rb/command-injection
1 parent b16b3c0 commit 99b9078

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ module CommandInjection {
4949
class ShellwordsEscapeAsSanitizer extends Sanitizer {
5050
ShellwordsEscapeAsSanitizer() {
5151
this = API::getTopLevelMember("Shellwords").getAMethodCall(["escape", "shellescape"])
52+
or
53+
// The method is also added as `String#shellescape`.
54+
this.(DataFlow::CallNode).getMethodName() = "shellescape"
5255
}
5356
}
5457
}

ruby/ql/test/query-tests/security/cwe-078/CommandInjection/CommandInjection.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ edges
1515
| CommandInjection.rb:81:20:81:25 | **args : | CommandInjection.rb:82:22:82:25 | args : |
1616
| CommandInjection.rb:82:22:82:25 | args : | CommandInjection.rb:82:22:82:37 | ...[...] : |
1717
| CommandInjection.rb:82:22:82:37 | ...[...] : | CommandInjection.rb:82:14:82:39 | "echo #{...}" |
18+
| CommandInjection.rb:94:16:94:21 | call to params : | CommandInjection.rb:94:16:94:28 | ...[...] : |
19+
| CommandInjection.rb:94:16:94:28 | ...[...] : | CommandInjection.rb:95:16:95:28 | "cat #{...}" |
1820
nodes
1921
| CommandInjection.rb:6:15:6:20 | call to params : | semmle.label | call to params : |
2022
| CommandInjection.rb:6:15:6:26 | ...[...] : | semmle.label | ...[...] : |
@@ -37,6 +39,9 @@ nodes
3739
| CommandInjection.rb:82:14:82:39 | "echo #{...}" | semmle.label | "echo #{...}" |
3840
| CommandInjection.rb:82:22:82:25 | args : | semmle.label | args : |
3941
| CommandInjection.rb:82:22:82:37 | ...[...] : | semmle.label | ...[...] : |
42+
| CommandInjection.rb:94:16:94:21 | call to params : | semmle.label | call to params : |
43+
| CommandInjection.rb:94:16:94:28 | ...[...] : | semmle.label | ...[...] : |
44+
| CommandInjection.rb:95:16:95:28 | "cat #{...}" | semmle.label | "cat #{...}" |
4045
subpaths
4146
#select
4247
| CommandInjection.rb:7:10:7:15 | #{...} | CommandInjection.rb:6:15:6:20 | call to params : | CommandInjection.rb:7:10:7:15 | #{...} | This command depends on a $@. | CommandInjection.rb:6:15:6:20 | call to params | user-provided value |
@@ -51,3 +56,4 @@ subpaths
5156
| CommandInjection.rb:65:14:65:29 | "echo #{...}" | CommandInjection.rb:64:18:64:23 | number : | CommandInjection.rb:65:14:65:29 | "echo #{...}" | This command depends on a $@. | CommandInjection.rb:64:18:64:23 | number | user-provided value |
5257
| CommandInjection.rb:73:14:73:34 | "echo #{...}" | CommandInjection.rb:72:23:72:33 | blah_number : | CommandInjection.rb:73:14:73:34 | "echo #{...}" | This command depends on a $@. | CommandInjection.rb:72:23:72:33 | blah_number | user-provided value |
5358
| CommandInjection.rb:82:14:82:39 | "echo #{...}" | CommandInjection.rb:81:20:81:25 | **args : | CommandInjection.rb:82:14:82:39 | "echo #{...}" | This command depends on a $@. | CommandInjection.rb:81:20:81:25 | **args | user-provided value |
59+
| CommandInjection.rb:95:16:95:28 | "cat #{...}" | CommandInjection.rb:94:16:94:21 | call to params : | CommandInjection.rb:95:16:95:28 | "cat #{...}" | This command depends on a $@. | CommandInjection.rb:94:16:94:21 | call to params | user-provided value |

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,13 @@ def foo(arg)
8888
end
8989
end
9090
end
91+
92+
class Foo < ActionController::Base
93+
def create
94+
file = params[:file]
95+
system("cat #{file}")
96+
# .shellescape
97+
system("cat #{file.shellescape}") # OK, because file is shell escaped
98+
99+
end
100+
end

0 commit comments

Comments
 (0)