CodeSampleX

Ejemplo

rack-protection 4.2.1: Show that `AuthenticityToken` defaults to global scope and is route-agnostic unless path and method are provided.

Muestra verificada para gem rack-protection 4.2.1: Show that `AuthenticityToken` defaults to global scope and is route-agnostic unless path and method are…

sha256:6914fb87c183aa8e2f7831611955f9a2ed775ed62c4e2e081cdf40bcd77b2124

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
2
Claves de firma que lo compilaron
2
Entorno declarado ruby linux x64 ruby ruby bundler

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
ruby 3 · linux debian/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · rubygems@1
2026-08-17
ruby 3 · linux debian/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · rubygems@1
2026-08-18

Caso

HOW
Objetivo
Show that `AuthenticityToken` defaults to global scope and is route-agnostic unless path and method are provided.
Paquetes
Símbolos
  • Rack::Protection::AuthenticityToken.token
Entorno
ruby
Creado
2026-08-17T02:44:55Z

Contrato

  1. Leaving `path` and `method` unspecified when calling `Rack::Protection::AuthenticityToken.token(session)` makes the token validate on any POST route, so it is global by default.
  2. assert repeated calls to `Rack::Protection::AuthenticityToken.token(session)` return different masked values for the same session, proving masking randomness and not a static raw token cache.
  3. assert a path-scoped token generated with `path` and `method` is rejected on a different path.

Archivos

  • Gemfile
  • Gemfile.lock
  • NOTES.md
  • csx.json
  • test/contract.rb

Descargar el artefacto de código fuente (tar.gz)

Código fuente

Gemfile
source "https://rubygems.org"

gem "rack-protection", "4.2.1"
gem "rack-session", "2.1.2"
gem "rack", "3.2.7"
Gemfile.lock
GEM
  remote: https://rubygems.org/
  specs:
    base64 (0.3.0)
    logger (1.7.0)
    rack (3.2.7)
    rack-protection (4.2.1)
      base64 (>= 0.1.0)
      logger (>= 1.6.0)
      rack (>= 3.0.0, < 4)
    rack-session (2.1.2)
      base64 (>= 0.1.0)
      rack (>= 3.0.0)

PLATFORMS
  ruby
  x64-mingw-ucrt

DEPENDENCIES
  rack (= 3.2.7)
  rack-protection (= 4.2.1)
  rack-session (= 2.1.2)

CHECKSUMS
  base64 (0.3.0) sha256=27337aeabad6ffae05c265c450490628ef3ebd4b67be58257393227588f5a97b
  bundler (4.0.18) sha256=02d9a17429de1847b4e0c9f27a9ee4b20c0a74c0a641b4e77195d6019e3618ac
  logger (1.7.0) sha256=196edec7cc44b66cfb40f9755ce11b392f21f7967696af15d274dde7edff0203
  rack (3.2.7) sha256=93e13e1c24f93556671d85d2d79fa228c3485815c50d7e2f265b5330c6528fb7
  rack-protection (4.2.1) sha256=cf6e2842df8c55f5e4d1a4be015e603e19e9bc3a7178bae58949ccbb58558bac
  rack-session (2.1.2) sha256=595434f8c0c3473ae7d7ac56ecda6cc6dfd9d37c0b2b5255330aa1576967ffe8

BUNDLED WITH
  4.0.18
NOTES.md
Search result check (`search_known_solution`) returned an EXACT cache hit for `pkg:gem/rack-protection@4.2.1` around `Rack::Protection::AuthenticityToken` (`sha256:5f76001b1fa263fb9245000306199bf74076f10301298827483ba4a6e9aa8a70`), while the Sinatra app trap is already covered elsewhere.

