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 a667543a69..491101dba2 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt @@ -2,14 +2,51 @@ package com.droiddeck.launcher.core object LogRedactor { private val EMAIL = Regex("[A-Za-z0-9._%+\\-]+@[A-Za-z0-9.\\-]+\\.[A-Za-z]{2,}") + private val HIDL_SHARED_LIBRARY = Regex( + "(?:android\\.(?:hardware|frameworks|hidl|system)|vendor\\.[A-Za-z0-9_]+\\.hardware)" + + "(?:\\.[A-Za-z0-9_]+)+@[0-9]+\\.[0-9]+(?:-[A-Za-z0-9_]+)*\\.so" + ) + private val hidlMatcher = object : ThreadLocal() { + override fun initialValue(): java.util.regex.Matcher = HIDL_SHARED_LIBRARY.toPattern().matcher("") + } + + private fun isHidlLoaderDiagnostic(input: String, match: MatchResult): Boolean { + val matcher = hidlMatcher.get()!! + val isLibrary = try { matcher.reset(match.value).matches() } finally { matcher.reset("") } + if (!isLibrary) return false + val start = match.range.first + val end = match.range.last + 1 + val prefix = "library \"" + val suffix = "\" not found" + val earliestQuote = (start - 4096).coerceAtLeast(0) + var quote = start - 1 + while (quote >= earliestQuote && input[quote] != '"') { + val char = input[quote] + if (!char.isLetterOrDigit() && char !in "/._-") return false + quote-- + } + if (quote < earliestQuote) return false + val pathStart = quote + 1 + if (pathStart < start && (input[start - 1] != '/' || + !(input.regionMatches(pathStart, "/vendor/", 0, 8) || + input.regionMatches(pathStart, "/system/", 0, 8)))) return false + val prefixStart = quote - prefix.length + 1 + val suffixEnd = end + suffix.length + return prefixStart >= 0 && suffixEnd <= input.length && + input.regionMatches(prefixStart, prefix, 0, prefix.length, ignoreCase = true) && + input.regionMatches(end, suffix, 0, suffix.length, ignoreCase = true) && + (prefixStart == 0 || !(input[prefixStart - 1].isLetterOrDigit() || input[prefixStart - 1] == '_')) && + (suffixEnd == input.length || !(input[suffixEnd].isLetterOrDigit() || input[suffixEnd] == '_')) + } + private val JWT_LABELLED = Regex("(?i)(\\bJWT[\\s=:]+)(\\d{8,})") private val JWT_BASE64 = Regex("ey[A-Za-z0-9_\\-]{6,}\\.[A-Za-z0-9_\\-]{6,}\\.[A-Za-z0-9_\\-]{6,}") private val GUID = Regex("\\b[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}\\b") private val SECRET_KV = Regex( - "(?i)\\b(access[_-]?token|refresh[_-]?token|auth[_-]?token|authtoken|token|authcode|" + + "(?i)(\\b(?:access[_-]?token|refresh[_-]?token|auth[_-]?token|authtoken|token|authcode|" + "auth[_-]?ticket|ticket|sessionid|steamloginsecure|webapikey|api[_-]?key|" + - "machine[_-]?auth(?:[_-]?token)?|machineauth|password|passwd|pwd|secret)" + - "(\\s*[=:]\\s*|=)([^\\s\"'<>&;,]{4,})" + "machine[_-]?auth(?:[_-]?token)?|machineauth|password|passwd|pwd|secret)[\"']?\\s*[=:]\\s*)" + + "(?:([\"'])((?:\\\\.|(?!\\2)[^\\\\\\r\\n]){4,})\\2|([^\\s\"'<>&;,]{4,}))" ) private val GUARD_CODE = Regex( "(?i)((?:steam\\s*)?guard\\s*code[\\s:=]*|two[\\s-]?factor[\\s:=]*|2fa[\\s:=]*)([A-Za-z0-9]{5})" @@ -291,7 +328,7 @@ object LogRedactor { if (out.has("jwt")) out.set(JWT_LABELLED.replace(out.text) { "${it.groupValues[1]}" }) if ("ey" in out.text) out.set(JWT_BASE64.replace(out.text, "")) if (out.has("token", "auth", "ticket", "sessionid", "steamloginsecure", "api", "machine", "pass", "pwd", "secret")) { - out.set(SECRET_KV.replace(out.text) { "${it.groupValues[1]}${it.groupValues[2]}" }) + out.set(SECRET_KV.replace(out.text) { "${it.groupValues[1]}${it.groupValues[2]}${it.groupValues[2]}" }) } if (out.has("guard", "factor", "2fa")) out.set(GUARD_CODE.replace(out.text) { "${it.groupValues[1]}" }) if (out.hasRun(32, ::isHex)) out.set(WEBAPI_KEY.replace(out.text, "")) @@ -309,7 +346,9 @@ object LogRedactor { } if (out.has("login")) out.set(LOGIN_KV.replace(out.text) { "${it.groupValues[1]}${it.groupValues[2]}${quoted(it.groupValues[3])}" }) for (r in accounts) if (r.needs == null || out.has(r.needs)) out.set(r.regex.replace(out.text, "")) - if ('@' in out.text) out.set(EMAIL.replace(out.text, "")) + if ('@' in out.text) out.set(EMAIL.replace(out.text) { m -> + m.value.takeIf { isHidlLoaderDiagnostic(out.text, m) } ?: "" + }) if (out.has("jwt", "token", "ticket", "sessionid", "steamloginsecure", "machineauth")) { out.set(RESIDUAL.replace(out.text) { "${it.groupValues[1]}=" }) } diff --git a/app/src/test/java/com/droiddeck/launcher/core/LogRedactorTest.kt b/app/src/test/java/com/droiddeck/launcher/core/LogRedactorTest.kt index ddeb3ac1fa..a67093e935 100644 --- a/app/src/test/java/com/droiddeck/launcher/core/LogRedactorTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/core/LogRedactorTest.kt @@ -37,6 +37,83 @@ class LogRedactorTest { assertEquals("login ok", LogRedactor.redact("login someone@example.com ok")) } + @Test fun onlyLoaderDiagnosticsKeepHidlSharedLibraryNames() { + val library = "android.hardware.graphics.mapper@4.0.so" + val line = "dlopen failed: library \"$library\" not found" + val pathLine = "dlopen failed: library \"/vendor/lib64/hw/$library\" not found" + + assertEquals(line, LogRedactor.redact(line)) + assertEquals(line, LogRedactor.redactForShare(line)) + assertEquals(pathLine, LogRedactor.redact(pathLine)) + assertEquals(pathLine, LogRedactor.redactForShare(pathLine)) + } + + @Test fun privacyRulesOverrideTheHidlSharedLibraryException() { + val library = "android.hardware.graphics.mapper@4.0.so" + assertEquals("token=", LogRedactor.redact("token=$library")) + assertEquals("username=", LogRedactor.redact("username=$library")) + assertEquals("{\"password\":\"\"}", LogRedactor.redact("{\"password\":\"$library\"}")) + assertEquals("{\"password\":\"\"}", LogRedactor.redactForShare("{\"password\":\"$library\"}")) + assertEquals("{\"password\":\"\"}", LogRedactor.redact("{\"password\":\"don't-use-$library\"}")) + assertEquals("{\"password\":\"\"}", LogRedactor.redactForShare("{\"password\":\"don't-use-$library\"}")) + + val mixed = "dlopen failed: library \"$library\" not found; token=abcdef123456; contact=alice!$library; {\"password\":\"library \\\"$library\\\" not found\"}" + val expected = "dlopen failed: library \"$library\" not found; token=; contact=alice!; {\"password\":\"\"}" + assertEquals(expected, LogRedactor.redact(mixed)) + assertEquals(expected, LogRedactor.redactForShare(mixed)) + + val root = java.nio.file.Files.createTempDirectory("hidl-redactor").toFile() + val users = java.io.File(root, "loginusers.vdf") + users.writeText("\"AccountName\" \"$library\"") + try { + LogRedactor.learnAccounts(users) + assertEquals("", LogRedactor.redact(library)) + } finally { + LogRedactor.learnAccounts() + root.deleteRecursively() + } + } + + @Test fun hidlSharedLibraryNamesAreRedactedOutsideLoaderDiagnostics() { + val library = "android.hardware.graphics.mapper@4.0.so" + + assertEquals("", LogRedactor.redact(library)) + assertEquals("", LogRedactor.redactForShare(library)) + assertEquals("/vendor/lib64/hw/", LogRedactor.redact("/vendor/lib64/hw/$library")) + for (punctuation in listOf("!", "#", "~", "/", "=")) { + val expected = "contact=alice$punctuation" + assertEquals(expected, LogRedactor.redact("contact=alice$punctuation$library")) + assertEquals(expected, LogRedactor.redactForShare("contact=alice$punctuation$library")) + } + assertEquals("", LogRedactor.redact("$library.evil")) + assertEquals("", LogRedactor.redact("someone@company.so")) + assertEquals("", LogRedactor.redact("not$library")) + assertEquals("_debug", LogRedactor.redact("${library}_debug")) + assertEquals(".1", LogRedactor.redact("${library}.1")) + assertEquals("@attacker", LogRedactor.redact("${library}@attacker")) + assertEquals("", LogRedactor.redact("android.hardware.graphics.mapper@4.0.software")) + assertEquals("", LogRedactor.redact("android.hardware.graphics.mapper@4.0.so.evil")) + } + + @Test fun loaderContextRequiresExactWordsAndABoundedAndroidPath() { + val library = "android.hardware.graphics.mapper@4.0.so" + val valid = "library \"/system/lib64/$library\" not found: needed by app" + assertEquals(valid, LogRedactor.redact(valid)) + assertEquals(valid, LogRedactor.redactForShare(valid)) + for (line in listOf( + "not_library \"$library\" not found", + "library \"$library\" not found_suffix", + "library \"/home/user/$library\" not found", + "library \"relative/$library\" not found", + "library \"/vendor/invalid!/$library\" not found", + "library \"/vendor/" + "a/".repeat(2050) + "$library\" not found", + )) { + val expected = line.replace(library, "") + assertEquals(expected, LogRedactor.redact(line)) + assertEquals(expected, LogRedactor.redactForShare(line)) + } + } + @Test fun clockTimeIsNotAnAddress() { val line = "[2026-09-27 10:03:05] Startup" assertEquals(line, LogRedactor.redact(line))