Skip to content

Commit

Permalink
Check for no action passed in argparse rule (#396)
Browse files Browse the repository at this point in the history
The default value for action when no value is passed is "store" which is
the exact value we are trying to detect for issues. So if a program
creates CLI arguments via add_argument with api-key or password arg and
unset action, it needs to surface this as an issue.

Signed-off-by: Eric Brown <eric.brown@securesauce.dev>
  • Loading branch information
ericwb committed Mar 28, 2024
1 parent a2a721d commit 97f9587
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion precli/rules/python/stdlib/argparse_sensitive_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def analyze_call(self, context: dict, call: Call) -> Result:
if (
"--password" in [arg0.value_str, arg1.value_str]
or "--api-key" in [arg0.value_str, arg1.value_str]
) and action.value_str == "store":
) and (action.value is None or action.value_str == "store"):
return Result(
rule_id=self.id,
location=Location(node=call.node),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# level: ERROR
# start_line: 13
# end_line: 17
# start_column: 0
# end_column: 1
import argparse


parser = argparse.ArgumentParser(
prog="ProgramName",
description="What the program does",
)
parser.add_argument(
"--api-key",
dest="api_key",
help="API key to connect to the server",
)
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def test_rule_meta(self):
@parameterized.expand(
[
"argparse_add_argument_api_key.py",
"argparse_add_argument_default_action.py",
"argparse_add_argument_password.py",
"argparse_add_argument_password_file.py",
"argparse_add_argument_password_store_true.py",
Expand Down

0 comments on commit 97f9587

Please sign in to comment.