fix(#38): too-large reads through BackupFiles, plain "w" for the fresh temp file

This commit is contained in:
makiolaj committed 2026-11-14 04:35:56 +01:00
1 parent 9436209f39
commit c8d1017b15
8 files changed
+41 -17

No files matched your search

@@ -3,6 +3,9 @@ package de.jeanlucmakiola.clockula.backup
/** The fixed filename a backup is written to inside the user-chosen folder (`docs/PLAN.md` §7). */
const val BACKUP_FILE_NAME: String = "clockula-backup-v1.json"
/** The picked document is larger than a backup can be. */
class BackupTooLargeException : java.io.IOException("backup exceeds the 8 MiB limit")
/**
* The on-device side of export/import: writing/reading the JSON text through
* SAF. [BackupEngine] does not know this is SAF — it only knows these two
@@ -12,6 +15,6 @@ interface BackupFiles {
/** Writes [content] as [BACKUP_FILE_NAME] inside the folder [treeUri] names, overwriting any existing one. */
suspend fun write(treeUri: String, content: String): Result<Unit>
/** Reads whatever text is at [documentUri]. */
/** Reads whatever text is at [documentUri]; fails with [BackupTooLargeException] past the size limit. */
suspend fun read(documentUri: String): Result<String>
}
@@ -6,7 +6,8 @@ import androidx.documentfile.provider.DocumentFile
import dagger.hilt.android.qualifiers.ApplicationContext
import de.jeanlucmakiola.clockula.backup.BACKUP_FILE_NAME
import de.jeanlucmakiola.clockula.backup.BackupFiles
import de.jeanlucmakiola.clockula.data.backup.MAX_BACKUP_BYTES
import de.jeanlucmakiola.clockula.backup.BackupTooLargeException
import de.jeanlucmakiola.floret.android.DocumentTooLargeException
import de.jeanlucmakiola.floret.android.readDocumentBytes
import de.jeanlucmakiola.floret.android.writeDocumentBytes
import de.jeanlucmakiola.floret.di.IoDispatcher
@@ -32,14 +33,15 @@ class SafBackupFiles @Inject constructor(
override suspend fun write(treeUri: String, content: String): Result<Unit> = withContext(io) {
runCatching {
val bytes = content.toByteArray(Charsets.UTF_8)
if (bytes.size > MAX_BACKUP_BYTES) throw IOException("backup exceeds the 8 MiB limit")
if (bytes.size > MAX_BACKUP_BYTES) throw BackupTooLargeException()
val tree = DocumentFile.fromTreeUri(context, treeUri.toUri())
?: error("could not open the chosen folder")
val temporary = tree.createFile("application/json", "$BACKUP_FILE_NAME.${UUID.randomUUID()}.tmp")
?: throw IOException("could not create a temporary backup")
var keepTemporary = false
try {
context.contentResolver.writeDocumentBytes(temporary.uri, bytes)
// Just created and empty: plain "w", as before, works on every provider.
context.contentResolver.writeDocumentBytes(temporary.uri, bytes, truncate = false)
val existing = tree.findFile(BACKUP_FILE_NAME)
if (existing != null && !existing.delete()) throw IOException("could not remove the previous backup")
// Some SAF providers cannot rename. Once the old file has been
@@ -57,7 +59,16 @@ class SafBackupFiles @Inject constructor(
override suspend fun read(documentUri: String): Result<String> = withContext(io) {
runCatching {
context.contentResolver.readDocumentBytes(documentUri.toUri(), MAX_BACKUP_BYTES).toString(Charsets.UTF_8)
val bytes = try {
context.contentResolver.readDocumentBytes(documentUri.toUri(), MAX_BACKUP_BYTES)
} catch (e: DocumentTooLargeException) {
throw BackupTooLargeException()
}
bytes.toString(Charsets.UTF_8)
}
}
private companion object {
const val MAX_BACKUP_BYTES = 8 * 1024 * 1024
}
}
@@ -53,7 +53,6 @@ object BackupCodec {
name = BACKUP_FORMAT,
version = BACKUP_VERSION,
minVersion = BACKUP_MIN_VERSION,
maxChars = MAX_BACKUP_CHARS,
)
private const val MAX_ALARMS = 10_000
private const val MAX_TIMERS = 10_000
@@ -12,12 +12,6 @@ const val BACKUP_VERSION: Int = 2
/** The oldest version this build still reads. v1 is v2 without the alarm dates. */
const val BACKUP_MIN_VERSION: Int = 1
/** The longest backup Clockula decodes, in characters. */
const val MAX_BACKUP_CHARS: Int = 8 * 1024 * 1024
/** The largest backup file Clockula reads or writes, in bytes. */
const val MAX_BACKUP_BYTES: Int = 8 * 1024 * 1024
/**
* The wire shape of `clockula-backup-v1.json`. `format`, `version` and
* `exportedAt` are floret core-backup's envelope keys and checked by its
@@ -5,8 +5,8 @@ import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import de.jeanlucmakiola.clockula.backup.BackupEngine
import de.jeanlucmakiola.clockula.backup.BackupFiles
import de.jeanlucmakiola.clockula.backup.BackupTooLargeException
import de.jeanlucmakiola.clockula.data.backup.BackupCodec
import de.jeanlucmakiola.floret.android.DocumentTooLargeException
import de.jeanlucmakiola.floret.backup.BackupProblem
import de.jeanlucmakiola.floret.backup.BackupResult
import kotlinx.coroutines.channels.Channel
@@ -75,7 +75,7 @@ class BackupViewModel @Inject internal constructor(
} else {
val failure = result.exceptionOrNull()
if (failure is CancellationException) throw failure
val problem = if (failure is DocumentTooLargeException) BackupProblem.TOO_LARGE else BackupProblem.UNREADABLE
val problem = if (failure is BackupTooLargeException) BackupProblem.TOO_LARGE else BackupProblem.UNREADABLE
BackupEvent.ImportFailed(problem)
}
_events.trySend(event)
@@ -2,6 +2,7 @@ package de.jeanlucmakiola.clockula.ui.settings
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.clockula.backup.BackupFiles
import de.jeanlucmakiola.clockula.backup.BackupTooLargeException
import de.jeanlucmakiola.clockula.data.backup.BackupCodec
import de.jeanlucmakiola.floret.backup.BackupProblem
import de.jeanlucmakiola.clockula.testing.MainDispatcherExtension
@@ -53,4 +54,19 @@ class BackupViewModelTest {
assertThat(viewModel.events.first()).isEqualTo(BackupEvent.ImportFailed(BackupProblem.NOT_APPLIED))
assertThat(viewModel.busy.value).isFalse()
}
@Test
fun `an oversized file is reported as too large`(@TempDir tempDir: Path) = runTest(main.dispatcher) {
val harness = backupHarness(tempDir)
val files = object : BackupFiles {
override suspend fun write(treeUri: String, content: String): Result<Unit> = error("unused")
override suspend fun read(documentUri: String): Result<String> = Result.failure(BackupTooLargeException())
}
val viewModel = BackupViewModel(harness.backupEngine, files)
viewModel.onImportFilePicked("content://backup")
assertThat(viewModel.events.first()).isEqualTo(BackupEvent.ImportFailed(BackupProblem.TOO_LARGE))
}
}
+3 -2
View File
@@ -6,8 +6,9 @@ own data, written to (and read from) a user-chosen folder via SAF
(`data/backup/BackupDto.kt`, `BackupCodec.kt`) implements exactly what is
written here. The envelope keys (`format`, `version`, `exportedAt`) and the
format, version and size rules are floret-kit `core-backup`'s `BackupFormat`,
shared with Calendula's settings backup; the file is still decoded in one
streaming pass into Clockula's own classes.
shared with Calendula's settings backup. The file is still decoded in one
streaming pass into Clockula's own classes, which also accept a number
written as a string where floret's tree reader would not.
## Top level