Skip to content

FILTER_FLAG_HOSTNAME accepts ending hyphen #16523

Closed
@ppaysant

Description

@ppaysant

Description

Hello,

The following code:

<?php

$domains = [
    'a-.bc.com',
    'a.bc-.com',
    'a.bc.com-',
];

foreach ($domains as $domain) {
    echo $domain . PHP_EOL;
    var_dump(filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME));
    echo PHP_EOL;
}

Resulted in this output:

a-.bc.com
bool(false)

a.bc-.com
bool(false)

a.bc.com-
string(9) "a.bc.com-"    // <------------ here is the bug

I expected this output instead: a.bc.com- test should return false.
I thought that the ending hyphen is not permitted on domain name (https://datatracker.ietf.org/doc/html/rfc1035#section-2.3.1).

This code is tested here : https://3v4l.org/XbMCV

This bug happens in PHP 8.1.30, PHP 8.2.24 and PHP 8.3.12

PHP Version

PHP 8.3.12

Operating System

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions