Skip to content

Commit

Permalink
Fix phpGH-16523: FILTER_FLAG_HOSTNAME accepts ending hyphen
Browse files Browse the repository at this point in the history
Domain name labels must not end with a hyphen, and that is also true
for the last label.  Apparently, this has been overlooked so far.

Closes phpGH-16540.
  • Loading branch information
cmb69 committed Oct 21, 2024
1 parent 51b642f commit f9ce5e7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ PHP NEWS
. Fixed bug GH-16397 (Segmentation fault when comparing FFI object).
(nielsdos)

- Filter:
. Fixed bug GH-16523 (FILTER_FLAG_HOSTNAME accepts ending hyphen). (cmb)

- GD:
. Fixed bug GH-16334 (imageaffine overflow on matrix elements).
(David Carlier)
Expand Down
2 changes: 1 addition & 1 deletion ext/filter/logical_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ static int _php_filter_validate_domain(char * domain, size_t len, zend_long flag
/* Reset label length counter */
i = 1;
} else {
if (i > 63 || (hostname && *s != '-' && !isalnum((int)*(unsigned char *)s))) {
if (i > 63 || (hostname && (*s != '-' || *(s + 1) == '\0') && !isalnum((int)*(unsigned char *)s))) {
return 0;
}

Expand Down
20 changes: 20 additions & 0 deletions ext/filter/tests/gh16523.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
GH-16523 (FILTER_FLAG_HOSTNAME accepts ending hyphen)
--EXTENSIONS--
filter
--FILE--
<?php
$domains = [
'a-.bc.com',
'a.bc-.com',
'a.bc.com-',
];

foreach ($domains as $domain) {
var_dump(filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME));
}
?>
--EXPECT--
bool(false)
bool(false)
bool(false)

0 comments on commit f9ce5e7

Please sign in to comment.