From 42ab67e4b4853c2b396f37436181f9262cb01b23 Mon Sep 17 00:00:00 2001 From: mak777uh Date: Mon, 26 Oct 2026 00:15:32 +0300 Subject: [PATCH] sd: add delete/wipe to the SD card file picker The SD host can only read: psbt files and exported wallet descriptors (.json) can be removed only from a PC. This adds "Delete a file" and "Delete ALL files" to the picker, a best-effort secure delete (os.urandom overwrite + fsync + remove), and keeps the picker open after a delete instead of dropping to the main menu. Adds 11 tests with a fake GUI and a real filesystem, including a check that the content is overwritten before os.remove. Closes #359 --- src/hosts/sd.py | 133 +++++++++++- src/platform.py | 36 ++++ .../tests_native/test_sd_host_delete_files.py | 204 ++++++++++++++++++ 3 files changed, 365 insertions(+), 8 deletions(-) create mode 100644 test/tests_native/test_sd_host_delete_files.py diff --git a/src/hosts/sd.py b/src/hosts/sd.py index 27095146..ea009284 100644 --- a/src/hosts/sd.py +++ b/src/hosts/sd.py @@ -4,6 +4,13 @@ import platform from binascii import hexlify from helpers import a2b_base64_stream +import lvgl as lv + +# Special values returned by the file picker instead of a file name. +# They can't collide with a real file, because the picker +# always returns a full path to a file on the SD card. +DELETE_FILE = "delete-file" +WIPE_SD_CARD = "wipe-sd-card" class SDHost(Host): """ @@ -11,11 +18,17 @@ class SDHost(Host): Manages communication with SD card: - loading unsigned transaction and authentications - saving signed transaction to the card + - deleting files from the card """ button = "Open SD card file" settings_button = "SD card" + # file types we can load from the SD card + # (wallet descriptors are saved as .json, + # signed transactions keep the extension of the source file) + extensions = (".psbt", ".txt", ".json") + def __init__(self, path, sdpath=fpath("/sd")): super().__init__(path) self.sdpath = sdpath @@ -46,9 +59,21 @@ async def get_data(self, raw=False, chunk_timeout=0.1): """ self.reset_and_mount() try: - sd_file = await self.select_file([".psbt", ".txt", ".json"]) - if sd_file is None: - return + while True: + sd_file = await self.select_file(self.extensions) + if sd_file is None: + return + # the user picked an action instead of a file - + # do it and show the picker again + if sd_file == DELETE_FILE: + await self.delete_files() + elif sd_file == WIPE_SD_CARD: + await self.wipe_sd_card() + else: + break + # nothing left to open - don't show an empty picker + if len(self.list_files(self.extensions)) == 0: + return self.sd_file = sd_file with open(self.fram, "wb") as fout: with open(self.sd_file, "rb") as fin: @@ -68,7 +93,8 @@ def truncate(self, fname): return fname return fname[:18]+"..."+fname[-12:] - async def select_file(self, extensions): + def list_files(self, extensions): + """Returns a sorted list of files on the SD card with given extensions""" files = sum([ [ f[0] for f in os.ilistdir(self.sdpath) @@ -76,13 +102,16 @@ async def select_file(self, extensions): and f[1] == 0x8000 ] for ext in extensions ], []) - + files.sort() + return files + + async def select_file(self, extensions, allow_delete=True): + files = self.list_files(extensions) if len(files) == 0: raise HostError("\n\nNo matching files found on the SD card\nAllowed: %s" % ", ".join(extensions)) # elif len(files) == 1: # return self.sdpath+"/"+ files[0] - - files.sort() + buttons = [] for ext in extensions: title = [(None, ext+" files")] @@ -95,10 +124,98 @@ async def select_file(self, extensions): buttons += [(None, "%s files - No files" % ext)] else: buttons += title + barr - + + # actions that don't open a file + if allow_delete: + buttons += [(None, None)] + buttons += [(DELETE_FILE, lv.SYMBOL.TRASH + " Delete a file")] + buttons += [(WIPE_SD_CARD, lv.SYMBOL.TRASH + " Delete ALL files")] + fname = await self.manager.gui.menu(buttons, title="Select a file", last=(None, "Cancel")) return fname + async def delete_files(self): + """ + Lets the user delete files from the SD card one by one. + SD card has to be mounted by the caller. + """ + while True: + if len(self.list_files(self.extensions)) == 0: + await self.manager.gui.alert( + "Nothing to delete", + "\n\nThere are no files on the SD card\nthat can be deleted.", + button_text="Close", + ) + return + fname = await self.select_file(self.extensions, allow_delete=False) + if fname is None: + return + short = self.truncate(fname.split("/")[-1]) + confirm = await self.manager.gui.prompt( + "Delete file?", + "\n\n%s\n\nThe file will be overwritten with random data " + "and deleted from the SD card." % short, + ) + if not confirm: + continue + try: + platform.secure_delete_file(fname) + except Exception as e: + print(e) + await self.manager.gui.alert( + "Error!", + "\n\nFailed to delete\n\n%s" % short, + ) + continue + await self.manager.gui.alert( + "Success!", + "\n\nFile deleted:\n\n%s" % short, + button_text="Close", + ) + + async def wipe_sd_card(self): + """ + Overwrites all files in the root folder of the SD card + with random data and deletes them. + SD card has to be mounted by the caller. + """ + names = [ + f[0] for f in os.ilistdir(self.sdpath) + if f[1] == 0x8000 + ] + if len(names) == 0: + await self.manager.gui.alert( + "Nothing to delete", + "\n\nThe SD card is already empty.", + button_text="Close", + ) + return + confirm = await self.manager.gui.prompt( + "Delete ALL files?", + "\n\nAll %d files on the SD card will be overwritten " + "with random data and deleted.\n\nThis cannot be undone!" % len(names), + ) + if not confirm: + return + failed = 0 + for name in names: + try: + platform.secure_delete_file(self.sdpath + "/" + name) + except Exception as e: + print(e) + failed += 1 + if failed > 0: + await self.manager.gui.alert( + "Error!", + "\n\nFailed to delete %d of %d files." % (failed, len(names)), + ) + else: + await self.manager.gui.alert( + "Success!", + "\n\nAll %d files are deleted." % len(names), + button_text="Close", + ) + def completed_filename(self, filename): suffix = "" if self.parent is None else ("."+hexlify(self.parent.fingerprint).decode()) if filename.endswith(".psbt"): diff --git a/src/platform.py b/src/platform.py index e0c9df7f..2715eadc 100644 --- a/src/platform.py +++ b/src/platform.py @@ -312,6 +312,42 @@ def delete_recursively(path, include_self=False): raise RuntimeError("Failed to delete folder %s" % path) +def secure_delete_file(fname, chunk_size=512): + """ + Deletes a file, overwriting its content with random data first. + This is best-effort: it removes the data from the blocks that are + currently mapped to the file, but it is not a secure erase of the + whole media. On flash storage with wear leveling (SD cards, internal + flash) the controller may keep copies in blocks that are not + reachable anymore, and free space on the card is not touched at all. + Use it as a defense in depth measure, not as a guarantee. + """ + try: + size = os.stat(fname)[6] + except OSError: + # file doesn't exist - nothing to delete + return + if size > 0: + try: + with open(fname, "r+b") as f: + left = size + while left > 0: + n = chunk_size if left > chunk_size else left + f.write(os.urandom(n)) + left -= n + try: + f.flush() + os.fsync(f) + except (AttributeError, OSError): + pass + except Exception as e: + # overwriting is best-effort, + # but we still want to remove the file + print(e) + os.remove(fname) + sync() + + if not simulator: stlk = pyb.UART("YB", 9600) diff --git a/test/tests_native/test_sd_host_delete_files.py b/test/tests_native/test_sd_host_delete_files.py new file mode 100644 index 00000000..2c2f03e2 --- /dev/null +++ b/test/tests_native/test_sd_host_delete_files.py @@ -0,0 +1,204 @@ +import sys + +if sys.implementation.name != 'micropython': + from native_support import setup_native_stubs + + setup_native_stubs() + +import os +import unittest + +import platform + +from hosts.sd import SDHost, DELETE_FILE, WIPE_SD_CARD + + +TEST_DIR = "testdir_sd" +SD_DIR = TEST_DIR + "/sd" + + +def run_async(coro): + """Runs a coroutine to completion on a fresh event loop.""" + try: + import asyncio + except ImportError: + import uasyncio as asyncio + if hasattr(asyncio, "run"): + return asyncio.run(coro) + loop = asyncio.get_event_loop() + return loop.run_until_complete(coro) + + +class FakeGUI: + """Minimal GUI stub that replays scripted answers.""" + + def __init__(self, menu=None, prompt=None): + self.menu_answers = list(menu or []) + self.prompt_answers = list(prompt or []) + # values of the buttons of every menu we were asked to show + self.menus = [] + self.alerts = [] + + async def menu(self, buttons=[], title="", note=None, last=None): + self.menus.append([b[0] for b in buttons]) + if len(self.menu_answers) > 0: + return self.menu_answers.pop(0) + return None + + async def prompt(self, title, msg, popup=False): + if len(self.prompt_answers) > 0: + return self.prompt_answers.pop(0) + return False + + async def alert(self, title, msg, button_text="OK", note=None): + self.alerts.append((title, msg)) + + @property + def messages(self): + return [msg for _, msg in self.alerts] + + +class FakeManager: + def __init__(self, gui): + self.gui = gui + + +class SDHostDeleteTest(unittest.TestCase): + def setUp(self): + self._rmtree(TEST_DIR) + platform.maybe_mkdir(TEST_DIR) + platform.maybe_mkdir(SD_DIR) + self.gui = FakeGUI() + self.host = SDHost(TEST_DIR + "/host", sdpath=SD_DIR) + self.host.manager = FakeManager(self.gui) + + def tearDown(self): + self._rmtree(TEST_DIR) + + def _rmtree(self, path): + try: + platform.delete_recursively(path, include_self=True) + except Exception: + pass + + def write(self, name, data=b"psbt-data"): + with open(SD_DIR + "/" + name, "wb") as f: + f.write(data) + + def names(self): + return sorted([f[0] for f in os.ilistdir(SD_DIR) if f[1] == 0x8000]) + + def test_picker_offers_delete_actions(self): + self.write("tx.psbt") + res = run_async(self.host.select_file(self.host.extensions)) + # no answer scripted -> user cancelled + self.assertIsNone(res) + values = self.gui.menus[0] + self.assertIn(DELETE_FILE, values) + self.assertIn(WIPE_SD_CARD, values) + # file itself is still offered + self.assertIn(SD_DIR + "/tx.psbt", values) + + def test_picker_without_delete_has_no_actions(self): + self.write("tx.psbt") + run_async(self.host.select_file(self.host.extensions, allow_delete=False)) + values = self.gui.menus[0] + self.assertNotIn(DELETE_FILE, values) + self.assertNotIn(WIPE_SD_CARD, values) + + def test_delete_single_file(self): + self.write("tx.psbt", b"unsigned transaction") + self.write("wallet.json", b"{}") + self.gui.menu_answers = [SD_DIR + "/tx.psbt", None] + self.gui.prompt_answers = [True] + run_async(self.host.delete_files()) + self.assertEqual(self.names(), ["wallet.json"]) + self.assertTrue(any("deleted" in m for m in self.gui.messages)) + + def test_delete_cancel_keeps_file(self): + self.write("tx.psbt", b"unsigned transaction") + self.gui.menu_answers = [SD_DIR + "/tx.psbt", None] + self.gui.prompt_answers = [False] + run_async(self.host.delete_files()) + self.assertEqual(self.names(), ["tx.psbt"]) + self.assertFalse(any("deleted" in m for m in self.gui.messages)) + + def test_delete_last_file_stops_picker(self): + self.write("tx.psbt", b"unsigned transaction") + self.gui.menu_answers = [SD_DIR + "/tx.psbt"] + self.gui.prompt_answers = [True] + run_async(self.host.delete_files()) + self.assertEqual(self.names(), []) + # after the last file there is nothing to pick, + # the flow should end without asking for a file again + self.assertEqual(len(self.gui.menus), 1) + self.assertTrue(any("Nothing to delete" in t for t, _ in self.gui.alerts)) + + def test_wipe_deletes_all_files(self): + self.write("tx.psbt") + self.write("wallet.json") + self.write("notes.txt") + self.gui.prompt_answers = [True] + run_async(self.host.wipe_sd_card()) + self.assertEqual(self.names(), []) + self.assertTrue(any("All 3 files are deleted" in m for m in self.gui.messages)) + + def test_wipe_cancel_keeps_files(self): + self.write("tx.psbt") + self.write("wallet.json") + self.gui.prompt_answers = [False] + run_async(self.host.wipe_sd_card()) + self.assertEqual(self.names(), ["tx.psbt", "wallet.json"]) + + def test_get_data_deletes_then_opens_file(self): + self.write("a.psbt", b"first") + self.write("b.psbt", b"second") + # SDHost.copy() uses MicroPython's write(buf, len) signature which + # doesn't exist in CPython where the native tests run. + # The flow we test here doesn't depend on it. + def copy(fin, fout): + fout.write(fin.read()) + self.host.copy = copy + self.gui.menu_answers = [DELETE_FILE, SD_DIR + "/a.psbt", None, SD_DIR + "/b.psbt"] + self.gui.prompt_answers = [True] + stream = run_async(self.host.get_data()) + self.assertEqual(self.names(), ["b.psbt"]) + data = stream.read() + self.assertTrue(data.startswith(b"sign ")) + self.assertIn(b"second", data) + stream.close() + + def test_get_data_stops_when_card_is_empty(self): + self.write("only.psbt", b"only file") + self.gui.menu_answers = [WIPE_SD_CARD] + self.gui.prompt_answers = [True] + stream = run_async(self.host.get_data()) + # nothing left to open - no exception and no empty picker + self.assertIsNone(stream) + self.assertEqual(self.names(), []) + + def test_secure_delete_overwrites_before_removing(self): + fname = SD_DIR + "/secret.psbt" + self.write("secret.psbt", b"\x00" * 64) + captured = {} + original_remove = os.remove + + def fake_remove(path): + if path == fname: + with open(path, "rb") as f: + captured["data"] = f.read() + return original_remove(path) + + os.remove = fake_remove + try: + platform.secure_delete_file(fname) + finally: + os.remove = original_remove + + self.assertFalse(platform.file_exists(fname)) + self.assertEqual(len(captured["data"]), 64) + self.assertNotEqual(captured["data"], b"\x00" * 64) + + def test_secure_delete_missing_file_is_noop(self): + platform.secure_delete_file(SD_DIR + "/not-there.psbt") + self.assertEqual(self.names(), [])