[minor] Bug in ObjectLoader's URL regex
See original GitHub issueThe regex in question:
/^(\/\/)|([a-z]+:(\/\/)?)/i
The second part of the branch should also have a ^.
How about this instead?
/^(\/|[a-z][a-z0-9\+\.\-]*:)/i
Reasoning:
- The
^needs to be outside of the group with|, so that it applies to both. Currently, it matches “a/x?y:z”, which is a relative path. - A single slash begins a relative URL to an absolute path, so only check for one slash. Currently, “/a?b=c” can turn into “http://example.com/texture/path//a?b=c”.
- The URI scheme (the part before the
:) is as specified on Wikipedia.- The backslashes in the character class aren’t strictly necessary.
- The check for “maybe two slashes” in the second branch is redundant. (Optional tails don’t affect regex tests.)
- Removed redundant parentheses.
The scheme part of the regex could be [a-z]+ for the sake of performance. Most schemes of interest only use letters. However, the cost of the regex test will probably be much smaller than the cost of loading the image itself.
The single-leading-slash case might need to be handled separately. Maybe an absolute path should resolve to “http://example.com/a?b=c” instead of “http://example.com/texture/path//a?b=c”. That’s something that needs to be decided and documented.
Also, “https:/a?b=c” is supposedly valid as an absolute path relative to the current host over a different protocol. It’s unlikely that anyone will use that, though, and both Chrome and Firefox resolve that path to “https://a?b=c”.
Alternatively, the regex can be replaced by a path-resolving utility function. If we don’t want a library, we should find a well-tested snippet somewhere.
- MDN has a snippet that can be adapted.
- Here’s a shim for URLUtils..
- A DOM-based hack using the
<base>tag.
Since Not Invented Here is more fun, I found a few ways of implementing it. Here’s one using the URL constructor (which might require the above shim):
path = new URL(image.url, scope.texturePath).href;
I think scope.texturePath must be an absolute URL (not path) here.
It’s, uh, just a minor bug.
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (8 by maintainers)

Top Related StackOverflow Question
Closing. Let’s only change the regex in
ObjectLoaderif other users reports issues in that regard. AFAIK, this was not the case since 2016.https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest says that
responseType = 'json'is not supported on Internet Explorer.