Fix XSS in Ruby on Rails
========================

Let ERB escape: <%= @name %> and tag helpers such as image_tag(..., alt:
@name) escape & < > " and ', unless the value is html_safe. Never call raw,
html_safe or <%== on user input; for rich text use sanitize with an allowlist.
Link only http and https URLs: escaping does not stop javascript:.

Page: https://softwaresecurity.com/fix/xss/ruby-rails/

What is in this archive
-----------------------
xss/ruby-rails/   the project: three implementations of the same code and one test suite
  app/views/profiles/vulnerable.html.erb   (vulnerable)
  app/views/profiles/fixed.html.erb   (fixed)
  app/views/profiles/wrong.html.erb   (a plausible fix that still fails)
run-all.mjs   runs the tests in all three modes and checks the results (Node.js and Docker)
LICENSE-CODE.txt

Run it
------
  Tested with Ruby 4.0.7. run-all.mjs needs Node.js 22 or later. Supported
  versions: https://softwaresecurity.com/fix/versions/

  From the xss/ruby-rails folder, install the dependencies once (this needs
  network access):

    BUNDLE_PATH=vendor/bundle BUNDLE_FROZEN=true bundle install

  Then run the tests. IMPL chooses the implementation the tests load:

    # every test passes
    BUNDLE_PATH=vendor/bundle IMPL=fixed bundle exec ruby -Itest test/profile_test.rb
    # an exploit test fails
    BUNDLE_PATH=vendor/bundle IMPL=vulnerable bundle exec ruby -Itest test/profile_test.rb
    # a test fails
    BUNDLE_PATH=vendor/bundle IMPL=wrong bundle exec ruby -Itest test/profile_test.rb

  Without Ruby installed, Docker runs the same commands in the image the card
  was verified in, pinned by its digest (run them from the xss/ruby-rails
  folder; the named volumes keep the dependency and build caches between runs,
  so the test runs need no network):

    docker run --rm --init \
      -v "$PWD":/card -w /card \
      -e BUNDLE_PATH=vendor/bundle \
      ruby:4.0.7-trixie@sha256:0192d5244fa068587325c3ef4f886fdb2d6775689757d598f9a5261d0f86d40e \
      sh -c 'BUNDLE_PATH=vendor/bundle BUNDLE_FROZEN=true bundle install'

    # every test passes
    docker run --rm --init --network=none \
      -v "$PWD":/card -w /card \
      -e IMPL=fixed -e BUNDLE_PATH=vendor/bundle \
      ruby:4.0.7-trixie@sha256:0192d5244fa068587325c3ef4f886fdb2d6775689757d598f9a5261d0f86d40e \
      sh -c 'bundle exec ruby -Itest test/profile_test.rb'
    # an exploit test fails
    docker run --rm --init --network=none \
      -v "$PWD":/card -w /card \
      -e IMPL=vulnerable -e BUNDLE_PATH=vendor/bundle \
      ruby:4.0.7-trixie@sha256:0192d5244fa068587325c3ef4f886fdb2d6775689757d598f9a5261d0f86d40e \
      sh -c 'bundle exec ruby -Itest test/profile_test.rb'
    # a test fails
    docker run --rm --init --network=none \
      -v "$PWD":/card -w /card \
      -e IMPL=wrong -e BUNDLE_PATH=vendor/bundle \
      ruby:4.0.7-trixie@sha256:0192d5244fa068587325c3ef4f886fdb2d6775689757d598f9a5261d0f86d40e \
      sh -c 'bundle exec ruby -Itest test/profile_test.rb'

  The JUnit report is written to reports/TEST-*.xml. To run all three modes and
  check the results in one step, run the harness from this top folder (it needs
  only Node.js and Docker: it runs the commands in the container; --install runs
  the install step first):

    node run-all.mjs --install xss/ruby-rails
    node run-all.mjs xss/ruby-rails

Verified
--------
  11 October 2026 on Debian 13 (trixie) in Docker, on macOS 27.0 (Darwin 27.0.0,
  arm64), with ruby 4.0.7 (ruby:4.0.7-trixie image, linux/arm64), bundler
  4.0.20, rails 8.1.4, minitest 6.0.6, minitest-reporters 1.8.0, nokogiri
  1.19.4, semgrep 1.180.0, brakeman 8.1.0. Versions and results hold for that
  date: run the tests with your own versions before relying on the fix.

Licence
-------
  The code is MIT licensed (LICENSE-CODE.txt). The page text is CC BY 4.0.
