Skip to content

Commit

Permalink
Add more negative patterns to exclude cases of reading from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
navhits committed Feb 10, 2024
1 parent b7c74f4 commit d4f6f8f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
22 changes: 21 additions & 1 deletion ruby/lang/security/bad-deserialization-yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ def bad_deserialization
data = YAML.dump(o)
# ruleid: bad-deserialization-yaml
obj = YAML.load(data)

end

def ok_deserialization
Expand All @@ -20,4 +19,25 @@ def ok_deserialization

# ok: bad-deserialization-yaml
YAML.load(File.read("test.txt"))

# ok: bad-deserialization-yaml
obj = YAML::load(ERB.new(File.read("test.yml")).result)

# ok: bad-deserialization-yaml
obj = YAML::load(ERB.new(File.read("test.yml")))

template = ERB.new(File.read("test.yml"))
# ok: bad-deserialization-yaml
obj = YAML::load(template)

template = ERB.new(File.read("test.yml")).result
# ok: bad-deserialization-yaml
obj = YAML::load(template)

template = ERB.new(File.read("test.yml"))
# ok: bad-deserialization-yaml
obj = YAML::load(template.result)

# ok: bad-deserialization-yaml
obj = YAML.load(File.read(File.join(Pathname.pwd, "hello.yml")))
end
18 changes: 15 additions & 3 deletions ruby/lang/security/bad-deserialization-yaml.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@ rules:
- pattern-not: |
YAML.load("...", ...)
- pattern-not-inside: |
$FILE = File.read("...", ...)
YAML.load(..., File.read(...), ...)
- pattern-not-inside: |
$FILE = File.read(...)
...
YAML.load(..., $FILE, ...)
- pattern-not-inside: |
$FILENAME = "..."
$FILENAME = ...
...
$FILE = File.read($FILENAME, ...)
...
YAML.load(..., $FILE, ...)
- pattern-not-inside: |
YAML.load(..., File.read("...", ...), ...)
YAML.load(..., $X.$Y(File.read(...)), ...)
- pattern-not-inside: |
YAML.load(..., $X.$Y(File.read(...)).$Z, ...)
- pattern-not-inside: |
$T = $MOD.$MET(File.read(...))
...
YAML.load(..., $T, ...)
- pattern-not-inside: |
$T = $MOD.$MET(File.read(...))
...
YAML.load(..., $T.$R, ...)
fix: Psych.safe_load($...ARGS)
message: >-
Unsafe deserialization from YAML. Objects in Ruby can be serialized into strings,
Expand Down

0 comments on commit d4f6f8f

Please sign in to comment.