Skip to content

Commit 0832e29

Browse files
authored
move archive api path traversal tests to cwe-022
1 parent a0f1c86 commit 0832e29

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
class TestContoller < ActionController::Base
2+
3+
# this is vulnerable
4+
def upload
5+
untar params[:file], params[:filename]
6+
end
7+
8+
# this is vulnerable
9+
def unpload_zip
10+
unzip params[:file]
11+
end
12+
13+
# these are not vulnerable because of the string compare sanitizer
14+
def safe_upload_string_compare
15+
filename = params[:filename]
16+
if filename == "safefile.tar"
17+
untar params[:file], filename
18+
end
19+
end
20+
21+
def safe_upload_zip_string_compare
22+
filename = params[:filename]
23+
if filename == "safefile.zip"
24+
unzip filename
25+
end
26+
end
27+
28+
# these are not vulnerable beacuse of the string array compare sanitizer
29+
def safe_upload_string_array_compare
30+
filename = params[:filename]
31+
if ["safefile1.tar", "safefile2.tar"].include? filename
32+
untar params[:file], filename
33+
end
34+
end
35+
36+
def safe_upload_zip_string_array_compare
37+
filename = params[:filename]
38+
if ["safefile1.zip", "safefile2.zip"].include? filename
39+
unzip filename
40+
end
41+
end
42+
43+
# these are our two sinks
44+
def untar(io, destination)
45+
Gem::Package::TarReader.new io do |tar|
46+
tar.each do |tarfile|
47+
destination_file = File.join destination, tarfile.full_name
48+
49+
if tarfile.directory?
50+
FileUtils.mkdir_p destination_file
51+
else
52+
destination_directory = File.dirname(destination_file)
53+
FileUtils.mkdir_p destination_directory unless File.directory?(destination_directory)
54+
File.open destination_file, "wb" do |f|
55+
f.print tarfile.read
56+
end
57+
end
58+
end
59+
end
60+
end
61+
62+
def unzip(file)
63+
Zip::File.open(file) do |zip_file|
64+
zip_file.each do |entry|
65+
entry.extract
66+
end
67+
end
68+
end
69+
end

ruby/ql/test/query-tests/security/cwe-022/PathInjection.expected

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
edges
2+
| ArchiveApiPathTraversal.rb:5:26:5:31 | call to params : | ArchiveApiPathTraversal.rb:5:26:5:42 | ...[...] : |
3+
| ArchiveApiPathTraversal.rb:5:26:5:42 | ...[...] : | ArchiveApiPathTraversal.rb:44:17:44:27 | destination : |
4+
| ArchiveApiPathTraversal.rb:10:11:10:16 | call to params : | ArchiveApiPathTraversal.rb:10:11:10:23 | ...[...] : |
5+
| ArchiveApiPathTraversal.rb:10:11:10:23 | ...[...] : | ArchiveApiPathTraversal.rb:62:13:62:16 | file : |
6+
| ArchiveApiPathTraversal.rb:44:17:44:27 | destination : | ArchiveApiPathTraversal.rb:47:38:47:48 | destination : |
7+
| ArchiveApiPathTraversal.rb:47:28:47:67 | call to join : | ArchiveApiPathTraversal.rb:54:21:54:36 | destination_file |
8+
| ArchiveApiPathTraversal.rb:47:38:47:48 | destination : | ArchiveApiPathTraversal.rb:47:28:47:67 | call to join : |
9+
| ArchiveApiPathTraversal.rb:62:13:62:16 | file : | ArchiveApiPathTraversal.rb:63:20:63:23 | file |
210
| tainted_path.rb:4:12:4:17 | call to params : | tainted_path.rb:4:12:4:24 | ...[...] : |
311
| tainted_path.rb:4:12:4:24 | ...[...] : | tainted_path.rb:5:26:5:29 | path |
412
| tainted_path.rb:10:12:10:43 | call to absolute_path : | tainted_path.rb:11:26:11:29 | path |
@@ -26,6 +34,16 @@ edges
2634
| tainted_path.rb:59:40:59:45 | call to params : | tainted_path.rb:59:40:59:52 | ...[...] : |
2735
| tainted_path.rb:59:40:59:52 | ...[...] : | tainted_path.rb:59:12:59:53 | call to new : |
2836
nodes
37+
| ArchiveApiPathTraversal.rb:5:26:5:31 | call to params : | semmle.label | call to params : |
38+
| ArchiveApiPathTraversal.rb:5:26:5:42 | ...[...] : | semmle.label | ...[...] : |
39+
| ArchiveApiPathTraversal.rb:10:11:10:16 | call to params : | semmle.label | call to params : |
40+
| ArchiveApiPathTraversal.rb:10:11:10:23 | ...[...] : | semmle.label | ...[...] : |
41+
| ArchiveApiPathTraversal.rb:44:17:44:27 | destination : | semmle.label | destination : |
42+
| ArchiveApiPathTraversal.rb:47:28:47:67 | call to join : | semmle.label | call to join : |
43+
| ArchiveApiPathTraversal.rb:47:38:47:48 | destination : | semmle.label | destination : |
44+
| ArchiveApiPathTraversal.rb:54:21:54:36 | destination_file | semmle.label | destination_file |
45+
| ArchiveApiPathTraversal.rb:62:13:62:16 | file : | semmle.label | file : |
46+
| ArchiveApiPathTraversal.rb:63:20:63:23 | file | semmle.label | file |
2947
| tainted_path.rb:4:12:4:17 | call to params : | semmle.label | call to params : |
3048
| tainted_path.rb:4:12:4:24 | ...[...] : | semmle.label | ...[...] : |
3149
| tainted_path.rb:5:26:5:29 | path | semmle.label | path |
@@ -63,6 +81,8 @@ nodes
6381
| tainted_path.rb:60:26:60:29 | path | semmle.label | path |
6482
subpaths
6583
#select
84+
| ArchiveApiPathTraversal.rb:54:21:54:36 | destination_file | ArchiveApiPathTraversal.rb:5:26:5:31 | call to params : | ArchiveApiPathTraversal.rb:54:21:54:36 | destination_file | This path depends on $@. | ArchiveApiPathTraversal.rb:5:26:5:31 | call to params | a user-provided value |
85+
| ArchiveApiPathTraversal.rb:63:20:63:23 | file | ArchiveApiPathTraversal.rb:10:11:10:16 | call to params : | ArchiveApiPathTraversal.rb:63:20:63:23 | file | This path depends on $@. | ArchiveApiPathTraversal.rb:10:11:10:16 | call to params | a user-provided value |
6686
| tainted_path.rb:5:26:5:29 | path | tainted_path.rb:4:12:4:17 | call to params : | tainted_path.rb:5:26:5:29 | path | This path depends on $@. | tainted_path.rb:4:12:4:17 | call to params | a user-provided value |
6787
| tainted_path.rb:11:26:11:29 | path | tainted_path.rb:10:31:10:36 | call to params : | tainted_path.rb:11:26:11:29 | path | This path depends on $@. | tainted_path.rb:10:31:10:36 | call to params | a user-provided value |
6888
| tainted_path.rb:17:26:17:29 | path | tainted_path.rb:16:28:16:33 | call to params : | tainted_path.rb:17:26:17:29 | path | This path depends on $@. | tainted_path.rb:16:28:16:33 | call to params | a user-provided value |

0 commit comments

Comments
 (0)