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

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented May 15, 2021

Summary

As discovered in a support topic, if a stylesheet is printed without a hostname, this currently is resulting in a stylesheet fetch error, for example:

image

This can be simulated with the following PHP plugin code:

add_filter(
	'style_loader_src',
	function ( $src ) {
		if ( ! $src ) {
			return $src;
		}

		$parsed_src = wp_parse_url( $src );
		if ( ! isset( $parsed_src['path'] ) ) {
			return $src;
		}

		$src = $parsed_src['path'];
		if ( isset( $parsed_src['query'] ) ) {
			$src .= '?' . $parsed_src['query'];
		}

		return $src;
	}
);

The issue is that neither \AMP_Style_Sanitizer::get_validated_url_file_path() nor \AMP_Style_Sanitizer::fetch_external_stylesheet() are supplying the current host name if the provided $stylesheet_url is missing one. This can be resolved by prefixing the provided URL with the origin (host and port).

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@westonruter westonruter added Bug Something isn't working CSS labels May 15, 2021
@westonruter westonruter added this to the v2.1.2 milestone May 15, 2021
@westonruter westonruter added this to In Progress in Ongoing May 15, 2021
@westonruter westonruter self-assigned this May 15, 2021
@westonruter westonruter force-pushed the fix/hostless-stylesheet-urls branch from 027b325 to c50371f Compare May 15, 2021 23:34
@westonruter westonruter modified the milestones: v2.1.2, v2.1.3 May 17, 2021
@westonruter westonruter modified the milestones: v2.1.3, v2.2 Jun 10, 2021
@westonruter westonruter modified the milestones: v2.2, v2.3 Dec 1, 2021
@westonruter westonruter added the P2 Low priority label May 26, 2022
@westonruter westonruter modified the milestones: v2.3, v2.3.1 Jun 9, 2022
@westonruter westonruter modified the milestones: v2.3.1, v2.4 Jan 12, 2023
@thelovekesh
Copy link
Collaborator

@westonruter Any update on this? Should we make it ready for the next release?

@westonruter westonruter removed this from the v2.4 milestone Jan 24, 2023
@westonruter westonruter removed this from In Progress in Ongoing Jan 24, 2023
@westonruter westonruter modified the milestones: v2.4, v2.3.1 Jan 24, 2023
@westonruter westonruter removed their assignment Jan 24, 2023
@westonruter
Copy link
Member Author

@thelovekesh Yes, even though nobody else has reported this as an issue, we might as well do it. Feel free to add tests to finish this out.

@thelovekesh thelovekesh marked this pull request as ready for review January 25, 2023 10:39
@github-actions
Copy link
Contributor

github-actions bot commented Jan 25, 2023

Plugin builds for a90ef6a are ready 🛎️!

@thelovekesh thelovekesh self-assigned this Jan 25, 2023
@thelovekesh
Copy link
Collaborator

@westonruter This PR is ready for your review now.

@@ -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.

@westonruter westonruter modified the milestones: v2.3.1, v2.4 Jan 26, 2023
@westonruter
Copy link
Member Author

Humm. Let's punt this since we haven't received any other reports of it being a need and I can't seem to reproduce it.

@westonruter westonruter removed this from the v2.4 milestone Jan 26, 2023
@westonruter westonruter added this to Backlog in Ongoing Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working CSS P2 Low priority Punted
Projects
Ongoing
  
Backlog
Development

Successfully merging this pull request may close these issues.

None yet

2 participants