Rust

filter

Module

filter_var(): the validation filters (FILTER_VALIDATE_INT, BOOL, FLOAT, REGEXP, URL, EMAIL, IP, MAC, DOMAIN), the sanitizing filters (FILTER_DEFAULT/FILTER_UNSAFE_RAW, FILTER_SANITIZE_*) and FILTER_CALLBACK, with their flags and options, ported from ext/filter (logical_filters.c, sanitizing_filters.c, callback_filter.c, filter.c) of PHP 8.5.

PHPRust
filter_var($value, $filter, $options)filter_var
filter_var($value, FILTER_CALLBACK, ['options' => $callable])filter_var_with

PHP values are Values (strings are bytes). The third argument is either flags or an options array (Options), read the way php_filter_call() reads it: "filter", "flags" and "options" keys, with zval_get_long()/zval_get_double() conversions of min_range and max_range, and the "default" option replacing a false (or, with FILTER_NULL_ON_FAILURE, null) result, even a valid false from FILTER_VALIDATE_BOOL.

FILTER_VALIDATE_REGEXP runs its regular expression on crate::pcre, through PHP's compiled-pattern cache like PHP does. FILTER_VALIDATE_EMAIL checks the language of PHP's fixed expressions directly (filter/email.rs), and runs the expression itself only for the addresses where PCRE2's backtracking limit could decide the result. FILTER_VALIDATE_URL parses with crate::url::parse_url, or with crate::url::rfc3986 for "uri_parser_class" => Uri\Rfc3986\Uri.

FILTER_CALLBACK calls a PHP callable; filter_var_with takes the Callables that stand for them: whether the "options" entry is callable (PHP throws TypeError when it is not), and calling it with the value converted to a string. filter_var knows no callable.

Like PHP, filtering an array goes on after an element throws; the exception reported is the last one thrown (a failed validation does not throw while another exception is pending, and callables are not called).

FILTER_SANITIZE_FULL_SPECIAL_CHARS encodes as htmlentities() with the default charset, which this port fixes at PHP's default, UTF-8 (default_charset/internal_encoding are not consulted).

Not supported (an Error with class Unsupported): the WHATWG URL parser (Uri\WhatWg\Url) for FILTER_VALIDATE_URL.

Every function here is checked against the real PHP function by bin/compat fuzz php-std (operations filter.*).

Contents

2 submodules, and what the module defines itself.

use php_std::filter::Error;
Rust
pub struct Error

An exception filter_var() throws (ValueError, TypeError, Filter\FilterFailedException), or Unsupported for what this port does not implement.

Also implements Display.
use php_std::filter::Filtered;
Rust
pub struct Filtered

What filter_var() returned, and the warning it emitted, if any.

use php_std::filter::NoCallables;
Rust
pub struct NoCallables

No callable at all: FILTER_CALLBACK always throws TypeError.

use php_std::filter::Options;
Rust
pub enum Options

The third argument of filter_var().

use php_std::Value;
Rust
pub enum Value

A PHP value given to or returned by filter_var().

use php_std::filter::Callables;
Rust
pub trait Callables

The PHP callables FILTER_CALLBACK can call, named by its "options" entry.

Implemented byNoCallables
use php_std::filter::filter_var;
Rust
pub fn filter_var(value: &Value, filter: i64, options: &Options) -> Result<Filtered, Error>

filter_var($value, $filter, $options), without callables (see filter_var_with).

use php_std::filter::filter_var_with;
Rust
pub fn filter_var_with(value: &Value, filter: i64, options: &Options, callables: &mut dyn Callables) -> Result<Filtered, Error>

filter_var($value, $filter, $options) where callables resolves and calls the callable of FILTER_CALLBACK (the "options" entry).

use php_std::filter::validate_domain;
Rust
pub fn validate_domain(d: &[u8], hostname: bool) -> bool

