Skip to content

Commit

Permalink
Fix inefficient assert pattern in Rack::Lint
Browse files Browse the repository at this point in the history
Partial backport of 1970771

This fixes an issue encountered when adding `Rack::Lint` to the Rails
test suite. Rails puts a lazily evaluated class inside the request env
that has the potential to raise when converted to a string. Since this
assertion in `Rack::Lint` calls `#inspect` on `env` whether or not `env`
is a `Hash`, the lazily evaluated class would raise at that point and
prevent the rest of `Rack::Lint` from validating the conformity of the
request.

By backporting this change, `#inspect` is now only called when the
`LintError` would be raised which avoids the problem.

Co-authored-by: Benoit Daloze <eregontp@gmail.com>
  • Loading branch information
skipkayhil and eregon committed Jul 30, 2023
1 parent 1fdcf1f commit afc1ce9
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions lib/rack/lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def check_env(env)
## The environment must be an unfrozen instance of Hash that includes
## CGI-like headers. The application is free to modify the
## environment.
assert("env #{env.inspect} is not a Hash, but #{env.class}") {
env.kind_of? Hash
}
raise LintError, "env #{env.inspect} is not a Hash, but #{env.class}" unless env.kind_of? Hash
assert("env should not be frozen, but is") {
!env.frozen?
}
Expand Down

0 comments on commit afc1ce9

Please sign in to comment.