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 folder views before rendering (#133)
Vishnu Srivatsava6b72ee025782
diff --git a/src/scanView.ts b/src/scanView.tsindex 25b8bc2..0ee2caa 100644--- a/src/scanView.ts+++ b/src/scanView.ts@@ -69,16 +69,41 @@ const entryName = (value: unknown): value is string => typeof value === "string"type NodeContext = Pick<ScanResponse, "root_path" | "read_only" | "total_size">;const invalidNode = () => new Error("Scan node response does not match this scan");+function isScanNode(node: unknown): node is ScanNode {+ return isRecord(node) && isU32(node.id) && typeof node.name === "string"+ && (node.parent_id === null || isU32(node.parent_id)) && isCount(node.size)+ && isCount(node.child_count) && typeof node.counted === "boolean" && typeof node.can_delete === "boolean"+ && (node.type === "directory" || (node.type === "file" && node.child_count === 0))+ && (node.counted || (node.size === 0 && node.child_count === 0));+}++export function parseScanView(raw: string, scope: ScanScope, nodeId: number): ScanView {+ const data: unknown = JSON.parse(raw);+ const validCursor = (cursor: unknown, direction: PageCursor["direction"], anchor: number | undefined) =>+ cursor === null || (anchor !== undefined && isRecord(cursor)+ && cursor.direction === direction && cursor.node_id === anchor);+ if (!isRecord(data) || data.generation !== scope.generation || data.revision !== scope.revision+ || !isScanNode(data.node) || data.node.id !== nodeId || typeof data.path !== "string"+ || !Array.isArray(data.children) || data.children.length > 128+ || !data.children.every(child => isScanNode(child) && child.parent_id === nodeId)+ || new Set(data.children.map(child => child.id)).size !== data.children.length+ || !validCursor(data.previous_cursor, "previous", data.children[0]?.id)+ || !validCursor(data.next_cursor, "next", data.children[data.children.length - 1]?.id)+ || !Array.isArray(data.breadcrumbs)+ || !data.breadcrumbs.every(crumb => isRecord(crumb) && isU32(crumb.id) && typeof crumb.name === "string")+ || !Array.isArray(data.chart) || data.chart.length > 65+ || !data.chart.every(tile => isRecord(tile) && (tile.node_id === null || isU32(tile.node_id))+ && typeof tile.name === "string" && isCount(tile.size))) {+ throw new Error("Scan query returned an unexpected view");+ }+ return data as unknown as ScanView;+}+/** Shared node/path contract; contextual actions still require their own scope. */export function parseNodeReference(value: unknown, expected: NodeContext): Pick<NodeDetail, "node" | "path"> {if (!isRecord(value) || !isRecord(value.node) || typeof value.path !== "string") throw invalidNode();const node = value.node;- if (!isU32(node.id) || typeof node.name !== "string"- || (node.parent_id !== null && !isU32(node.parent_id)) || !isCount(node.size) || node.size > expected.total_size- || !isCount(node.child_count) || typeof node.counted !== "boolean" || typeof node.can_delete !== "boolean"- || (node.type !== "file" && node.type !== "directory")- || (node.type === "file" && node.child_count !== 0)- || (!node.counted && (node.size !== 0 || node.child_count !== 0))) throw invalidNode();+ if (!isScanNode(node) || node.size > expected.total_size) throw invalidNode();const pathBytes = textBytes(value.path);if (pathBytes > 16 * 1024 || (node.can_delete && (expected.read_only || node.id === 0|| /[\r\n\0]/.test(value.path) || pathBytes + "DELETE ".length > 16 * 1024))) throw invalidNode();diff --git a/src/useScanView.ts b/src/useScanView.tsindex 0dac885..5663c44 100644--- a/src/useScanView.ts+++ b/src/useScanView.ts@@ -1,7 +1,7 @@import { useEffect, useReducer, useRef, useState } from "react";import { invoke } from "@tauri-apps/api/core";-import { sameScan, viewKey, viewReducer } from "./scanView";-import type { PageCursor, ScanScope, ScanView } from "./scanView";+import { parseScanView, viewKey, viewReducer } from "./scanView";+import type { PageCursor, ScanScope } from "./scanView";export function useScanView(scope: ScanScope, nodeId: number, enabled: boolean) {const { generation, revision } = scope;@@ -24,8 +24,7 @@ export function useScanView(scope: ScanScope, nodeId: number, enabled: boolean)void invoke<string>("get_scan_view", {generation, revision, nodeId, cursor: queryCursor,}).then(raw => {- const page: ScanView = JSON.parse(raw);- if (!sameScan(page, { generation, revision }) || page.node.id !== nodeId) throw new Error("Scan query returned an unexpected view");+ const page = parseScanView(raw, { generation, revision }, nodeId);if (current) dispatch({ type: "success", request: id, page });}).catch(error => {if (current) dispatch({ type: "error", request: id, error: String(error) });diff --git a/tests/folder_expansion_fixture.jsx b/tests/folder_expansion_fixture.jsxindex b036e46..6344cfb 100644--- a/tests/folder_expansion_fixture.jsx+++ b/tests/folder_expansion_fixture.jsx@@ -29,7 +29,7 @@ const waitFor = async (predicate, message) => {const end = Date.now() + 3000;while (!predicate()) { if (Date.now() > end) throw Error(message); await pause(); }};-const directory = (id, name, child_count) => ({ id, name, child_count, type: "directory", size: 3, counted: true, can_delete: false });+const directory = (id, name, child_count) => ({ id, parent_id: id - 1, name, child_count, type: "directory", size: 3, counted: true, can_delete: false });const parent = directory(1, "parent", 2);const nested = directory(2, "nested", 1);const file = name => ({ id: 3, parent_id: 1, name, child_count: 0, type: "file", size: 1, counted: true, can_delete: false });diff --git a/tests/scan_view.test.mjs b/tests/scan_view.test.mjsindex 3a8a39c..de435e9 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), "uconst { outputText } = ts.transpileModule(source, {compilerOptions: { target: ts.ScriptTarget.ES2020, module: ts.ModuleKind.ESNext },});-const { sameScan, viewKey, viewReducer, parseNodeDetail, parseScanSummary, parseDeleteResult } = await import(+const { sameScan, viewKey, viewReducer, parseNodeDetail, parseScanSummary, parseDeleteResult, parseScanView } = await import(`data:text/javascript;base64,${Buffer.from(outputText).toString("base64")}`);const scope = { generation: "scan-a", revision: 0 };@@ -157,3 +157,21 @@ test("scan and deletion summaries validate before becoming the active snapshot",assert.throws(() => parseDeleteResult({ ...deletion, removed: { ...deletion.removed, files: -1 } }));assert.throws(() => parseDeleteResult({ ...deletion, summary: {} }));});++test("folder views reject malformed children, cursor anchors and chart data", () => {+ const node = { id: 0, parent_id: null, name: "/", type: "directory", size: 1,+ counted: true, child_count: 1, can_delete: false };+ const child = { ...node, id: 1, parent_id: 0, name: "file", type: "file", child_count: 0 };+ const view = { ...scope, node, path: "/", children: [child], breadcrumbs: [{ id: 0, name: "/" }],+ chart: [{ node_id: 1, name: "file", size: 1 }], previous_cursor: null, next_cursor: null };+ const parse = data => parseScanView(JSON.stringify(data), scope, 0);+ assert.deepEqual(parse(view), view);+ for (const invalid of [+ { ...view, revision: 1 }, { ...view, children: null }, { ...view, children: [child, child] },+ { ...view, children: [{ ...child, parent_id: 2 }] },+ { ...view, next_cursor: { direction: "next", node_id: 2 } },+ { ...view, chart: [{ ...view.chart[0], size: -1 }] },+ ]) {+ assert.throws(() => parse(invalid), /unexpected view/);+ }+});