Skip to content

Commit 1c2a4b7

Browse files
committed
🔒 Replace mktemp with NamedTemporaryFile (CWE-377)
1 parent 998c246 commit 1c2a4b7

1 file changed

Lines changed: 40 additions & 37 deletions

File tree

indi_aws/aws_utils.py

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -431,49 +431,52 @@ def test_bucket_access(creds_path, output_directory):
431431
"""
432432

433433
# Import packages
434+
import logging
434435
import os
435-
import tempfile
436+
from tempfile import NamedTemporaryFile
436437

437-
import botocore.exceptions as bexc
438+
from botocore.exceptions import ClientError
439+
from boto3.exceptions import S3UploadFailedError
438440
from indi_aws import fetch_creds
439441

440442
# Init variables
441443
s3_str = 's3://'
442-
test_file = tempfile.mktemp()
443-
444-
# Explicitly lower-case the "s3"
445-
if output_directory.lower().startswith(s3_str):
446-
out_dir_sp = output_directory.split('/')
447-
out_dir_sp[0] = out_dir_sp[0].lower()
448-
output_directory = '/'.join(out_dir_sp)
449-
450-
# Get bucket name
451-
bucket_name = output_directory.replace(s3_str, '').split('/')[0]
452-
453-
# Get bucket
454-
bucket = fetch_creds.return_bucket(creds_path, bucket_name)
455-
456-
# Create local file
457-
with open(test_file, 'w') as f:
458-
f.write('test123')
459-
f.close()
460-
461-
# Formulate test ouput key in bucket path output directory
462-
rel_key_path = output_directory.replace(
463-
os.path.join(s3_str, bucket_name), '').lstrip('/')
464-
write_test_key = os.path.join(rel_key_path, os.path.basename(test_file))
465-
466-
# Attempt a write to bucket
467-
try:
468-
bucket.upload_file(test_file, write_test_key)
469-
print('S3 write access confirmed!')
470-
test_key = bucket.Object(key=write_test_key)
471-
test_key.delete()
472-
s3_write_access = True
473-
# Otherwise we set the access flag to false
474-
except bexc.ClientError:
475-
print('S3 write access is not available!')
476-
s3_write_access = False
444+
with NamedTemporaryFile('w', encoding='utf8') as _temp_file:
445+
test_file = _temp_file.name
446+
447+
# Explicitly lower-case the "s3"
448+
if output_directory.lower().startswith(s3_str):
449+
out_dir_sp = output_directory.split('/')
450+
out_dir_sp[0] = out_dir_sp[0].lower()
451+
output_directory = '/'.join(out_dir_sp)
452+
453+
# Get bucket name
454+
bucket_name = output_directory.replace(s3_str, '').split('/')[0]
455+
456+
# Get bucket
457+
bucket = fetch_creds.return_bucket(creds_path, bucket_name)
458+
459+
# Create local file
460+
with open(test_file, 'w') as f:
461+
f.write('test123')
462+
f.close()
463+
464+
# Formulate test ouput key in bucket path output directory
465+
rel_key_path = output_directory.replace(
466+
os.path.join(s3_str, bucket_name), '').lstrip('/')
467+
write_test_key = os.path.join(rel_key_path, os.path.basename(test_file))
468+
469+
# Attempt a write to bucket
470+
try:
471+
bucket.upload_file(test_file, write_test_key)
472+
logging.info('S3 write access confirmed!')
473+
test_key = bucket.Object(key=write_test_key)
474+
test_key.delete()
475+
s3_write_access = True
476+
# Otherwise we set the access flag to false
477+
except (ClientError, S3UploadFailedError) as exc:
478+
logging.warning('S3 write access is not available!')
479+
s3_write_access = False
477480

478481
# Return the access flag
479482
return s3_write_access

0 commit comments

Comments
 (0)