From fa16c5b20a92c8ee7489b612e384dd2a28ee6137 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 17 Jun 2020 19:47:28 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20Fix=20breaking=20parser=20on=20in?= =?utf8?q?valid=20SSK/USKs?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/text/SoneTextParser.kt | 19 ++++++++++++------- .../net/pterodactylus/sone/text/SoneTextParserTest.kt | 9 +++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt index 24b04fa..c97a1fc 100644 --- a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt +++ b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt @@ -66,13 +66,15 @@ class SoneTextParser @Inject constructor(private val soneProvider: SoneProvider? } SSK, USK -> try { - FreenetURI(linkWithoutBacklink).let { uri -> - uri.allMetaStrings - ?.takeIf { (it.size > 1) || ((it.size == 1) && (it.single() != "")) } - ?.lastOrNull() - ?: uri.docName - ?: "${uri.keyType}@${uri.routingKey.asFreenetBase64}" - }.let { FreenetLinkPart(linkWithoutBacklink.removeSuffix("/"), it, trusted = context?.routingKey?.contentEquals(FreenetURI(linkWithoutBacklink).routingKey) == true) } + FreenetURI(linkWithoutBacklink) + .workaroundForFaultyConstructorInFred1485AndBelow() + .let { uri -> + uri.allMetaStrings + ?.takeIf { (it.size > 1) || ((it.size == 1) && (it.single() != "")) } + ?.lastOrNull() + ?: uri.docName + ?: "${uri.keyType}@${uri.routingKey.asFreenetBase64}" + }.let { FreenetLinkPart(linkWithoutBacklink.removeSuffix("/"), it, trusted = context?.routingKey?.contentEquals(FreenetURI(linkWithoutBacklink).routingKey) == true) } } catch (e: MalformedURLException) { PlainTextPart(linkWithoutBacklink) } @@ -93,6 +95,9 @@ class SoneTextParser @Inject constructor(private val soneProvider: SoneProvider? } +private fun FreenetURI.workaroundForFaultyConstructorInFred1485AndBelow() = + also { if (it.routingKey == null) throw MalformedURLException("SSK/USK without routing key") } + private fun List.mergeMultipleEmptyLines() = fold(emptyList()) { previous, current -> if (previous.isEmpty()) { previous + current diff --git a/src/test/kotlin/net/pterodactylus/sone/text/SoneTextParserTest.kt b/src/test/kotlin/net/pterodactylus/sone/text/SoneTextParserTest.kt index 3367faf..bd14858 100644 --- a/src/test/kotlin/net/pterodactylus/sone/text/SoneTextParserTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/text/SoneTextParserTest.kt @@ -18,6 +18,7 @@ package net.pterodactylus.sone.text import com.google.inject.Guice.* +import freenet.keys.FreenetURI import net.pterodactylus.sone.data.* import net.pterodactylus.sone.data.impl.* import net.pterodactylus.sone.database.* @@ -251,6 +252,14 @@ class SoneTextParserTest { } @Test + fun `broken usk links is parsed as plain text`() { + val context = SoneTextParserContext(IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU")) + val parts = soneTextParser.parse("USK@/someCrazyName.R1/0", context) + FreenetURI("USK@/someCrazyName.R1/0") + assertThat("Part Text", convertText(parts), equalTo("USK@/someCrazyName.R1/0")) + } + + @Test fun `test basic ksk links`() { val parts: Iterable = soneTextParser.parse("KSK@gpl.txt", null) assertThat("Part Text", convertText(parts, FreenetLinkPart::class.java), equalTo("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]")) -- 2.7.4