php_filter_validate_domain_ex(): whether a non-empty string passes filter_var($d, FILTER_VALIDATE_DOMAIN), with FILTER_FLAG_HOSTNAME when hostname is set, without building the filter's result.

Rust
pub const FILTER_CALLBACK: i64 = 0x0400

Calls the callable in the options entry, as PHP's FILTER_CALLBACK.

Rust
pub const FILTER_DEFAULT: i64 = 0x0204

The default filter, which leaves the string as is, as PHP's FILTER_DEFAULT.

Rust
pub const FILTER_FLAG_ALLOW_FRACTION: i64 = 0x1000

Keeps the decimal separator when sanitizing floats, as PHP's FILTER_FLAG_ALLOW_FRACTION.

Rust
pub const FILTER_FLAG_ALLOW_HEX: i64 = 0x0002

Accepts hexadecimal integers (0x prefix), as PHP's FILTER_FLAG_ALLOW_HEX.

Rust
pub const FILTER_FLAG_ALLOW_OCTAL: i64 = 0x0001

Accepts octal integers (0 or 0o prefix), as PHP's FILTER_FLAG_ALLOW_OCTAL.

Rust
pub const FILTER_FLAG_ALLOW_SCIENTIFIC: i64 = 0x4000

Keeps e and E when sanitizing floats, as PHP's FILTER_FLAG_ALLOW_SCIENTIFIC.

Rust
pub const FILTER_FLAG_ALLOW_THOUSAND: i64 = 0x2000

Accepts or keeps thousand separators, as PHP's FILTER_FLAG_ALLOW_THOUSAND.

Rust
pub const FILTER_FLAG_EMAIL_UNICODE: i64 = 0x0010_0000

Accepts Unicode in an email's local part, as PHP's FILTER_FLAG_EMAIL_UNICODE.

Rust
pub const FILTER_FLAG_EMPTY_STRING_NULL: i64 = 0x0100

Turns an empty string into null, as PHP's FILTER_FLAG_EMPTY_STRING_NULL.

Rust
pub const FILTER_FLAG_ENCODE_AMP: i64 = 0x0040

Encodes & as &#38;, as PHP's FILTER_FLAG_ENCODE_AMP.

Rust
pub const FILTER_FLAG_ENCODE_HIGH: i64 = 0x0020

Encodes bytes 127 and above as &#N;, as PHP's FILTER_FLAG_ENCODE_HIGH.

Rust
pub const FILTER_FLAG_ENCODE_LOW: i64 = 0x0010

Encodes bytes below 32 as &#N;, as PHP's FILTER_FLAG_ENCODE_LOW.

Rust
pub const FILTER_FLAG_GLOBAL_RANGE: i64 = 0x2000_0000

Accepts globally routable IP addresses only, as PHP's FILTER_FLAG_GLOBAL_RANGE.

Rust
pub const FILTER_FLAG_HOSTNAME: i64 = 0x0010_0000

Requires a valid host name for FILTER_VALIDATE_DOMAIN, as PHP's FILTER_FLAG_HOSTNAME.

Rust
pub const FILTER_FLAG_IPV4: i64 = 0x0010_0000

Accepts IPv4 addresses only, as PHP's FILTER_FLAG_IPV4.

Rust
pub const FILTER_FLAG_IPV6: i64 = 0x0020_0000

Accepts IPv6 addresses only, as PHP's FILTER_FLAG_IPV6.

Rust
pub const FILTER_FLAG_NO_ENCODE_QUOTES: i64 = 0x0080

Leaves ' and " unencoded, as PHP's FILTER_FLAG_NO_ENCODE_QUOTES.

Rust
pub const FILTER_FLAG_NO_PRIV_RANGE: i64 = 0x0080_0000

Rejects private IP ranges, as PHP's FILTER_FLAG_NO_PRIV_RANGE.

Rust
pub const FILTER_FLAG_NO_RES_RANGE: i64 = 0x0040_0000

