From a83bbeaf5f415f366a193e13f20226b56a8df859 Mon Sep 17 00:00:00 2001 From: Malek Kadri <93520204+malekkadri@users.noreply.github.com> Date: Wed, 2 Dec 2026 16:29:11 +0100 Subject: [PATCH] fix: honor default values for float, URL, IP and MAC filters --- php-rust/crates/php-builtins/src/var.rs | 9 ++++----- .../php-builtins/tests/filter_validate.rs | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/php-rust/crates/php-builtins/src/var.rs b/php-rust/crates/php-builtins/src/var.rs index 632598ba..4972dc42 100644 --- a/php-rust/crates/php-builtins/src/var.rs +++ b/php-rust/crates/php-builtins/src/var.rs @@ -937,7 +937,6 @@ pub(crate) fn filter_var(args: &[Zval], ctx: &mut Ctx) -> Result } } }; - let miss = || if flags & NULL_ON_FAILURE != 0 { Zval::Null } else { Zval::Bool(false) }; let s = convert::to_zstr_cast(value, ctx.diags); let text = String::from_utf8_lossy(s.as_bytes()); let trimmed = text.trim(); @@ -977,7 +976,7 @@ pub(crate) fn filter_var(args: &[Zval], ctx: &mut Ctx) -> Result } VALIDATE_FLOAT => match trimmed.parse::() { Ok(f) => Ok(Zval::Double(f)), - Err(_) => Ok(miss()), + Err(_) => Ok(default_or_miss()), }, // FILTER_VALIDATE_REGEXP: php_filter_validate_regexp — the value must // match the `regexp` option (a full PCRE pattern, delimiters included); @@ -1036,7 +1035,7 @@ pub(crate) fn filter_var(args: &[Zval], ctx: &mut Ctx) -> Result scheme_ok && host_ok } }; - if ok { Ok(Zval::Str(s)) } else { Ok(miss()) } + if ok { Ok(Zval::Str(s)) } else { Ok(default_or_miss()) } } // FILTER_VALIDATE_IP: faithful port of php_filter_validate_ip // (ext/filter/logical_filters.c) — strict v4 (no leading zeros, no @@ -1044,7 +1043,7 @@ pub(crate) fn filter_var(args: &[Zval], ctx: &mut Ctx) -> Result // v4 tail), and the RFC 6890 special-purpose tables behind the // IPV4/IPV6/NO_PRIV_RANGE/NO_RES_RANGE/GLOBAL_RANGE flags. 275 => { - if validate_ip(s.as_bytes(), flags) { Ok(Zval::Str(s)) } else { Ok(miss()) } + if validate_ip(s.as_bytes(), flags) { Ok(Zval::Str(s)) } else { Ok(default_or_miss()) } } // FILTER_VALIDATE_EMAIL: php_filter_validate_email — the 320-octet cap, // then the logical_filters.c pattern (`\pL\pN` + `/u` under FILTER_FLAG_EMAIL_UNICODE). @@ -1082,7 +1081,7 @@ pub(crate) fn filter_var(args: &[Zval], ctx: &mut Ctx) -> Result }, _ => None, }; - if validate_mac(s.as_bytes(), sep) { Ok(Zval::Str(s)) } else { Ok(miss()) } + if validate_mac(s.as_bytes(), sep) { Ok(Zval::Str(s)) } else { Ok(default_or_miss()) } } // FILTER_SANITIZE_NUMBER_INT: strip everything but digits, `+`, `-` // (php_filter_number_int's [^0-9+-] deny list). diff --git a/php-rust/crates/php-builtins/tests/filter_validate.rs b/php-rust/crates/php-builtins/tests/filter_validate.rs index 5c89c6b7..453d8da3 100644 --- a/php-rust/crates/php-builtins/tests/filter_validate.rs +++ b/php-rust/crates/php-builtins/tests/filter_validate.rs @@ -45,3 +45,23 @@ fn filter_validate_domain() { "11 11 10 00 00 00 10 10 10 11 11 00 11 00 10 11 NULL\nstring(1) \"d\"\n" ); } + +#[test] +fn filter_validate_defaults_on_failure() { + assert_eq!( + out(" ['bad', '1.25'], + FILTER_VALIDATE_URL => ['bad', 'https://example.com'], + FILTER_VALIDATE_IP => ['bad', '127.0.0.1'], + FILTER_VALIDATE_MAC => ['bad', '00:11:22:33:44:55']] as $filter => [$bad, $good]) { + var_dump(filter_var($bad, $filter)); + var_dump(filter_var($bad, $filter, FILTER_NULL_ON_FAILURE)); + var_dump(filter_var($bad, $filter, ['options' => ['default' => 'fallback']])); + var_dump(filter_var($bad, $filter, ['flags' => FILTER_NULL_ON_FAILURE, 'options' => ['default' => 0]])); + var_dump(filter_var($good, $filter, ['options' => ['default' => 'fallback']])); + }"), + "bool(false)\nNULL\nstring(8) \"fallback\"\nint(0)\nfloat(1.25)\n\ + bool(false)\nNULL\nstring(8) \"fallback\"\nint(0)\nstring(19) \"https://example.com\"\n\ + bool(false)\nNULL\nstring(8) \"fallback\"\nint(0)\nstring(9) \"127.0.0.1\"\n\ + bool(false)\nNULL\nstring(8) \"fallback\"\nint(0)\nstring(17) \"00:11:22:33:44:55\"\n" + ); +}