fix: shared session logs are always scrubbed, whatever their size (#1065)

This commit is contained in:
Resul Övür authored and GitHub committed 2026-10-20 17:16:36 -04:00
1 parent b3122aece0
commit 67ab3fa634
5 files changed
+186 -13

No files matched your search

@@ -0,0 +1,86 @@
package com.droiddeck.launcher.core
import java.io.StringWriter
import java.io.Writer
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
class LogRedactorStreamTest {
@get:Rule val tmp = TemporaryFolder()
@Test fun utf16AndNulDelimitedSecretsAreWithheld() {
val token = "synthetic_private_value"
for (encoding in listOf(Charsets.UTF_16LE, Charsets.UTF_16BE, Charsets.UTF_16)) {
val src = tmp.newFile()
src.writeBytes("sessionid=$token".toByteArray(encoding))
val out = StringWriter()
LogRedactor.scrubForShare(src, out)
assertFalse(out.toString().replace("\u0000", "").contains(token))
assertTrue(out.toString().contains("withheld"))
}
}
@Test fun aNulAfterTheInitialBinaryProbeWithholdsTheWholeLine() {
val src = tmp.newFile()
src.writeText(".".repeat(9000) + "\u0000sessionid=synthetic_private_value")
val out = StringWriter()
LogRedactor.scrubForShare(src, out)
assertFalse(out.toString().contains("synthetic_private_value"))
assertTrue(out.toString().contains("withheld"))
}
@Test fun aVeryLongLineIsWithheldAndTheFollowingLineIsScrubbed() {
val src = tmp.newFile()
src.bufferedWriter().use { writer ->
writer.write("sessionid=synthetic_private_value ")
repeat(128) { writer.write(".".repeat(64 * 1024)) }
writer.write("\nready=1 token=another_private_value\n")
}
val out = StringWriter()
LogRedactor.scrubForShare(src, out)
assertTrue("oversized line was retained", out.toString().length < 1024)
assertTrue(out.toString().contains("withheld"))
assertTrue(out.toString().contains("ready=1 token=<redacted:token>"))
assertFalse(out.toString().contains("private_value"))
}
@Test fun malformedUtf8WithholdsWithoutReturningTheUnredactedLine() {
val src = tmp.newFile()
src.writeBytes("sessionid=synthetic_private_value ".toByteArray() + byteArrayOf(0xc3.toByte(), 0x28))
val out = StringWriter()
LogRedactor.scrubForShare(src, out)
assertFalse(out.toString().contains("synthetic_private_value"))
assertTrue(out.toString().contains("withheld"))
}
@Test fun normalUnicodeAndLineEndingsKeepTheirDiagnosticContent() {
val src = tmp.newFile()
src.writeText("hazır=✓ token=synthetic_private_value\r\n\rstatus=ok\nson")
val out = StringWriter()
LogRedactor.scrubForShare(src, out)
assertEquals("hazır=✓ token=<redacted:token>\n\nstatus=ok\nson\n", out.toString())
}
@Test fun aLogAboveTheInMemoryLimitIsStillStreamed() {
val src = tmp.newFile()
val block = ("status=ok " + ".".repeat(4090) + "\n").repeat(16)
src.bufferedWriter().use { writer ->
while (src.length() <= LogRedactor.IN_MEMORY_SCRUB_MAX) {
writer.write(block)
writer.flush()
}
}
var chars = 0L
val out = object : Writer() {
override fun write(buffer: CharArray, offset: Int, length: Int) { chars += length }
override fun flush() {}
override fun close() {}
}
LogRedactor.scrubForShare(src, out)
assertEquals(src.length(), chars)
}
}
@@ -32,6 +32,35 @@ class SessionLogShareTest {
"rules $rules\n" + files.joinToString("") { val f = File(folder, it); "${f.length()}\t${f.lastModified()}\t$it\n" })
}
@Test fun aLogPastTheInMemoryCapIsStillScrubbedIntoTheZip() {
val folder = tmp.newFolder("2026-10-18-01-steam")
val line = ("x".repeat(1023) + "\n").toByteArray()
File(folder, "session.log").outputStream().buffered().use { out ->
out.write("$secret\n".toByteArray())
repeat(65 * 1024) { out.write(line) }
}
assertTrue(File(folder, "session.log").length() >= LogRedactor.IN_MEMORY_SCRUB_MAX)
val out = zip(folder)
assertTrue(out.containsKey("session.log"))
assertFalse(out.getValue("session.log").contains(leaked))
}
@Test fun aLogWithANulByteIsScrubbedNotCopiedRaw() {
val folder = tmp.newFolder("2026-10-18-02-steam")
File(folder, "crash.log").writeBytes(byteArrayOf(0) + "$secret\n".toByteArray())
val out = zip(folder)
assertTrue(out.containsKey("crash.log"))
assertFalse(out.getValue("crash.log").contains(leaked))
}
@Test fun utf16SecretsDoNotEscapeInTheSharedZip() {
val folder = tmp.newFolder("2026-10-18-03-steam")
File(folder, "crash.log").writeBytes("sessionid=$leaked".toByteArray(Charsets.UTF_16LE))
val output = zip(folder).getValue("crash.log")
assertFalse(output.replace("\u0000", "").contains(leaked))
assertTrue(output.contains("withheld"))
}
@Test fun aRecordedUnchangedFileIsScrubbedAgainOnTheWayIntoTheZip() {
val folder = tmp.newFolder("2026-10-06-01-steam")
File(folder, "session.log").writeText("$secret\n")