Skip to content

Replace CGI.parse with internal parser for Ruby 4.0 compatibility#13

Open
Filipe Goncalves (basex) wants to merge 2 commits into
snowplow-referer-parser:developfrom
basex:fix/ruby-4-cgi-parse
Open

Replace CGI.parse with internal parser for Ruby 4.0 compatibility#13
Filipe Goncalves (basex) wants to merge 2 commits into
snowplow-referer-parser:developfrom
basex:fix/ruby-4-cgi-parse

Conversation

@basex

Copy link
Copy Markdown

Problem

Ruby 4.0 slimmed down the cgi standard library and removed CGI.parse. As a result, RefererParser::Parser#parse raises when a referer URL has a query string:

undefined method 'parse' for class CGI (NoMethodError)
    query_params = CGI.parse(url.query)

(CGI.escape/CGI.unescape remain available; only CGI.parse and the larger CGI surface were removed.)

Fix

Replace the CGI.parse call with a small internal parse_query helper that returns the same Hash-of-arrays shape, using the still-available CGI.unescape for value decoding. No new dependencies, and it remains compatible with older Rubies.

Verification

Tested on Ruby 4.0.5 against real referer URLs:

  • + and %XX decoding (q=hello+world, q=ruby%204)
  • repeated keys (q=&q=second -> picks first non-blank)
  • params present but search term absent -> nil
  • no query string -> nil
  • malformed/bare/empty keys (q=x&flag&=&weird) -> no longer raises

All produce the expected results; behavior matches the previous CGI.parse-based output.

Ruby 4.0 removed CGI.parse from the cgi stdlib, so parsing a referer's
query string raised "undefined method 'parse' for class CGI". Add a small
dependency-free parse_query helper (using the still-available CGI.unescape)
that returns the same Hash-of-arrays shape CGI.parse produced.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 9, 2026 09:59
@snowplowcla

Copy link
Copy Markdown

Thanks for your pull request. Is this your first contribution to a Snowplow open source project? Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://docs.snowplowanalytics.com/docs/contributing/contributor-license-agreement/ to learn more and sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates referer query parsing to avoid relying on CGI.parse by introducing a local query-string parser.

Changes:

  • Replaced CGI.parse(url.query) with a new parse_query(url.query) helper.
  • Added parse_query to parse application/x-www-form-urlencoded query strings into a hash-of-arrays.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/referer-parser/parser.rb
A bare key like "a" in "a&b=1" was dropped because its value is nil. CGI.parse
keeps it as an empty array ("a&b=1" => {"a"=>[], "b"=>["1"]}; "a=" => {"a"=>[""]}).
Always materialize the key and only append when a value is present, and set the
hash default to [] so missing keys behave like CGI.parse.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@basex

Copy link
Copy Markdown
Author

Thanks for your pull request. Is this your first contribution to a Snowplow open source project? Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://docs.snowplowanalytics.com/docs/contributing/contributor-license-agreement/ to learn more and sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.

I signed it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants