From 8ca34cda63ee1236cb6f1b1fc13a48c695cabf22 Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Sat, 17 Aug 2024 08:55:22 -0400
Subject: [PATCH] Naming: lookup fixes

- Don't lookup b33 in BFNS
- Fail immediately on bad b32
- Fix stripping .alt in ConvertToHash
- Strip URLs to hostname in ConvertToHash, for convenience
---
 .../i2p/router/naming/BlockfileNamingService.java   |  4 ++--
 core/java/src/net/i2p/util/ConvertToHash.java       | 13 ++++++++++++-
 .../src/net/i2p/router/client/LookupDestJob.java    |  3 +++
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/apps/addressbook/java/src/net/i2p/router/naming/BlockfileNamingService.java b/apps/addressbook/java/src/net/i2p/router/naming/BlockfileNamingService.java
index d72a23a8b2..2c3f8720c4 100644
--- a/apps/addressbook/java/src/net/i2p/router/naming/BlockfileNamingService.java
+++ b/apps/addressbook/java/src/net/i2p/router/naming/BlockfileNamingService.java
@@ -797,7 +797,7 @@ public class BlockfileNamingService extends DummyNamingService {
             if (d != null)
                 return d;
             // Base32 failed?
-            if (hostname.length() == BASE32_HASH_LENGTH + 8 && hostname.toLowerCase(Locale.US).endsWith(".b32.i2p"))
+            if (hostname.length() >= BASE32_HASH_LENGTH + 8 && hostname.toLowerCase(Locale.US).endsWith(".b32.i2p"))
                 return null;
         }
 
@@ -848,7 +848,7 @@ public class BlockfileNamingService extends DummyNamingService {
      */
     private List<Destination> lookupAll2(String hostname, Properties lookupOptions, List<Properties> storedOptions) {
         // only use cache for b32
-        if (hostname.length() == BASE32_HASH_LENGTH + 8 && hostname.toLowerCase(Locale.US).endsWith(".b32.i2p")) {
+        if (hostname.length() >= BASE32_HASH_LENGTH + 8 && hostname.toLowerCase(Locale.US).endsWith(".b32.i2p")) {
             Destination d = super.lookup(hostname, null, null);
             if (d != null) {
                 if (storedOptions != null)
diff --git a/core/java/src/net/i2p/util/ConvertToHash.java b/core/java/src/net/i2p/util/ConvertToHash.java
index 2cb2896a4f..5fd460d726 100644
--- a/core/java/src/net/i2p/util/ConvertToHash.java
+++ b/core/java/src/net/i2p/util/ConvertToHash.java
@@ -33,9 +33,20 @@ public class ConvertToHash {
         if (peer == null)
             return null;
         String peerLC = peer.toLowerCase(Locale.US);
+        if (peerLC.startsWith("http://")) {
+            peer = peer.substring(7);
+            peerLC = peerLC.substring(7);
+        } else if (peerLC.startsWith("https://")) {
+            peer = peer.substring(8);
+            peerLC = peerLC.substring(8);
+        }
+        if (peer.endsWith("/")) {
+            peer = peer.substring(0, peer.length() - 1);
+            peerLC = peerLC.substring(0, peerLC.length() - 1);
+        }
         if (peerLC.endsWith(".i2p.alt")) {
             peer = peer.substring(0, peer.length() - 4);
-            peerLC = peerLC.substring(0, peer.length() - 4);
+            peerLC = peerLC.substring(0, peerLC.length() - 4);
         }
         // b64 hash
         if (peer.length() == 44 && !peerLC.endsWith(".i2p")) {
diff --git a/router/java/src/net/i2p/router/client/LookupDestJob.java b/router/java/src/net/i2p/router/client/LookupDestJob.java
index 2bb8c1de8c..22321c7af5 100644
--- a/router/java/src/net/i2p/router/client/LookupDestJob.java
+++ b/router/java/src/net/i2p/router/client/LookupDestJob.java
@@ -126,6 +126,9 @@ class LookupDestJob extends JobImpl {
                             // h and name both null, runJob will fail immediately
                         }
                     }
+                } else {
+                    // base32 decode fail
+                    name = null;
                 }
             }
         }
-- 
GitLab