The wrong assumption fails silently: if you assume the default token is route-scoped, the same token from `/token` still gets a `200` on `/other` because the unset `path`/`method` option keeps the token global.
csx.json
{"case":{"believed":"Developers should expect `Rack::Protection::AuthenticityToken.token(session)` to produce a route-specific token by default.","caseId":"case:sha256:7ecc0bde6ef8e689a1018791550016f91ff3150886929700b8d6bfa720baaa84","contract":["Leaving `path` and `method` unspecified when calling `Rack::Protection::AuthenticityToken.token(session)` makes the token validate on any POST route, so it is global by default.","assert repeated calls to `Rack::Protection::AuthenticityToken.token(session)` return different masked values for the same session, proving masking randomness and not a static raw token cache.","assert a path-scoped token generated with `path` and `method` is rejected on a different path."],"goal":"Show that `AuthenticityToken` defaults to global scope and is route-agnostic unless path and method are provided.","kind":"HOW","packages":["pkg:gem/rack-protection@4.2.1","pkg:gem/rack-session@2.1.2","pkg:gem/rack@3.2.7"],"schemaVersion":1,"symbols":["Rack::Protection::AuthenticityToken.token"]},"contractCommand":["ruby","test/contract.rb"],"environment":{"arch":"x64","ecosystem":"gem","executionContext":"ruby","language":"ruby","os":"linux","packageManager":"bundler","runtime":"ruby","schemaVersion":1},"license":"MIT-0","packages":["pkg:gem/rack-protection@4.2.1","pkg:gem/rack-session@2.1.2","pkg:gem/rack@3.2.7"],"schemaVersion":1,"symbols":["Rack::Protection::AuthenticityToken.token"],"verifierAdapter":"rubygems@1"}
test/contract.rb
require "rack"
require "rack/mock"
require "rack/protection"
require "rack/session/cookie"

def contract
  app = Rack::Builder.new do
    use Rack::Session::Cookie,
      secret: "this-is-a-long-secret-key-used-only-for-test-purposes-that-meets-byte-requirements-1234567890",
      key: "rack.session"
    use Rack::Protection::AuthenticityToken
    run(proc do |env|
      request = Rack::Request.new(env)
      session = request.env["rack.session"]

      case request.path_info
      when "/token"
        token = Rack::Protection::AuthenticityToken.token(session)
        [200, {"content-type" => "text/plain"}, [token]]
      when "/scoped-token"
        token = Rack::Protection::AuthenticityToken.token(session, path: "/protected", method: :post)
        [200, {"content-type" => "text/plain"}, [token]]
      when "/protected", "/other"
        [200, {"content-type" => "text/plain"}, ["ok"]]
      else
        [404, {"content-type" => "text/plain"}, ["not found"]]
      end
    end)
  end.to_app

  request = Rack::MockRequest.new(app)
  first = request.get("/token")
  cookie = first["set-cookie"].split(";", 2).first

  assert(!cookie.nil? && !cookie.empty?, "session cookie was issued")

  first_token = first.body
  second = request.get("/token", "HTTP_COOKIE" => cookie)
  second_token = second.body

  assert(first_token != second_token, "masked tokens are not stable across calls when options are unset")
  assert(request.post("/protected", "HTTP_COOKIE" => cookie, :input => "authenticity_token=#{first_token}").status == 200, "default token works on protected path")
  assert(request.post("/other", "HTTP_COOKIE" => cookie, :input => "authenticity_token=#{first_token}").status == 200, "default token unexpectedly works on a different path")

  scoped_token = request.get("/scoped-token", "HTTP_COOKIE" => cookie).body
  assert(request.post("/protected", "HTTP_COOKIE" => cookie, :input => "authenticity_token=#{scoped_token}").status == 200, "scoped token works on its target path")
  assert(request.post("/other", "HTTP_COOKIE" => cookie, :input => "authenticity_token=#{scoped_token}").status == 403, "scoped token is rejected on wrong path")
end

def assert(condition, message)
  raise "Contract failed: #{message}" unless condition
end

contract
puts "contract passed"

Seeder de origen

csx-seed