neiam
/recourse
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
Log in to mark this repository.
Off-site links through a signed /out interstitial; bare URLs, footnotes, code blocks
7250478 · gmorell · 2026-12-03 03:11
Message
{commit_body(@commit)}
Files changed
modified
Cargo.lock
+3
−0
| 3228 | 3228 | "axum-prometheus", |
| 3229 | 3229 | "chrono", |
| 3230 | 3230 | "clap", |
| 3231 | + "hmac 0.12.1", | |
| 3232 | + "idna", | |
| 3231 | 3233 | "log", |
| 3232 | 3234 | "mail-prism-client", |
| 3233 | 3235 | "metrics-process", |
| 3246 | 3248 | "tower-http", |
| 3247 | 3249 | "tracing", |
| 3248 | 3250 | "tracing-subscriber", |
| 3251 | + "url", | |
| 3249 | 3252 | "uuid", |
| 3250 | 3253 | ] |
| 3251 | 3254 |
modified
Cargo.toml
+5
−0
| 60 | 60 | # Markdown rendering for post bodies |
| 61 | 61 | pulldown-cmark = "0.12" |
| 62 | 62 | ammonia = "4" |
| 63 | +# The /out interstitial: signed destinations, parsed and shown with their | |
| 64 | +# host (Unicode and xn-- forms). | |
| 65 | +hmac = "0.12" | |
| 66 | +url = "2" | |
| 67 | +idna = "1" | |
| 63 | 68 | |
| 64 | 69 | # CLI (manage binary) |
| 65 | 70 | clap = { version = "4.5", features = ["derive"] } |
modified
README.md
+24
−0
| 101 | 101 | - **Log:** every staff action, from the web or the CLI, is in |
| 102 | 102 | `/admin/log` (also per user). |
| 103 | 103 | |
| 104 | +### Links in posts | |
| 105 | + | |
| 106 | +Posts are markdown (CommonMark plus tables, strikethrough, task lists, | |
| 107 | +footnotes), and bare `http(s)://` addresses become links. | |
| 108 | + | |
| 109 | +**Off-site links go through `/out`.** It is a page that says "You are | |
| 110 | +leaving recourse @ this host to go to:", with the address and its host in | |
| 111 | +bold. An internationalised host is shown both ways (`bücher.example` / | |
| 112 | +`xn--bcher-kva.example`). It has Continue and Back buttons, never redirects | |
| 113 | +by itself, and works signed out. | |
| 114 | + | |
| 115 | +- Links to this site (`PUBLIC_URL`'s host) and relative links stay direct. | |
| 116 | +- Every link gets `rel="nofollow ugc noopener noreferrer"`. | |
| 117 | +- `/out?u=…&s=…` carries an HMAC of the destination (`LINK_SECRET`). An | |
| 118 | + unsigned or altered link shows the address as text, with a warning and no | |
| 119 | + Continue, so the page can't be used as an open redirect. | |
| 120 | +- The rewrite happens when a post is shown, not when it's saved. Old posts, | |
| 121 | + a new host name or a rotated secret all just work, with no recook. | |
| 122 | +- The API sends the same links, absolute. The app shows its own "Leaving" | |
| 123 | + dialog for them and for any other off-site link. | |
| 124 | +- Off-site images still load directly from their hosts. A caching image | |
| 125 | + proxy would hide readers' addresses from them; it isn't built. | |
| 126 | + | |
| 104 | 127 | ### Client addresses |
| 105 | 128 | |
| 106 | 129 | **What is collected:** the address an account used when it: |
| 164 | 187 | | `FLAG_HIDE_ENOUGH_FLAGS` | `10` | …or this many, whatever the readership | |
| 165 | 188 | | `TRUSTED_PROXIES` | _(none)_ | Comma-separated CIDRs of reverse proxies whose `X-Forwarded-For` / `X-Real-IP` is believed, e.g. `127.0.0.1/32` (nginx on the host), `10.0.0.0/8` (a cluster ingress). See Client addresses | |
| 166 | 189 | | `IP_RETENTION_DAYS` | `180` | How long client addresses are kept | |
| 190 | +| `LINK_SECRET` | _(a development value)_ | Signs `/out` links (see Links in posts). Set a long random string in production; changing it just re-signs links as they're shown | | |
| 167 | 191 | | `MAIL_PRISM_URL` / `MAIL_PRISM_API_KEY` | _(unset)_ | Send mail through mail-prism. Unset (or blank): mail is only logged | |
| 168 | 192 | | `MAIL_FROM_ADDRESS` | _(the key's application address)_ | Must equal the mail-prism application address if set | |
| 169 | 193 | | `MAIL_FROM_NAME` | `APP_NAME` | From display name | |
added
android/app/src/main/java/org/neiam/recourse/app/data/Leaving.kt
+58
−0
| 1 | +package org.neiam.recourse.app.data | |
| 2 | + | |
| 3 | +import java.net.IDN | |
| 4 | +import java.net.URI | |
| 5 | +import java.net.URLDecoder | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Where a link in a post goes, as the app decides what to do with a tap. | |
| 9 | + * The server sends off-site links as its own `/out?u=…&s=…` interstitial | |
| 10 | + * (src/links.rs); the app unwraps those and shows a native "Leaving" dialog | |
| 11 | + * instead, for those and for any other off-site link. | |
| 12 | + */ | |
| 13 | +sealed interface LinkTarget { | |
| 14 | + /** This forum (relative, or the paired server's host): open it. */ | |
| 15 | + data class Internal(val url: String) : LinkTarget | |
| 16 | + | |
| 17 | + /** Somewhere else: ask first. `host` is shown emphasised; `unicodeHost` | |
| 18 | + * only when it's an IDN (`xn--`). */ | |
| 19 | + data class External(val url: String, val host: String, val unicodeHost: String?) : LinkTarget | |
| 20 | + | |
| 21 | + /** mailto:, tel: and the like: hand to the system. */ | |
| 22 | + data class Other(val url: String) : LinkTarget | |
| 23 | +} | |
| 24 | + | |
| 25 | +object Leaving { | |
| 26 | + fun classify(href: String, baseUrl: String): LinkTarget { | |
| 27 | + val base = runCatching { URI(baseUrl.trimEnd('/') + "/") }.getOrNull() | |
| 28 | + val resolved = runCatching { if (base != null) base.resolve(href.trim()) else URI(href.trim()) }.getOrNull() | |
| 29 | + ?: return LinkTarget.Other(href) | |
| 30 | + val scheme = resolved.scheme?.lowercase() | |
| 31 | + if (scheme != "http" && scheme != "https") return LinkTarget.Other(resolved.toString()) | |
| 32 | + val ownHost = base?.host?.lowercase() | |
| 33 | + val host = resolved.host?.lowercase() ?: return LinkTarget.Other(resolved.toString()) | |
| 34 | + if (host == ownHost) { | |
| 35 | + // Our own interstitial: unwrap to the destination it names. | |
| 36 | + if (resolved.rawPath == "/out") { | |
| 37 | + val u = query(resolved.rawQuery, "u") | |
| 38 | + if (u != null) return external(u) ?: LinkTarget.Other(u) | |
| 39 | + } | |
| 40 | + return LinkTarget.Internal(resolved.toString()) | |
| 41 | + } | |
| 42 | + return external(resolved.toString()) ?: LinkTarget.Other(resolved.toString()) | |
| 43 | + } | |
| 44 | + | |
| 45 | + private fun external(url: String): LinkTarget.External? { | |
| 46 | + val u = runCatching { URI(url) }.getOrNull() ?: return null | |
| 47 | + if (u.scheme?.lowercase() !in setOf("http", "https")) return null | |
| 48 | + val host = u.host ?: return null | |
| 49 | + val unicode = runCatching { IDN.toUnicode(host) }.getOrNull()?.takeIf { it != host } | |
| 50 | + return LinkTarget.External(url, host, unicode) | |
| 51 | + } | |
| 52 | + | |
| 53 | + private fun query(raw: String?, key: String): String? = | |
| 54 | + raw?.split('&')?.firstNotNullOfOrNull { part -> | |
| 55 | + val (k, v) = part.split('=', limit = 2).let { it[0] to it.getOrElse(1) { "" } } | |
| 56 | + if (k == key) URLDecoder.decode(v, "UTF-8") else null | |
| 57 | + } | |
| 58 | +} |
modified
android/app/src/main/java/org/neiam/recourse/app/ui/Common.kt
+77
−1
| 1 | 1 | package org.neiam.recourse.app.ui |
| 2 | 2 | |
| 3 | +import android.content.Intent | |
| 4 | +import android.net.Uri | |
| 5 | +import android.text.SpannableStringBuilder | |
| 3 | 6 | import android.text.method.LinkMovementMethod |
| 7 | +import android.text.style.ClickableSpan | |
| 8 | +import android.text.style.URLSpan | |
| 9 | +import android.view.View | |
| 4 | 10 | import android.widget.TextView |
| 11 | +import androidx.compose.ui.platform.LocalContext | |
| 12 | +import androidx.compose.ui.text.SpanStyle | |
| 13 | +import androidx.compose.ui.text.buildAnnotatedString | |
| 14 | +import androidx.compose.ui.text.font.FontFamily | |
| 15 | +import androidx.compose.ui.text.withStyle | |
| 5 | 16 | import androidx.compose.foundation.Canvas |
| 6 | 17 | import androidx.compose.foundation.background |
| 7 | 18 | import androidx.compose.foundation.clickable |
| 97 | 108 | val color = MaterialTheme.colorScheme.onBackground.toArgb() |
| 98 | 109 | val link = MaterialTheme.colorScheme.primary.toArgb() |
| 99 | 110 | val sizeSp = MaterialTheme.typography.bodyMedium.fontSize.value |
| 111 | + val context = LocalContext.current | |
| 112 | + val baseUrl = LocalSession.current.creds.baseUrl | |
| 113 | + // An off-site link (or the server's /out interstitial) asks first. | |
| 114 | + var leaving by remember { mutableStateOf<LinkTarget.External?>(null) } | |
| 115 | + val open = { url: String -> | |
| 116 | + runCatching { context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) } | |
| 117 | + Unit | |
| 118 | + } | |
| 119 | + val onLink = { href: String -> | |
| 120 | + when (val t = Leaving.classify(href, baseUrl)) { | |
| 121 | + is LinkTarget.External -> leaving = t | |
| 122 | + is LinkTarget.Internal -> open(t.url) | |
| 123 | + is LinkTarget.Other -> open(t.url) | |
| 124 | + } | |
| 125 | + } | |
| 100 | 126 | AndroidView( |
| 101 | 127 | modifier = modifier, |
| 102 | 128 | factory = { ctx -> |
| 109 | 135 | tv.setTextColor(color) |
| 110 | 136 | tv.setLinkTextColor(link) |
| 111 | 137 | tv.textSize = sizeSp |
| 112 | − tv.text = HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_COMPACT).trimEnd() | |
| 138 | + val spanned = SpannableStringBuilder(HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_COMPACT).trimEnd()) | |
| 139 | + // Every link goes through onLink instead of straight to a browser. | |
| 140 | + spanned.getSpans(0, spanned.length, URLSpan::class.java).forEach { span -> | |
| 141 | + val start = spanned.getSpanStart(span) | |
| 142 | + val end = spanned.getSpanEnd(span) | |
| 143 | + val flags = spanned.getSpanFlags(span) | |
| 144 | + val href = span.url | |
| 145 | + spanned.removeSpan(span) | |
| 146 | + spanned.setSpan(object : ClickableSpan() { | |
| 147 | + override fun onClick(widget: View) = onLink(href) | |
| 148 | + }, start, end, flags) | |
| 149 | + } | |
| 150 | + tv.text = spanned | |
| 151 | + }, | |
| 152 | + ) | |
| 153 | + leaving?.let { t -> LeavingDialog(t, onDismiss = { leaving = null }) { leaving = null; open(t.url) } } | |
| 154 | +} | |
| 155 | + | |
| 156 | +/** "Leaving recourse @ host → url": the web's /out page, natively. */ | |
| 157 | +@Composable | |
| 158 | +fun LeavingDialog(target: LinkTarget.External, onDismiss: () -> Unit, onContinue: () -> Unit) { | |
| 159 | + val here = runCatching { Uri.parse(LocalSession.current.creds.baseUrl).host }.getOrNull() ?: "" | |
| 160 | + AlertDialog( | |
| 161 | + onDismissRequest = onDismiss, | |
| 162 | + title = { Text("Leaving recourse") }, | |
| 163 | + text = { | |
| 164 | + Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { | |
| 165 | + Text("You are leaving recourse @ $here to go to:") | |
| 166 | + val i = target.url.indexOf(target.host) | |
| 167 | + Text( | |
| 168 | + buildAnnotatedString { | |
| 169 | + if (i >= 0) { | |
| 170 | + withStyle(SpanStyle(color = MaterialTheme.colorScheme.onSurfaceVariant)) { append(target.url.substring(0, i)) } | |
| 171 | + withStyle(SpanStyle(fontWeight = FontWeight.Bold)) { append(target.host) } | |
| 172 | + withStyle(SpanStyle(color = MaterialTheme.colorScheme.onSurfaceVariant)) { append(target.url.substring(i + target.host.length)) } | |
| 173 | + } else { | |
| 174 | + append(target.url) | |
| 175 | + } | |
| 176 | + }, | |
| 177 | + fontFamily = FontFamily.Monospace, | |
| 178 | + style = MaterialTheme.typography.bodySmall, | |
| 179 | + ) | |
| 180 | + target.unicodeHost?.let { | |
| 181 | + Text( | |
| 182 | + "$it is written ${target.host}. Look-alike letters can imitate a familiar site.", | |
| 183 | + style = MaterialTheme.typography.labelSmall, | |
| 184 | + ) | |
| 185 | + } | |
| 186 | + } | |
| 113 | 187 | }, |
| 188 | + confirmButton = { TextButton(onClick = onContinue) { Text("Continue") } }, | |
| 189 | + dismissButton = { TextButton(onClick = onDismiss) { Text("Back") } }, | |
| 114 | 190 | ) |
| 115 | 191 | } |
| 116 | 192 |
modified
android/app/src/test/java/org/neiam/recourse/app/data/ContractTest.kt
+4
−0
Click to load diff…
added
android/app/src/test/java/org/neiam/recourse/app/data/LeavingTest.kt
+31
−0
Click to load diff…
modified
android/app/src/test/resources/api/bookmarks.json
+3
−3
Click to load diff…
modified
android/app/src/test/resources/api/categories.json
+103
−28
Click to load diff…
modified
android/app/src/test/resources/api/category.json
+29
−29
Click to load diff…
modified
android/app/src/test/resources/api/category_staff_only.json
+2
−2
Click to load diff…
modified
android/app/src/test/resources/api/devices.json
+4
−4
Click to load diff…
modified
android/app/src/test/resources/api/error_silenced.json
+1
−1
Click to load diff…
modified
android/app/src/test/resources/api/me.json
+5
−5
Click to load diff…
modified
android/app/src/test/resources/api/me_silenced.json
+8
−8
Click to load diff…
modified
android/app/src/test/resources/api/notifications.json
+6
−6
Click to load diff…
modified
android/app/src/test/resources/api/reply.json
+5
−5
Click to load diff…
modified
android/app/src/test/resources/api/search.json
+56
−4
Click to load diff…
modified
android/app/src/test/resources/api/tag.json
+18
−18
Click to load diff…
modified
android/app/src/test/resources/api/tags.json
+5
−0
Click to load diff…
modified
android/app/src/test/resources/api/topic.json
+21
−21
Click to load diff…
modified
android/app/src/test/resources/api/topic_locked.json
+26
−26
Click to load diff…
modified
assets/f/css/style.css
+37
−0
Click to load diff…
modified
compose.yml
+4
−0
Click to load diff…
modified
docs/api.md
+3
−1
Click to load diff…
modified
src/api/find.rs
+2
−1
Click to load diff…
modified
src/api/forum.rs
+9
−2
Click to load diff…
modified
src/config.rs
+8
−0
Click to load diff…
modified
src/handlers/bookmarks.rs
+2
−1
Click to load diff…
modified
src/handlers/forum.rs
+2
−1
Click to load diff…
modified
src/handlers/mod.rs
+1
−0
Click to load diff…
modified
src/handlers/moderation.rs
+1
−1
Click to load diff…
added
src/handlers/out.rs
+41
−0
Click to load diff…
modified
src/handlers/posts.rs
+3
−1
Click to load diff…
modified
src/lib.rs
+1
−0
Click to load diff…
added
src/links.rs
+242
−0
Click to load diff…
modified
src/main.rs
+7
−0
Click to load diff…
modified
src/markdown.rs
+167
−7
Click to load diff…
modified
src/web.rs
+1
−0
Click to load diff…
added
templates/out.html
+41
−0
Click to load diff…
modified
templates/posts/moderate.html
+1
−1
Click to load diff…
modified
tests/api_db.rs
+1
−1
Click to load diff…
modified
tests/moderation_db.rs
+53
−0
Click to load diff…
Parents: 9b377f5