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

Scheme-relative URLs aren't handled correctly #10

Closed
michaeltelford opened this issue Mar 29, 2021 · 1 comment
Closed

Scheme-relative URLs aren't handled correctly #10

michaeltelford opened this issue Mar 29, 2021 · 1 comment
Assignees

Comments

@michaeltelford
Copy link
Owner

Description

Protocol-relative URLs aren't handled correctly. They are currently processed as relative when they are designed to be used for external resources. This has a knock on effect where these types of links are returned by Document#internal_links instead of external_links.

Additional reading: https://www.paulirish.com/2010/the-protocol-relative-url/

Reproduce

Steps to reproduce the behavior:

url = Wgit::Url.new '//fonts.google.com'
url.relative? # true - which isn't right, it should be false.

Expected Behavior

  1. Url#relative? should return false for a protocol relative URL.
  2. Document#external_links should return any protocol relative URLs, not internal_links. The returned URLs should have the correct @url.scheme prefixed so they are valid?.

Possible Solutions

Point 1 above can be achieved by adding the following line (not tested):

# Add this to Wgit::Url#relative? on line 164
return false if starts_with?('//)

Point 2 above can be achieved with something like (not tested):

# Replace this method in the Wgit::Document class
def external_links
    return [] if @links.empty?

    links = @links
        .map do |link|
            link = Wgit::Url.new("#{@url.scheme}:#{link}") if link.start_with?('//')
            link.omit_trailing_slash
        end
        .reject { |link| link.relative?(host: @url.to_origin) }

    Wgit::Utils.sanitize(links)
end

Tests

What tests would prove this bug is fixed?

  • See the expected behavior and write tests to assert it.
@michaeltelford michaeltelford self-assigned this Mar 29, 2021
@michaeltelford michaeltelford changed the title Protocol-relative URLs aren't handled correctly Scheme-relative URLs aren't handled correctly Mar 31, 2021
@michaeltelford
Copy link
Owner Author

Fixed in v0.10.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant