esc_url_raw( $data['site']['origin'] ?? '' ), 'createdAt' => sanitize_text_field( $data['createdAt'] ?? '' ), 'uploaded' => current_time( 'mysql' ), 'skipped' => $skipped, 'fixes' => $fixes, ); } private static function sanitize_fix( $raw ) { $base = array( 'id' => preg_replace( '/[^a-z0-9]/', '', strtolower( (string) ( $raw['id'] ?? '' ) ) ), 'kind' => (string) ( $raw['kind'] ?? '' ), 'title' => sanitize_text_field( $raw['title'] ?? '' ), 'wcag' => array_map( 'sanitize_text_field', array_filter( (array) ( $raw['wcag'] ?? array() ), 'is_string' ) ), ); if ( '' === $base['id'] ) { return null; } switch ( $base['kind'] ) { case 'set-lang': $lang = (string) ( $raw['lang'] ?? '' ); return preg_match( '/^[a-zA-Z]{2,3}(-[a-zA-Z0-9]{2,8})*$/', $lang ) ? $base + array( 'lang' => $lang ) : null; case 'image-alt': $filename = sanitize_file_name( alexibility_fixes_file_name_of( (string) ( $raw['filename'] ?? '' ) ) ); $decorative = ! empty( $raw['decorative'] ); $alt = $decorative ? '' : sanitize_text_field( $raw['alt'] ?? '' ); if ( '' === $filename || ( ! $decorative && '' === $alt ) ) { return null; } return $base + array( 'filename' => $filename, 'stem' => alexibility_fixes_wp_stem( $filename ), 'alt' => $alt, 'decorative' => $decorative, // An image that is a link's only content may have alt=""; the alt text replaces it. 'replace_empty' => ! $decorative && ! empty( $raw['replaceEmpty'] ), ); case 'contrast-color': $hex = '/^#[0-9a-f]{6}$/i'; $selectors = array_values( array_filter( (array) ( $raw['selectors'] ?? array() ), array( __CLASS__, 'safe_selector' ) ) ); foreach ( array( 'fg', 'bg', 'replacement' ) as $key ) { if ( ! preg_match( $hex, (string) ( $raw[ $key ] ?? '' ) ) ) { return null; } } return $selectors ? $base + array( 'fg' => strtolower( $raw['fg'] ), 'bg' => strtolower( $raw['bg'] ), 'replacement' => strtolower( $raw['replacement'] ), 'selectors' => $selectors, ) : null; case 'focus-visible': $selector = (string) ( $raw['selector'] ?? '' ); return self::safe_selector( $selector ) ? $base + array( 'selector' => $selector ) : null; case 'skip-link': return $base; case 'accessible-name': $tag = strtolower( (string) ( $raw['tag'] ?? '' ) ); // Only a graphic can be decorative (hidden with aria-hidden); controls always get a name. $decorative = 'svg' === $tag && ! empty( $raw['decorative'] ); $name = $decorative ? '' : sanitize_text_field( $raw['name'] ?? '' ); $attrs = array(); foreach ( (array) ( $raw['attrs'] ?? array() ) as $key => $value ) { if ( is_string( $key ) && is_string( $value ) && preg_match( '/^[a-z][a-z0-9:_.-]*$/', $key ) ) { $attrs[ $key ] = $value; } } if ( ! in_array( $tag, array( 'a', 'button', 'input', 'select', 'textarea', 'svg' ), true ) || ( '' === $name && ! $decorative ) || ! $attrs ) { return null; } return $base + array( 'tag' => $tag, 'attrs' => $attrs, 'criteria' => alexibility_fixes_match_criteria( $attrs ), 'name' => $name, 'decorative' => $decorative ); case 'css-repair': $selector = (string) ( $raw['selector'] ?? '' ); $css = self::safe_declarations( $raw['css'] ?? null ); return self::safe_selector( $selector ) && $css ? $base + array( 'selector' => $selector, 'css' => $css ) : null; case 'attr-repair': $repair = self::safe_attr_repair( $raw ); return $repair ? $base + $repair : null; } return null; } /** Attributes an attr-repair may remove. */ const ATTR_REMOVE = array( 'align', 'role' ); /** Attributes an attr-repair may set, and the values allowed: a tab, or no role at all. */ const ATTR_SET = array( 'role' => '/^(tab|none|presentation)$/' ); /** Style properties an attr-repair may add: an old align attribute moved to CSS. */ const ATTR_STYLE = array( 'text-align' => '/^(left|right|center|justify)$/' ); /** * The parts of an attr-repair, if every one is allowed, else null. It may only change an attribute the * match names with its old value, so it reaches only elements that still have that value and a second * pass changes nothing. */ public static function safe_attr_repair( $raw ) { $match = $raw['match'] ?? null; $change = (string) ( $raw['change'] ?? '' ); if ( ! is_array( $match ) || ! in_array( $change, array( 'align', 'tab-role', 'banner' ), true ) ) { return null; } $tag = (string) ( $match['tag'] ?? '' ); if ( ! preg_match( '/^[a-z][a-z0-9]{0,15}$/', $tag ) ) { return null; } $name = '/^[a-z][a-z0-9-]{0,39}$/'; $classes = array(); foreach ( (array) ( $match['classes'] ?? array() ) as $class ) { if ( ! is_string( $class ) || ! preg_match( '/^-?[_a-zA-Z][_a-zA-Z0-9-]{0,79}$/', $class ) ) { return null; } $classes[] = $class; } $attrs = array(); foreach ( (array) ( $match['attrs'] ?? array() ) as $key => $value ) { if ( ! is_string( $key ) || ! preg_match( $name, $key ) || ! is_string( $value ) || strlen( $value ) > 100 || preg_match( '/[\x00-\x1f<>"]/', $value ) ) { return null; } $attrs[ $key ] = $value; } $has = array(); foreach ( (array) ( $match['has'] ?? array() ) as $key ) { if ( ! is_string( $key ) || ! preg_match( $name, $key ) ) { return null; } $has[] = $key; } $remove = array(); foreach ( (array) ( $raw['remove'] ?? array() ) as $key ) { if ( ! in_array( $key, self::ATTR_REMOVE, true ) || ! isset( $attrs[ $key ] ) ) { return null; } $remove[] = $key; } $set = array(); foreach ( (array) ( $raw['set'] ?? array() ) as $key => $value ) { $pattern = self::ATTR_SET[ (string) $key ] ?? null; if ( null === $pattern || ! is_string( $value ) || ! preg_match( $pattern, $value ) || ! isset( $attrs[ $key ] ) ) { return null; } $set[ $key ] = $value; } $style = array(); foreach ( (array) ( $raw['style'] ?? array() ) as $property => $value ) { $pattern = self::ATTR_STYLE[ (string) $property ] ?? null; if ( null === $pattern || ! is_string( $value ) || ! preg_match( $pattern, $value ) ) { return null; } $style[ $property ] = $value; } if ( ! $attrs || count( $classes ) > 4 || count( $attrs ) > 4 || count( $has ) > 4 || ( ! $remove && ! $set && ! $style ) ) { return null; } return array( 'change' => $change, 'match' => array( 'tag' => $tag, 'classes' => $classes, 'attrs' => $attrs, 'has' => $has, ), 'remove' => $remove, 'set' => $set, 'style' => $style, ); } /** Properties a css-repair may set: layout and colour only, so it can't load anything or hide content. */ const REPAIR_PROPERTIES = array( 'display' => '/^(flow-root|inline-block|block)$/', 'margin-top' => '/^\\d{1,3}(\\.\\d{1,2})?px$/', 'margin-right' => '/^\\d{1,3}(\\.\\d{1,2})?px$/', 'margin-bottom' => '/^\\d{1,3}(\\.\\d{1,2})?px$/', 'margin-left' => '/^\\d{1,3}(\\.\\d{1,2})?px$/', 'padding-top' => '/^\\d{1,3}(\\.\\d{1,2})?px$/', 'padding-right' => '/^\\d{1,3}(\\.\\d{1,2})?px$/', 'padding-bottom' => '/^\\d{1,3}(\\.\\d{1,2})?px$/', 'padding-left' => '/^\\d{1,3}(\\.\\d{1,2})?px$/', 'background-color' => '/^#[0-9a-f]{6}$/i', ); /** The declarations if every one is allowed, else null: a partly applied repair could do harm. */ public static function safe_declarations( $css ) { if ( ! is_array( $css ) || ! $css || count( $css ) > 8 ) { return null; } $out = array(); foreach ( $css as $property => $value ) { $pattern = self::REPAIR_PROPERTIES[ (string) $property ] ?? null; if ( null === $pattern || ! is_string( $value ) || ! preg_match( $pattern, $value ) ) { return null; } $out[ (string) $property ] = strtolower( $value ); } return $out; } /** A CSS selector that cannot escape its rule or the style element. */ public static function safe_selector( $selector ) { return is_string( $selector ) && '' !== trim( $selector ) && strlen( $selector ) <= 300 && ! preg_match( '/[<{};@\\\\]|\/\*/', $selector ); } }