timing-attack

A secret compared with input by ==.

Group
Code
Runs
Runs under ur scan: ur scan --rule timing-attack
Level
warning
Reads
Every file
Fix
None: the safe form depends on what the value is meant to be

Why

A password, digest, or token is compared with a value from a request by == or !=, which stop at the first byte that differs, so the time the check takes tells whoever sends the value how much of it was right, and they can learn the secret a byte at a time. Compare secrets with ActiveSupport::SecurityUtils.secure_compare or Rack::Utils.secure_compare, which take the same time whatever matches.

The fix, as Ur suggests it: compare it with secure_compare.

For agents

Never compare a secret with input by == or !=; use secure_compare.

Example

app/controllers/webhooks_controller.rb
class WebhooksController < ApplicationController
  def create
    head :forbidden and return if params[:token] != Setting.webhook_token
    head :ok
  end
end
What Ur prints
$ ur scan --rule timing-attack
warning[timing-attack]: `params`, a request parameter, is compared with the secret `webhook_token` by `!=`, which takes longer the more of it matches
  --> ./app/controllers/webhooks_controller.rb:3:35 method `create`
  = step 1: ./app/controllers/webhooks_controller.rb:3:35 `params`, a request parameter,
  = hint: compare it with secure_compare
  = see: CWE-208: Observable Timing Discrepancy, MITRE (2024-11-19), https://cwe.mitre.org/data/definitions/208.html
scanned 1 file (no framework recognized)
  timing-attack: 1 high, 0 medium
1 finding and 0 of medium confidence

Try it in the playground →

Source

CWE-208: Observable Timing Discrepancy, MITRE (2024-11-19).