Repository navigation
fix: harden JSON import/export boundaries and unify HTTP Master PIN rate limit - #1509
Conversation
…ster PIN rate limit
|
ⓘ Your Qodo trial ends soon. Ask your workspace admin to set up billing to keep reviews running after the trial. Manage billing |
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
PR Summary by QodoHarden JSON import/export and share HTTP Master PIN limits
AI Description
Diagram
High-Level Assessment
Files changed (9)
|
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewTip of the day💡 Did you know, you can pick a display mode instead of tweaking every Display option by hand |
Intent
Proceed with sequential local implementation Batch 1 (Items 2, 3, and 5 of the revised security plan), subject to these decisions and acceptance criteria:
database.manageuser cannot grant permissions or strand administration through import.x-master-pinfor the download route; never accept URL query credentials.revoked_tokenstoEXPORT_EXCLUDE_TABLES.main/services/posthog.ts:368). Do not introduce blanketposthog_*redaction.npm run lint,npm run build, andnpm test; run browser/Electron checks for affected flows and documentation checks for changed contracts. Report fresh results, limitations, final diff, and git status.What Changed
/api/db-tools/importso importedusersrows never overwrite existing accounts or import credentials, activation state, or privileged roles (owner/manager), instead creating inactive non-privileged placeholders (cashier/server/chef) with unusable password hashes, and blocked JSON import mutations torole_permission_overrides,user_permission_overrides,authorization_audit_log, andauth_audit_log.requireMasterPinandPOST /api/auth/recover-passwordunder a shared per-IP key (master-pin:${normalizeIpForRateLimit(ip)}) with IPv6 loopback normalization (::1and0:0:0:0:0:0:0:1to127.0.0.1), acceptedx-master-pinheader credentials on Master PIN routes, and enabled dotfile streaming on/api/db-tools/downloadtemporary snapshots.revoked_tokensand redactedGOOGLE_DRIVE_PRIVATE_SETTING_KEYSin/api/db-tools/export, and updated the authentication, frontend, and API documentation and regression test suites to match.Risk Assessment
✅ Low: The changes cleanly enforce the JSON import/export security boundaries, x-master-pin header support, and shared normalized-IP Master PIN rate limiting with complete regression test coverage and updated documentation.
Testing
Ran
npm run build,npm run test:master-pin,npm run test:database-tools-api,npm run docs:check, andgit diff --check, then stood up the real Express API server (startServer()onhttp://127.0.0.1:3001) against an isolated SQLite store directory and drove all 6 end-to-end HTTP scenarios live, confirming thatGET /api/db/downloadwithx-master-pinstreams a valid SQLite snapshot (SQLite format 3\0, 1,011,712 bytes) while rejecting query-string PINs with 403, JSON import neutralizes credentialed/privileged user rows and blocks permission/audit table tampering,GET /api/db/exportomitsrevoked_tokensand redacts all 27 Google Drive private setting keys while preserving PostHog settings and audit logs, and HTTP Master PIN failures share a single 5-attempt budget per normalized client IP across routes and password recovery. Every check and live scenario passed; this is a backend/API and documentation change with no UI/visual surface change.Evidence: Live HTTP validation evidence across all 6 Batch 1 security scenarios
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 2 issues found → auto-fixed ✅
main/routes/database.ts:450- InPOST /api/db/import,if (tableName === 'users') continue;is placed atmain/routes/database.ts:479, after theif (rows.length === 0)overwrite-delete block (main/routes/database.ts:448-465), whilemain/routes/database.ts:450repeats theIMPORT_BLOCKED_SECURITY_TABLES.has(tableName)check already executed atmain/routes/database.ts:439. WhenimportData.usersis an empty array (users: []) andoverwriteorhasVersionMismatchis true,main/routes/database.ts:458runsDELETE FROM users, forcingmergeUserSecurityState(main/db.ts:6188-6216) to reconstruct deleted accounts viainsertPreservedUser, which dropsterms_accepted_at(NULL) and overwritescreated_atandupdated_atwithnow(). SkiptableName === 'users'at the top of the table loop (main/routes/database.ts:439) and remove the duplicateIMPORT_BLOCKED_SECURITY_TABLEScheck atmain/routes/database.ts:450.docs/architecture/authentication-and-authorization.md:297-docs/architecture/authentication-and-authorization.md:297states thatnormalizeIpForRateLimit(ip)causes equivalent loopback representations::1,127.0.0.1, and::ffff:127.0.0.1to share one 5-attempt Master PIN budget acrossrequireMasterPin(main/middleware/master-pin.ts:7) andPOST /api/auth/recover-password(main/routes/auth.ts:876, which explicitly accepts::1,127.0.0.1, and::ffff:127.0.0.1inLOCAL_SETUP_HOSTSatmain/routes/auth.ts:77). However,normalizeIpForRateLimit(main/middleware/security.ts:24-27) only strips::ffff:, returning'::1'for IPv6 loopback instead of'127.0.0.1'and leavingmaster-pin:::1on a separate failure budget. Normalize IPv6 loopback (::1/0:0:0:0:0:0:0:1) to'127.0.0.1'innormalizeIpForRateLimit(and cover::1intests/master-pin.test.ts:187).🔧 Fix applied.
✅ Re-checked - no issues remain.
🔧 **Test** - 2 issues found → auto-fixed ✅
main/routes/database.ts:968- LiveGET /api/db/downloadwith a validx-master-pinheader passesrequireMasterPinand creates the checkpointed backup in.flo-download-*, butres.download(snapshotPath, filename, (error) => settle(error))inmain/routes/database.ts:934-981immediately fails with ExpresssendNotFoundError: Not Found(404) becausetempDirstarts with a leading dot (.flo-download-) andres.downloaddefaults todotfiles: 'ignore'. Because a callback is supplied tores.download, Express does not forward the error tonext(err), andmain/routes/database.tslogs[DB Download] Stream error: Not Foundwithout endingres, causing every realGET /api/db/downloadrequest to hang until timeout.tests/database-tools-api.test.ts:181-186hid this bug by monkey-patchingres.downloadfor/api/db/download. Fixmain/routes/database.ts(e.g. pass{ dotfiles: 'allow' }or use a non-dot temp directory prefix, and end the response if an error occurs before headers are sent) and remove the test stub intests/database-tools-api.test.ts:181-186so the test exercises realres.download.npm run test:master-pinnpm run test:database-tools-apinpm run test:issue-127-password-recoverynpm run test:securitynpm run test:authorization-permissionsnpm run buildnpm run docs:checkLive HTTP scenario run against compileddist/main/serverwith isolated SQLite store (live-batch1-validation.json)🔧 Fix applied.
✅ Re-checked - no issues remain.
npm run buildnpm run test:master-pinnpm run test:database-tools-apinpm run docs:checkgit diff --checkLive HTTP scenario validation againststartServer()onhttp://127.0.0.1:3001writing~/.no-mistakes/evidence/01M6WE39BZ0B9FAKWAG7GDX8Y9/live-batch1-validation.json✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.