Skip to content

A high-performance Android storage analyzer that bypasses MTP using a native C++ daemon, a Rust TCP bridge, and a React Treemap UI.

Rust

Validate scan summaries before publishing them to the UI (#132)

Vishnu Srivatsava27c8b4c3789f
diff --git a/src/App.tsx b/src/App.tsx
index 2593a26..0fa9e69 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -2,8 +2,8 @@ import { useState, useCallback, useRef } from "react";
import { invoke } from "@tauri-apps/api/core";
import { checkAdb, confirmDaemonShutdown, startDaemon, stopDaemon } from "./daemonIpc";
import { formatBytes } from "./formatBytes";
-import { sameScan, parseNodeDetail } from "./scanView";
-import type { ScanResponse, NodeDetail, DeleteResult } from "./scanView";
+import { sameScan, parseNodeDetail, parseDeleteResult } from "./scanView";
+import type { ScanResponse, NodeDetail } from "./scanView";
import { DeleteConfirmation } from "./DeleteConfirmation";
import { ToastContainer } from "./ToastContainer";
import { useToasts } from "./useToasts";
@@ -185,7 +185,7 @@ function App() {
generation: target.generation, revision: target.revision, nodeId: target.node.id,
});
if (!sameScan(snapshot.current, target)) return;
- const result: DeleteResult = JSON.parse(raw);
+ const result = parseDeleteResult(JSON.parse(raw));
if (result.summary.generation !== target.generation || result.summary.revision !== target.revision + 1) {
throw new Error("Deletion returned an unexpected scan snapshot");
}
diff --git a/src/scanView.ts b/src/scanView.ts
index 0621c57..25b8bc2 100644
--- a/src/scanView.ts
+++ b/src/scanView.ts
@@ -100,6 +100,35 @@ export function parseNodeReference(value: unknown, expected: NodeContext): Pick<
} };
}
+export function parseScanSummary(data: unknown): ScanResponse {
+ const invalid = () => new Error("Invalid scan summary");
+ if (!isRecord(data) || data.status !== "ok" || typeof data.generation !== "string" || !data.generation
+ || !isU32(data.revision) || typeof data.root_path !== "string" || !data.root_path.startsWith("/")
+ || data.root_path.includes("\0") || typeof data.read_only !== "boolean" || !isCount(data.total_size)
+ || !isCount(data.total_files) || !isCount(data.total_dirs) || !isCount(data.errors)
+ || !isCount(data.skipped) || !isCount(data.scan_time_ms)
+ || (data.streamed !== undefined && typeof data.streamed !== "boolean")) throw invalid();
+ for (const field of ["traverse_ms", "sort_ms", "serialize_ms", "send_ms", "bytes_sent"]) {
+ if (data[field] !== undefined && !isCount(data[field])) throw invalid();
+ }
+ const { node } = parseNodeReference({ node: data.root, path: data.root_path }, {
+ root_path: data.root_path, read_only: data.read_only, total_size: data.total_size,
+ });
+ if (node.id !== 0 || node.size !== data.total_size) throw invalid();
+ return { ...data, root: node } as ScanResponse;
+}
+
+export function parseDeleteResult(data: unknown): DeleteResult {
+ if (!isRecord(data) || typeof data.message !== "string" || !isRecord(data.removed)
+ || !isCount(data.removed.files) || !isCount(data.removed.dirs) || !isCount(data.removed.size)) {
+ throw new Error("Invalid deletion result");
+ }
+ return {
+ summary: parseScanSummary(data.summary), message: data.message,
+ removed: { files: data.removed.files, dirs: data.removed.dirs, size: data.removed.size },
+ };
+}
+
/** Validate the fresh detail used by confirmation without accepting UI paths. */
export function parseNodeDetail(raw: string, expected: ScanScope & NodeContext, nodeId: number): NodeDetail {
const data: unknown = JSON.parse(raw);
diff --git a/src/useScanRequest.ts b/src/useScanRequest.ts
index df54b48..b5ea329 100644
--- a/src/useScanRequest.ts
+++ b/src/useScanRequest.ts
@@ -1,5 +1,6 @@
import { useCallback, useRef, useState } from "react";
import { Channel, invoke } from "@tauri-apps/api/core";
+import { parseScanSummary } from "./scanView";
import type { ScanResponse } from "./scanView";
export interface ScanProgress {
@@ -29,9 +30,7 @@ export function useScanRequest(onError: (message: string) => void) {
};
try {
const raw = await invoke<string>("run_scan", { path, scanId, onProgress });
- const parsed: ScanResponse = JSON.parse(raw);
- if (parsed.status !== "ok") throw new Error("Scan returned non-ok status");
- return parsed;
+ return parseScanSummary(JSON.parse(raw));
} finally {
if (activeScan.current === scanId) activeScan.current = null;
setCancelling(false);
diff --git a/tests/scan_view.test.mjs b/tests/scan_view.test.mjs
index c074312..3a8a39c 100644
--- a/tests/scan_view.test.mjs
+++ b/tests/scan_view.test.mjs
@@ -7,7 +7,7 @@ const source = await readFile(new URL("../src/scanView.ts", import.meta.url), "u
const { outputText } = ts.transpileModule(source, {
compilerOptions: { target: ts.ScriptTarget.ES2020, module: ts.ModuleKind.ESNext },
});
-const { sameScan, viewKey, viewReducer, parseNodeDetail } = await import(
+const { sameScan, viewKey, viewReducer, parseNodeDetail, parseScanSummary, parseDeleteResult } = await import(
`data:text/javascript;base64,${Buffer.from(outputText).toString("base64")}`
);
const scope = { generation: "scan-a", revision: 0 };
@@ -135,3 +135,25 @@ test("node detail rejects misleading paths and unsafe deletion flags", () => {
const readOnly = detail(); readOnly.node.can_delete = false;
assert.deepEqual(parseDetail(readOnly, { ...detailScope, read_only: true }), readOnly);
});
+
+test("scan and deletion summaries validate before becoming the active snapshot", () => {
+ const summary = {
+ ...scope, status: "ok", root_path: "/", read_only: true,
+ total_size: 0, total_files: 0, total_dirs: 1, errors: 0, skipped: 0, scan_time_ms: 0,
+ root: { id: 0, parent_id: null, name: "/", type: "directory", size: 0,
+ counted: true, child_count: 0, can_delete: false },
+ };
+ assert.deepEqual(parseScanSummary(summary), summary);
+ for (const invalid of [
+ null, { ...summary, total_files: "0" }, { ...summary, revision: -1 },
+ { ...summary, root: { ...summary.root, can_delete: true } },
+ { ...summary, total_size: 1 }, { ...summary, send_ms: null },
+ ]) {
+ assert.throws(() => parseScanSummary(invalid));
+ }
+
+ const deletion = { summary, message: "Deleted", removed: { files: 1, dirs: 0, size: 0 } };
+ assert.deepEqual(parseDeleteResult(deletion), deletion);
+ assert.throws(() => parseDeleteResult({ ...deletion, removed: { ...deletion.removed, files: -1 } }));
+ assert.throws(() => parseDeleteResult({ ...deletion, summary: {} }));
+});