Brakeman flags the use of unfiltered user data to select a Class or Method for dynamic dispatch via Object#send as a security risk. To mitigate this, avoid passing raw parameters directly as the method or target. Instead, use a whitelist to map user input to specific, safe symbols or classes.
Unsafe Patterns:
- Passing
params directly as the method name to send. - Using
params to dynamically constantize a class (e.g., via .classify.constantize) and then calling send on that class.
Safe Patterns:
- Using a conditional or a lookup table to select a specific method symbol based on user input.
- Using a conditional to select a specific Class based on user input.
- Passing user data as arguments to a method, provided the method itself is designed to handle potentially untrusted data safely.
# Unsafe use of method
method = params[:method]
@result = User.send(method.to_sym)
# Safe use of method (whitelisting)
method = params[:method] == 1 ? :method_a : :method_b
@result = User.send(method, *args)
# Unsafe use of target
table = params[:table]
model = table.classify.constantize
@result = model.send(:method)
# Safe use of target (whitelisting)
target = params[:target] == 1 ? Account : User
@result = target.send(:method, *args)
# Safe use of arguments (if the method handles data safely)
args = params["args"] || []
@result = User.send(:method, *args)