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

Opt-in to CORS mode for external font stylesheet links #1289

Merged
merged 1 commit into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions includes/sanitizers/class-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,18 @@ private function process_link_element( DOMElement $element ) {
if ( $href !== $normalized_url ) {
$element->setAttribute( 'href', $normalized_url );
}

/*
* Opt-in to CORS Mode for the stylesheet. This ensures that a service worker caching the external
* stylesheet will not inflate the storage quota.
*
* See:
* - https://developers.google.com/web/tools/workbox/guides/storage-quota#beware_of_opaque_responses
* - https://developers.google.com/web/tools/workbox/guides/handle-third-party-requests#cross-origin_requests_and_opaque_responses
*/
if ( ! $element->hasAttribute( 'crossorigin' ) ) {
$element->setAttribute( 'crossorigin', 'anonymous' );
}
return;
}

Expand Down
25 changes: 25 additions & 0 deletions tests/test-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1186,11 +1186,36 @@ public function test_font_urls( $url, $error_codes ) {
preg_replace( '#^(http:)?(?=//)#', 'https:', $url ),
$link->getAttribute( 'href' )
);
$this->assertEquals( 'anonymous', $link->getAttribute( 'crossorigin' ) );
} else {
$this->assertEmpty( $link );
}
}

/**
* Test addition of crossorigin attribute to external stylesheet links.
*
* @covers AMP_Style_Sanitizer::process_link_element()
*/
public function test_cors_enabled_stylesheet_url() {

// Test supplying crossorigin attribute.
$document = AMP_DOM_Utils::get_dom( '<html><head><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Tangerine"></head></html>' ); // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
$sanitizer = new AMP_Style_Sanitizer( $document, array( 'use_document_element' => true ) );
$sanitizer->sanitize();
$link = $document->getElementsByTagName( 'link' )->item( 0 );
$this->assertInstanceOf( 'DOMElement', $link );
$this->assertEquals( 'anonymous', $link->getAttribute( 'crossorigin' ) );

// Test that existing crossorigin attribute is not overridden.
$document = AMP_DOM_Utils::get_dom( '<html><head><link rel="stylesheet" crossorigin="use-credentials" href="https://fonts.googleapis.com/css?family=Tangerine"></head></html>' ); // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
$sanitizer = new AMP_Style_Sanitizer( $document, array( 'use_document_element' => true ) );
$sanitizer->sanitize();
$link = $document->getElementsByTagName( 'link' )->item( 0 );
$this->assertInstanceOf( 'DOMElement', $link );
$this->assertEquals( 'use-credentials', $link->getAttribute( 'crossorigin' ) );
}

/**
* Test CSS imports.
*
Expand Down