Fix server-side request forgery in Ruby on Rails
================================================

Attach the policy to the connection, not the host name string. Allow only http
and https on ports 80 and 443 without credentials; read IP literals with
IPAddr, refuse other IPv4 notations, resolve names and reject any non-public
address; pin the connection with Net::HTTP#ipaddr=; vet every redirect;
disable the proxy and cap time and bytes.

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

What is in this archive
-----------------------
ssrf/ruby-rails/   the project: three implementations of the same code and one test suite
  app/fetch/url_fetch/vulnerable.rb   (vulnerable)
  app/fetch/url_fetch/fixed.rb   (fixed)
  app/fetch/url_fetch/wrong.rb   (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 ssrf/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/fetch_test.rb
    # an exploit test fails
    BUNDLE_PATH=vendor/bundle IMPL=vulnerable bundle exec ruby -Itest test/fetch_test.rb
    # a test fails
    BUNDLE_PATH=vendor/bundle IMPL=wrong bundle exec ruby -Itest test/fetch_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 ssrf/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/fetch_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/fetch_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/fetch_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 ssrf/ruby-rails
    node run-all.mjs ssrf/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), net-http
  0.9.1, ipaddr 1.2.8, bundler 4.0.20, rails 8.1.4, minitest 6.0.6,
  minitest-reporters 1.8.0, 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.