Rejects reserved IP ranges, as PHP's FILTER_FLAG_NO_RES_RANGE.

Rust
pub const FILTER_FLAG_NONE: i64 = 0x0000

No flags, as PHP's FILTER_FLAG_NONE.

Rust
pub const FILTER_FLAG_PATH_REQUIRED: i64 = 0x04_0000

Requires a path in a validated URL, as PHP's FILTER_FLAG_PATH_REQUIRED.

Rust
pub const FILTER_FLAG_QUERY_REQUIRED: i64 = 0x08_0000

Requires a query string in a validated URL, as PHP's FILTER_FLAG_QUERY_REQUIRED.

Rust
pub const FILTER_FLAG_STRIP_BACKTICK: i64 = 0x0200

Strips backticks, as PHP's FILTER_FLAG_STRIP_BACKTICK.

Rust
pub const FILTER_FLAG_STRIP_HIGH: i64 = 0x0008

Strips bytes 127 and above, as PHP's FILTER_FLAG_STRIP_HIGH.

Rust
pub const FILTER_FLAG_STRIP_LOW: i64 = 0x0004

Strips bytes below 32, as PHP's FILTER_FLAG_STRIP_LOW.

Rust
pub const FILTER_FORCE_ARRAY: i64 = 0x0400_0000

Wraps a scalar result in an array, as PHP's FILTER_FORCE_ARRAY.

Rust
pub const FILTER_NULL_ON_FAILURE: i64 = 0x0800_0000

Returns null instead of false on failure, as PHP's FILTER_NULL_ON_FAILURE.

Rust
pub const FILTER_REQUIRE_ARRAY: i64 = 0x0100_0000

Requires an array and filters each element, as PHP's FILTER_REQUIRE_ARRAY.

Rust
pub const FILTER_REQUIRE_SCALAR: i64 = 0x0200_0000

Requires a scalar value, as PHP's FILTER_REQUIRE_SCALAR.

Rust
pub const FILTER_SANITIZE_ADD_SLASHES: i64 = 0x020b

Escapes as addslashes(), as PHP's FILTER_SANITIZE_ADD_SLASHES.

Rust
pub const FILTER_SANITIZE_EMAIL: i64 = 0x0205

Removes the bytes an email address cannot hold, as PHP's FILTER_SANITIZE_EMAIL.

Rust
pub const FILTER_SANITIZE_ENCODED: i64 = 0x0202

URL-encodes the string, as PHP's FILTER_SANITIZE_ENCODED.

Rust
pub const FILTER_SANITIZE_FULL_SPECIAL_CHARS: i64 = 0x020a

Encodes as htmlentities() with ENT_QUOTES, as PHP's FILTER_SANITIZE_FULL_SPECIAL_CHARS.

Rust
pub const FILTER_SANITIZE_NUMBER_FLOAT: i64 = 0x0208

Keeps only digits, signs and the allowed float characters, as PHP's FILTER_SANITIZE_NUMBER_FLOAT.

Rust
pub const FILTER_SANITIZE_NUMBER_INT: i64 = 0x0207

Keeps only digits and signs, as PHP's FILTER_SANITIZE_NUMBER_INT.

Rust
pub const FILTER_SANITIZE_SPECIAL_CHARS: i64 = 0x0203

HTML-encodes '"<>& and bytes below 32, as PHP's FILTER_SANITIZE_SPECIAL_CHARS.

Rust
pub const FILTER_SANITIZE_STRING: i64 = 0x0201

PHP deprecates the constant (8.1), not the filter: the ID runs without a notice.

Rust
pub const FILTER_SANITIZE_STRIPPED: i64 = 0x0201

An alias of FILTER_SANITIZE_STRING (also a deprecated constant).

Rust
pub const FILTER_SANITIZE_URL: i64 = 0x0206

Removes the bytes a URL cannot hold, as PHP's FILTER_SANITIZE_URL.

