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

Detect amp_css_bad_file_extension prior to looking at filesystem #890

Merged
merged 1 commit into from
Jan 26, 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
14 changes: 7 additions & 7 deletions includes/class-amp-wp-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ public function get_validated_css_file_path( $src, $handle ) {

/** This filter is documented in wp-includes/class.wp-styles.php */
$src = apply_filters( 'style_loader_src', $src, $handle );
$src = esc_url_raw( $src );

// Strip query and fragment from URL.
$src = preg_replace( ':[\?#].+:', '', $src );
$src = esc_url_raw( $src );
$src = preg_replace( ':[\?#].*$:', '', $src );

if ( ! preg_match( '/\.(css|less|scss|sass)$/i', $src ) ) {
/* translators: %1$s is stylesheet handle, %2$s is stylesheet URL */
return new WP_Error( 'amp_css_bad_file_extension', sprintf( __( 'Skipped stylesheet %1$s which does not have recognized CSS file extension (%2$s).', 'amp' ), $handle, $src ) );
}

$includes_url = includes_url( '/' );
$content_url = content_url( '/' );
Expand All @@ -77,11 +82,6 @@ public function get_validated_css_file_path( $src, $handle ) {
$css_path = ABSPATH . 'wp-admin' . substr( $src, strlen( $admin_url ) - 1 );
}

if ( ! preg_match( '/\.(css|less|scss|sass)$/i', $css_path ) ) {
/* translators: %1$s is stylesheet handle, %2$s is stylesheet URL */
return new WP_Error( 'amp_css_bad_file_extension', sprintf( __( 'Skipped stylesheet %1$s which does not have recognized CSS file extension (%2$s).', 'amp' ), $handle, $src ) );
}

if ( ! $css_path || false !== strpos( '../', $css_path ) || 0 !== validate_file( $css_path ) || ! file_exists( $css_path ) ) {
/* translators: %1$s is stylesheet handle, %2$s is stylesheet URL */
return new WP_Error( 'amp_css_path_not_found', sprintf( __( 'Unable to locate filesystem path for stylesheet %1$s (%2$s).', 'amp' ), $handle, $src ) );
Expand Down
4 changes: 4 additions & 0 deletions tests/test-class-amp-wp-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public function test_get_validated_css_file_path() {
$r = $wp_styles->get_validated_css_file_path( content_url( 'themes/twentyseventeen/404.css' ), 'bad' );
$this->assertInstanceOf( 'WP_Error', $r );
$this->assertEquals( 'amp_css_path_not_found', $r->get_error_code() );

$r = $wp_styles->get_validated_css_file_path( get_template_directory() . '/style.css', 'bad' );
$this->assertInstanceOf( 'WP_Error', $r );
$this->assertEquals( 'amp_css_path_not_found', $r->get_error_code() );
}

/**
Expand Down