mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 05:16:01 +00:00
fix(agent): secure and refresh unrooted log exports (#2131)
## Summary - Keep raw diagnostic staging in private cache and give each exported archive a unique request directory. - Remove that request directory after the CLI pull, including failed transfers. - Replace files from the previous extracted snapshot while preserving unrelated destination files. - Document that the old rooted-device fallback is not scrubbed. Follow-up to #2061. The refreshed export tests now cover independent archive outputs and stale snapshot replacement. ## Validation - `wsl python3 -m unittest tools.tests.test_droiddeckctl` — 49 passed. - `:app:testDebugUnitTest -x buildVkBridgeServer --tests com.droiddeck.launcher.session.SessionLogShareZipTest.anArchiveCanBeWrittenOutsideTheShareFolder` — passed. - `git diff --check` — passed.
This commit is contained in:
9 files changed
+659
-98
No files matched your search
@@ -0,0 +1,65 @@
|
||||
package com.droiddeck.launcher.agent
|
||||
|
||||
import android.os.Bundle
|
||||
import com.droiddeck.launcher.session.SessionState
|
||||
import com.droiddeck.launcher.session.installJvmSecureSourceOpener
|
||||
import com.droiddeck.launcher.session.resetSecureSourceOpener
|
||||
import org.json.JSONObject
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import java.io.File
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [33], manifest = Config.NONE)
|
||||
class AgentLogExportTest {
|
||||
private val context get() = RuntimeEnvironment.getApplication()
|
||||
private var previousLogDirectory: File? = null
|
||||
|
||||
@Before fun prepare() {
|
||||
previousLogDirectory = SessionState.logDirectory
|
||||
installJvmSecureSourceOpener()
|
||||
}
|
||||
|
||||
@After fun cleanup() {
|
||||
SessionState.logDirectory = previousLogDirectory
|
||||
resetSecureSourceOpener()
|
||||
}
|
||||
|
||||
@Test fun independentLogRequestsKeepBothExportDirectories() {
|
||||
val session = File(context.cacheDir, "agent-session").apply { mkdirs() }
|
||||
File(session, "session.log").writeText("booted\n")
|
||||
SessionState.logDirectory = session
|
||||
val provider = Robolectric.buildContentProvider(AgentBridgeProvider::class.java).create().get()
|
||||
var firstDir: File? = null
|
||||
var secondDir: File? = null
|
||||
|
||||
try {
|
||||
fun requestLogs() = JSONObject(provider.call("logs", null, Bundle()).getString("json")!!)
|
||||
val first = requestLogs()
|
||||
val second = requestLogs()
|
||||
firstDir = File(first.getString("cleanupDir"))
|
||||
secondDir = File(second.getString("cleanupDir"))
|
||||
val firstZip = File(first.getString("zip"))
|
||||
val secondZip = File(second.getString("zip"))
|
||||
|
||||
assertNotEquals(firstDir, secondDir)
|
||||
assertEquals(firstDir, firstZip.parentFile)
|
||||
assertEquals(secondDir, secondZip.parentFile)
|
||||
assertTrue(firstZip.isFile)
|
||||
assertTrue(secondZip.isFile)
|
||||
} finally {
|
||||
firstDir?.deleteRecursively()
|
||||
secondDir?.deleteRecursively()
|
||||
session.deleteRecursively()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import java.io.IOException
|
||||
import java.io.RandomAccessFile
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.channels.FileChannel
|
||||
@@ -497,4 +498,35 @@ class EnhancedSessionLogShareTest {
|
||||
assertFalse(EnhancedSessionLogShare.hasSourceOpenerForTest())
|
||||
SessionLogShare.clear(app)
|
||||
}
|
||||
|
||||
@Test fun sourceReadFailureIsRecordedWithoutFailingTheArchive() {
|
||||
val session = tmp.newFolder("2026-11-04-16-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? = FileChannel.open(root.resolve(relative), StandardOpenOption.READ).use { channel ->
|
||||
block(object : SeekableByteChannel by channel {
|
||||
override fun read(dst: ByteBuffer): Int = throw IOException("injected source read failure")
|
||||
})
|
||||
}
|
||||
})
|
||||
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 = JSONObject(archive.getInputStream(archive.getEntry("diagnostics-manifest.json"))
|
||||
.bufferedReader().readText()).getJSONArray("entries")
|
||||
val record = (0 until manifest.length()).map { manifest.getJSONObject(it) }
|
||||
.single { it.optString("entry").endsWith("session.log") }
|
||||
assertEquals("error", record.getString("status"))
|
||||
assertEquals("IOException", record.getString("reason"))
|
||||
}
|
||||
} finally {
|
||||
resetSecureSourceOpener()
|
||||
}
|
||||
SessionLogShare.clear(app)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.droiddeck.launcher.session
|
||||
import com.droiddeck.launcher.runtime.LinuxRuntime
|
||||
import java.io.File
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.UUID
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
import java.util.zip.ZipFile
|
||||
@@ -26,18 +27,35 @@ class SessionLogShareZipTest {
|
||||
|
||||
@After fun resetSourceOpener() = resetSecureSourceOpener()
|
||||
|
||||
@Test fun startupRemovesAbandonedDiagnosticStagesAndPreservesActiveSnapshots() {
|
||||
val abandoned = File(app.cacheDir, "share-${UUID.randomUUID()}.diagnostic-stage").apply { mkdirs() }
|
||||
val active = EnhancedSessionLogShare.acquireDiagnosticStage(app, "share")
|
||||
try {
|
||||
EnhancedSessionLogShare.cleanupAbandonedStages(app)
|
||||
assertFalse(abandoned.exists())
|
||||
assertTrue(active.directory.isDirectory)
|
||||
} finally {
|
||||
active.close()
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun anArchiveCanBeWrittenOutsideTheShareFolder() {
|
||||
SessionLogShare.clear(app)
|
||||
val session = File(LinuxRuntime.logDir(app), "2026-11-03-01-steam").apply { mkdirs() }
|
||||
val out = File(app.getExternalFilesDir(null), "agent-logs")
|
||||
val out = File(app.getExternalFilesDir(null), "agent-logs/request-1")
|
||||
val secondOut = File(app.getExternalFilesDir(null), "agent-logs/request-2")
|
||||
try {
|
||||
File(session, "session.log").writeText("booted\n")
|
||||
val zip = SessionLogShare.zipFolder(app, session, out)!!
|
||||
val secondZip = SessionLogShare.zipFolder(app, session, secondOut)!!
|
||||
assertEquals(out, zip.parentFile)
|
||||
assertFalse(File(app.cacheDir, "share").exists())
|
||||
assertTrue(zip.isFile)
|
||||
assertEquals(secondOut, secondZip.parentFile)
|
||||
assertTrue(secondZip.isFile)
|
||||
} finally {
|
||||
out.deleteRecursively()
|
||||
secondOut.deleteRecursively()
|
||||
session.deleteRecursively()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user