Rust
pub const FILTER_THROW_ON_FAILURE: i64 = 0x1000_0000

Throws Filter\FilterFailedException on failure, as PHP's FILTER_THROW_ON_FAILURE.

Rust
pub const FILTER_UNSAFE_RAW: i64 = 0x0204

Leaves the string as is, optionally stripping or encoding bytes, as PHP's FILTER_UNSAFE_RAW.

Rust
pub const FILTER_VALIDATE_BOOL: i64 = 0x0102

Validates a boolean ("1", "true", "on", "yes" and their opposites), as PHP's FILTER_VALIDATE_BOOL.

Rust
pub const FILTER_VALIDATE_DOMAIN: i64 = 0x0115

Validates a domain name, as PHP's FILTER_VALIDATE_DOMAIN.

Rust
pub const FILTER_VALIDATE_EMAIL: i64 = 0x0112

Validates an email address, as PHP's FILTER_VALIDATE_EMAIL.

Rust
pub const FILTER_VALIDATE_FLOAT: i64 = 0x0103

Validates a float, as PHP's FILTER_VALIDATE_FLOAT.

Rust
pub const FILTER_VALIDATE_INT: i64 = 0x0101

Validates an integer, as PHP's FILTER_VALIDATE_INT.

Rust
pub const FILTER_VALIDATE_IP: i64 = 0x0113

Validates an IPv4 or IPv6 address, as PHP's FILTER_VALIDATE_IP.

Rust
pub const FILTER_VALIDATE_MAC: i64 = 0x0114

Validates a MAC address, as PHP's FILTER_VALIDATE_MAC.

Rust
pub const FILTER_VALIDATE_REGEXP: i64 = 0x0110

Validates against the regexp option, as PHP's FILTER_VALIDATE_REGEXP.

Rust
pub const FILTER_VALIDATE_URL: i64 = 0x0111

Validates a URL, as PHP's FILTER_VALIDATE_URL.

filter::email

Own page

FILTER_VALIDATE_EMAIL without its regular expression.

php_filter_validate_email() matches the address (at most 320 bytes) against one of two fixed regular expressions (EMAIL, or EMAIL_UNICODE with FILTER_FLAG_EMAIL_UNICODE). Every part of them is deterministic except two lookaheads, so this module checks the same language directly, one character at a time (a byte, or a code point for the /u expression):

  • (?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,}) and the same with {65,}@: the units of these lookaheads are a character other than \ and ", or \ and a character up to \x7E, each with an optional " on both sides. Where each unit starts is forced, so the address is refused when 255 units follow each other from the start, or 65 or more are followed by @ (units_refuse).
  • the local part: dot-separated words, each an atom or a quoted string.
  • the domain: dot-separated labels of letters, digits and inner hyphens, none of 64 characters or more, ending in one starting with a letter; or an address literal ([IPv4], [IPv6:...] with at most 6 groups around ::, [IPv6:...:IPv4] with at most 4).

The expressions are case-insensitive: with /u, PCRE2 also matches K with U+212A KELVIN SIGN and S with U+017F LATIN SMALL LETTER LONG S, and \pL/\pN follow PCRE2's Unicode 15.0.0 tables.

The one thing a direct check cannot see is PCRE2 running out of pcre.backtrack_limit, which fails the validation. Only the lookaheads backtrack enough to get there, by trying both sides for each " between two units: an address with more than MAX_QUOTES double quotes runs the regular expression itself (see validate).

filter::sanitize

Own page

The sanitizing filters of ext/filter/sanitizing_filters.c (PHP 8.5), with the parts of ext/standard they call: php_strip_tags_ex() (string.c), php_escape_html_entities_ex() (html.c, as FILTER_SANITIZE_FULL_SPECIAL_CHARS calls it: every HTML 4.01 entity, UTF-8, no double encoding) and php_addslashes().

Every function takes the value already converted to a string and returns the filtered value; none of them can fail.