Rules / Ruby
SHIELD-RUBY-020
Insecure randomness for tokens
What it detects
Using rand or Random for security tokens produces predictable values.
How to fix
Generate tokens with SecureRandom.hex or SecureRandom.uuid.
Vulnerable — Shield flags thisapp/models/password_reset.rb
class PasswordReset
def generate_token
self.token = rand(10**20).to_s
end
end
Fixed — scans cleanapp/models/password_reset.rb
require "securerandom"
class PasswordReset
def generate_token
self.token = SecureRandom.hex(32)
end
end
Both snippets are verified against the shipped scanner: the vulnerable one triggers SHIELD-RUBY-020, the fixed one does not.