question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Location.getHref() throws MalformedURLException when location is "about:blank#foo"

See original GitHub issue

Problem in brief

java.net.MalformedURLException: unknown protocol: about is thrown by Location.getHref() when the location is about:blank and location.hash is also set.

Reproducing

The code below:

  • Prints about:blank#foo in Chrome / Firefox / Edge.
  • Prints about:blank in IE11.
  • Throws the aforementioned java.net.MalformedURLException: unknown protocol: about exception in HtmlUnit 2.46.0.
<html>
<head>
<script>
function test() {
    var iframe = document.getElementsByTagName('IFRAME')[0];

    iframe.contentWindow.location.hash = 'foo';
    console.log(iframe.contentWindow.location.href); // "about:blank#foo"
}
</script>
</head>
<body onload="test()">
<iframe src="about:blank"></iframe>
</body>
</html>

Details

The exception is thrown by this call to UrlUtils.getUrlWithNewRef() because it is internally using a version of new URL(...) that does not support the about protocol. Incidentally, the variable url itself initially holds “about:blank” because unlike getUrlWithNew*(), UrlUtils’s other methods toUrl*() do support about:blank.

Possible fixes

Solution 1: Patch Location.getHref() with a hack for about:blank

     public String getHref() {
         final Page page = window_.getWebWindow().getEnclosedPage();
...
         try {
             URL url = page.getUrl();
             final boolean encodeHash = getBrowserVersion().hasFeature(JS_LOCATION_HREF_HASH_IS_ENCODED);
             final String hash = getHash(encodeHash);
             if (hash != null) {
+                if ("about:blank".equals(url.toExternalForm())) {
+                    return url.toExternalForm() + "#" + hash;
+                }
                 url = UrlUtils.getUrlWithNewRef(url, hash);
             }
             String s = url.toExternalForm();

Solution 2: Change UrlUtils to better support about:blank across all its methods

 	private static URL createNewUrl(final String protocol, final String authority,
 			final String path, final String ref, final String query) throws MalformedURLException {
 
...
 		if (ref != null) {
 			if (ref.isEmpty() || ref.charAt(0) != '#') {
 				s.append('#');
 			}
 			s.append(ref);
 		}
 
-		return new URL(s.toString());
+		return toUrlUnsafe(s.toString());
 	}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rbricommented, Jan 25, 2021

i think this is fixed now; made a new Snapshot build available. Please reopen or open a new issue if you still have problems.

Many thanks for all the detailed reports and your support.

1reaction
rbricommented, Jan 24, 2021

Prints about:blank#foo in Chrome / Firefox / Edge.

I can only reproduce this with chrome/edge, in FF i get a security error - do you agree?

The exception should be fixed - please have a look at the latest snapshot (will be available soon - see twitter).

Read more comments on GitHub >

github_iconTop Results From Across the Web

java.net.MalformedURLException: no protocol on URL based ...
Thrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string...
Read more >
The URL Class - Learning Java [Book] - O'Reilly
If the protocol doesn't make sense, or if Java can't find a protocol handler for it, the URL constructor throws a MalformedURLException ....
Read more >
What is a MalformedURLException and how to fix it in java?
The only Solution for this is to make sure that the url you have passed is legal, with a proper protocol. The best...
Read more >
Class URL - java.net - developer.classpath.org!
If the protocol handler is not found in any of those locations, a MalformedURLException would be thrown. Please note that a protocol handler...
Read more >
URL (Java Platform SE 7 ) - Oracle Help Center
If this class does not exist, or if the class exists but it is not a subclass of URLStreamHandler , then a MalformedURLException...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found