fix(core): resolve P0/P1 audit findings across security, tenant isolation, and scheduler'
Full Project Audit & Quality Remediation Summary
Overview
This PR implements comprehensive security, architectural, and business logic fixes identified during the autonomous full-project-audit cycle. It addresses all P0 (Critical) and P1 (High) findings with minimal safe changes, strictly maintaining backwards compatibility and adhering to the repository's layered architecture (Route -> Controller -> Service -> Repository -> Prisma).
Key Changes & Fixes
Critical Fixes (P0)
-
SSRF Prevention in Webhook Deliveries (
BUG-P0-01):- Replaced unconstrained
axios.postinWebhookDeliveryServicewithgetSecureAxios()(secureHttpAgent/secureHttpsAgent) to block internal subnet, loopback, and cloud metadata (169.254.169.254) exploitation.
- Replaced unconstrained
-
Cross-Tenant Extraction Isolation (
BUG-P0-02):- Added
findByUserAndDomaintoExtractionTemplateRepositorywith UUID validation. - Scoped
runExtractionIfTemplateandpersistBatchResultsacross all crawl modes (SCRAPE,SITEMAP,URL_LIST,CRAWL) to ensure user templates are never leaked or cross-executed on other tenants' crawl jobs.
- Added
-
Scheduler Multi-Instance Concurrency Protection (
BUG-P0-03):- Implemented atomic conditional update
claimDueSchedule(id, now, nextRunAt)inCrawlScheduleRepositoryusing PostgreSQL row-level conditions (where: { id, isActive: true, nextRunAt: { lte: now } }), preventing duplicate job execution across clustered/replicated worker instances.
- Implemented atomic conditional update
High Priority Fixes (P1)
-
Schedule Quota & Inactive Account Enforcement (
BUG-P1-01):- Added validation checks in
processDueSchedulesto prevent deactivated or soft-deleted users (isActive = falseordeletedAt != null) from continuing background automated crawls.
- Added validation checks in
-
Vietnam Timezone (UTC+7) Daily Quota (
BUG-P1-02):- Fixed
startOfDaycalculation inCrawlJobServiceusingAsia/Ho_Chi_Minhtimezone conversion helpers, ensuring daily quotas reset accurately at00:00:00 UTC+7.
- Fixed
-
Database Index Optimization (
BUG-P1-03):- Added
@@index([crawlJobId])onCrawlAsset. - Added
@@index([userId, createdAt]),@@index([action]),@@index([createdAt])onAuditLoginprisma/schema.prisma.
- Added
-
N+1 DNS Lookup Optimization (
BUG-P1-04):- Parallelized URL verification in
URL_LISTmode using chunkedPromise.all(chunkSize = 10).
- Parallelized URL verification in
-
Layered Architecture Compliance (
BUG-P1-05):- Created
src/modules/webhooks/webhook.repository.ts. - Removed direct
prismaimports fromauth.middleware.ts,api-key.middleware.ts,crawl-job.service.ts,webhook-config.service.ts, andwebhook-delivery.service.ts.
- Created
Medium & Minor Enhancements (P2 / P3)
-
CSV Formula Injection Sanitization (
BUG-P2-01):- Neutralized potential DDE / formula injection triggers (
=, +, -, @, \t, \r) incsv-export.service.ts.
- Neutralized potential DDE / formula injection triggers (
-
OpenAPI / Swagger Synchronization:
- Updated
CreateCrawlJobRequestschema inswagger.tswithurlsarray and regeneratedswagger.json.
- Updated