Rejects additional urls with %20 and fixes Wss ones.

This commit is contained in:
Vitor Pamplona
2025-10-10 10:55:35 -04:00
parent 5975130824
commit 5c09cd0b55
2 changed files with 17 additions and 4 deletions

View File

@@ -87,7 +87,7 @@ class RelayUrlNormalizer {
@OptIn(ExperimentalContracts::class) @OptIn(ExperimentalContracts::class)
fun fix(url: String): String? { fun fix(url: String): String? {
if (url.length < 4) return null if (url.length < 4) return null
if (url.contains("%00")) return null if (url.contains("%00") || url.contains("%20")) return null
if (url.length > 50) { if (url.length > 50) {
// removes multiple urls in the same line // removes multiple urls in the same line
@@ -138,9 +138,14 @@ class RelayUrlNormalizer {
return "wss://${trimmed.drop(6)}" return "wss://${trimmed.drop(6)}"
} }
// fast for good ww:// urls
if (trimmed.startsWith("Wss://")) {
return "wss://${trimmed.drop(6)}"
}
if (trimmed.contains("://")) { if (trimmed.contains("://")) {
// some other scheme we cannot connect to. // some other scheme we cannot connect to.
Log.w("RelayUrlNormalizer", "Rejected relay URL: $url") Log.w("RelayUrlNormalizer", "Rejected $url")
return null return null
} }
@@ -173,14 +178,14 @@ class RelayUrlNormalizer {
normalizedUrls.put(url, NormalizationResult.Success(normalized)) normalizedUrls.put(url, NormalizationResult.Success(normalized))
normalized normalized
} else { } else {
Log.w("NormalizedRelayUrl", "Rejected Error $url") Log.w("NormalizedRelayUrl", "Rejected $url")
normalizedUrls.put(url, NormalizationResult.Error) normalizedUrls.put(url, NormalizationResult.Error)
null null
} }
} catch (e: Exception) { } catch (e: Exception) {
if (e is CancellationException) throw e if (e is CancellationException) throw e
normalizedUrls.put(url, NormalizationResult.Error) normalizedUrls.put(url, NormalizationResult.Error)
Log.w("NormalizedRelayUrl", "Rejected Error $url") Log.w("NormalizedRelayUrl", "Rejected $url")
null null
} }
} }

View File

@@ -23,6 +23,7 @@ package com.vitorpamplona.quartz.nip01Core.relay
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.RelayUrlNormalizer import com.vitorpamplona.quartz.nip01Core.relay.normalizer.RelayUrlNormalizer
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertNull
class RelayUrlFormatterTest { class RelayUrlFormatterTest {
@Test @Test
@@ -43,5 +44,12 @@ class RelayUrlFormatterTest {
assertEquals("ws://a.onion/", RelayUrlNormalizer.normalizeOrNull("a.onion/")?.url) assertEquals("ws://a.onion/", RelayUrlNormalizer.normalizeOrNull("a.onion/")?.url)
assertEquals("wss://nostr.mom/", RelayUrlNormalizer.normalizeOrNull("wss://nostr.mom")?.url) assertEquals("wss://nostr.mom/", RelayUrlNormalizer.normalizeOrNull("wss://nostr.mom")?.url)
assertEquals("wss://relay.nostr.band/", RelayUrlNormalizer.normalizeOrNull("Wss://relay.nostr.band")?.url)
}
@Test
fun weirdRelay() {
assertNull(RelayUrlNormalizer.normalizeOrNull("wss://relay%20list%20to%20discover%20the%20user's%20content"))
} }
} }