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 progress messages (#134)

Vishnu Srivatsavace4630ff19f2
diff --git a/src/ScanningScreen.tsx b/src/ScanningScreen.tsx
index 9e33042..e5cb69c 100644
--- a/src/ScanningScreen.tsx
+++ b/src/ScanningScreen.tsx
@@ -1,5 +1,5 @@
import { formatBytes } from "./formatBytes";
-import type { ScanProgress } from "./useScanRequest";
+import type { ScanProgress } from "./scanView";
function formatNumber(n: number): string {
return n.toLocaleString("en-US");
diff --git a/src/scanView.ts b/src/scanView.ts
index 0ee2caa..79af998 100644
--- a/src/scanView.ts
+++ b/src/scanView.ts
@@ -66,6 +66,23 @@ const textBytes = (value: string) => {
};
const entryName = (value: unknown): value is string => typeof value === "string" && value !== "" && value !== "." && value !== ".."
&& !value.includes("/") && !value.includes("\0") && textBytes(value) <= 255;
+export interface ScanProgress {
+ scan_id: string;
+ files: number;
+ directories: number;
+ total_size: number;
+ recent_names: string[];
+}
+
+export function parseScanProgress(value: unknown, scanId: string): ScanProgress | null {
+ if (!isRecord(value) || value.scan_id !== scanId || !isCount(value.files)
+ || !isCount(value.directories) || !isCount(value.total_size)
+ || !Array.isArray(value.recent_names) || value.recent_names.length > 5
+ || !value.recent_names.every(entryName)) return null;
+ return { scan_id: scanId, files: value.files, directories: value.directories,
+ total_size: value.total_size, recent_names: value.recent_names };
+}
+
type NodeContext = Pick<ScanResponse, "root_path" | "read_only" | "total_size">;
const invalidNode = () => new Error("Scan node response does not match this scan");
diff --git a/src/useScanRequest.ts b/src/useScanRequest.ts
index b5ea329..3edb6b3 100644
--- a/src/useScanRequest.ts
+++ b/src/useScanRequest.ts
@@ -1,15 +1,7 @@
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 {
- scan_id: string;
- files: number;
- directories: number;
- total_size: number;
- recent_names: string[];
-}
+import { parseScanProgress, parseScanSummary } from "./scanView";
+import type { ScanProgress, ScanResponse } from "./scanView";
export function useScanRequest(onError: (message: string) => void) {
const [scanProgress, setScanProgress] = useState<ScanProgress | null>(null);
@@ -21,11 +13,13 @@ export function useScanRequest(onError: (message: string) => void) {
activeScan.current = scanId;
setScanProgress(null);
setCancelling(false);
- const onProgress = new Channel<ScanProgress>();
+ const onProgress = new Channel<unknown>();
onProgress.onmessage = (progress) => {
- if (activeScan.current === scanId && progress.scan_id === scanId) setScanProgress(progress);
- void invoke("acknowledge_scan_progress", { scanId: progress.scan_id }).catch(() => {
- // The completed/cancelled scan may already have released its channel.
+ const parsed = parseScanProgress(progress, scanId);
+ if (activeScan.current === scanId && parsed) setScanProgress(parsed);
+ // Release backpressure for this channel even if its payload was ignored.
+ void invoke("acknowledge_scan_progress", { scanId }).catch(() => {
+ // The scan may have already finished.
});
};
try {
diff --git a/tests/scan_view.test.mjs b/tests/scan_view.test.mjs
index de435e9..10f6ed2 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, parseScanSummary, parseDeleteResult, parseScanView } = await import(
+const { sameScan, viewKey, viewReducer, parseNodeDetail, parseScanSummary, parseDeleteResult, parseScanView, parseScanProgress } = await import(
`data:text/javascript;base64,${Buffer.from(outputText).toString("base64")}`
);
const scope = { generation: "scan-a", revision: 0 };
@@ -175,3 +175,12 @@ test("folder views reject malformed children, cursor anchors and chart data", ()
assert.throws(() => parse(invalid), /unexpected view/);
}
});
+
+test("progress accepts only bounded payloads for the channel's scan", () => {
+ const progress = { scan_id: "current", files: 1, directories: 0, total_size: 0, recent_names: [" file "] };
+ assert.deepEqual(parseScanProgress(progress, "current"), progress);
+ for (const value of [null, { ...progress, scan_id: "old" }, { ...progress, files: -1 },
+ { ...progress, recent_names: [null] }, { ...progress, recent_names: Array(6).fill("file") }]) {
+ assert.equal(parseScanProgress(value, "current"), null);
+ }
+});