Logs: one set of scrubbing rules for the stores' log and the shared zip

StoreLog.redactLine and the zip's last pass each carried their own URL
and token rules. Both now use SecretScrub: URLs lose query, fragment and
user:password - the stores' lines keep only the host and a plain first
segment, the zip keeps paths with token-bearing segments blanked - and
token values and Authorization / Cookie headers are blanked everywhere.
This commit is contained in:
The412Banner committed 2026-10-09 12:38:42 -04:00
1 parent 56bc2cd3f7
commit 8d94f0c948
4 files changed
+74 -58

No files matched your search

@@ -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)(?<![A-Za-z0-9])(__token__|f_token|hdnts|id_token|code|exchange_code|authorizationCode)(["']?\s*=\s*["']?)[^\s&;,"'<>}]{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]}<redacted:token>" }
out = HEADER.replace(out) { "${it.groupValues[1]}${it.groupValues[2]}<redacted:header>" }
out
SecretScrub.scrub(redact(line), SecretScrub.Urls.KEEP_PATH, "<redacted:token>")
} catch (t: Throwable) {
"<redaction failed; line withheld>"
}
}
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)) "<redacted:token>" 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") }
@@ -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)(?<![A-Za-z0-9_])(__token__|f_token|hdnts|access_token|refresh_token|id_token|token)(["']?\s*[=:]\s*["']?)[^\s&;,"'<>}]+""")
private val CODE = Regex("""(?i)(?<![A-Za-z0-9_])(code|exchange_code|authorizationCode)(["']?\s*=\s*["']?)[^\s&;,"'<>}]{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<String, 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('@')
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" } ?: "")
}
}
@@ -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)(?<![A-Za-z0-9_])(__token__|f_token|hdnts|access_token|refresh_token|id_token|token)(["']?\s*[=:]\s*["']?)[^\s&;,"'}]+""")
private val CODE = Regex("""(?i)(?<![A-Za-z0-9_])(code|exchange_code|authorizationCode)(["']?\s*=\s*["']?)[^\s&;,"'}]+""")
private val AUTH_HEADER = Regex("""(?i)(authorization|cookie)(\s*[:=]\s*).*""")
/**
* What any store line may say, for the Downloads log and logcat alike: every URL cut to
* `scheme://host/first-segment/…` (the segment kept only when it is a plain name - a GOG secure
* link carries its token in the path), no query, fragment or userinfo; and outside URLs, the
* values of token-like keys and of Authorization / Cookie headers blanked.
* What any store line may say, for the stores' log and logcat alike: URLs cut to their host and a
* plain first path segment, token values and Authorization / Cookie headers blanked - the shared
* rules of [com.droiddeck.launcher.core.SecretScrub], which a shared log zip applies too.
*/
@JvmStatic
fun redactLine(text: String): String {
var out = URL_PATTERN.replace(text) { m -> 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, "…")
}