mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 22:36:00 +00:00
fix: shared session logs are always scrubbed, whatever their size (#1065)
This commit is contained in:
1 parent
06a6fd96db
commit
606ba5a9db
5 files changed
+186
-13
No files matched your search
@@ -146,16 +146,21 @@ object LogRedactor {
|
||||
return out
|
||||
}
|
||||
|
||||
fun isText(file: java.io.File): Boolean = try {
|
||||
file.isFile && file.length() < 64L * 1024 * 1024 && file.inputStream().use { input ->
|
||||
/** The at-rest rewrite reads a file into memory twice; larger logs are only scrubbed on the way out. */
|
||||
const val IN_MEMORY_SCRUB_MAX = 64L * 1024 * 1024
|
||||
|
||||
fun isBinary(file: java.io.File): Boolean = try {
|
||||
file.inputStream().use { input ->
|
||||
val buf = ByteArray(4096)
|
||||
val n = input.read(buf)
|
||||
n <= 0 || (0 until n).none { buf[it].toInt() == 0 }
|
||||
n > 0 && (0 until n).any { buf[it].toInt() == 0 }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
true
|
||||
}
|
||||
|
||||
fun isText(file: java.io.File): Boolean = file.isFile && file.length() < IN_MEMORY_SCRUB_MAX && !isBinary(file)
|
||||
|
||||
const val RULES_VERSION = 3
|
||||
|
||||
private val LAN_IPV4 = Regex(
|
||||
@@ -176,8 +181,62 @@ object LogRedactor {
|
||||
}
|
||||
}
|
||||
|
||||
private const val SHARE_LINE_MAX = 64 * 1024
|
||||
|
||||
fun scrubForShare(src: java.io.File, out: java.io.Writer) {
|
||||
src.forEachLine { line -> out.write(redactForShare(line)); out.write("\n") }
|
||||
src.inputStream().buffered().use { input ->
|
||||
input.mark(2)
|
||||
val first = input.read()
|
||||
val second = input.read()
|
||||
input.reset()
|
||||
if ((first == 0xff && second == 0xfe) || (first == 0xfe && second == 0xff)) {
|
||||
out.write("<unsupported encoding; file withheld>\n")
|
||||
return
|
||||
}
|
||||
val decoder = Charsets.UTF_8.newDecoder()
|
||||
.onMalformedInput(java.nio.charset.CodingErrorAction.REPORT)
|
||||
.onUnmappableCharacter(java.nio.charset.CodingErrorAction.REPORT)
|
||||
java.io.InputStreamReader(input, decoder).use { reader ->
|
||||
val buffer = CharArray(8192)
|
||||
val line = StringBuilder()
|
||||
var withheld = false
|
||||
var afterCr = false
|
||||
fun emit() {
|
||||
out.write(if (withheld) "<unsupported or oversized line withheld>" else redactForShare(line.toString()))
|
||||
out.write("\n")
|
||||
line.setLength(0)
|
||||
withheld = false
|
||||
}
|
||||
try {
|
||||
while (true) {
|
||||
val size = reader.read(buffer)
|
||||
if (size < 0) break
|
||||
for (i in 0 until size) {
|
||||
val ch = buffer[i]
|
||||
if (afterCr) {
|
||||
afterCr = false
|
||||
if (ch == '\n') continue
|
||||
}
|
||||
if (ch == '\r' || ch == '\n') {
|
||||
emit()
|
||||
afterCr = ch == '\r'
|
||||
} else if (!withheld) {
|
||||
// Splitting a line can separate a credential from its label.
|
||||
if (ch == '\u0000' || line.length == SHARE_LINE_MAX) {
|
||||
withheld = true
|
||||
line.setLength(0)
|
||||
} else {
|
||||
line.append(ch)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (withheld || line.isNotEmpty()) emit()
|
||||
} catch (_: java.nio.charset.CharacterCodingException) {
|
||||
out.write("<unsupported encoding; file withheld>\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun quoted(value: String): String {
|
||||
|
||||
@@ -118,7 +118,9 @@ object SessionArtifacts {
|
||||
|
||||
private fun scrubFolder(dir: File, record: Record) {
|
||||
dir.listFiles { f -> f.isFile && !f.name.startsWith(".") && f !in record.files }?.forEach { f ->
|
||||
if (!LogRedactor.isText(f)) return@forEach
|
||||
if (LogRedactor.isBinary(f)) return@forEach
|
||||
// Too large to rewrite in memory: the folder must not be recorded as scrubbed.
|
||||
if (!LogRedactor.isText(f)) { record.failed = true; return@forEach }
|
||||
val done = scrubFile(dir, f)
|
||||
if (done == null) record.failed = true else record.files[f] = done
|
||||
}
|
||||
|
||||
@@ -66,15 +66,12 @@ object SessionLogShare {
|
||||
return zip
|
||||
}
|
||||
|
||||
/** Every entry is scrubbed line by line, whatever its size: a raw copy can carry tokens out of the device. */
|
||||
private fun addEntry(z: ZipOutputStream, name: String, f: File) {
|
||||
z.putNextEntry(ZipEntry(name))
|
||||
if (LogRedactor.isText(f)) {
|
||||
val w = z.bufferedWriter()
|
||||
LogRedactor.scrubForShare(f, w)
|
||||
w.flush()
|
||||
} else {
|
||||
f.inputStream().use { it.copyTo(z) }
|
||||
}
|
||||
val w = z.bufferedWriter()
|
||||
LogRedactor.scrubForShare(f, w)
|
||||
w.flush()
|
||||
z.closeEntry()
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user