fix(app): validate persisted client state (#3289)
* fix(app): validate persisted client state Persisted replica data could satisfy TypeScript through an assertion while missing fields required by renderers. Treat every AsyncStorage value as untrusted input, clear values that fail strict schemas, and version the replica DTO so incompatible caches cannot hydrate. * fix(app): preserve validated legacy state * fix(app): preserve validated preferences Preserve supported legacy location fields and every shipped theme while keeping unknown persisted fields rejected. Align browser test seeds with the current preference shape so strict validation exercises the intended provider. * fix(app): migrate legacy agent preferences Released clients stored host/location fields and per-provider thinkingOptionId together. Migrate those exact strict bytes into current preferences without admitting unknown fields. * fix(app): preserve drafts with legacy reviews Accept the exact strict shape released for workspace review attachments so migration can discard that obsolete attachment without clearing every persisted draft.
This commit is contained in:
@@ -18,6 +18,9 @@ function inMemoryStorage(initial: Record<string, string> = {}): InMemoryStorage
|
||||
storage.setCallCount += 1;
|
||||
items.set(key, value);
|
||||
},
|
||||
async removeItem(key) {
|
||||
items.delete(key);
|
||||
},
|
||||
};
|
||||
return storage;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,20 @@
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import { z } from "zod";
|
||||
import { readValidatedString } from "@/storage/validated-storage";
|
||||
|
||||
const CLIENT_ID_STORAGE_KEY = "@paseo:client-id-v1";
|
||||
|
||||
export interface ClientIdStorage {
|
||||
getItem(key: string): Promise<string | null>;
|
||||
setItem(key: string, value: string): Promise<void>;
|
||||
removeItem(key: string): Promise<void>;
|
||||
}
|
||||
|
||||
export interface ClientIdResolver {
|
||||
getOrCreate(): Promise<string>;
|
||||
}
|
||||
|
||||
function normalizeStoredClientId(value: unknown): string | null {
|
||||
if (typeof value !== "string") {
|
||||
return null;
|
||||
}
|
||||
const trimmed = value.trim();
|
||||
return trimmed.length > 0 ? trimmed : null;
|
||||
}
|
||||
const ClientIdSchema = z.string().trim().min(1);
|
||||
|
||||
export function createClientIdResolver(deps: {
|
||||
storage: ClientIdStorage;
|
||||
@@ -38,8 +35,7 @@ export function createClientIdResolver(deps: {
|
||||
}
|
||||
|
||||
inFlight = (async () => {
|
||||
const stored = await deps.storage.getItem(storageKey);
|
||||
const existing = normalizeStoredClientId(stored);
|
||||
const existing = await readValidatedString(deps.storage, storageKey, ClientIdSchema);
|
||||
if (existing) {
|
||||
cached = existing;
|
||||
return existing;
|
||||
@@ -61,7 +57,7 @@ export function createClientIdResolver(deps: {
|
||||
}
|
||||
|
||||
function generateUuidFromGlobalCrypto(): string {
|
||||
const cryptoObj = globalThis.crypto as { randomUUID?: () => string } | undefined;
|
||||
const cryptoObj = globalThis.crypto;
|
||||
if (cryptoObj && typeof cryptoObj.randomUUID === "function") {
|
||||
return cryptoObj.randomUUID().replace(/-/g, "");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user