From 2f94165c1e5121d6756d74a1c6f20b7b977f1f80 Mon Sep 17 00:00:00 2001 From: Felipe Trombini Date: Sun, 25 Oct 2026 14:50:37 -0300 Subject: [PATCH] fix: filter_var FILTER_VALIDATE_EMAIL and FILTER_VALIDATE_DOMAIN Both filter ids had no arm in filter_var and fell through to the FILTER_DEFAULT catch-all, so every string validated. - 274: port of php_filter_validate_email (320-byte cap, then the logical_filters.c pattern; the \pL\pN /u variant under FILTER_FLAG_EMAIL_UNICODE). - 277: port of php_filter_validate_domain_ex (FILTER_FLAG_HOSTNAME label rules). A miss returns default_or_miss(), honouring the `default` option and FILTER_NULL_ON_FAILURE. Tests are oracle-verified against PHP 8.5.11. Fixes #6 --- php-rust/crates/php-builtins/src/var.rs | 61 +++++++++++++++++++ .../crates/php-builtins/tests/builtins.rs | 34 +++++++++++ 2 files changed, 95 insertions(+) diff --git a/php-rust/crates/php-builtins/src/var.rs b/php-rust/crates/php-builtins/src/var.rs index 212869ba..632598ba 100644 --- a/php-rust/crates/php-builtins/src/var.rs +++ b/php-rust/crates/php-builtins/src/var.rs @@ -1046,6 +1046,16 @@ pub(crate) fn filter_var(args: &[Zval], ctx: &mut Ctx) -> Result 275 => { if validate_ip(s.as_bytes(), flags) { Ok(Zval::Str(s)) } else { Ok(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). + 274 => { + if validate_email(s.as_bytes(), flags) { Ok(Zval::Str(s)) } else { Ok(default_or_miss()) } + } + // FILTER_VALIDATE_DOMAIN: php_filter_validate_domain_ex (FILTER_FLAG_HOSTNAME + // adds the alnum/`-` label rules). + 277 => { + if validate_domain(s.as_bytes(), flags) { Ok(Zval::Str(s)) } else { Ok(default_or_miss()) } + } // FILTER_VALIDATE_MAC (same file): 17-byte `xx:xx:…`/`xx-xx-…` or // 14-byte EUI-64 `xxxx.xxxx.xxxx`. The `separator` option must be one // character; a mismatch is a plain validation miss. @@ -1354,6 +1364,57 @@ fn validate_mac(b: &[u8], sep: Option) -> bool { } true } + +/// `php_filter_validate_email`: at most 320 bytes, then a full match of the +/// logical_filters.c pattern (`regexp1`, or `regexp0` under FILTER_FLAG_EMAIL_UNICODE). +fn validate_email(b: &[u8], flags: i64) -> bool { + const FLAG_EMAIL_UNICODE: i64 = 1_048_576; + const PATTERN: &[u8] = br"/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iD"; + const PATTERN_UNICODE: &[u8] = br"/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E\pL\pN]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F\pL\pN]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E\pL\pN]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F\pL\pN]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iDu"; + if b.len() > 320 { + return false; + } + let pat = if flags & FLAG_EMAIL_UNICODE != 0 { PATTERN_UNICODE } else { PATTERN }; + php_runtime::preg::compile(pat).is_some_and(|re| { + php_runtime::preg::subject_text(b, php_runtime::preg::pattern_is_unicode(pat)) + .is_some_and(|st| re.captures(st.as_str()).is_some()) + }) +} + +/// `php_filter_validate_domain_ex`: ≤253 bytes (one trailing dot ignored), no +/// empty label, labels ≤63 bytes. Out-of-range reads see NUL like the C string. +fn validate_domain(b: &[u8], flags: i64) -> bool { + const FLAG_HOSTNAME: i64 = 1_048_576; + let hostname = flags & FLAG_HOSTNAME != 0; + let at = |i: usize| b.get(i).copied().unwrap_or(0); + let mut l = b.len(); + if l > 0 && b[l - 1] == b'.' { + l -= 1; + } + if l > 253 { + return false; + } + if at(0) == b'.' || (hostname && !at(0).is_ascii_alphanumeric()) { + return false; + } + let mut label = 1u8; + for i in 0..l { + if b[i] == b'.' { + if at(i + 1) == b'.' + || (hostname && (!at(i - 1).is_ascii_alphanumeric() || !at(i + 1).is_ascii_alphanumeric())) + { + return false; + } + label = 1; + } else { + if label > 63 || (hostname && (b[i] != b'-' || at(i + 1) == 0) && !b[i].is_ascii_alphanumeric()) { + return false; + } + label += 1; + } + } + true +} /// `filter_var_array(array $array, array|int $options = FILTER_DEFAULT, bool $add_empty = true)` /// — apply filters to an array. A single filter int is applied to every element /// (result keyed like `$array`); an array `$options` is a per-key spec (result diff --git a/php-rust/crates/php-builtins/tests/builtins.rs b/php-rust/crates/php-builtins/tests/builtins.rs index 247ba6c7..45ecf445 100644 --- a/php-rust/crates/php-builtins/tests/builtins.rs +++ b/php-rust/crates/php-builtins/tests/builtins.rs @@ -2263,6 +2263,40 @@ fn filter_validate_regexp_miss_and_null_on_failure() { ); } +// FILTER_VALIDATE_EMAIL (php_filter_validate_email) and FILTER_VALIDATE_DOMAIN +// (php_filter_validate_domain_ex). Oracle-verified (PHP 8.5.11). +#[test] +fn filter_validate_email() { + assert_eq!( + out(" ['default' => 'fallback']]));"), + "true true true true false false false false false false false false false false false bool(false)\n\ + string(20) \"ünicode@example.com\"\nNULL\nstring(8) \"fallback\"\n" + ); +} + +#[test] +fn filter_validate_domain() { + assert_eq!( + out(" ['default' => 'd']]));"), + "11 11 10 00 00 00 10 10 10 11 11 00 11 00 10 11 NULL\nstring(1) \"d\"\n" + ); +} + // Session 8: class-target attributes on INTERFACES are reflected (Symfony's // ErrorListener resolves #[WithHttpStatus] declared on an exception interface). #[test]