fix: handle relative and invalid URLs in redirects when passing URL to loadImage() #866
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #865, see the description of the original issue there.
This PR does two things:
One, this wraps the
http.get()
/https.get()
callback inloadImage()
in atry...catch
block so that exceptions are properly handled and passed to callers instead of crashing the Node process.Two, this passes
url.origin
as the second parameter tonew URL()
to ensure that "relative" redirects (where thelocation
header is something like/some/resource.jpg
instead ofhttps://site.com/some/resource.jpg
) do not cause the constructor to throw an exception.Redirects to another domain (for example, a
location
header tohttps://other.com
from the originhttps://site.com
) are not affected by adding the second parameter. Only relative URLs are affected -- the kind that currently cause crashes in user code.Without this fix, passing URLs to
loadImage()
does not reliably work when the URL has a redirect, and will instead crash the Node process without a full stack trace, even if the call toloadImage()
is in atry
block.