Skip to content

Before filter adds empty captures to params #1388

@alexedwards

Description

@alexedwards

Using a before filter with no pattern adds an empty captures attribute to params. This is unexpected behaviour when your application doesn't contain any regexp patterns with captures, and can cause problems for applications which are mapping params directly to another object.

A minimal case:

require 'sinatra'

before do
end

get '/' do
  params.to_s
end
$ curl -i localhost:9292
HTTP/1.1 200 OK 
Content-Type: text/html;charset=utf-8
Content-Length: 16
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Vary: Accept-Encoding
Server: WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21)
Date: Thu, 15 Feb 2018 10:20:14 GMT
Connection: Keep-Alive

{"captures"=>[]}

I suspect this might also be the cause of issue #1380.

I think it's happening due to this line of code, which defaults the before filter pattern to the regexp /.*/:

def before(path = /.*/, **options, &block)
        add_filter(:before, path, options, &block)
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions