Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide origin for stylesheet URLs which are absolute paths #6257

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
16 changes: 16 additions & 0 deletions includes/sanitizers/class-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,22 @@ private function process_link_element( DOMElement $element ) {
* @return string|WP_Error Stylesheet string on success, or WP_Error on failure.
*/
private function get_stylesheet_from_url( $stylesheet_url ) {
// For absolute paths, provide the origin (host and port).
if ( '/' === substr( $stylesheet_url, 0, 1 ) && '//' !== substr( $stylesheet_url, 0, 2 ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried disabling this condition and then attempted to run the test to see if the test was asserting the expected case, but the test still passed when this code was disabled. 😕

Looking further, I'm not sure this logic is needed given this logic in get_validated_url_file_path:

$needs_base_url = (
! preg_match( '|^(https?:)?//|', $url )
&&
! ( $this->content_url && 0 === strpos( $url, $this->content_url ) )
);
if ( $needs_base_url ) {
$url = $this->base_url . '/' . ltrim( $url, '/' );
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Additionally, I can attest that get_validated_url_file_path precisely covers file path validation. So long as there are no use cases that it cannot handle, we can rely on it and avoid adding additional logic for the same task.

$parsed_home_url = wp_parse_url( home_url() );
if ( ! isset( $parsed_home_url['host'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$parsed_home_url['host'] = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : 'localhost';
}

$stylesheet_origin = '//' . $parsed_home_url['host'];
if ( isset( $parsed_home_url['port'] ) ) {
$stylesheet_origin .= ':' . $parsed_home_url['port'];
}

$stylesheet_url = $stylesheet_origin . $stylesheet_url;
}

$stylesheet = false;
$css_file_path = $this->get_validated_url_file_path( $stylesheet_url, [ 'css', 'less', 'scss', 'sass' ] );
if ( ! is_wp_error( $css_file_path ) ) {
Expand Down
32 changes: 32 additions & 0 deletions tests/php/test-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,38 @@ static function( $preempt, $request, $url ) {
}
}

/**
* Test get_stylesheet_from_url with a bad URL.
*
* @covers AMP_Style_Sanitizer::get_stylesheet_from_url()
*/
public function test_get_stylesheet_from_url_bad_url() {
$dom = Document::fromHtml( '<html><head></head><body></body></html>', Options::DEFAULTS );

$sanitizer = new AMP_Style_Sanitizer( $dom, [] );

$css_url = 'https://example.com/style.css';
$stylesheet = $this->call_private_method( $sanitizer, 'get_stylesheet_from_url', [ $css_url ] );

$this->assertTrue( is_wp_error( $stylesheet ) );
$this->assertInstanceOf( WP_Error::class, $stylesheet );
$this->assertStringStartsWith( 'Failed to fetch:', $stylesheet->get_error_message() );

$css_url = amp_get_asset_url( 'css/amp-default.css' );
$stylesheet = $this->call_private_method( $sanitizer, 'get_stylesheet_from_url', [ $css_url ] );

$this->assertIsString( $stylesheet );
$this->assertNotEmpty( $stylesheet );
$this->assertFalse( is_wp_error( $stylesheet ) );

$css_url = '/wp-includes/css/admin-bar.css';
$stylesheet = $this->call_private_method( $sanitizer, 'get_stylesheet_from_url', [ $css_url ] );
thelovekesh marked this conversation as resolved.
Show resolved Hide resolved

$this->assertIsString( $stylesheet );
$this->assertNotEmpty( $stylesheet );
$this->assertFalse( is_wp_error( $stylesheet ) );
}

/**
* Add test coverage for the property_allowlist condition in process_css_declaration_block which is not currently reachable given the spec.
*
Expand Down