*/ final class PresizedDivValidator { /** * The HTML tags a pre-sized div template may contain. Containers only: * nothing that executes, embeds, navigates or loads a resource. * * @since 4.4.0 * @var array */ const ALLOWED_TAGS = array( 'div', 'span', 'section', 'aside', 'ins' ); /** * The attribute names allowed on a template element, besides `data-*`. * * @since 4.4.0 * @var array */ const ALLOWED_ATTRIBUTES = array( 'id', 'class', 'style' ); /** * The CSS properties an inline `style` attribute may declare: the sizing * properties a space-reserving container needs, nothing that paints, * positions over content or loads a resource. * * @since 4.4.0 * @var array */ const ALLOWED_STYLE_PROPERTIES = array( 'width', 'min-width', 'max-width', 'height', 'min-height', 'max-height', 'margin', 'margin-top', 'margin-right', 'margin-bottom', 'margin-left', 'padding', 'padding-top', 'padding-right', 'padding-bottom', 'padding-left', 'display', 'box-sizing', 'overflow', 'overflow-x', 'overflow-y', 'float', 'clear', 'text-align', 'vertical-align', 'line-height', 'aspect-ratio', ); /** * The CSS functions an inline `style` attribute value may call. * * @since 4.4.0 * @var array */ const ALLOWED_STYLE_FUNCTIONS = array( 'calc', 'min', 'max', 'clamp' ); /** * Validate a full slot as received from the API (poll or push). * * Checks the slot template, every `xpathConfig[].htmlCode` override and the * `cssCode` stylesheet. The first failure wins; its reason names the field * and, at most, a bounded {@see excerpt()} of the offending token, never * the payload body. * * @since 4.4.0 * @param mixed $slot The slot entry from the configuration payload. * @return array{valid: bool, reason: string} */ public static function validate_slot( $slot ) { if ( ! is_array( $slot ) || ! isset( $slot['slotName'] ) || ! is_string( $slot['slotName'] ) ) { return self::invalid( 'slot is not an object with a slotName' ); } if ( isset( $slot['htmlCode'] ) ) { if ( ! is_string( $slot['htmlCode'] ) ) { return self::invalid( 'htmlCode is not a string' ); } $template = self::validate_template( $slot['htmlCode'] ); if ( ! $template['valid'] ) { return self::invalid( 'htmlCode: ' . $template['reason'] ); } } if ( isset( $slot['cssCode'] ) ) { $css = self::validate_css( $slot['cssCode'] ); if ( ! $css['valid'] ) { return self::invalid( 'cssCode: ' . $css['reason'] ); } } if ( isset( $slot['xpathConfig'] ) ) { if ( ! is_array( $slot['xpathConfig'] ) ) { return self::invalid( 'xpathConfig is not a list' ); } foreach ( $slot['xpathConfig'] as $index => $xpath_config ) { $entry = self::excerpt( (string) $index ); if ( ! is_array( $xpath_config ) ) { return self::invalid( 'xpathConfig entry ' . $entry . ' is not an object' ); } if ( ! isset( $xpath_config['htmlCode'] ) || $xpath_config['htmlCode'] === '' ) { continue; } if ( ! is_string( $xpath_config['htmlCode'] ) ) { return self::invalid( 'xpathConfig entry ' . $entry . ' htmlCode is not a string' ); } $template = self::validate_template( $xpath_config['htmlCode'] ); if ( ! $template['valid'] ) { return self::invalid( 'xpathConfig entry ' . $entry . ' htmlCode: ' . $template['reason'] ); } } } return array( 'valid' => true, 'reason' => '', ); } /** * Validate an HTML template against the container allow-list. * * The template is parsed as an XML fragment — the exact parse * `DOMDocumentFragment::appendXML()` applies at render time, so whatever * passes here is whatever renders. Every element must be an allow-listed * container tag, every attribute an allow-listed name (`id`, `class`, * `data-*`, or `style` limited to sizing properties) whose value carries * no markup character (`<`, `>`). Text nodes are allowed; comments, CDATA * sections and processing instructions are not. * * @since 4.4.0 * @param string $template The HTML template. * @return array{valid: bool, reason: string} */ public static function validate_template( $template ) { if ( ! is_string( $template ) ) { return self::invalid( 'template is not a string' ); } if ( trim( $template ) === '' ) { return array( 'valid' => true, 'reason' => '', ); } $previous_errors = libxml_use_internal_errors( true ); $document = new \DOMDocument(); $loaded = $document->loadXML( '' . $template . '' ); libxml_clear_errors(); libxml_use_internal_errors( $previous_errors ); if ( $loaded === false || $document->documentElement === null ) { return self::invalid( 'template is not a well-formed fragment' ); } return self::validate_node_children( $document->documentElement ); } /** * Validate `cssCode` as a plain stylesheet. * * The known wrapper (``) is stripped * first, the way the render path has always done. The inner stylesheet must * carry no markup at all (`<`) and no escape sequence, checked raw: the * HTML parser ends a `` pair; everything downstream works on the inner stylesheet. * * @since 4.4.0 * @param string $css The raw `cssCode` value. * @return string The inner stylesheet. */ public static function strip_css_wrapper( $css ) { return str_replace( array( '' ), '', $css ); } /** * Sanitize a stored template for rendering. * * Defense in depth on the render path: stored values are validated at write * time, but a value written before this validator existed, or directly in * the database, still passes through here. The template is re-checked with * {@see validate_template()}, the exact write-time gate, and rendered * unchanged when it passes or dropped entirely when it does not. A * `wp_kses()` pass is deliberately not used here: its `safecss_filter_attr()` * strips the allow-listed sizing functions (`calc`, `min`, `max`, `clamp`) * on the WordPress versions the plugin supports (any `(` on 4.7, `calc()` * only from 5.8, `min()`/`max()`/`clamp()` later still), and its HTML * tokenizer diverges from the XML parse `appendXML()` applies, so a kses * pass could render something other than what was validated. * * @since 4.4.0 * @param string $template The stored HTML template. * @return string The template when it passes validation, empty otherwise. */ public static function sanitize_for_render( $template ) { if ( ! is_string( $template ) || trim( $template ) === '' ) { return ''; } $result = self::validate_template( $template ); if ( ! $result['valid'] ) { return ''; } return $template; } /** * Bound a payload-derived token for use in a rejection reason. * * Rejection reasons are logged and journaled for Site Health, so the * offending tag, attribute, property or slot name is only ever quoted as * a short printable excerpt: control and non-ASCII characters removed, * length capped. The payload body never travels further than this. * * @since 4.4.0 * @param mixed $value The payload-derived token. * @param int $max The maximum excerpt length. * @return string The bounded excerpt. */ public static function excerpt( $value, $max = 40 ) { if ( ! is_string( $value ) ) { return ''; } $value = preg_replace( '/[^\x20-\x7e]/', '', $value ); if ( $value === null ) { return ''; } if ( strlen( $value ) > $max ) { $value = substr( $value, 0, $max ) . '...'; } return $value; } /** * Walk the children of a parsed template node against the allow-list. * * @since 4.4.0 * @param \DOMNode $node The parent node. * @return array{valid: bool, reason: string} */ private static function validate_node_children( $node ) { foreach ( $node->childNodes as $child ) { if ( $child->nodeType === XML_TEXT_NODE ) { continue; } if ( $child->nodeType !== XML_ELEMENT_NODE ) { return self::invalid( 'node type not allowed in template' ); } $tag = strtolower( $child->nodeName ); if ( ! in_array( $tag, self::ALLOWED_TAGS, true ) ) { return self::invalid( 'tag not allowed: ' . self::excerpt( $tag ) ); } if ( $child->attributes !== null ) { foreach ( $child->attributes as $attribute ) { $result = self::validate_attribute( $attribute ); if ( ! $result['valid'] ) { return $result; } } } $result = self::validate_node_children( $child ); if ( ! $result['valid'] ) { return $result; } } return array( 'valid' => true, 'reason' => '', ); } /** * Validate one template attribute against the allow-list. * * @since 4.4.0 * @param \DOMAttr $attribute The attribute node. * @return array{valid: bool, reason: string} */ private static function validate_attribute( $attribute ) { $name = strtolower( $attribute->nodeName ); if ( $name === 'style' ) { return self::validate_style_attribute( $attribute->nodeValue === null ? '' : $attribute->nodeValue ); } if ( strpos( $name, 'data-actirise' ) === 0 && $name !== 'data-actirise-slot' ) { return self::invalid( 'reserved attribute not allowed: ' . self::excerpt( $name ) ); } $allowed = in_array( $name, self::ALLOWED_ATTRIBUTES, true ) || preg_match( '/^data-[a-z0-9_.-]+$/', $name ) === 1; if ( ! $allowed ) { return self::invalid( 'attribute not allowed: ' . self::excerpt( $name ) ); } $value = $attribute->nodeValue === null ? '' : $attribute->nodeValue; if ( strpos( $value, '<' ) !== false || strpos( $value, '>' ) !== false ) { return self::invalid( 'markup in attribute value: ' . self::excerpt( $name ) ); } return array( 'valid' => true, 'reason' => '', ); } /** * Validate an inline `style` attribute: sizing properties only, with * values that cannot carry code or a resource load. * * @since 4.4.0 * @param string $style The attribute value. * @return array{valid: bool, reason: string} */ private static function validate_style_attribute( $style ) { $declarations = explode( ';', $style ); foreach ( $declarations as $declaration ) { $declaration = trim( $declaration ); if ( $declaration === '' ) { continue; } $parts = explode( ':', $declaration, 2 ); if ( count( $parts ) !== 2 ) { return self::invalid( 'malformed style declaration' ); } $property = strtolower( trim( $parts[0] ) ); $value = trim( $parts[1] ); if ( ! in_array( $property, self::ALLOWED_STYLE_PROPERTIES, true ) ) { return self::invalid( 'style property not allowed: ' . self::excerpt( $property ) ); } if ( preg_match( '/^[a-z0-9\s.,%\/*+!()\-]*$/i', $value ) !== 1 ) { return self::invalid( 'style value not allowed for: ' . $property ); } if ( preg_match_all( '/([a-z\-]+)\s*\(/i', $value, $functions ) > 0 ) { foreach ( $functions[1] as $function ) { if ( ! in_array( strtolower( $function ), self::ALLOWED_STYLE_FUNCTIONS, true ) ) { return self::invalid( 'style function not allowed: ' . self::excerpt( strtolower( $function ) ) ); } } } } return array( 'valid' => true, 'reason' => '', ); } /** * Build a failed validation result. * * @since 4.4.0 * @param string $reason The rejection reason, carrying at most a bounded {@see excerpt()} of the payload. * @return array{valid: bool, reason: string} */ private static function invalid( $reason ) { return array( 'valid' => false, 'reason' => $reason, ); } }