Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22af6f5

Browse files
committed
Restrict download_file() to boto3 lib
1 parent 2d38993 commit 22af6f5

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

python/ql/src/experimental/Security/CWE-022bis/UnsafeUnpack.ql

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ class UnsafeUnpackingConfig extends TaintTracking::Configuration {
3636
source.(AttrRead).accesses(o, any(string s))
3737
)
3838
or
39-
// A source catching a S3 filename download
40-
exists(API::Node s3 | source = s3.getMember("download_file").getACall().getArg(2))
39+
// A source catching an S3 filename download
40+
// see boto3: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.download_file
41+
exists(MethodCallNode mcn, Node s3, Node bc |
42+
bc = API::moduleImport("boto3").getMember("client").getACall() and
43+
bc = s3.getALocalSource() and
44+
mcn.calls(s3, "download_file") and
45+
source = mcn.getArg(2)
46+
)
4147
or
4248
// A source download a file using wget
4349
exists(MethodCallNode mcn |

python/ql/test/experimental/query-tests/Security/CWE-022/UnsafeUnpack.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,17 @@
5454
if unpack_path:
5555
shutil.unpack_archive(to_path, unpack_path) # $result=BAD
5656
to_path = unpack_path
57+
58+
59+
# A source catching an S3 filename download
60+
# see boto3: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.download_file
61+
import boto3
62+
63+
remote_ziped_name = "remote_name.tar.gz"
64+
base_dir = "/tmp/basedir"
65+
local_ziped_path = os.path.join(base_dir, remote_ziped_name)
66+
bucket_name = "mybucket"
67+
68+
s3 = boto3.client('s3')
69+
s3.download_file(bucket_name, remote_ziped_name, local_ziped_path)
70+
shutil.unpack_archive(local_ziped_path, base_dir) # $result=BAD

0 commit comments

Comments
 (0)