diff --git a/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt b/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt index b406212b41..23bad40c6a 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt @@ -212,41 +212,21 @@ object LogRedactor { src.forEachLine { line -> out.write(redact(line)); out.write("\n") } } - private val URL_IN_TEXT = Regex("""[A-Za-z][A-Za-z0-9+.\-]*://[^\s"'<>]+""") - /** A path segment that carries a signed token (a GOG secure link puts its token in the path). */ - private val TOKEN_SEGMENT = Regex("""(?i)(token|hdnts|hmac|signature|sig|exp)=""") - private val SHARE_KV = Regex("""(?i)(?}]{6,}""") - private val HEADER = Regex("""(?i)\b(authorization|cookie|set-cookie)(\s*:\s*).+""") - /** - * The last pass for a file going into a shared zip: [redact], then every URL without its query, - * fragment and user:password and with any path segment that carries a token blanked, then the - * token-like values [redact] does not know (`__token__`, `f_token`, `hdnts`, OAuth `code=`) and - * Authorization / Cookie headers. A line already clean comes out unchanged. + * The last pass for a file going into a shared zip: [redact], then the rules the stores' log + * uses too ([SecretScrub]) - URLs without query, fragment and user:password and with any + * token-bearing path segment blanked, token values and Authorization / Cookie headers blanked. + * A line already clean comes out unchanged. */ fun redactForShare(line: String): String { if (line.isEmpty()) return line return try { - var out = redact(line) - out = URL_IN_TEXT.replace(out) { m -> shareUrl(m.value) } - out = SHARE_KV.replace(out) { "${it.groupValues[1]}${it.groupValues[2]}" } - out = HEADER.replace(out) { "${it.groupValues[1]}${it.groupValues[2]}" } - out + SecretScrub.scrub(redact(line), SecretScrub.Urls.KEEP_PATH, "") } catch (t: Throwable) { "" } } - private fun shareUrl(url: String): String { - val schemeEnd = url.indexOf("://") - val rest = url.substring(schemeEnd + 3).substringBefore('#').substringBefore('?') - val slash = rest.indexOf('/') - val authority = (if (slash >= 0) rest.substring(0, slash) else rest).substringAfterLast('@') - val path = if (slash >= 0) rest.substring(slash) else "" - val cleanPath = path.split('/').joinToString("/") { seg -> if (TOKEN_SEGMENT.containsMatchIn(seg)) "" else seg } - return url.substring(0, schemeEnd) + "://" + authority + cleanPath - } - /** [src]'s lines through [redactForShare], to [out]: the pass every text file in a shared zip gets. */ fun scrubForShare(src: java.io.File, out: java.io.Writer) { src.forEachLine { line -> out.write(redactForShare(line)); out.write("\n") } diff --git a/app/src/main/java/com/droiddeck/launcher/core/SecretScrub.kt b/app/src/main/java/com/droiddeck/launcher/core/SecretScrub.kt new file mode 100644 index 0000000000..76a2fe302c --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/core/SecretScrub.kt @@ -0,0 +1,57 @@ +package com.droiddeck.launcher.core + +/** + * The one set of rules that keeps URLs and tokens out of anything the app writes or shares: the + * stores' log ([com.droiddeck.launcher.stores.StoreLog.redactLine]) and the last pass over every + * text file in a shared log zip ([LogRedactor.redactForShare]). + * + * - Every URL loses its query, fragment and `user:password@`. [Urls.SHORT] then keeps only the host + * and a plain first path segment (the stores' lines, where a GOG secure link carries its token in + * the path); [Urls.KEEP_PATH] keeps the path but blanks any segment that carries a token. + * - Outside URLs, the values of token-like keys (`token`, `__token__`, `f_token`, `hdnts`, access / + * refresh / id tokens; `code=` and exchange codes of four characters or more) and of + * Authorization / Cookie headers are blanked with [mark]. + * + * A line already scrubbed comes out unchanged. + */ +object SecretScrub { + enum class Urls { SHORT, KEEP_PATH } + + private val URL = Regex("""[A-Za-z][A-Za-z0-9+.\-]*://[^\s"'<>]+""") + private val SAFE_SEGMENT = Regex("""[A-Za-z0-9._\-]{1,40}""") + private val TOKEN_SEGMENT = Regex("""(?i)(token|hdnts|hmac|signature|sig|exp)=""") + private val TOKEN = Regex("""(?i)(?}]+""") + private val CODE = Regex("""(?i)(?}]{4,}""") + private val HEADER = Regex("""(?i)\b(authorization|cookie|set-cookie)(\s*[:=]\s*).+""") + + fun scrub(text: String, urls: Urls, mark: String): String { + var out = URL.replace(text) { m -> if (urls == Urls.SHORT) short(m.value) else keepPath(m.value, mark) } + out = TOKEN.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + mark } + out = CODE.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + mark } + out = HEADER.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + mark } + return out + } + + private fun split(url: String): Triple { + val schemeEnd = url.indexOf("://") + val rest = url.substring(schemeEnd + 3).substringBefore('#').substringBefore('?') + val slash = rest.indexOf('/') + val authority = (if (slash >= 0) rest.substring(0, slash) else rest).substringAfterLast('@') + return Triple(url.substring(0, schemeEnd), authority, if (slash >= 0) rest.substring(slash + 1) else null) + } + + private fun short(url: String): String { + val (scheme, authority, path) = split(url) + if (path == null) return "$scheme://$authority" + val first = path.substringBefore('/') + if (first.isEmpty()) return "$scheme://$authority/" + val shown = if (SAFE_SEGMENT.matches(first)) first else "…" + return "$scheme://$authority/$shown" + if (path.length > first.length && shown != "…") "/…" else "" + } + + private fun keepPath(url: String, mark: String): String { + val (scheme, authority, path) = split(url) + val clean = path?.split('/')?.joinToString("/") { seg -> if (TOKEN_SEGMENT.containsMatchIn(seg)) mark else seg } + return "$scheme://$authority" + (clean?.let { "/$it" } ?: "") + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreNet.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreNet.kt index 1a94c847aa..4eeba0cab5 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreNet.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreNet.kt @@ -125,40 +125,12 @@ object StoreLog { } } - private val URL_PATTERN = Regex("""[A-Za-z][A-Za-z0-9+.\-]*://[^\s"'<>]+""") - private val SAFE_SEGMENT = Regex("""[A-Za-z0-9._\-]{1,40}""") - private val SECRET = Regex("""(?i)(? shortUrl(m.value) } - out = SECRET.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + "…" } - out = CODE.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + "…" } - out = AUTH_HEADER.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + "…" } - return out - } - - private fun shortUrl(url: String): String { - val schemeEnd = url.indexOf("://") - val scheme = url.substring(0, schemeEnd) - var rest = url.substring(schemeEnd + 3) - rest = rest.substringBefore('#').substringBefore('?') - val slash = rest.indexOf('/') - val authority = (if (slash >= 0) rest.substring(0, slash) else rest).substringAfterLast('@') - if (slash < 0) return "$scheme://$authority" - val path = rest.substring(slash + 1) - val first = path.substringBefore('/') - val more = path.length > first.length - if (first.isEmpty()) return "$scheme://$authority/" - val shown = if (SAFE_SEGMENT.matches(first)) first else "…" - return "$scheme://$authority/$shown" + if (more && shown != "…") "/…" else "" - } + fun redactLine(text: String): String = + com.droiddeck.launcher.core.SecretScrub.scrub(text, com.droiddeck.launcher.core.SecretScrub.Urls.SHORT, "…") } diff --git a/docs/progress-log.md b/docs/progress-log.md index c2763adbd5..c0829621a5 100644 --- a/docs/progress-log.md +++ b/docs/progress-log.md @@ -29,6 +29,13 @@ the timeline, then lessons and backlog. Companion to the README (what the app *d `SessionLogShareTest` covers it. Nothing was relaxed: the per-session scrub, `scrubOlder` and every existing rule stay as they were. - **File manager:** the Session logs shortcut opens `files/logs` (the app reads its own storage). +- **On main after Stores (#476) and the KDE Plasma desktop (#473):** the stores' log is already + `files/logs/stores/stores-.log` and the zip carries its last seven days under `stores/`. One + set of rules scrubs both the stores' log and the zip (`SecretScrub`): `StoreLog.redactLine` keeps + URLs to their host and first plain segment, the zip pass keeps paths with token segments blanked, + both blank token values and Authorization / Cookie headers; the two test suites pass on it. The + Plasma desktop writes nothing outside the session folder (its output is the session's + `desktop.log`), so there was nothing new to move. ## 2026-10-08 - `feat/stores`: GOG, Epic Games and Amazon Games in the launcher (in progress) A new **Stores** section - the three storefronts' libraries and public catalogs, one download