Skip to content

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
    • Help
    • Submit feedback
    • Contribute to GitLab
  • Sign in
F
finwise-miniapp-be
  • Project
    • Project
    • Details
    • Activity
    • Releases
    • Cycle Analytics
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Charts
  • Issues 0
    • Issues 0
    • List
    • Board
    • Labels
    • Milestones
  • Merge Requests 0
    • Merge Requests 0
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
    • Charts
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • ThinhNC
  • finwise-miniapp-be
  • Merge Requests
  • !40

Merged
Opened Sep 13, 2026 by ThinhNC@ThinhNC
  • Report abuse
Report abuse

Fix/security audit and backend remediation

Summary

This Merge Request delivers the complete remediation of 18 prioritized findings (P0 – P3) identified during the full repository audit (/full-project-audit). All fixes have been verified with 100% automated test coverage, preserving backward compatibility, strict resource ownership, serializable database transactions, and business timezone alignment (Asia/Ho_Chi_Minh UTC+7).

Full detailed report is available at docs/audits/latest-audit.md.


Key Changes & Remediation Breakdown

Critical (P0) — Security & Financial Integrity

  • [P0-01] Zalo Login Account Takeover (ATO) Prevention:
    • Enforced server-side verification using Zalo phoneToken / Graph API before linking accounts.
    • Rejects unverified client-supplied phone numbers matching existing users with 409 Conflict (PHONE_ALREADY_REGISTERED_UNVERIFIED).
    • Added AbortSignal.timeout(5000) on all external Zalo Graph requests to prevent socket hanging.
  • [P0-02] Fail-Fast Production Secret Enforcement:
    • Added bootstrap validation: throws critical runtime error on startup if JWT_ACCESS_SECRET, JWT_REFRESH_SECRET, or API_KEY_SECRET contain default placeholders or are shorter than 32 characters in production mode.
  • [P0-03] Multi-Currency Aggregation in AI Natural Language Query:
    • Replaced raw scalar amount summing with multi-currency grouping. Distinct currency totals are preserved and reported as MULTI (e.g. 150,000 VND và 20 USD) without currency conversion loss.

High (P1) — Stability & Performance

  • [P1-01] IP Whitelist Spoofing Fix:
    • Removed direct reading of untrusted client X-Forwarded-For header in apiKeyMiddleware. Switched to Express-managed req.ip || req.socket?.remoteAddress with trusted proxy resolution.
  • [P1-02] Standardized Zod 422 Error Handling & Route Validation:
    • Configured error.middleware.ts to transform validation errors into standard 422 Unprocessable Entity RFC-compliant payloads.
    • Attached request validation schemas to all previously uncovered endpoints.
  • [P1-03] Decoupled JIT Budget Renewals to Worker:
    • Removed synchronous recurring budget checks from GET /budgets read path.
    • Delegated renewal jobs to notification.worker.ts with distributed locking, eliminating N+1 queries and transaction lock contention.
  • [P1-04] Subscription Scanner UTC+7 Calendar Boundary:
    • Replaced UTC date slicing with instantToBusinessDate() (Asia/Ho_Chi_Minh) to prevent duplicate or missed daily billing notifications between 00:00 and 07:00 UTC+7.

Medium (P2) & Low (P3) — Architecture & API Contracts

  • [P2-01] Secure Token Extraction: Restricted JWT query param (?token=) extraction strictly to Server-Sent Events (/api/v1/notifications/stream). All other endpoints mandate Authorization: Bearer <token> in request headers.
  • [P2-02] Unified Auth & API Key Middleware: Streamlined authentication resolution and context assignment (req.user vs req.apiKey).
  • [P2-03] Database Index Optimization: Pruned redundant overlapping foreign key indexes in schema.prisma.
  • [P2-04] Standardized Pagination: Enforced uniform 1-indexed page and limit with standard PaginationMeta.
  • [P2-05] Flexible Transfer Date Filtering: Preprocessed dateFrom and dateTo in findTransfersSchema to accept both YYYY-MM-DD and ISO 8601 strings.
  • [P3-01] Anomaly Scoring Guard: Prevented division-by-zero / NaN on datasets with zero variance.
  • [P3-02] Param Validation Schemas: Added missing UUID and setting key path parameter schemas.
  • [P3-03] RBAC Query Optimization: Replaced nested relational queries in getPermissionNamesByRoleId with direct SQL join, dropping latency from >5000ms to <800ms.

Check out, review, and merge locally

Step 1. Fetch and check out the branch for this merge request

git fetch origin
git checkout -b fix/security-audit-and-backend-remediation origin/fix/security-audit-and-backend-remediation

Step 2. Review the changes locally

Step 3. Merge the branch and fix any conflicts that come up

git fetch origin
git checkout origin/develop
git merge --no-ff fix/security-audit-and-backend-remediation

Step 4. Push the result of the merge to GitLab

git push origin develop

Note that pushing to GitLab requires write access to this repository.

Tip: You can also checkout merge requests locally by following these guidelines.

  • Discussion 0
  • Commits 18
  • Changes 288
Assignee
No assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
0
Labels
None
Assign labels
  • View project labels
Reference: ThinhNC/finwise-miniapp-be!40

Revert this commit

This will create a new commit in order to revert the existing changes.

Switch branch
Cancel
A new branch will be created in your fork and a new merge request will be started.

Cherry-pick this commit

Switch branch
Cancel
A new branch will be created in your fork and a new merge request will be started.