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 device discovery replies before rendering (#135)

Vishnu Srivatsavab1ffde52f24d
diff --git a/src/devices.ts b/src/devices.ts
index 1ce8ea2..0fb4b37 100644
--- a/src/devices.ts
+++ b/src/devices.ts
@@ -5,6 +5,20 @@ export interface AndroidDevice {
model: string | null;
}
+export function parseDeviceList(value: unknown): AndroidDevice[] {
+ const invalid = () => new Error("Invalid device discovery response");
+ if (!Array.isArray(value)) throw invalid();
+ const serials = new Set<string>();
+ return value.map((device: unknown) => {
+ if (typeof device !== "object" || device === null || Array.isArray(device)) throw invalid();
+ const { serial, state, model } = device as Record<string, unknown>;
+ if (typeof serial !== "string" || !serial || serials.has(serial)
+ || typeof state !== "string" || !state || (model !== null && typeof model !== "string")) throw invalid();
+ serials.add(serial);
+ return { serial, state, model };
+ });
+}
+
export function preserveDeviceSelection(current: string, devices: AndroidDevice[]): string {
if (current) return current;
const ready = devices.filter(device => device.state === "device");
diff --git a/src/useDevices.ts b/src/useDevices.ts
index 092cc5d..128e07a 100644
--- a/src/useDevices.ts
+++ b/src/useDevices.ts
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useRef, useState } from "react";
import { invoke } from "@tauri-apps/api/core";
-import { deviceDiscoveryMessage, preserveDeviceSelection } from "./devices";
+import { deviceDiscoveryMessage, parseDeviceList, preserveDeviceSelection } from "./devices";
import { boundDiagnostic } from "./logHistory";
import type { AndroidDevice } from "./devices";
@@ -18,7 +18,8 @@ export function useDevices(enabled: boolean) {
try {
// StrictMode replays setup effects while their IPC is still pending.
// Share only that in-flight command; settled lists are never cached.
- const result = await (pending.current ??= invoke<AndroidDevice[]>("list_devices")
+ const result = await (pending.current ??= invoke<unknown>("list_devices")
+ .then(parseDeviceList)
.finally(() => { pending.current = null; }));
if (id !== request.current) return;
setDevices(result);
diff --git a/tests/scan_ui_fixture.js b/tests/scan_ui_fixture.js
index c2ddc1d..e8ed4e7 100644
--- a/tests/scan_ui_fixture.js
+++ b/tests/scan_ui_fixture.js
@@ -309,6 +309,10 @@
diagnostics.querySelector("summary").click();
check(!diagnostics.open && diagnostics.getBoundingClientRect().height <= closedHeight + 1, "Collapsed diagnostics still occupied the screen");
discoveryError = false;
+ listedDevices = [null];
+ await refreshDevices();
+ check(picker().value === "phone-two" && connect.disabled && status.textContent.includes("Couldn't check connected devices"),
+ "Malformed device discovery did not preserve the selection and disable Connect");
listedDevices = [...readyDevices].reverse();
await refreshDevices();
check(picker().value === "phone-two" && !connect.disabled, "Reordered/reconnected devices lost selection");