Skip to content

Commit 2ddca2c

Browse files
committed
Document and test YAML.safe_load
1 parent 760dbd7 commit 2ddca2c

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

ql/src/queries/security/cwe-502/UnsafeDeserialization.qhelp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ are capable of deserializing to arbitrary objects, this is inherently unsafe.
2727
</p>
2828
<sample src="examples/UnsafeDeserializationBad.rb"/>
2929
<p>
30-
Using <code>YAML.parse</code> instead, with the default options, removes the
31-
vulnerability.
30+
Using <code>JSON.parse</code> and <code>YAML.safe_load</code> instead, as in the
31+
following example, removes the vulnerability. Note that there is no safe way to
32+
deserialize untrusted data using <code>Marshal</code>.
3233
</p>
3334
<sample src="examples/UnsafeDeserializationGood.rb"/>
3435
</example>

ql/src/queries/security/cwe-502/examples/UnsafeDeserializationGood.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ def safe_json_example
55
object = JSON.parse params[:json]
66
# ...
77
end
8+
9+
def safe_yaml_example
10+
object = YAML.safe_load params[:yaml]
11+
# ...
12+
end
813
end

ql/test/query-tests/security/cwe-502/UnsafeDeserialization.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,10 @@ def route5
3838
yaml_data = params[:key]
3939
object = YAML.load yaml_data
4040
end
41+
42+
# GOOD
43+
def route6
44+
yaml_data = params[:key]
45+
object = YAML.safe_load yaml_data
46+
end
4147
end

0 commit comments

Comments
 (0)