Skip to content

Commit 8f2f928

Browse files
Fridayai700claude
andauthored
Fix B615 false positive when revision is set via variable (#1358)
Fix B615 false positive when revision is a variable or expression When `revision` or `commit_id` is passed as a variable, attribute, subscript, or other non-literal expression, `get_call_arg_value()` cannot resolve the actual value. This caused false positives because the check treated unresolvable values as missing. Inspect the raw AST keywords before calling `get_call_arg_value()`. If a `revision` or `commit_id` keyword has a non-Constant value (Name, Attribute, Subscript, Call, etc.), give the user the benefit of the doubt and suppress the warning. Fixes #1345 Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e27493f commit 8f2f928

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

bandit/plugins/huggingface_unsafe_download.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
.. versionadded:: 1.8.6
6060
6161
"""
62+
import ast
6263
import string
6364

6465
import bandit
@@ -113,7 +114,19 @@ def huggingface_unsafe_download(context):
113114
if not any(module in qualname_parts for module in required_modules):
114115
return
115116

116-
# Check for revision parameter (the key security control)
117+
# Check for revision parameter (the key security control).
118+
# First, check the raw AST to see if a revision/commit_id keyword was
119+
# passed as a non-literal expression (variable, attribute, subscript,
120+
# function call, etc.). In those cases we cannot statically determine
121+
# the value, so we give the user the benefit of the doubt.
122+
call_node = context._context.get("call")
123+
if call_node is not None:
124+
for kw in getattr(call_node, "keywords", []):
125+
if kw.arg in ("revision", "commit_id") and not isinstance(
126+
kw.value, ast.Constant
127+
):
128+
return
129+
117130
revision_value = context.get_call_arg_value("revision")
118131
commit_id_value = context.get_call_arg_value("commit_id")
119132

examples/huggingface_unsafe_download.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,18 @@
147147
repo_id="org/model_name",
148148
revision="5d0f2e8a7f1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d"
149149
)
150+
151+
152+
# Example #24: Revision passed as a variable (can't be statically checked)
153+
MODEL_REVISION = "548fc3543a"
154+
safe_model_variable = AutoModel.from_pretrained(
155+
"org/model_name",
156+
revision=MODEL_REVISION
157+
)
158+
159+
# Example #25: Revision from a dict/subscript access
160+
config = {"revision": "abc1234567"}
161+
safe_model_subscript = AutoModel.from_pretrained(
162+
"org/model_name",
163+
revision=config["revision"]
164+
)

0 commit comments

Comments
 (0)