Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions php-rust/crates/php-builtins/src/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,6 @@ pub(crate) fn filter_var(args: &[Zval], ctx: &mut Ctx) -> Result<Zval, PhpError>
}
}
};
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();
Expand Down Expand Up @@ -977,7 +976,7 @@ pub(crate) fn filter_var(args: &[Zval], ctx: &mut Ctx) -> Result<Zval, PhpError>
}
VALIDATE_FLOAT => match trimmed.parse::<f64>() {
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);
Expand Down Expand Up @@ -1036,15 +1035,15 @@ pub(crate) fn filter_var(args: &[Zval], ctx: &mut Ctx) -> Result<Zval, PhpError>
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
// whitespace), the hand-rolled v6 grammar (`::` compression, bundled
// 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).
Expand Down Expand Up @@ -1082,7 +1081,7 @@ pub(crate) fn filter_var(args: &[Zval], ctx: &mut Ctx) -> Result<Zval, PhpError>
},
_ => 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).
Expand Down
20 changes: 20 additions & 0 deletions php-rust/crates/php-builtins/tests/filter_validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<?php foreach ([FILTER_VALIDATE_FLOAT => ['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"
);
}