ruby-grape / ruby-grape/grape

Grape::Middleware::Versioner::Header doesn't work for vendor strings with underscores

Open
#1,770 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug?
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

Upgrading to Grape >= 0.14.0 crashes our some of our requests.

undefined method `[]' for nil:NilClass
~/.rvm/gems/ruby-2.3.7/gems/grape-1.0.3/lib/grape/middleware/versioner/header.rb:181:in `request_vendor'
~/.rvm/gems/ruby-2.3.7/gems/grape-1.0.3/lib/grape/middleware/versioner/header.rb:123:in `block in headers_contain_wrong_vendor?'
~/.rvm/gems/ruby-2.3.7/gems/grape-1.0.3/lib/grape/middleware/versioner/header.rb:122:in `each'
~/.rvm/gems/ruby-2.3.7/gems/grape-1.0.3/lib/grape/middleware/versioner/header.rb:122:in `all?'
~/.rvm/gems/ruby-2.3.7/gems/grape-1.0.3/lib/grape/middleware/versioner/header.rb:122:in `headers_contain_wrong_vendor?'
~/.rvm/gems/ruby-2.3.7/gems/grape-1.0.3/lib/grape/middleware/versioner/header.rb:37:in `before'
~/.rvm/gems/ruby-2.3.7/gems/grape-1.0.3/lib/grape/middleware/base.rb:29:in `call!'
~/.rvm/gems/ruby-2.3.7/gems/grape-1.0.3/lib/grape/middleware/base.rb:24:in `call'
~/.rvm/gems/ruby-2.3.7/gems/warden-1.2.7/lib/warden/manager.rb:32:in `call'
~/.rvm/gems/ruby-2.3.7/gems/grape-1.0.3/lib/grape/middleware/error.rb:37:in `block in call!'
~/.rvm/gems/ruby-2.3.7/gems/grape-1.0.3/lib/grape/middleware/error.rb:36:in `catch'
~/.rvm/gems/ruby-2.3.7/gems/grape-1.0.3/lib/grape/middleware/error.rb:36:in `call!'
~/.rvm/gems/ruby-2.3.7/gems/grape-1.0.3/lib/grape/middleware/base.rb:24:in `call'
~/.rvm/gems/ruby-2.3.7/gems/rack-2.0.5/lib/rack/head.rb:12:in `call'

The issue seems related to the Regex that captures the vendor within Middleware Versioner Headers.
Our application uses a Warden middleware, which is raising this exception.

VENDOR_VERSION_HEADER_REGEX = /\Avnd\.([a-z0-9.\-_!#\$&\^]+?)(?:-([a-z0-9*.]+))?(?:\+([a-z0-9*\-.]+))?\z/

The Middleware raises an exception caused by the Regex not matching.

# https://github.com/ruby-grape/grape/blob/v0.14.0/lib/grape/middleware/versioner/header.rb#L181
subtype.match(VENDOR_VERSION_HEADER_REGEX)[1]

Any suggestion would be appreciated.

Edit

Unfortunately some of our requests used underscores in the format, which would lead to the chain of lazy matching breaking and making everything unmatched (i.e. vendor).

vnd.mycompany-v1.custom_format

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/grape/middleware/versioner/header.rb at request_vendor and VENDOR_VERSION_HEADER_REGEX. Reproduce the failure with the vendor string vnd.mycompany-v1.custom_format and inspect why the match becomes nil. Done means vendor strings containing underscores no longer raise an exception and are parsed correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.