Modern Apps git

← Modern-Apps.git

notes: sanitize share file name derived from title

Commit
aa41f94a31c9423f3851dece5ca65356f92b6a8e
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:10 UTC (29 days ago)
Parents
79f963c
Symptom: sharing or copying a note whose title contains a slash
(for example "trips/day 1") crashed instead of opening the share
sheet: the raw title became a file name under cacheDir/shared_notes,
so writeText hit a non-existent subdirectory, and a title like
"../../x" escaped the directory FileProvider is configured for.

Root cause: NotesViewModel.requestShare and markdownCacheUri both
built File(cachePath, "<title>.md") from unsanitized user text.

Fix: one shared shareMarkdownFileName() replaces path-hostile and
control characters, trims leading dots, and falls back to note.md;
both call sites use it.

Verification: new NoteStoresTest case asserts a slash/dotdot title
writes inside shared_notes and pins the fallback names. Full notes
unit-test suite run at the end of this stack.

Diff

notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.kt b/notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.ktindex 0a7ff2c8f5..936067832d 100644--- a/notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.kt+++ b/notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.kt@@ -53,10 +53,21 @@ fun exportNoteMarkdown(context: Context, note: Note): String =  * [NotesViewModel.requestShare], so no extra manifest setup is needed. Used by copy  * so a note with inlined (multi-MB) images is passed as a URI instead of raw text.  */+/**+ * Cache file name for a shared/copied note. The title is user text: used raw+ * it can contain `/` or `..`, which either makes the write fail (missing+ * parent dir, crashing the share) or escapes the shared_notes directory so+ * FileProvider refuses the URI. Strip path-hostile characters instead.+ */+internal fun shareMarkdownFileName(title: String): String {+    val cleaned = title.trim().replace(Regex("[\\\\/:*?\"<>|\\p{Cntrl}]"), "_").trim('.')+    return "${cleaned.ifBlank { "note" }}.md"+}+ fun markdownCacheUri(context: Context, note: Note, markdown: String): Uri {     val cachePath = File(context.cacheDir, "shared_notes")     cachePath.mkdirs()-    val file = File(cachePath, "${note.title.ifBlank { "note" }}.md")+    val file = File(cachePath, shareMarkdownFileName(note.title))     file.writeText(markdown)     return FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file) }
notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.kt b/notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.ktindex 730d84b6aa..e752728e9e 100644--- a/notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.kt+++ b/notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.kt@@ -165,7 +165,7 @@ class NotesViewModel(                 val markdown = exportNoteMarkdown(ctx, note)                 val cachePath = File(ctx.cacheDir, "shared_notes")                 cachePath.mkdirs()-                val file = File(cachePath, "${note.title.ifBlank { "note" }}.md")+                val file = File(cachePath, shareMarkdownFileName(note.title))                 file.writeText(markdown)                 val uri = FileProvider.getUriForFile(                     ctx,
notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt
diff --git a/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt b/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.ktindex c2f39c609e..483a2bacf4 100644--- a/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt+++ b/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt@@ -133,6 +133,18 @@ class NoteStoresTest {         assertTrue(md.contains("data:image/jpeg;base64,"))     } +    @Test fun markdownCacheUriSanitizesPathHostileTitles() {+        val uri = markdownCacheUri(context, Note(title = "a/b ../c", content = ""), "body")+        assertTrue(uri.toString().isNotEmpty())+        val dir = File(context.cacheDir, "shared_notes")+        val written = File(dir, shareMarkdownFileName("a/b ../c"))+        assertTrue(written.exists())+        assertEquals(dir.absolutePath, written.parentFile!!.absolutePath)+        assertEquals("note.md", shareMarkdownFileName(""))+        assertEquals("note.md", shareMarkdownFileName("..."))+        assertEquals("My Note.md", shareMarkdownFileName("My Note"))+    }+     @Test fun markdownCacheUriWritesFile() {         val note = Note(title = "My Note", content = "x")         val uri = markdownCacheUri(context, note, "# hello")