Skip to content

Commit a883afe

Browse files
committed
sec: apply security concerns for preview fetch
1 parent b41b7b2 commit a883afe

11 files changed

Lines changed: 985 additions & 69 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Wire
3+
* Copyright (C) 2026 Wire Swiss GmbH
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see http://www.gnu.org/licenses/.
17+
*/
18+
package com.wire.kalium.logic.data.message.linkpreview
19+
20+
import kotlinx.cinterop.ByteVar
21+
import kotlinx.cinterop.CPointerVar
22+
import kotlinx.cinterop.alloc
23+
import kotlinx.cinterop.allocArray
24+
import kotlinx.cinterop.memScoped
25+
import kotlinx.cinterop.pointed
26+
import kotlinx.cinterop.ptr
27+
import kotlinx.cinterop.sizeOf
28+
import kotlinx.cinterop.toKString
29+
import platform.posix.AF_UNSPEC
30+
import platform.posix.NI_MAXHOST
31+
import platform.posix.NI_NUMERICHOST
32+
import platform.posix.addrinfo
33+
import platform.posix.freeaddrinfo
34+
import platform.posix.getaddrinfo
35+
import platform.posix.getnameinfo
36+
import platform.posix.memset
37+
38+
internal actual suspend fun resolvePreviewHostAddresses(host: String): List<String> = memScoped {
39+
val result = alloc<CPointerVar<addrinfo>>()
40+
val hints = alloc<addrinfo>()
41+
memset(hints.ptr, 0, sizeOf<addrinfo>().toULong())
42+
hints.ai_family = AF_UNSPEC
43+
44+
if (getaddrinfo(host, null, hints.ptr, result.ptr) != 0) {
45+
return@memScoped emptyList()
46+
}
47+
48+
val addresses = mutableSetOf<String>()
49+
try {
50+
var current = result.value
51+
while (current != null) {
52+
val hostBuffer = allocArray<ByteVar>(NI_MAXHOST.toInt())
53+
if (getnameinfo(
54+
current.pointed.ai_addr,
55+
current.pointed.ai_addrlen,
56+
hostBuffer,
57+
NI_MAXHOST.toUInt(),
58+
null,
59+
0u,
60+
NI_NUMERICHOST
61+
) == 0
62+
) {
63+
addresses += hostBuffer.toKString().substringBefore('%')
64+
}
65+
current = current.pointed.ai_next
66+
}
67+
} finally {
68+
freeaddrinfo(result.value)
69+
}
70+
71+
addresses.toList()
72+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Wire
3+
* Copyright (C) 2026 Wire Swiss GmbH
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see http://www.gnu.org/licenses/.
17+
*/
18+
package com.wire.kalium.logic.data.message.linkpreview
19+
20+
import platform.Foundation.NSString
21+
22+
internal actual fun normalizePreviewUnicode(input: String): String =
23+
(input as NSString).precomposedStringWithCompatibilityMapping
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Wire
3+
* Copyright (C) 2026 Wire Swiss GmbH
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see http://www.gnu.org/licenses/.
17+
*/
18+
package com.wire.kalium.logic.data.message.linkpreview
19+
20+
import java.net.InetAddress
21+
22+
internal actual suspend fun resolvePreviewHostAddresses(host: String): List<String> =
23+
runCatching {
24+
InetAddress.getAllByName(host)
25+
.mapNotNull { it.hostAddress?.substringBefore('%') }
26+
.distinct()
27+
}.getOrDefault(emptyList())
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Wire
3+
* Copyright (C) 2026 Wire Swiss GmbH
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see http://www.gnu.org/licenses/.
17+
*/
18+
package com.wire.kalium.logic.data.message.linkpreview
19+
20+
import java.text.Normalizer
21+
22+
internal actual fun normalizePreviewUnicode(input: String): String =
23+
Normalizer.normalize(input, Normalizer.Form.NFKC)

0 commit comments

Comments
 (0)