Repository navigation
fix(main): harden IPC, telemetry & restore token - #1316
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@greptileai review this at apex |
[Medium impact] The PR affects several bounded backend workflows in the desktop app. Fix the database startup failure before merging so a missing Findings
|
Fixes #1296 - Enforce isTrustedMainWindowSender(event) in main/ipc.ts handle() so secondary Electron windows cannot invoke privileged main-window IPC handlers. - Guard app.getVersion() / app.isPackaged in main/services/telemetry.ts via electronApp() and cache resolved analytics consent level and identity in main/services/posthog.ts with settings revision invalidation. - Cache the ideal schema inspection snapshot in main/services/schema-health.ts once per process (closing the temporary in-memory DB immediately) and invalidate if MIGRATIONS identity changes. - Peek and validate backup file existence and schema version before consuming the single-use selection_token in POST /api/db/restore. - Index active KDS orders by ID in a Map in buildActiveOrdersSnapshot instead of calling orders.find per item.
… settings revisions
…ry lifecycle callers
…d restore documentation
82237a0 to
b9ac11a
Compare
|
@greptileai review this at apex |
There was a problem hiding this comment.
Greptile has paused reviews on this repository — it used its 750 free open-source review credits for this billing period. Reviews resume automatically on November 2. To continue before then, an organization admin can keep reviews running past the free credits — those bill as normal usage.
|
@greptileai review this at apex |
Fixes #1296
Intent
Fix the double-verified backend security, crash-safety, schema-health caching, and restore-token defects from the architecture audit (
c7755f35, GitHub Issue #1296):main/ipc.ts:208-213:handle()validatesisTrustedSender(event.senderFrame)instead ofisTrustedMainWindowSender(event), allowing secondary Electron windows (customer display, print preview) to invoke privileged main-window IPC handlers. EnforceisTrustedMainWindowSender(event)inhandle().main/services/telemetry.ts:144, 153, 186&main/services/posthog.ts:132-139:telemetry.tscalls rawapp.getVersion()instead ofelectronApp()?.getVersion() ?? '0.0.0'insendEvent,sendCrashReport, andtrackEvent;posthog.tsruns an uncached synchronous SQLite querySELECT value FROM settings WHERE key = 'analytics_consent_level'on everycaptureEventcall when consent is'full'.main/services/schema-health.ts:292:runHealthCheck()rebuildsbuildIdealSchemaDb()(~55ms synchronous in-memory 117-migration build) on every/api/health/detailedcall instead of caching the ideal schema metadata once per process.main/routes/database.ts:794-807&main/services/restore-file-selection.ts:34:POST /api/db/restoreconsumes the single-useselection_tokenbeforegetSchemaVersionFromBackup(resolvedPath)runs, forcing the user to reopen the native file dialog if they first picked an invalid backup file.main/services/kds.ts:682-690:buildActiveOrdersSnapshotcallsorders.find(...)3 times per item insideallItems.map(...)(O(N * M)) instead of indexingordersin aMap(O(N + M)).What Changed
isTrustedMainWindowSenderin themain/ipc.tshandle()wrapper so privileged IPC handlers require the active mainBrowserWindow, top-levelmainFrame, and local server port, and validate the restoreselection_tokenwithpeekRestoreFileSelectionbefore consuming it inPOST /api/db/restoreafter file-existence and backup schema-version checks pass.app.getVersion()andapp.isPackagedaccesses inmain/services/telemetry.tswithelectronApp()guards, and cache PostHog consent, identity, and diagnostics state inmain/services/posthog.tsfor up to 30 seconds with immediate invalidation onsettingsanduserstable mutations viagetSettingsRevision()and SQLite temp triggers inmain/db.ts.DbSchemaSnapshotinmain/services/schema-health.tsacrossrunHealthCheck()calls keyed by migration registry identity, and index orders in aMapinmain/services/kds.ts(buildActiveOrdersSnapshot) to avoid repeated linearorders.findlookups per item.Risk Assessment
✅ Low: The changes cleanly enforce main-window IPC sender validation, guard telemetry Electron app access, cache PostHog consent and ideal schema snapshots with revision-based invalidation, preserve restore selection tokens across pre-validation failures, and index active KDS orders in a Map, with comprehensive behavioral test coverage.
Testing
Built the backend and drove all seven happy-path and adversarial scenarios live against a real Electron 44.9.0 process with main and secondary BrowserWindows, the Express HTTP/KDS server on an isolated port, and a disposable SQLite database, capturing a rendered window screenshot and JSON verification report alongside passing targeted test suites for IPC hardening, telemetry/PostHog caching, schema health, backup/restore tokens, KDS contracts, and script coverage.
Evidence: Live Electron and Express end-to-end verification report JSON
{ "timestamp": "2026-10-22T21:50:01.424Z", "serverPort": 13001, "scenarios": { "ipcSenderEnforcement": { "mainWindow": { "origin": "http://localhost:13001", "appInfo": { "version": "44.9.0", "name": "Electron", "electron": "44.9.0", "node": "24.22.0", "platform": "darwin" }, "healthSummary": { "safeCount": 0, "manualReviewCount": 0 }, "settingsCount": 85, "pinStatus": { "available": true, "isSet": false } }, "secondaryWindow": { "origin": "http://localhost:13001", "appInfo": { "error": "Unauthorized sender" }, "health": { "error": "Unauthorized sender" }, "settings": { "error": "Unauthorized sender" }, "pickRestore": { "error": "Unauthorized sender" }, "backup": { "error": "Unauthorized sender" } } }, "telemetryAndPostHog": { "headlessTelemetry": { "sentPing": true, "sentDiagDisposition": "accepted", "pingAppVersion": "0.0.0", "diagAppVersion": "0.0.0" }, "postHogCache": { "warmPrepareCountAcross20Captures": 0, "revisionBeforeDirectSqlUpdate": 141, "revisionAfterDirectSqlUpdate": 142, "prepareCallsOnNextCaptureAfterTrigger": 8, "collectorRequestsReceived": 1 } }, "schemaHealthCaching": { "totalMemoryDbBuildsAcrossProcess": 1, "memoryDbOpensDuringSubsequentChecks": 0, "detectedFinding": "missing_table:held_orders", "appliedFixes": ["missing_table:held_orders"], "findingsAfterFix": 0 }, "restoreSelectionToken": { "attempt1InvalidBackup": { "status": 400, "error": "Invalid backup file: missing schema version metadata. This backup may have been created with an older version of Flo." }, "attempt2MissingBackup": { "status": 404, "error": "Backup file no longer exists" }, "attempt3RetrySameTokenWithValidBackup": { "status": 200, "body": { "success": true, "mode": "direct", "backupVersion": 116, "currentVersion": 116, "tablesRestored": 66, "cleanupPending": false }, "transientCategoryRemoved": true }, "attempt4ReplayConsumedToken": { "status": 400, "error": "Choose a backup file in the restore dialog before confirming" } }, "kdsSnapshotMapIndexing": { "stationId": "station-bar", "activeOrdersReturned": 5, "httpDisplayOrdersReturned": 5, "orderNumbers": ["KDS-101", "KDS-102", "KDS-103", "KDS-104", "KDS-105"], "orderArrayFindCalls": 0 } } }Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
✅ **Review** - passed
✅ No issues found.
✅ **Test** - passed
✅ No issues found.
npm run buildNODE_PATH="$(pwd)/node_modules" ./node_modules/.bin/electron --no-sandbox /tmp/flo-live-verify-01M5JRP143CCKA3B8FHS8K8KMT.cjsnpm run test:kds-window-hardeningnpm run test:printer-ipcnpm run test:orders-layout-settingsnpm run test:theme-mode-settingsnode tests/run-electron-node-test.cjs tests/restore-managed-path.test.tsnode tests/run-electron-node-test.cjs tests/restore-from-file-api.test.tsnode tests/run-electron-node-test.cjs tests/posthog-client.test.tsnpm run test:telemetry-install-provenancenpm run test:schema-healthnpm run test:kds-contractnpm run test:telemetrynpm run test:backupnpm run test:script-coverage✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.