From 9940a86ac0e18343495df81b41e04429793db97b Mon Sep 17 00:00:00 2001 From: Jannik Date: Sat, 27 Sep 2025 13:42:50 +0200 Subject: [PATCH] feat(stores): Verify store data --- src/stores.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/stores.ts b/src/stores.ts index 16ee67c..358ada2 100644 --- a/src/stores.ts +++ b/src/stores.ts @@ -61,12 +61,28 @@ export const SyncPayload = z.object({ export type SyncPayload = z.infer; -// TODO: verify data +export function verifyStoreData(type: StoreType, data: string) { + const schema = storeTypes[type]; + if(!schema) return false; + try { + const parsedData = JSON.parse(data); + schema.parse(parsedData); + return true; + } catch { + return false; + } +} + export async function sync(payload: SyncPayload, user: User) { const changes: Store[] = []; // Apply changes from client for(const change of payload.changes) { const store = await Store.findOne({ where: { id: change.id }, relations: { user: true } }); + if(!verifyStoreData(change.type, change.data)) { + // Invalid data + if(store) changes.push(store); // Send back the store to the client to overwrite their changes + continue; + } if(!store) { // Store doesn't exist, create it const newStore = new Store();