Skip to content

fix(importer): harden review jobs against NaN spend-cap bypass and prototype keys #1853

Closed

Description

@JOY

Goal

Prevent malformed numeric rows, prototype-property keys, and lone UTF-16 surrogates from bypassing the monthly AI USD spend cap, throwing runtime RangeError/URIError exceptions, or mutating Object.prototype keys in @repo/crawlers review jobs and helpers.

Requirements

  1. monthSpendUsd NaN spend-cap bypass (apps/importer/src/shopee/jobs/review-ai-points.ts, around lines 822 and 1124):
    • Summing Number(b.cost_usd ?? b.reserved_usd ?? 0) without a Number.isFinite check lets a single malformed string (e.g. "NaN") taint monthSpendUsd = NaN, causing result.monthSpendUsd + result.reservedUsd > cap to evaluate false and bypass the monthly spend cap.
    • Coerce non-finite or negative row amounts safely so malformed rows cannot poison monthSpendUsd (or fail closed when a batch row carries a non-finite positive reservation/cost).
  2. Prototype-key guards (apps/importer/src/shopee/jobs/review-ai-points.ts:800 and apps/importer/src/shopee/product-points-ai-prompt.ts:327-329):
    • Replace if (key in total) on total with Object.hasOwn(total, key) in storeAiRows.
    • Use Object.create(null) or Map for byCriterion in product-points-ai-prompt.ts so untrusted keys like __proto__, constructor, or toString never hit Object.prototype.
  3. Finite cost & rating bounds (apps/importer/src/shopee/review-ai-prompt.ts:370 and apps/importer/src/shopee/jobs/review-values.ts:402):
    • Tighten batchCostUsd validation in review-ai-prompt.ts from typeof v === "number" && v > 0 to typeof v === "number" && Number.isFinite(v) && v > 0 (rejecting Infinity).
    • Tighten expectedMasterRatings in review-values.ts from typeof value !== "number" || value < 1 || value > 5 to also reject !Number.isFinite(value) so NaN cannot reach toUnits(NaN, V_UNIT) and throw RangeError: The number NaN cannot be converted to a BigInt.
  4. UTF-16 surrogate safety (apps/importer/src/shopee/jobs/review-ai-points.ts:332,343,353,373 and apps/importer/src/shopee/logger.ts:143,161):
    • Call .toWellFormed() before encodeURIComponent(...) on external batch/file IDs in review-ai-points.ts to prevent URIError: URI malformed.
    • Ensure sanitizeAiGeneratedText truncation at safeMaxLen in logger.ts does not split a surrogate pair and leave a trailing lone high surrogate (e.g. slice cleanly at code-point boundary or normalize with .toWellFormed() without exceeding safeMaxLen).

Approach

Apply minimal, surgical guards in the five importer files above and add targeted unit regression tests covering "NaN" / Infinity batch cost rows, NaN expected master ratings, __proto__ / toString keys, lone-surrogate batch IDs, and surrogate-pair boundary truncation in sanitizeAiGeneratedText.

Acceptance

  • A malformed "NaN" or Infinity cost/reservation value cannot make monthSpendUsd NaN or bypass AI_MONTHLY_CAP_USD.
  • expectedMasterRatings ignores NaN / Infinity ratings instead of throwing RangeError in BigInt.
  • Prototype keys (__proto__, constructor, toString) do not match or mutate Object.prototype in storeAiRows or byCriterion.
  • Lone surrogates in batch/file IDs do not throw URIError, and sanitizeAiGeneratedText never emits a trailing lone surrogate when truncating at maxLen.
  • Unit tests in apps/importer and full monorepo typecheck, lint, test, and build pass.

Files

  • apps/importer/src/shopee/jobs/review-ai-points.ts
  • apps/importer/src/shopee/review-ai-prompt.ts
  • apps/importer/src/shopee/product-points-ai-prompt.ts
  • apps/importer/src/shopee/jobs/review-values.ts
  • apps/importer/src/shopee/logger.ts
  • apps/importer/src/shopee/__tests__/review-ai-points.test.ts
  • apps/importer/src/shopee/__tests__/review-values.test.ts

Activity

  1. added
    bugSomething isn't working
    status:readyTask ready to be picked up by an Agent
    po:antigravityDispatched by the Antigravity Product Owner
    on Nov 22, 2026
  2. added theissue type on Nov 22, 2026
  3. JOY commented on Nov 22, 2026

    @JOY
    ContributorAuthor

    🔒 [Agent: Guard] Claim lock refs/claims/1853 taken by claimant antigravity:93ebb889-4915-4c9f-bb0b-53a96791ec91 (Status: in-progress).

  4. added
    status:in-progressTask currently claimed and being worked on by an Agent
    agent:guardClaimed by Agent: Guard
    and removed
    status:readyTask ready to be picked up by an Agent
    on Nov 22, 2026
  5. self-assigned this
    on Nov 22, 2026
  6. JOY commented on Nov 22, 2026

    @JOY
    ContributorAuthor

    [Agent: Guard] Claimed this issue on branch fix/1853-review-jobs-hardening (Status: Ready -> In Progress).

  7. added
    status:in-reviewTask completed and waiting for review / PR merge
    and removed
    status:in-progressTask currently claimed and being worked on by an Agent
    on Nov 22, 2026
  8. JOY commented on Nov 22, 2026

    @JOY
    ContributorAuthor

    [Agent: Guard] Checkpoint: opened PR #1857 into dev at 9682b308. Local verification passed (pnpm --filter @repo/crawlers test, pnpm --filter @repo/crawlers test:dist, pnpm typecheck, pnpm lint, pnpm build). Next step: complete 2 independent fresh-context subagent reviews and wait for GitHub CI checks on PR #1857 before PO acceptance gate.

  9. JOY commented on Nov 22, 2026

    @JOY
    ContributorAuthor

    🔓 [Agent: Guard] Claim lock refs/claims/1853 of antigravity:93ebb889-4915-4c9f-bb0b-53a96791ec91 (Agent Guard) since 2026-11-22T09:47:39Z released by claimant antigravity:def296a6-5f67-48da-bb74-ee3ede4b7061 because #1853 is closed (Status: done).

  10. added
    status:doneTask completed and verified
    and removed
    status:in-reviewTask completed and waiting for review / PR merge
    on Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

agent:guardClaimed by Agent: Guardarea:crawlersbugSomething isn't workingpo:antigravityDispatched by the Antigravity Product Ownerstatus:doneTask completed and verified

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions