php / php/php-src

FILTER_VALIDATE_URL returns false when underscore present in URL

Open
#17,842 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Extension: filter Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php
var_dump(filter_var('https://sub_domain.example.com', FILTER_VALIDATE_URL));
var_dump(filter_var('https://ex_ample.com', FILTER_VALIDATE_URL));

Resulted in this output:

bool(false)
bool(false)

But I expected this output instead:

string(30) "https://sub_domain.example.com"
string(20) "https://ex_ample.com"

The underscore is a valid character according to the RFC 2396 section 2.3:

Unreserved Characters

Data characters that are allowed in a URI but do not have a reserved
purpose are called unreserved. These include upper and lower case
letters, decimal digits, and a limited set of punctuation marks and
symbols.

  unreserved  = alphanum | mark

  mark        = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"

Unreserved characters can be escaped without changing the semantics
of the URI, but this should not be done unless the URI is being used
in a context that does not allow the unescaped character to appear.

But this filter fails if a underscore is present in the domain or subdomain portion of the URL.

This RFC is superseded by RFC 3986, but the underscore is still in the unreserved characters:

unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"

PHP Version

PHP 8.4.4

Operating System

No response

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 by reproducing the filter_var() calls with FILTER_VALIDATE_URL shown in the issue, then trace the URL validation implementation in php-src. Compare the handling of underscores in host and subdomain components with the cited RFC sections. Done means the documented examples produce the expected validation results and relevant tests cover the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.