Skip to content

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
    • Help
    • Submit feedback
    • Contribute to GitLab
  • Sign in
U
upgrade-data-crawler-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
  • upgrade-data-crawler-be
  • Merge Requests
  • !2

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

fix(core): remediate backend audit findings across P0-P3 vulnerabilities and performance issues

Backend Audit Remediation & Quality Enhancements (P0 – P3)

Overview

This PR resolves all confirmed findings from the full-project audit across security, concurrency/memory safety, database performance, and API contract compliance.


Key Changes

1. Security & Authentication (P0 & P1)

  • Fail-Fast Secret Validation: Added boot-time validation requiring JWT_ACCESS_SECRET / JWT_REFRESH_SECRET (>= 32 chars) and WEBHOOK_ENCRYPTION_KEY (64-character hex string) to prevent silent fallback to default keys.
  • Dedicated Email Verification Secret: Isolated email verification signing via jwtConfig.emailVerificationSecret to eliminate token reuse vectors.
  • Cryptographic Reset Password Flow: Isolated structural decode, user resolution, and cryptographic signature validation in verifyResetToken(), ensuring no state mutation or token revocation occurs prior to verification.
  • Real-Time Role Enforcement: Updated authMiddleware to assign req.user.role from the freshly queried database record rather than relying on stale JWT claims.
  • Token Leak Prevention: Removed raw password-reset token printing from stdout/dev console logs.
  • CORS Configuration: Decoupled CORS configuration from email settings, added multi-origin whitelist support via CORS_ALLOWED_ORIGINS, and configured maxAge: 86400 preflight caching.

2. Database & Resource Optimization (P0 & P2)

  • Memory-Safe Diff Projection: Replaced full page loading (include: { pages: true }) with selective field projection (url, normalizedUrl, contentHash, wordCount, status, statusCode, title, crawledAt) during diff generation, preventing worker heap exhaustion on large crawl jobs.
  • Asset Pagination: Added page and limit (max 500) parameters with skip/take pagination to GET /crawl-jobs/:id/assets.
  • Single-Query API Key Auth: Joined the user relation in ApiKeyRepository.findByHash, eliminating a sequential redundant database roundtrip.
  • Database Indexing: Added composite index @@index([jobId, createdAt]) on CrawlJobLog and @@index([ipAddress]) on AuditLog in prisma/schema.prisma.
  • Repository Cleanup: Removed unused duplicate findByUserId methods in favor of standardized findAllByUser.

3. API Contract & Validation (P1 & P3)

  • Standardized Response Envelopes: Wrapped GET /crawl-jobs/:id/diff in { success: true, data: diffReport } to strictly match the API response contract.
  • SSE Connection Guarding: Added a 30-minute maximum stream TTL guard and increased the polling interval to 3 seconds in streamEvents to prevent lingering database connections.
  • Reusable Path Validation: Added validateParams() middleware in validate.middleware.ts returning standard 422 VALIDATION_ERROR envelopes.
  • Password Refinement: Added validation rule requiring newPassword !== currentPassword in changePasswordSchema.
  • OpenAPI Documentation: Updated and regenerated Swagger definitions (src/docs/swagger.json).

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/backend-audit-remediation-p0-p3 origin/fix/backend-audit-remediation-p0-p3

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/backend-audit-remediation-p0-p3

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 1
  • Changes 71
Assignee
No assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
0
Labels
None
Assign labels
  • View project labels
Reference: ThinhNC/upgrade-data-crawler-be!2

Revert this merge request

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 merge request

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