From 4aab4734b3a789023dbffaee68ce285c3c1fbbf6 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 17 Jun 2024 23:19:04 +0900 Subject: [PATCH] Allow empty PATH_INFO. (#2214) --- SPEC.rdoc | 2 +- config/external.yaml | 3 +++ lib/rack/lint.rb | 4 +++- test/spec_lint.rb | 4 ++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/SPEC.rdoc b/SPEC.rdoc index e343bc430..ed5d98244 100644 --- a/SPEC.rdoc +++ b/SPEC.rdoc @@ -130,7 +130,7 @@ There are the following restrictions: * There may be a valid early hints callback in rack.early_hints * The REQUEST_METHOD must be a valid token. * The SCRIPT_NAME, if non-empty, must start with / -* The PATH_INFO, if provided, must be a valid request target. +* The PATH_INFO, if provided, must be a valid request target or an empty string. * Only OPTIONS requests may have PATH_INFO set to * (asterisk-form). * Only CONNECT requests may have PATH_INFO set to an authority (authority-form). Note that in HTTP/2+, the authority-form is not a valid request target. * CONNECT and OPTIONS requests must not have PATH_INFO set to a URI (absolute-form). diff --git a/config/external.yaml b/config/external.yaml index dfb0fc67f..7ca421b10 100644 --- a/config/external.yaml +++ b/config/external.yaml @@ -11,3 +11,6 @@ roda: grape: url: https://github.com/ruby-grape/grape command: bundle exec rspec --exclude-pattern=spec/integration/**/*_spec.rb +sinatra: + url: https://github.com/sinatra/sinatra + command: bundle exec rake test diff --git a/lib/rack/lint.rb b/lib/rack/lint.rb index 1ab3c271b..d1720884d 100644 --- a/lib/rack/lint.rb +++ b/lib/rack/lint.rb @@ -361,7 +361,7 @@ def check_environment(env) raise LintError, "SCRIPT_NAME must start with /" end - ## * The PATH_INFO, if provided, must be a valid request target. + ## * The PATH_INFO, if provided, must be a valid request target or an empty string. if env.include?(PATH_INFO) case env[PATH_INFO] when REQUEST_PATH_ASTERISK_FORM @@ -381,6 +381,8 @@ def check_environment(env) end when REQUEST_PATH_ORIGIN_FORM ## * Otherwise, PATH_INFO must start with a / and must not include a fragment part starting with '#' (origin-form). + when "" + # Empty string is okay. else raise LintError, "PATH_INFO must start with a '/' and must not include a fragment part starting with '#' (origin-form)" end diff --git a/test/spec_lint.rb b/test/spec_lint.rb index 573eb4153..efa5de870 100644 --- a/test/spec_lint.rb +++ b/test/spec_lint.rb @@ -310,6 +310,10 @@ def result.name message.must_include('response array has 4 elements instead of 3') end + it "accepts empty PATH_INFO" do + Rack::Lint.new(valid_app).call(env("PATH_INFO" => "")).first.must_equal 200 + end + it "notices request-target asterisk form errors" do # A non-empty PATH_INFO starting with something other than / has # implications for Rack::Request#path and methods downstream from