Skip to content

Commit

Permalink
Allow http in AMP markup if host is localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
thelovekesh committed Mar 31, 2023
1 parent be2728b commit e8c6261
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ class AMP_Tag_And_Attribute_Sanitizer extends AMP_Base_Sanitizer {
const SPECIFIED_LAYOUT_INVALID = 'SPECIFIED_LAYOUT_INVALID';
const WRONG_PARENT_TAG = 'WRONG_PARENT_TAG';

/**
* Key for localhost.
*
* @var string
*/
const LOCALHOST = 'localhost';

/**
* Allowed tags.
*
Expand Down Expand Up @@ -1940,7 +1947,9 @@ private function check_attr_spec_rule_allowed_protocol( DOMElement $node, $attr_
if ( $node->hasAttribute( $attr_name ) ) {
foreach ( $this->extract_attribute_urls( $node->getAttributeNode( $attr_name ) ) as $url ) {
$url_scheme = $this->parse_protocol( $this->normalize_url_from_attribute_value( $url ) );
if ( isset( $url_scheme ) && ! in_array( strtolower( $url_scheme ), $attr_spec_rule[ AMP_Rule_Spec::VALUE_URL ][ AMP_Rule_Spec::ALLOWED_PROTOCOL ], true ) ) {
if ( self::LOCALHOST === wp_parse_url( $url, PHP_URL_HOST ) && $this->args['allow_localhost_http_protocol'] ) {
return AMP_Rule_Spec::PASS;
} elseif ( isset( $url_scheme ) && ! in_array( strtolower( $url_scheme ), $attr_spec_rule[ AMP_Rule_Spec::VALUE_URL ][ AMP_Rule_Spec::ALLOWED_PROTOCOL ], true ) ) {
return AMP_Rule_Spec::FAIL;
}
}
Expand All @@ -1952,7 +1961,9 @@ private function check_attr_spec_rule_allowed_protocol( DOMElement $node, $attr_
if ( $node->hasAttribute( $alternative_name ) ) {
foreach ( $this->extract_attribute_urls( $node->getAttributeNode( $alternative_name ), $attr_name ) as $url ) {
$url_scheme = $this->parse_protocol( $this->normalize_url_from_attribute_value( $url ) );
if ( isset( $url_scheme ) && ! in_array( strtolower( $url_scheme ), $attr_spec_rule[ AMP_Rule_Spec::VALUE_URL ][ AMP_Rule_Spec::ALLOWED_PROTOCOL ], true ) ) {
if ( self::LOCALHOST === wp_parse_url( $url, PHP_URL_HOST ) && $this->args['allow_localhost_http_protocol'] ) {
return AMP_Rule_Spec::PASS;
} elseif ( isset( $url_scheme ) && ! in_array( strtolower( $url_scheme ), $attr_spec_rule[ AMP_Rule_Spec::VALUE_URL ][ AMP_Rule_Spec::ALLOWED_PROTOCOL ], true ) ) {
return AMP_Rule_Spec::FAIL;
}
}
Expand Down

0 comments on commit e8c6261

Please sign in to comment.