ruby / ruby/uri

URI::MailTo::EMAIL_REGEXP matches whole strings but URI::RFC2396_PARSER.make_regexp matches partial strings

Open
#226 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
125
Forks
65
Avg merge
6h 4m
Merged PRs (30d)
2

Description

The way to test if a string is a valid email address is to directly use URI::MailTo::EMAIL_REGEXP

require 'uri'

puts URI::MailTo::EMAIL_REGEXP.match?('me@example.com') # true
puts URI::MailTo::EMAIL_REGEXP.match?('me@example.com ') # false (trailing space)

However, the correct way to validate that a string is a valid URL is to wrap the regex in anchors:

require 'uri'
puts /\A#{URI::RFC2396_PARSER.make_regexp}\z/.match?('https://example.com/') # true
puts /\A#{URI::RFC2396_PARSER.make_regexp}\z/.match?('https://example.com/ ') # false (trailing space)

If I directly use make_regexp, that second string matches because of a partial match.

require 'uri'
puts URI::RFC2396_PARSER.make_regexp.match?('https://example.com/ ') # true (trailing space)

Same behavior if I pass schemes into make_regexp or use the too. I'm on Ruby 4.0.1 but I don't think this behavior changed recently.

Would it be okay if I made a PR adding a keyword argument to make_regexp? It would get called like:

URI::RFC2396_PARSER.make_regexp(full_match: true).match?('https://example.com/ ') # true (trailing space)

I figured changing the default behavior could be a breaking change for a lot of people and that doesn't feel worth it.

Contributor guide

No contributing guide indexed for this repository

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

Review the public URI::RFC2396_PARSER.make_regexp entry point and its interaction with URI::MailTo::EMAIL_REGEXP; first determine the existing matching semantics and available tests. Done should preserve current default behavior while providing an explicit full-match option whose coverage rejects trailing whitespace.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.