Make secure log export testable under Robolectric

This commit is contained in:
wallentx authored and Kurt Himebauch committed 2026-11-05 13:32:46 -05:00
1 parent 5614bd5d5a
commit 3c6447528a
5 files changed
+134

No files matched your search

@@ -16,6 +16,8 @@ import org.robolectric.annotation.Config
import java.io.RandomAccessFile
import java.nio.file.Files
import java.util.zip.ZipFile
import org.junit.After
import org.junit.Before
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
@@ -23,6 +25,10 @@ class EnhancedSessionLogShareTest {
@get:Rule val tmp = TemporaryFolder()
private val app get() = RuntimeEnvironment.getApplication()
@Before fun installSecureSourceOpener() = installJvmSecureSourceOpener()
@After fun resetSourceOpener() = resetSecureSourceOpener()
@Test fun diagnosticsAreSplitIntoManifestedArchivesWithOneSessionIdentity() {
val session = tmp.newFolder("2026-11-04-01-steam")
session.resolve("session.log").writeText("startup ok\n")
@@ -328,4 +334,29 @@ class EnhancedSessionLogShareTest {
}
SessionLogShare.clear(app)
}
@Test fun injectedSecureOpenFailureFailsClosedAndTheOverrideCanBeReset() {
val session = tmp.newFolder("2026-11-04-15-steam")
session.resolve("session.log").writeText("must not be shared\n")
EnhancedSessionLogShare.setSourceOpenerForTest(object : SecureSourceOpener {
override fun <T> withChannel(
root: java.nio.file.Path,
relative: java.nio.file.Path,
block: (java.nio.channels.SeekableByteChannel) -> T,
): T? = null
})
try {
val sessionZip = EnhancedSessionLogShare.create(app, session).single { it.name.endsWith("-session.zip") }
ZipFile(sessionZip).use { archive ->
assertFalse(archive.entries().toList().any { it.name.endsWith("session.log") })
val manifest = archive.getInputStream(archive.getEntry("diagnostics-manifest.json"))
.bufferedReader().readText()
assertTrue(manifest.contains("unsafe-or-unavailable-source"))
}
} finally {
resetSecureSourceOpener()
}
assertFalse(EnhancedSessionLogShare.hasSourceOpenerForTest())
SessionLogShare.clear(app)
}
}
@@ -0,0 +1,72 @@
package com.droiddeck.launcher.session
import java.nio.channels.SeekableByteChannel
import java.nio.file.DirectoryStream
import java.nio.file.Files
import java.nio.file.LinkOption
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.SecureDirectoryStream
import java.nio.file.StandardOpenOption
import java.nio.file.attribute.BasicFileAttributeView
import java.nio.file.attribute.BasicFileAttributes
internal object JvmSecureSourceOpener : SecureSourceOpener {
override fun <T> withChannel(root: Path, relative: Path, block: (SeekableByteChannel) -> T): T? {
val expected = root.resolve(relative).normalize()
if (!expected.startsWith(root.normalize())) return null
val initial = runCatching {
Files.readAttributes(expected, BasicFileAttributes::class.java, LinkOption.NOFOLLOW_LINKS)
}.getOrNull() ?: return null
val links = runCatching {
(Files.getAttribute(expected, "unix:nlink", LinkOption.NOFOLLOW_LINKS) as Number).toLong()
}.getOrNull() ?: return null
val expectedKey = initial.fileKey() ?: return null
if (!initial.isRegularFile || initial.isSymbolicLink || links != 1L) return null
val first = Files.newDirectoryStream(root)
if (first !is SecureDirectoryStream<*>) {
first.close()
return null
}
@Suppress("UNCHECKED_CAST")
val secureFirst = first as SecureDirectoryStream<Path>
val opened = ArrayList<DirectoryStream<Path>>()
opened += first
var current = secureFirst
return try {
val parts = relative.map { it.toString() }
if (parts.isEmpty()) return null
for (part in parts.dropLast(1)) {
val child = Paths.get(part)
val attrs = current.getFileAttributeView(
child, BasicFileAttributeView::class.java, LinkOption.NOFOLLOW_LINKS,
).readAttributes()
if (!attrs.isDirectory || attrs.isSymbolicLink) return null
val next = current.newDirectoryStream(child, LinkOption.NOFOLLOW_LINKS)
opened += next
current = next
}
val leaf = Paths.get(parts.last())
val attrs = current.getFileAttributeView(
leaf, BasicFileAttributeView::class.java, LinkOption.NOFOLLOW_LINKS,
).readAttributes()
if (!attrs.isRegularFile || attrs.isSymbolicLink || attrs.fileKey() != expectedKey) return null
current.newByteChannel(leaf, setOf(StandardOpenOption.READ, LinkOption.NOFOLLOW_LINKS)).use { channel ->
val after = current.getFileAttributeView(
leaf, BasicFileAttributeView::class.java, LinkOption.NOFOLLOW_LINKS,
).readAttributes()
if (after.fileKey() != expectedKey) null else block(channel)
}
} finally {
opened.asReversed().forEach { runCatching { it.close() } }
}
}
}
internal fun installJvmSecureSourceOpener() {
EnhancedSessionLogShare.setSourceOpenerForTest(JvmSecureSourceOpener)
}
internal fun resetSecureSourceOpener() {
EnhancedSessionLogShare.setSourceOpenerForTest(null)
}
@@ -8,6 +8,8 @@ import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.After
import org.junit.Before
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@@ -24,6 +26,10 @@ import java.util.zip.ZipFile
class SessionLogShareTest {
@get:Rule val tmp = TemporaryFolder()
@Before fun installSecureSourceOpener() = installJvmSecureSourceOpener()
@After fun resetSourceOpener() = resetSecureSourceOpener()
private val secret = "GET /x?sessionid=abcdef123456 200"
private val leaked = "abcdef123456"
@@ -9,6 +9,8 @@ import java.util.zip.ZipFile
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@@ -20,6 +22,10 @@ import org.robolectric.annotation.Config
class SessionLogShareZipTest {
private val app get() = RuntimeEnvironment.getApplication()
@Before fun installSecureSourceOpener() = installJvmSecureSourceOpener()
@After fun resetSourceOpener() = resetSecureSourceOpener()
@Test fun aLargeSteamLogIsRedactedInTheSharedArchive() {
val session = File(LinuxRuntime.logDir(app), "2026-10-08-02-steam").apply { mkdirs() }
val raw = File(session, "steam-42.log")