script:" was refused by one * copy and applied by the other. Both now strip the same explicit set. * 2. Anchor with `\z`, never `$`. PCRE's `$` also matches before a trailing * newline; JavaScript's does not. * 3. A regex that fails (invalid UTF-8 under /u returns null/false) counts as a * refusal, never as a pass. * * Framework-free like the applicator: no WordPress functions, no ABSPATH * guard, so the CLI tests can exercise it directly. * * Values here originate in our own generator today (color arithmetic, labels * read off the page's own markup), so this is a bound on what a bug or a new * rule source can put on a customer page, not a hole being plugged. */ class AbleGuard_Fix_Safety { /** * Attribute names the fix engine may write. Mirrors * ALLOWED_FIX_ATTRIBUTES — the exact set the producers emit. * * "aria-*" is deliberately not a pattern: aria-label is the only aria * attribute any producer emits, and a wildcard also admitted * aria-hidden="true", which removes an element from the accessibility * tree — the opposite of what this product does. */ const ALLOWED_ATTRIBUTES = array( 'alt', 'aria-label', 'content', 'lang', 'role', 'tabindex', 'title' ); /** * Refused by name. The allowlist above already rejects these; naming them * records the intent — no fix repoints navigation, embeds a document, or * changes where a form submits. */ const REFUSED_ATTRIBUTES = array( 'href', 'src', 'srcset', 'srcdoc', 'formaction', 'action', 'xlink:href', 'data', 'poster', 'background', 'style', 'http-equiv', ); /** * Attributes whose value a browser may resolve as a URL — the only place a * "javascript:"/"data:" prefix means anything. * * Scheme filtering used to run on every attribute, which refused real * generated text: aria-label="Data: quarterly revenue" from a field * placeholder, alt="Data: 2026 revenue by region" from the AI route. Those * are prose, not URLs. */ const URL_VALUED_ATTRIBUTES = array( 'href', 'src', 'srcset', 'srcdoc', 'formaction', 'action', 'xlink:href', 'data', 'poster', 'background', 'style', 'http-equiv', 'content', 'ping', 'cite', 'longdesc', ); /** Value prefixes that turn a URL-valued attribute into code or an inline payload. */ const DANGEROUS_SCHEMES = array( 'javascript:', 'data:', 'vbscript:' ); /** * NO CHARACTER CLASS IN THIS FILE MAY CONSULT A UNICODE TABLE. * * Every class is a literal ASCII byte range. "\s", "\p{Z}" and "\p{Cf}" are * banned: they look identical here and in the TypeScript copies and they are * not, because each engine resolves them against its own bundled tables. * U+0085 (NEL) is White_Space to PCRE and not to ECMAScript; U+0890 and * U+13439 are Cf to Node and not to the PCRE2 build PHP 8.0 ships. Either * one silently made this connector disagree with the verifier about the same * string. The blank set used to be spelled "\s\p{Z}\p{Cf}" and called * explicit; it was two table lookups. * * As a bonus, an ASCII-only pattern needs no /u, so it cannot fail on * invalid UTF-8 and there is no null return to handle. A multi-byte * character's bytes are all >= 0x80, so a byte-wise ASCII class reaches the * same verdict as the code-point-wise JavaScript one. * * The conservatism the old set was reaching for is kept by two positive * rules: a URL scheme must be printable ASCII, and a selector must contain at * least one printable ASCII character. */ const ASCII_BLANK_PATTERN = '/[\x00-\x20\x7f]/'; /** ASCII whitespace and controls removed. Never trim(): the runtimes differ. */ private static function strip_blanks( $value ) { return preg_replace( self::ASCII_BLANK_PATTERN, '', (string) $value ); } /** Blanks removed and ASCII case folded, matching the TypeScript copies. */ private static function flatten( $value ) { return strtolower( self::strip_blanks( $value ) ); } /** * True when a string carries nothing a selector or declaration could be * built from. Requires one printable ASCII character rather than asking "is * this only whitespace", which is what refuses an all-invisible value * without a table lookup. */ private static function has_no_usable_character( $value ) { return ! self::shape_matches( '/[\x21-\x7e]/', (string) $value ); } /** True when an attribute the element may not have actually carries a value. */ private static function is_present( $value ) { return '' !== self::strip_blanks( $value ); } /** * True when a required shape matches. A regex failure counts as "does not * match", so the caller refuses. * * @param string $pattern Regex. * @param string $subject Subject. * @return bool */ private static function shape_matches( $pattern, $subject ) { return 1 === preg_match( $pattern, $subject ); } /** * True when something unsafe is present. A regex failure counts as unsafe. * * @param string $pattern Regex. * @param string $subject Subject. * @return bool */ private static function unsafe_matches( $pattern, $subject ) { $result = preg_match( $pattern, $subject ); return 1 === $result || false === $result; } /** * Is this value safe in an attribute a browser resolves as a URL? * * Public because the parity vectors drive it directly: the normalization * here is exactly where the copies drifted. * * @param string $value Attribute value. * @return string|null Null when safe, else a short reason. */ public static function url_scheme_refusal_reason( $value ) { if ( ! is_string( $value ) ) { return 'value must be a string'; } $flat = self::flatten( $value ); foreach ( self::DANGEROUS_SCHEMES as $scheme ) { if ( 0 === strpos( $flat, $scheme ) ) { return 'value refused: starts with ' . $scheme; } } // A URL scheme is ASCII by definition, so anything else in that position is // either meaningless or an attempt to hide one. This is what refuses // "javascript:", "javascript:" and "javascript:" without // needing a list of invisible characters or a Unicode table. $colon = strpos( $flat, ':' ); if ( false !== $colon && $colon > 0 && self::unsafe_matches( '/[^\x21-\x7e]/', substr( $flat, 0, $colon ) ) ) { return 'value refused: a URL scheme must be printable ASCII'; } return null; } /** * @param string $name Attribute name. * @param string $value Attribute value. * @param string|null $tag_name Tag name of the target element, if known. * @param string|null $http_equiv The element's existing http-equiv, if any. * @param string|null $element_name The element's "name" attribute, if any. * @return string|null Null when the write is allowed, else a short reason. */ public static function attribute_refusal_reason( $name, $value, $tag_name = null, $http_equiv = null, $element_name = null ) { if ( ! is_string( $name ) || ! is_string( $value ) ) { return 'attribute name and value must be strings'; } // Blanks are stripped rather than trimmed: the runtimes disagree on // which characters a trim removes, and an attribute name never has one. $attr = self::flatten( $name ); if ( '' === $attr ) { return 'empty attribute name'; } if ( ! self::shape_matches( '/^[a-z][a-z0-9:-]*\z/', $attr ) ) { return 'unsupported attribute name: ' . $name; } if ( 0 === strpos( $attr, 'on' ) ) { return 'event handler attribute refused: ' . $attr; } if ( in_array( $attr, self::REFUSED_ATTRIBUTES, true ) ) { return 'navigation/embedding attribute refused: ' . $attr; } if ( in_array( $attr, self::URL_VALUED_ATTRIBUTES, true ) ) { $refusal = self::url_scheme_refusal_reason( $value ); if ( null !== $refusal ) { return $refusal; } } if ( 'content' === $attr ) { // The only meta the generator touches is the viewport // (generator.ts metaViewportRules). Every other meta is site // metadata a bad selector must not overwrite — robots, referrer, // description — and on an http-equiv meta the same attribute is a // redirect or a CSP. Unknown element or unknown name → refuse. $tag = self::flatten( (string) $tag_name ); $meta = self::flatten( (string) $element_name ); if ( 'meta' !== $tag ) { return 'content is only written to elements'; } if ( self::is_present( (string) $http_equiv ) ) { return 'content is not written to an http-equiv '; } if ( 'viewport' !== $meta ) { return 'content is only written to the viewport '; } return null; } return in_array( $attr, self::ALLOWED_ATTRIBUTES, true ) ? null : 'attribute not on the fix allowlist: ' . $attr; } /** * A selector list is spliced into a stylesheet element verbatim. * * Inside that raw-text element only a "" is * allowed on purpose — it is the child combinator the generator emits * ("body > section…", axe targets like "#menu > a") and cannot terminate * the element on its own. * * Non-ASCII stays allowed here, unlike in declarations: axe targets and the * AI route's img[src="…"] selectors legitimately carry non-ASCII, and none * of the refused characters can be spelled with one. * * @param array $selectors Selector list. * @return string|null Null when every selector is safe, else a reason. */ public static function selector_list_refusal_reason( $selectors ) { if ( ! is_array( $selectors ) || empty( $selectors ) ) { return 'no selectors'; } foreach ( $selectors as $selector ) { if ( ! is_string( $selector ) ) { return 'empty selector'; } if ( self::has_no_usable_character( $selector ) ) { return 'empty selector'; } if ( false !== stripos( $selector, ' [!important] (contrast fixes, toHex) * - text-decoration: underline [!important] (link-underline companion) * * Declarations are printable ASCII only. Everything the generator emits is, * and allowing more would mean three runtimes having to agree on which code * points count as whitespace inside a value — the drift that made * "font-family: AB" pass here and fail there. * * @param string $declarations Declaration text. * @return string|null Null when the text is safe, else a reason. */ public static function declarations_refusal_reason( $declarations ) { if ( ! is_string( $declarations ) ) { return 'declarations must be a string'; } if ( self::has_no_usable_character( $declarations ) ) { return 'empty declarations'; } if ( false !== stripos( $declarations, ' '/^#(?:[0-9a-f]{3}|[0-9a-f]{6})\z/i', 'text-decoration' => '/^underline\z/i', ); } /** * The one way to turn a css-action rule into stylesheet text. * * @param array $selectors Selector list. * @param string $declarations Declaration text. * @return string|null The block, or null if either half is unsafe — in * which case the rule is dropped whole. A partial * block would leave a stylesheet nobody reviewed. */ public static function css_block( $selectors, $declarations ) { if ( null !== self::selector_list_refusal_reason( $selectors ) ) { return null; } if ( null !== self::declarations_refusal_reason( $declarations ) ) { return null; } return implode( ",\n", $selectors ) . " {\n " . $declarations . ";\n}"; } }