From 415b51bc490ee3d0639f87050182a6c8593e56c1 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sat, 28 Nov 2015 12:54:41 +0000
Subject: [PATCH] i2psnark: Fix NPE caused by URL-to-URI conversion in -2
 (ticket #1715) Fix some other similar places

---
 .../java/src/org/klomp/snark/TrackerClient.java    |  2 ++
 .../net/i2p/client/streaming/I2PSocketEepGet.java  |  2 ++
 core/java/src/net/i2p/util/EepGet.java             | 14 +++++++++++---
 core/java/src/net/i2p/util/EepHead.java            | 14 +++++++++++---
 core/java/src/net/i2p/util/SSLEepGet.java          |  2 ++
 5 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/apps/i2psnark/java/src/org/klomp/snark/TrackerClient.java b/apps/i2psnark/java/src/org/klomp/snark/TrackerClient.java
index fc48c6d87b..5397cd757b 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/TrackerClient.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/TrackerClient.java
@@ -907,6 +907,8 @@ public class TrackerClient implements Runnable {
     if (!"http".equals(url.getScheme()))
         return null;
     String host = url.getHost();
+    if (host == null)
+        return null;
     if (host.endsWith(".i2p"))
         return ConvertToHash.getHash(host);
     if (host.equals("i2p")) {
diff --git a/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketEepGet.java b/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketEepGet.java
index 73ede8bb05..bd82c12f3a 100644
--- a/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketEepGet.java
+++ b/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketEepGet.java
@@ -116,6 +116,8 @@ public class I2PSocketEepGet extends EepGet {
             URI url = new URI(_actualURL);
             if ("http".equals(url.getScheme())) {
                 String host = url.getHost();
+                if (host == null)
+                    throw new MalformedURLException("no hostname: " + _actualURL);
                 int port = url.getPort();
                 if (port <= 0 || port > 65535)
                     port = 80;
diff --git a/core/java/src/net/i2p/util/EepGet.java b/core/java/src/net/i2p/util/EepGet.java
index 19816bb41d..1c2406291a 100644
--- a/core/java/src/net/i2p/util/EepGet.java
+++ b/core/java/src/net/i2p/util/EepGet.java
@@ -731,11 +731,17 @@ public class EepGet {
                     // RFC 1945 (HTTP/1.0 1996), so it isn't clear what the point of this is.
                     // This oddly adds a ":" even if no port, but that seems to work.
                     URI url = new URI(_actualURL);
-		    if (_redirectLocation.startsWith("/"))
-                        _actualURL = "http://" + url.getHost() + ":" + url.getPort() + _redirectLocation;
+                    String host = url.getHost();
+                    if (host == null)
+                        throw new MalformedURLException("Redirected to invalid URL");
+                    int port = url.getPort();
+                    if (port < 0)
+                        port = 80;
+                    if (_redirectLocation.startsWith("/"))
+                        _actualURL = "http://" + host + ":" + port + _redirectLocation;
                     else
                         // this blows up completely on a redirect to https://, for example
-                        _actualURL = "http://" + url.getHost() + ":" + url.getPort() + "/" + _redirectLocation;
+                        _actualURL = "http://" + host+ ":" + port + "/" + _redirectLocation;
                 }
             } catch (URISyntaxException use) {
                 IOException ioe = new MalformedURLException("Redirected to invalid URL");
@@ -1232,6 +1238,8 @@ public class EepGet {
                 URI url = new URI(_actualURL);
                 if ("http".equals(url.getScheme())) {
                     String host = url.getHost();
+                    if (host == null)
+                        throw new MalformedURLException("URL is not supported:" + _actualURL);
                     String hostlc = host.toLowerCase(Locale.US);
                     if (hostlc.endsWith(".i2p"))
                         throw new UnknownHostException("I2P addresses must be proxied");
diff --git a/core/java/src/net/i2p/util/EepHead.java b/core/java/src/net/i2p/util/EepHead.java
index 3c53314937..c9f375e458 100644
--- a/core/java/src/net/i2p/util/EepHead.java
+++ b/core/java/src/net/i2p/util/EepHead.java
@@ -186,11 +186,17 @@ public class EepHead extends EepGet {
                     // RFC 1945 (HTTP/1.0 1996), so it isn't clear what the point of this is.
                     // This oddly adds a ":" even if no port, but that seems to work.
                     URI url = new URI(_actualURL);
-		    if (_redirectLocation.startsWith("/"))
-                        _actualURL = "http://" + url.getHost() + ":" + url.getPort() + _redirectLocation;
+                    String host = url.getHost();
+                    if (host == null)
+                        throw new MalformedURLException("Redirected to invalid URL");
+                    int port = url.getPort();
+                    if (port < 0)
+                        port = 80;
+                    if (_redirectLocation.startsWith("/"))
+                        _actualURL = "http://" + host + ":" + port + _redirectLocation;
                     else
                         // this blows up completely on a redirect to https://, for example
-                        _actualURL = "http://" + url.getHost() + ":" + url.getPort() + "/" + _redirectLocation;
+                        _actualURL = "http://" + host+ ":" + port + "/" + _redirectLocation;
                 }
             } catch (URISyntaxException use) {
                 IOException ioe = new MalformedURLException("Redirected to invalid URL");
@@ -264,6 +270,8 @@ public class EepHead extends EepGet {
             throw ioe;
         }
         String host = url.getHost();
+        if (host == null)
+            throw new MalformedURLException("Bad URL");
         int port = url.getPort();
         String path = url.getRawPath();
         String query = url.getRawQuery();
diff --git a/core/java/src/net/i2p/util/SSLEepGet.java b/core/java/src/net/i2p/util/SSLEepGet.java
index c62da0c125..35905dec21 100644
--- a/core/java/src/net/i2p/util/SSLEepGet.java
+++ b/core/java/src/net/i2p/util/SSLEepGet.java
@@ -560,6 +560,8 @@ public class SSLEepGet extends EepGet {
             URI url = new URI(_actualURL);
             if ("https".equals(url.getScheme())) {
                 host = url.getHost();
+                if (host == null)
+                    throw new MalformedURLException("Bad URL");
                 if (host.toLowerCase(Locale.US).endsWith(".i2p"))
                     throw new MalformedURLException("I2P addresses unsupported");
                 port = url.getPort();
-- 
GitLab