Skip to content

Find the holes before someone else does. A fast, 100% Rust vulnerability scanner: XSS, SQL injection, leaked secrets, dependency CVEs and AI agent setups.

fix(code): flag yaml.unsafe_load, yaml.load_all, YAML.unsafe_load and Marshal.restore (#64)

Ahmad Ghozalia7789db64b17
diff --git a/src/rules/code.rs b/src/rules/code.rs
index 3963537..a03fb82 100644
--- a/src/rules/code.rs
+++ b/src/rules/code.rs
@@ -418,11 +418,25 @@ pub static RULES: &[Rule] = &[
"A template is built from request input, so users can run code in the template engine (SSTI).",
"Render fixed template files and pass user input only as variables."
),
+ // Split from yaml.load/load_all below: `unless` is matched against the
+ // whole line, so a SafeLoader call on the same line would otherwise hide
+ // an unsafe call (e.g. `yaml.unsafe_load(x); yaml.load(y,
+ // Loader=yaml.SafeLoader)`). These calls have no safe-loader form, so
+ // they need no exception.
rule!(
"code.unsafe-deserialization",
High,
py,
- r"\bpickle\.loads?\s*\(|\byaml\.load\s*\(|\bmarshal\.loads?\s*\(",
+ r"\bpickle\.loads?\s*\(|\byaml\.unsafe_load\s*\(|\bmarshal\.loads?\s*\(",
+ None,
+ DESER_MSG,
+ DESER_FIX
+ ),
+ rule!(
+ "code.unsafe-deserialization",
+ High,
+ py,
+ r"\byaml\.load\s*\(|\byaml\.load_all\s*\(",
Some(r"Loader\s*=\s*(?:yaml\.)?C?SafeLoader"),
DESER_MSG,
DESER_FIX
@@ -449,8 +463,12 @@ pub static RULES: &[Rule] = &[
"code.unsafe-deserialization",
High,
rb,
- r"\bMarshal\.load\s*\(|\bYAML\.load\s*\(",
- Some(r"permitted_classes|safe_load"),
+ r"\bMarshal\.load\s*\(|\bMarshal\.restore\s*\(|\bYAML\.load\s*\(|\bYAML\.unsafe_load\s*\(",
+ // No safe_load exception: the pattern never matches YAML.safe_load,
+ // so keeping it here only let a safe call on the same line hide an
+ // unsafe one. permitted_classes stays: YAML.load with an allow list
+ // is the safe form.
+ Some(r"permitted_classes"),
DESER_MSG,
DESER_FIX
),
diff --git a/tests/fixtures/code/EXPECTED b/tests/fixtures/code/EXPECTED
index 292b49f..92ecfdf 100644
--- a/tests/fixtures/code/EXPECTED
+++ b/tests/fixtures/code/EXPECTED
@@ -23,6 +23,9 @@ api/users_controller.rb:1 code.sql-injection
api/users_controller.rb:3 code.command-injection
api/users_controller.rb:4 code.unsafe-deserialization
api/users_controller.rb:5 code.xss-raw-html
+api/users_controller.rb:6 code.unsafe-deserialization
+api/users_controller.rb:7 code.unsafe-deserialization
+api/users_controller.rb:9 code.unsafe-deserialization
api/views.py:1 code.sql-injection
api/views.py:10 code.unsafe-deserialization
api/views.py:13 code.tls-verify-off
@@ -34,6 +37,9 @@ api/views.py:18 code.ssrf
api/views.py:19 code.eval
api/views.py:2 code.sql-injection
api/views.py:20 code.template-injection
+api/views.py:21 code.unsafe-deserialization
+api/views.py:22 code.unsafe-deserialization
+api/views.py:25 code.unsafe-deserialization
api/views.py:4 code.command-injection
api/views.py:7 code.command-injection
api/views.py:9 code.unsafe-deserialization
diff --git a/tests/fixtures/code/api/users_controller.rb b/tests/fixtures/code/api/users_controller.rb
index 8747eff..b576805 100644
--- a/tests/fixtures/code/api/users_controller.rb
+++ b/tests/fixtures/code/api/users_controller.rb
@@ -3,3 +3,8 @@ User.where(name: params[:name])
system("convert #{params[:file]} out.png")
obj = Marshal.load(data)
<%= raw @comment.body %>
+obj = YAML.unsafe_load(data)
+obj = Marshal.restore(data)
+obj = YAML.safe_load(data)
+a = YAML.unsafe_load(data); b = YAML.safe_load(other)
+a = YAML.load(data, permitted_classes: [Date])
diff --git a/tests/fixtures/code/api/views.py b/tests/fixtures/code/api/views.py
index 4974d06..afe9b29 100644
--- a/tests/fixtures/code/api/views.py
+++ b/tests/fixtures/code/api/views.py
@@ -18,3 +18,8 @@ return open(request.args.get('path')).read()
r = requests.get(request.args['url'])
result = eval(expr)
tpl = render_template_string(request.form['tpl'])
+data = yaml.unsafe_load(payload)
+docs = yaml.load_all(stream)
+docs = yaml.load_all(stream, Loader=yaml.SafeLoader)
+cfg = yaml.safe_load(stream)
+a = yaml.unsafe_load(payload); b = yaml.load(other, Loader=yaml.SafeLoader)