From 32b652775a41781f74944b2b785c0f5a133654c9 Mon Sep 17 00:00:00 2001 From: Denis Loh Date: Wed, 25 Jun 2025 14:28:23 +0200 Subject: [PATCH 1/6] Removed negative look ahead for anchors --- .../net/folivo/trixnity/core/MatrixRegex.kt | 20 +++++++++---------- .../folivo/trixnity/core/MatrixRegexTest.kt | 13 ++++++++++-- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt b/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt index dff286d91..331462968 100644 --- a/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt +++ b/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt @@ -74,8 +74,8 @@ object MatrixRegex { private val eventPermalinkRegex = """$matrixToRegex($exlMark$opaqueIdRegex$colon$servernameRegex)\/$dollar($opaqueIdRegex(?:$colon$servernameRegex)?)$viaArgumentRegex?""" - private fun getAnchor(regex: String, maxLength: Int): String = - "(.*?)<\\/a>" + private fun getAnchor(regex: String): String = + "(.*?)<\\/a>" val domain by lazy { servernameRegex.toRegex() } val userIdLocalpart by lazy { userLocalpartRegex.toRegex(255) } @@ -98,15 +98,15 @@ object MatrixRegex { private val roomAliasPermalink by lazy { roomAliasPermalinkRegex.toRegex(255) } private val eventIdPermalink by lazy { eventPermalinkRegex.toRegex(255) } - internal val userIdPermalinkAnchor by lazy { getAnchor(userPermalinkRegex, 255).toRegex() } - internal val roomIdPermalinkAnchor by lazy { getAnchor(roomIdPermalinkRegex, 255).toRegex() } - internal val roomAliasPermalinkAnchor by lazy { getAnchor(roomAliasPermalinkRegex, 255).toRegex() } - internal val eventIdPermalinkAnchor by lazy { getAnchor(eventPermalinkRegex, 255).toRegex() } + internal val userIdPermalinkAnchor by lazy { getAnchor(userPermalinkRegex).toRegex() } + internal val roomIdPermalinkAnchor by lazy { getAnchor(roomIdPermalinkRegex).toRegex() } + internal val roomAliasPermalinkAnchor by lazy { getAnchor(roomAliasPermalinkRegex).toRegex() } + internal val eventIdPermalinkAnchor by lazy { getAnchor(eventPermalinkRegex).toRegex() } - private val userIdUriAnchor by lazy { getAnchor(userUriRegex, 255).toRegex() } - private val roomIdUriAnchor by lazy { getAnchor(roomIdUriRegex, 255).toRegex() } - private val roomAliasUriAnchor by lazy { getAnchor(roomAliasUriRegex, 255).toRegex() } - private val eventIdUriAnchor by lazy { getAnchor(eventUriRegex, 255).toRegex() } + private val userIdUriAnchor by lazy { getAnchor(userUriRegex).toRegex() } + private val roomIdUriAnchor by lazy { getAnchor(roomIdUriRegex).toRegex() } + private val roomAliasUriAnchor by lazy { getAnchor(roomAliasUriRegex).toRegex() } + private val eventIdUriAnchor by lazy { getAnchor(eventUriRegex).toRegex() } fun findMentions(message: String): Map { val mentions = findUserIdMentions(message) diff --git a/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt b/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt index a952f3d1a..8e43d7281 100644 --- a/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt +++ b/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt @@ -865,11 +865,12 @@ class MatrixRegexTest : TrixnityBaseTest() { val karl = "Dr. Karl Tanaka (Demo Bot)" val wolfgang = "Dr. Wolfgang Reidorf (Demo Bot)" + val birgit = "Dr. Birgit Simonis (Demo Bot)" - val message = "$karl und $wolfgang wie geht's euch?" + val message = "$karl, $birgit und $wolfgang wie geht's euch?" val result = findMentions(message) - result.size shouldBe 2 + result.size shouldBe 3 result.values.any { it.match == karl @@ -886,6 +887,14 @@ class MatrixRegexTest : TrixnityBaseTest() { "demobot2", "demo.example.de" ) + + result.values.any { + it.match == birgit + } shouldBe true + (result.entries.first { it.value.match == birgit }.value as Mention.User).userId shouldBe UserId( + "demobot12", + "demo.example.com" + ) } // Permalink: Room Alias -- GitLab From e0409867f38506c0ddddbbb12a4b3033ebf6abe7 Mon Sep 17 00:00:00 2001 From: Denis Loh Date: Wed, 25 Jun 2025 14:41:16 +0200 Subject: [PATCH 2/6] Updated changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7eb8db8c..5c3aa5718 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fixed MatrixRegex.findMentions with longer texts + ### Security ## 4.16.5 -- GitLab From 2cbd711abd2ac6b1ea9bb0082cca1a45bf6ea2ce Mon Sep 17 00:00:00 2001 From: Denis Loh Date: Thu, 26 Jun 2025 14:07:35 +0200 Subject: [PATCH 3/6] Fixed length --- .../net/folivo/trixnity/core/MatrixRegex.kt | 23 +++++++++---------- .../folivo/trixnity/core/MatrixRegexTest.kt | 4 +++- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt b/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt index 331462968..77c2d8f0e 100644 --- a/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt +++ b/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt @@ -74,8 +74,8 @@ object MatrixRegex { private val eventPermalinkRegex = """$matrixToRegex($exlMark$opaqueIdRegex$colon$servernameRegex)\/$dollar($opaqueIdRegex(?:$colon$servernameRegex)?)$viaArgumentRegex?""" - private fun getAnchor(regex: String): String = - "(.*?)<\\/a>" + private fun getAnchor(regex: String, maxLength: Int): String = + "(.*?)<\\/a>" val domain by lazy { servernameRegex.toRegex() } val userIdLocalpart by lazy { userLocalpartRegex.toRegex(255) } @@ -98,15 +98,15 @@ object MatrixRegex { private val roomAliasPermalink by lazy { roomAliasPermalinkRegex.toRegex(255) } private val eventIdPermalink by lazy { eventPermalinkRegex.toRegex(255) } - internal val userIdPermalinkAnchor by lazy { getAnchor(userPermalinkRegex).toRegex() } - internal val roomIdPermalinkAnchor by lazy { getAnchor(roomIdPermalinkRegex).toRegex() } - internal val roomAliasPermalinkAnchor by lazy { getAnchor(roomAliasPermalinkRegex).toRegex() } - internal val eventIdPermalinkAnchor by lazy { getAnchor(eventPermalinkRegex).toRegex() } + internal val userIdPermalinkAnchor by lazy { getAnchor(userPermalinkRegex, 255).toRegex() } + internal val roomIdPermalinkAnchor by lazy { getAnchor(roomIdPermalinkRegex, 255).toRegex() } + internal val roomAliasPermalinkAnchor by lazy { getAnchor(roomAliasPermalinkRegex, 255).toRegex() } + internal val eventIdPermalinkAnchor by lazy { getAnchor(eventPermalinkRegex, 255).toRegex() } - private val userIdUriAnchor by lazy { getAnchor(userUriRegex).toRegex() } - private val roomIdUriAnchor by lazy { getAnchor(roomIdUriRegex).toRegex() } - private val roomAliasUriAnchor by lazy { getAnchor(roomAliasUriRegex).toRegex() } - private val eventIdUriAnchor by lazy { getAnchor(eventUriRegex).toRegex() } + private val userIdUriAnchor by lazy { getAnchor(userUriRegex, 255).toRegex() } + private val roomIdUriAnchor by lazy { getAnchor(roomIdUriRegex, 255).toRegex() } + private val roomAliasUriAnchor by lazy { getAnchor(roomAliasUriRegex, 255).toRegex() } + private val eventIdUriAnchor by lazy { getAnchor(eventUriRegex, 255).toRegex() } fun findMentions(message: String): Map { val mentions = findUserIdMentions(message) @@ -292,5 +292,4 @@ object MatrixRegex { private fun String.toRegex(maxLength: Int) = "(?!.{${maxLength + 1},})$this".toRegex() private fun IntRange.contains(other: IntRange): Boolean = - this.start <= other.start && other.endInclusive <= this.endInclusive && this != other - + this.start <= other.start && other.endInclusive <= this.endInclusive && this != other \ No newline at end of file diff --git a/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt b/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt index 8e43d7281..113533e43 100644 --- a/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt +++ b/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt @@ -867,7 +867,9 @@ class MatrixRegexTest : TrixnityBaseTest() { "Dr. Wolfgang Reidorf (Demo Bot)" val birgit = "Dr. Birgit Simonis (Demo Bot)" - val message = "$karl, $birgit und $wolfgang wie geht's euch?" + val mallory = "Dr. Mallory Böse (Demo Bot)" + + val message = "$karl, $birgit, $mallory und $wolfgang wie geht's euch?" val result = findMentions(message) result.size shouldBe 3 -- GitLab From 75c9b309dc57de4467c301e9fb718d0824517a9a Mon Sep 17 00:00:00 2001 From: Denis Loh Date: Fri, 27 Jun 2025 11:22:11 +0200 Subject: [PATCH 4/6] Fixed length constraint also for the other regex --- .../net/folivo/trixnity/core/MatrixRegex.kt | 71 +++++++++++-------- .../folivo/trixnity/core/MatrixRegexTest.kt | 37 ++++++++-- 2 files changed, 73 insertions(+), 35 deletions(-) diff --git a/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt b/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt index 77c2d8f0e..a9394eff2 100644 --- a/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt +++ b/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt @@ -49,19 +49,24 @@ object MatrixRegex { private val eventIdRegex = """\$($opaqueIdRegex(?::$servernameRegex)?)""" // https://spec.matrix.org/v1.11/appendices/#matrix-uri-scheme + private const val matrixProtocolPrefixRegex = """matrix:""" + private const val userUriPrefixRegex = """${matrixProtocolPrefixRegex}u\/""" + private const val roomIdUriPrefixRegex = """${matrixProtocolPrefixRegex}roomid\/""" + private const val roomAliasUriPrefixRegex = """${matrixProtocolPrefixRegex}r\/""" + private val queryParameterRegex = """($qesMark$namespaceIdRegex$eq([^\s&]+)(?:$amp$namespaceIdRegex$eq([^\s&]+))*)""" private val userUriRegex = - """matrix:u\/($userLocalpartRegex):($servernameRegex)$queryParameterRegex?""" + """$userUriPrefixRegex($userLocalpartRegex):($servernameRegex)$queryParameterRegex?""" private val roomIdUriRegex = - """matrix:roomid\/($opaqueIdRegex):($servernameRegex)$queryParameterRegex?""" + """$roomIdUriPrefixRegex($opaqueIdRegex):($servernameRegex)$queryParameterRegex?""" private val roomAliasUriRegex = - """matrix:r\/($roomAliasLocalpartRegex):($servernameRegex)$queryParameterRegex?""" + """$roomAliasUriPrefixRegex($roomAliasLocalpartRegex):($servernameRegex)$queryParameterRegex?""" private val eventUriRegex = - """matrix:(roomid\/$opaqueIdRegex:$servernameRegex)\/e\/($opaqueIdRegex)$queryParameterRegex?""" + """$matrixProtocolPrefixRegex(roomid\/$opaqueIdRegex:$servernameRegex)\/e\/($opaqueIdRegex)$queryParameterRegex?""" // https://spec.matrix.org/v1.11/appendices/#matrixto-navigation - private val viaArgumentRegex = """(?:\?(via=$servernameRegex))""" + private val viaArgumentRegex = """(?:$qesMark(via=$servernameRegex))""" private val matrixToRegex = """https?:\/\/matrix\.to\/$hash\/""" private val userPermalinkRegex = """$matrixToRegex$at($userLocalpartRegex)$colon($servernameRegex)$viaArgumentRegex?""" @@ -74,8 +79,12 @@ object MatrixRegex { private val eventPermalinkRegex = """$matrixToRegex($exlMark$opaqueIdRegex$colon$servernameRegex)\/$dollar($opaqueIdRegex(?:$colon$servernameRegex)?)$viaArgumentRegex?""" - private fun getAnchor(regex: String, maxLength: Int): String = - "(.*?)<\\/a>" + // matches at "word"-boundary + private fun getUriRegex(regex: String, maxLength: Int, prefix: String): String = + """(?:^|\s+)(?:(?=$prefix.{0,$maxLength}?(?:\?.*)?(?:\s+|$))$regex)(?:\s+|$)""" + + private fun getAnchor(regex: String, maxLength: Int, prefix: String): String = + """(.*?)<\/a>""" val domain by lazy { servernameRegex.toRegex() } val userIdLocalpart by lazy { userLocalpartRegex.toRegex(255) } @@ -88,25 +97,25 @@ object MatrixRegex { val roomAlias by lazy { roomAliasRegex.toRegex(255) } val eventId by lazy { eventIdRegex.toRegex(255) } - val userIdUri by lazy { userUriRegex.toRegex(255) } - val roomIdUri by lazy { roomIdUriRegex.toRegex(255) } - val roomAliasUri by lazy { roomAliasUriRegex.toRegex(255) } - val eventIdUri by lazy { eventUriRegex.toRegex(255) } + val userIdUri by lazy { getUriRegex(userUriRegex, 255, userUriPrefixRegex).toRegex() } + val roomIdUri by lazy { getUriRegex(roomIdUriRegex, 255, roomIdUriPrefixRegex).toRegex() } + val roomAliasUri by lazy { getUriRegex(roomAliasUriRegex, 255, roomAliasUriPrefixRegex).toRegex() } + val eventIdUri by lazy { getUriRegex(eventUriRegex, 255, roomIdUriPrefixRegex).toRegex() } - private val userIdPermalink by lazy { userPermalinkRegex.toRegex(255) } - private val roomIdPermalink by lazy { roomIdPermalinkRegex.toRegex(255) } - private val roomAliasPermalink by lazy { roomAliasPermalinkRegex.toRegex(255) } - private val eventIdPermalink by lazy { eventPermalinkRegex.toRegex(255) } + private val userIdPermalink by lazy { getUriRegex(userPermalinkRegex, 255, matrixToRegex).toRegex() } + private val roomIdPermalink by lazy { getUriRegex(roomIdPermalinkRegex, 255, matrixToRegex).toRegex() } + private val roomAliasPermalink by lazy { getUriRegex(roomAliasPermalinkRegex, 255, matrixToRegex).toRegex() } + private val eventIdPermalink by lazy { getUriRegex(eventPermalinkRegex, 255, matrixToRegex).toRegex() } - internal val userIdPermalinkAnchor by lazy { getAnchor(userPermalinkRegex, 255).toRegex() } - internal val roomIdPermalinkAnchor by lazy { getAnchor(roomIdPermalinkRegex, 255).toRegex() } - internal val roomAliasPermalinkAnchor by lazy { getAnchor(roomAliasPermalinkRegex, 255).toRegex() } - internal val eventIdPermalinkAnchor by lazy { getAnchor(eventPermalinkRegex, 255).toRegex() } + internal val userIdPermalinkAnchor by lazy { getAnchor(userPermalinkRegex, 255, matrixToRegex).toRegex() } + internal val roomIdPermalinkAnchor by lazy { getAnchor(roomIdPermalinkRegex, 255, matrixToRegex).toRegex() } + internal val roomAliasPermalinkAnchor by lazy { getAnchor(roomAliasPermalinkRegex, 255, matrixToRegex).toRegex() } + internal val eventIdPermalinkAnchor by lazy { getAnchor(eventPermalinkRegex, 255, matrixToRegex).toRegex() } - private val userIdUriAnchor by lazy { getAnchor(userUriRegex, 255).toRegex() } - private val roomIdUriAnchor by lazy { getAnchor(roomIdUriRegex, 255).toRegex() } - private val roomAliasUriAnchor by lazy { getAnchor(roomAliasUriRegex, 255).toRegex() } - private val eventIdUriAnchor by lazy { getAnchor(eventUriRegex, 255).toRegex() } + private val userIdUriAnchor by lazy { getAnchor(userUriRegex, 255, userUriPrefixRegex).toRegex() } + private val roomIdUriAnchor by lazy { getAnchor(roomIdUriRegex, 255, roomIdUriPrefixRegex).toRegex() } + private val roomAliasUriAnchor by lazy { getAnchor(roomAliasUriRegex, 255, roomAliasUriPrefixRegex).toRegex() } + private val eventIdUriAnchor by lazy { getAnchor(eventUriRegex, 255, roomIdUriPrefixRegex).toRegex() } fun findMentions(message: String): Map { val mentions = findUserIdMentions(message) @@ -129,7 +138,7 @@ object MatrixRegex { private fun findUserIdMentions(message: String): Map { fun handleMention(result: List, options: List): Mention.User { - val match = result[0] + val match = result[0].trim() val localpart = result[1] val domain = result[2] val (params, label) = parseOptions(options, anchor = match.startsWith("")) @@ -151,7 +160,7 @@ object MatrixRegex { result: List, options: List ): Mention.Room { - val match = result[0] + val match = result[0].trim() val localpart = result[1] val domain = result[2] val (params, label) = parseOptions(options, anchor = match.startsWith("")) @@ -170,7 +179,7 @@ object MatrixRegex { private fun findRoomAliasMentions(message: String): Map { fun handleMention(result: List, options: List): Mention.RoomAlias { - val match = result[0] + val match = result[0].trim() val localpart = result[1] val domain = result[2] val (params, label) = parseOptions(options, anchor = match.startsWith("")) @@ -191,7 +200,7 @@ object MatrixRegex { val ids = eventId.findAll(message).associate { val result = it.groupValues.filter(String::isNotBlank) - val match = result[0] + val match = result[0].trim() val eventId = result[1] it.range to Mention.Event(eventId = EventId("$$eventId"), match = match) @@ -202,7 +211,7 @@ object MatrixRegex { it.take(3) to it.drop(3) } - val match = result[0] + val match = result[0].trim() val roomId = result[1].replaceFirst("roomid/", "!") val eventId = result[2] val (params, _) = parseOptions(options, false) @@ -215,7 +224,7 @@ object MatrixRegex { it.take(3) to it.drop(3) } - val match = result[0] + val match = result[0].trim() val roomId = result[1].replaceFirst("roomid/", "!") val eventId = result[2] val (params, label) = parseOptions(options, true) @@ -234,7 +243,7 @@ object MatrixRegex { it.take(3) to it.drop(3) } - val match = result[0] + val match = result[0].trim() val roomId = result[1] val eventId = result[2] val (params, _) = parseOptions(options, false) @@ -247,7 +256,7 @@ object MatrixRegex { it.take(3) to it.drop(3) } - val match = result[0] + val match = result[0].trim() val roomId = result[1] val eventId = result[2] val (params, label) = parseOptions(options, true) diff --git a/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt b/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt index 113533e43..b24646112 100644 --- a/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt +++ b/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt @@ -840,6 +840,16 @@ class MatrixRegexTest : TrixnityBaseTest() { ) } + @Test + fun shouldFailVeryLongUserPermalinkWithinAnchorTag() { + PermalinkTest.user( + "Hallo", + "user".repeat(100), + "example.com", + expected = false + ) + } + @Test fun shouldPassEncodedUserPermalinkWithinAnchorTag() { PermalinkTest.user( @@ -861,15 +871,34 @@ class MatrixRegexTest : TrixnityBaseTest() { } @Test - fun shouldPassUsersPermalinksWithinAnchorTag() { + fun shouldPassOnlyValidUsersPermalinksWithinAnchorTag() { + val karl = "Dr. Karl Tanaka (Demo Bot)" + + val mallory = "Dr. Mallory Böse (Demo Bot)" + + val message = "$karl und $mallory wie geht's euch?" + + val result = findMentions(message) + result.size shouldBe 1 + + result.values.any { + it.match == karl + } shouldBe true + (result.entries.first { it.value.match == karl }.value as Mention.User).userId shouldBe UserId( + "demobot8", + "demo.example.de" + ) + } + + @Test + fun shouldPassMultipleUsersPermalinksWithinAnchorTag() { val karl = "Dr. Karl Tanaka (Demo Bot)" val wolfgang = "Dr. Wolfgang Reidorf (Demo Bot)" - val birgit = "Dr. Birgit Simonis (Demo Bot)" - val mallory = "Dr. Mallory Böse (Demo Bot)" + val birgit = "Dr. Birgit Simonis (Demo Bot)" - val message = "$karl, $birgit, $mallory und $wolfgang wie geht's euch?" + val message = "$karl, $birgit und $wolfgang wie geht's euch?" val result = findMentions(message) result.size shouldBe 3 -- GitLab From fff3903796d2ec8bb80f08a105573b099309860c Mon Sep 17 00:00:00 2001 From: Denis Loh Date: Mon, 30 Jun 2025 07:03:36 +0000 Subject: [PATCH 5/6] Apply 1 suggestion(s) to 1 file(s) Co-authored-by: Adam Brangenberg --- .../commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt b/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt index a9394eff2..9d8eda683 100644 --- a/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt +++ b/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt @@ -25,7 +25,7 @@ object MatrixRegex { private val roomAliasLocalpartRegex = """(?:[^:\s]+)""" // https://spec.matrix.org/v1.11/appendices/#opaque-identifiers - private val opaqueIdRegex = """(?:[0-9A-Za-z-._~]+)""" + private val opaqueIdRegex = """(?:[0-9A-Za-z-._~]{1,255})""" // https://spec.matrix.org/v1.11/appendices/#server-name private const val basePortRegex = """:[0-9]{1,5}""" -- GitLab From c56ee4c3b95b154f85fd48f2dd50af8298db8e80 Mon Sep 17 00:00:00 2001 From: Denis Loh Date: Wed, 2 Jul 2025 07:40:48 +0200 Subject: [PATCH 6/6] Removed trimming --- .../net/folivo/trixnity/core/MatrixRegex.kt | 18 +++++++++--------- .../folivo/trixnity/core/MatrixRegexTest.kt | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt b/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt index 9d8eda683..8e047988f 100644 --- a/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt +++ b/trixnity-core/src/commonMain/kotlin/net/folivo/trixnity/core/MatrixRegex.kt @@ -81,7 +81,7 @@ object MatrixRegex { // matches at "word"-boundary private fun getUriRegex(regex: String, maxLength: Int, prefix: String): String = - """(?:^|\s+)(?:(?=$prefix.{0,$maxLength}?(?:\?.*)?(?:\s+|$))$regex)(?:\s+|$)""" + """(?=$prefix.{0,$maxLength}?(?:\?.*)?(?:\s+|$))$regex""" private fun getAnchor(regex: String, maxLength: Int, prefix: String): String = """(.*?)<\/a>""" @@ -138,7 +138,7 @@ object MatrixRegex { private fun findUserIdMentions(message: String): Map { fun handleMention(result: List, options: List): Mention.User { - val match = result[0].trim() + val match = result[0] val localpart = result[1] val domain = result[2] val (params, label) = parseOptions(options, anchor = match.startsWith("")) @@ -160,7 +160,7 @@ object MatrixRegex { result: List, options: List ): Mention.Room { - val match = result[0].trim() + val match = result[0] val localpart = result[1] val domain = result[2] val (params, label) = parseOptions(options, anchor = match.startsWith("")) @@ -179,7 +179,7 @@ object MatrixRegex { private fun findRoomAliasMentions(message: String): Map { fun handleMention(result: List, options: List): Mention.RoomAlias { - val match = result[0].trim() + val match = result[0] val localpart = result[1] val domain = result[2] val (params, label) = parseOptions(options, anchor = match.startsWith("")) @@ -200,7 +200,7 @@ object MatrixRegex { val ids = eventId.findAll(message).associate { val result = it.groupValues.filter(String::isNotBlank) - val match = result[0].trim() + val match = result[0] val eventId = result[1] it.range to Mention.Event(eventId = EventId("$$eventId"), match = match) @@ -211,7 +211,7 @@ object MatrixRegex { it.take(3) to it.drop(3) } - val match = result[0].trim() + val match = result[0] val roomId = result[1].replaceFirst("roomid/", "!") val eventId = result[2] val (params, _) = parseOptions(options, false) @@ -224,7 +224,7 @@ object MatrixRegex { it.take(3) to it.drop(3) } - val match = result[0].trim() + val match = result[0] val roomId = result[1].replaceFirst("roomid/", "!") val eventId = result[2] val (params, label) = parseOptions(options, true) @@ -243,7 +243,7 @@ object MatrixRegex { it.take(3) to it.drop(3) } - val match = result[0].trim() + val match = result[0] val roomId = result[1] val eventId = result[2] val (params, _) = parseOptions(options, false) @@ -256,7 +256,7 @@ object MatrixRegex { it.take(3) to it.drop(3) } - val match = result[0].trim() + val match = result[0] val roomId = result[1] val eventId = result[2] val (params, label) = parseOptions(options, true) diff --git a/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt b/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt index b24646112..4023aa6cd 100644 --- a/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt +++ b/trixnity-core/src/commonTest/kotlin/net/folivo/trixnity/core/MatrixRegexTest.kt @@ -299,7 +299,7 @@ class MatrixRegexTest : TrixnityBaseTest() { @Test fun shouldFailUserOver255Bytes() { - userIdTest("@${"users".repeat(50)}:example.com", "users".repeat(50), "example.com", expected = false) + userIdTest("@${"users".repeat(100)}:example.com", "users".repeat(100), "example.com", expected = false) } @Test @@ -841,7 +841,7 @@ class MatrixRegexTest : TrixnityBaseTest() { } @Test - fun shouldFailVeryLongUserPermalinkWithinAnchorTag() { + fun shouldFailUserPermalinkOver255BytesWithinAnchorTag() { PermalinkTest.user( "Hallo", "user".repeat(100), -- GitLab