Rules / Ruby
SHIELD-RUBY-012
Unsafe deserialization via Marshal.load
What it detects
Marshal.load on untrusted data can instantiate arbitrary objects and execute code.
How to fix
Never deserialize untrusted data with Marshal; use JSON with a strict schema instead.
Vulnerable — Shield flags thissession_cache.rb
class SessionCache
def read(key)
raw = redis.get(key)
Marshal.load(raw) if raw
end
end
Fixed — scans cleansession_cache.rb
require "json"
class SessionCache
def read(key)
raw = redis.get(key)
JSON.parse(raw, symbolize_names: true) if raw
end
end
Both snippets are verified against the shipped scanner: the vulnerable one triggers SHIELD-RUBY-012, the fixed one does not.