Skip to content

Commit cd1e2bf

Browse files
authored
Merge pull request #4137 from ampproject/enhancement/4070-validate-props
Raise validation errors for properties in attributes
2 parents b8ccb97 + 67ad354 commit cd1e2bf

7 files changed

+347
-115
lines changed

includes/sanitizers/class-amp-meta-sanitizer.php

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ static function ( $element ) {
7979
);
8080

8181
foreach ( $elements as $element ) {
82+
83+
// Strip whitespace around equal signs. Won't be needed after <https://github.com/ampproject/amphtml/issues/26496> is resolved.
84+
if ( $element->hasAttribute( 'content' ) ) {
85+
$element->setAttribute(
86+
'content',
87+
preg_replace( '/\s*=\s*/', '=', $element->getAttribute( 'content' ) )
88+
);
89+
}
90+
8291
/**
8392
* Meta tag to process.
8493
*
@@ -126,37 +135,7 @@ protected function ensure_charset_is_present() {
126135
protected function ensure_viewport_is_present() {
127136
if ( empty( $this->meta_tags[ self::TAG_VIEWPORT ] ) ) {
128137
$this->meta_tags[ self::TAG_VIEWPORT ][] = $this->create_viewport_element( static::AMP_VIEWPORT );
129-
return;
130-
}
131-
132-
// Ensure we have the 'width=device-width' setting included.
133-
/**
134-
* Viewport tag.
135-
*
136-
* @var DOMElement $viewport_tag
137-
*/
138-
$viewport_tag = $this->meta_tags[ self::TAG_VIEWPORT ][0];
139-
$viewport_content = $viewport_tag->getAttribute( 'content' );
140-
$viewport_settings = array_map( 'trim', explode( ',', $viewport_content ) );
141-
$width_found = false;
142-
143-
// @todo The invalid/missing properties should raise validation errors. See <https://github.com/ampproject/amp-wp/pull/3758/files#r348074703>.
144-
foreach ( $viewport_settings as $index => $viewport_setting ) {
145-
list( $property, $value ) = array_map( 'trim', explode( '=', $viewport_setting ) );
146-
if ( 'width' === $property ) {
147-
if ( 'device-width' !== $value ) {
148-
$viewport_settings[ $index ] = 'width=device-width';
149-
}
150-
$width_found = true;
151-
break;
152-
}
153138
}
154-
155-
if ( ! $width_found ) {
156-
array_unshift( $viewport_settings, 'width=device-width' );
157-
}
158-
159-
$viewport_tag->setAttribute( 'content', implode( ',', $viewport_settings ) );
160139
}
161140

162141
/**

includes/sanitizers/class-amp-style-sanitizer.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,11 +2131,11 @@ private function process_css_declaration_block( RuleSet $ruleset, CSSList $css_l
21312131
$vendorless_property_name = preg_replace( '/^-\w+-/', '', $property->getRule() );
21322132
if ( ! in_array( $vendorless_property_name, $options['property_whitelist'], true ) ) {
21332133
$error = [
2134-
'code' => self::CSS_SYNTAX_INVALID_PROPERTY,
2135-
'property_name' => $property->getRule(),
2136-
'property_value' => $property->getValue(),
2137-
'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE,
2138-
'spec_name' => $options['spec_name'],
2134+
'code' => self::CSS_SYNTAX_INVALID_PROPERTY,
2135+
'css_property_name' => $property->getRule(),
2136+
'css_property_value' => $property->getValue(),
2137+
'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE,
2138+
'spec_name' => $options['spec_name'],
21392139
];
21402140
$sanitized = $this->should_sanitize_validation_error( $error );
21412141
if ( $sanitized ) {
@@ -2149,11 +2149,11 @@ private function process_css_declaration_block( RuleSet $ruleset, CSSList $css_l
21492149
$properties = $ruleset->getRules( $illegal_property_name );
21502150
foreach ( $properties as $property ) {
21512151
$error = [
2152-
'code' => self::CSS_SYNTAX_INVALID_PROPERTY_NOLIST,
2153-
'property_name' => $property->getRule(),
2154-
'property_value' => (string) $property->getValue(),
2155-
'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE,
2156-
'spec_name' => $options['spec_name'],
2152+
'code' => self::CSS_SYNTAX_INVALID_PROPERTY_NOLIST,
2153+
'css_property_name' => $property->getRule(),
2154+
'css_property_value' => (string) $property->getValue(),
2155+
'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE,
2156+
'spec_name' => $options['spec_name'],
21572157
];
21582158
$sanitized = $this->should_sanitize_validation_error( $error );
21592159
if ( $sanitized ) {
@@ -2397,11 +2397,11 @@ private function process_css_keyframes( KeyFrame $css_list, $options ) {
23972397
$vendorless_property_name = preg_replace( '/^-\w+-/', '', $property->getRule() );
23982398
if ( ! in_array( $vendorless_property_name, $options['property_whitelist'], true ) ) {
23992399
$error = [
2400-
'code' => self::CSS_SYNTAX_INVALID_PROPERTY,
2401-
'property_name' => $property->getRule(),
2402-
'property_value' => (string) $property->getValue(),
2403-
'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE,
2404-
'spec_name' => $options['spec_name'],
2400+
'code' => self::CSS_SYNTAX_INVALID_PROPERTY,
2401+
'css_property_name' => $property->getRule(),
2402+
'css_property_value' => (string) $property->getValue(),
2403+
'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE,
2404+
'spec_name' => $options['spec_name'],
24052405
];
24062406
$sanitized = $this->should_sanitize_validation_error( $error );
24072407
if ( $sanitized ) {
@@ -2447,11 +2447,11 @@ private function transform_important_qualifiers( RuleSet $ruleset, CSSList $css_
24472447
$ruleset->removeRule( $property->getRule() );
24482448
} else {
24492449
$error = [
2450-
'code' => self::CSS_SYNTAX_INVALID_IMPORTANT,
2451-
'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE,
2452-
'property_name' => $property->getRule(),
2453-
'property_value' => $property->getValue(),
2454-
'spec_name' => $options['spec_name'],
2450+
'code' => self::CSS_SYNTAX_INVALID_IMPORTANT,
2451+
'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE,
2452+
'css_property_name' => $property->getRule(),
2453+
'css_property_value' => $property->getValue(),
2454+
'spec_name' => $options['spec_name'],
24552455
];
24562456
$sanitized = $this->should_sanitize_validation_error( $error );
24572457
if ( $sanitized ) {

0 commit comments

Comments
 (0)