skip to content

In a Rails 8.1 app, how do you run Brakeman, read its confidence levels, and record a reviewed false positive in config/brakeman.ignore?

level: seniorimportance: should knowfreq 35%

answer

  1. bin/brakeman adds --ensure-latest
  2. High, Medium, Weak confidence
  3. -w3 means high only
  4. -I writes fingerprints with notes
  5. fingerprint ignores line numbers

basics

~20 s

Run bin/brakeman, which Rails 8.1 generates with --ensure-latest prepended; each warning has a type and a High, Medium or Weak confidence; fix real findings, and record reviewed false positives with bin/brakeman -I, which writes fingerprints and notes to config/brakeman.ignore.

solid answer

~50 s

A new Rails 8.1 app ships `gem "brakeman"` and a `bin/brakeman` binstub that prepends `--ensure-latest`, so the scan fails whenever a newer Brakeman release exists, not only on new warnings. `config/ci.rb` runs it with `--quiet --no-pager --exit-on-warn --exit-on-error`. Each warning names a type, a location, the code and a confidence of High, Medium or Weak: how sure Brakeman is that input reaches the sink, not how severe the impact is. `-w` sets a minimum confidence (`-w3` shows High only); it does not filter by type. For a confirmed false positive, `bin/brakeman -I` lets you ignore it with a note, stored in `config/brakeman.ignore` as JSON keyed by a fingerprint. The fingerprint hashes the warning code, the code, its class/method or template location, the file and the confidence, but not the line, so moving code keeps it while rewriting the expression brings the warning back. `--ensure-ignore-notes` fails on ignores without a note.

code

bash · 5 lines
bash
bin/brakeman                       # full scan, fails if Brakeman is outdated
bin/brakeman -w3                   # only High-confidence warnings
bin/brakeman -I                    # review warnings, ignore some with notes
bin/brakeman --show-ignored        # include ignored warnings in the report
bin/brakeman --ensure-ignore-notes # fail if any ignore lacks a note

go deeper

for a junior

Know that Rails 8.1 apps include Brakeman, that bin/brakeman runs it, and that a warning names a type, a location and a confidence.

for a middle

Read a warning end to end, explain confidence versus severity, -w as a minimum confidence, and why bin/brakeman can fail on an outdated version alone.

for a senior

Run a triage process: trace inputs, fix or ignore with notes, enforce notes and obsolete-entry checks in CI, and understand why fingerprints change after refactors.

for a principal

Decide how scanner findings gate merges, who may approve ignores, and how ignored warnings are revisited so the file does not become a graveyard.

## What a new app ships Rails 8.1's generator adds Brakeman to the `development, test` group with `require: false` and writes `bin/brakeman`: ```ruby ARGV.unshift("--ensure-latest") load Gem.bin_path("brakeman", "brakeman") ``` It is wired into two places: - `config/ci.rb`, run by `bin/ci`: `bin/brakeman --quiet --no-pager --exit-on-warn --exit-on-error`. - The generated GitHub workflow's `scan_ruby` job: `bin/brakeman --no-pager`. Because of `--ensure-latest`, a CI run can fail on a day you changed nothing, simply because Brakeman released a new version. Since Brakeman 8.0.3, `--ensure-latest DAYS` (1 to 15) tolerates a release until it is that many days old; updating the gem is the usual response. ## Reading a warning Each warning carries: | Field | Meaning | |---|---| | Warning type | the class of bug, e.g. SQL Injection, Cross-Site Scripting, Redirect, Mass Assignment, Remote Code Execution | | Confidence | High, Medium or Weak: how certain Brakeman is that untrusted input reaches the sink | | Location | file, line, and the controller method or template | | Code | the expression it flagged, with user input highlighted | Confidence is not severity. A Weak SQL injection can be worse than a High one-line redirect; it only means Brakeman could not prove the input path. `-w LEVEL` sets the **minimum** confidence to report: `-w1` everything, `-w2` Medium and High, `-w3` High only. It never selects warning types; that is what `-t`/`-x` style check selection is for. Some type names surprise people. `params[:type].constantize` is reported as **Remote Code Execution** with the code `unsafe_constantize`, because instantiating an attacker-chosen class can run arbitrary initializers. The fix is an allowlist: `{ "car" => Car, "truck" => Truck }.fetch(params[:type])`. ## Triage steps 1. Trace the input: does the flagged value really come from the request, a cookie or a model attribute a user controls? 2. If yes, fix it; most Rails fixes are a bind value, an allowlist, an explicit permit list, `url_from`, or removing `html_safe`. 3. If Brakeman is wrong, for instance the value was mapped through a constant hash it cannot follow, record it as ignored **with a note** saying why. 4. Re-run and confirm the warning is gone from the default output but visible with `--show-ignored`. ## The ignore file `bin/brakeman -I` walks through the warnings interactively and saves the ones you mark to `config/brakeman.ignore`, the file Brakeman reads by default. Each entry is the warning's JSON plus a `note`, and the file records the Brakeman version. Matching uses the warning's **fingerprint**, a SHA-256 of: - the warning code; - the flagged code expression; - its location: class and method, or template; - the relative file path; - the confidence. The line number is not part of it, so adding lines above the code keeps the ignore valid. Rewriting the expression, renaming the method or moving the file produces a new fingerprint: the warning reappears and the old entry becomes obsolete. That is deliberate; a changed expression deserves a fresh look. Useful guards for CI: - `--ensure-ignore-notes` fails when an ignored warning has no note. - `--ensure-no-obsolete-ignore-entries` fails when an entry no longer matches anything. - `--compare old.json` reports new and fixed warnings against an earlier JSON report. ## Exit codes `--exit-on-warn` is on by default: a scan that reports any warning, ignored ones excepted, exits non-zero, which is what fails CI. `--exit-on-error` does the same for parse errors and crashes. `--no-exit-on-warn` turns a run into a report only, useful for a first scan of a legacy app before the backlog is triaged. ## In an interview The senior answer is the process: Brakeman runs on every push with a failing exit code, every ignore carries a reviewed note, obsolete entries are cleaned up, and confidence is used to order the work, not to skip Weak findings.

  • CI's Brakeman step failed but nobody changed code. What happened?
    The generated `bin/brakeman` prepends `--ensure-latest`, so the scan fails whenever a newer Brakeman version has been released. Update the gem with `bundle update brakeman`, or, if a short grace period is acceptable, pass a minimum age such as `--ensure-latest 7` in your CI command.
  • You ignored a warning last month and it is back after a refactor. Why?
    Ignore entries match the warning's fingerprint, which hashes the warning code, the flagged expression, its class and method or template, the file and the confidence. A refactor that changed any of those created a new fingerprint. Re-review the new code, ignore it again with a note if it is still safe, and remove the obsolete entry.
  • Does bin/brakeman -w3 restrict the scan to SQL injection and XSS?
    No. `-w` sets a minimum confidence: `-w3` shows only High-confidence warnings of every type. Selecting checks is a separate option. Using `-w3` in CI hides Medium and Weak findings, which can include real bugs Brakeman could not fully prove.

  • -w3 limits Brakeman to the three most severe warning types
  • Weak confidence means low severity, so it can be skipped
  • Ignores are keyed by file and line number
  • bin/brakeman fails only when new warnings appear
  • Ignore the unsafe constantize warning since constantize only loads classes

Assumes Rails 8.1 (bin/brakeman with --ensure-latest, config/ci.rb) and Brakeman 8.1; the optional 1-15 day minimum age for --ensure-latest arrived in Brakeman 8.0.3.