Since the pre-commit hook does not allow even warnings and commits issued by bandit, I need to find a way to execute bash commands from python scripts without bandit complaining.
Using the subprocess python package, bandit has always complained so far, no matter what I did. I used ".run()", ".check_call()", ".Popen()", .. all without shell=True and yet there's no avail.
If there is a secure alternative to subprocess, I'd also be interested, but I'm sure it must work somehow with subprocess as well.
Example which is not accepted by bandit:
import shlex
import subprocess
...
bash_command = (
f'aws s3 cp {source_dir} s3://{target_bucket_name} --recursive'
f' --profile {profile_name}')
subprocess.check_call(shlex.split(bash_command), text=True)

In order for the code to be secure, you need to know that
source_dirtarget_bucket_nameprofile_namearen't malicious: e.g. can an untrusted user pass.sshas the value to be copied?Once you know the subprocess line is secure, you can add
# noseccomment to tell bandit not to give a warning about the line:(The command
aws s3 ...running insubprocess.check_callisn't running in a bash shell, which might confuse people reading the question. Python will directly start theawsprocess, passing arguments.)