Repository navigation
Encode chargeable social scrape states - #1039
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configuration
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
1 issue found across 13 files
Confidence score: 3/5
- The batch response can expose raw provider or backend exception details to API clients, and this test locks that behavior in (
lib/apify/__tests__/socialScrapeBillingRoutes.test.ts). Return a generic failure message and update the production error path.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="lib/apify/__tests__/socialScrapeBillingRoutes.test.ts">
<violation number="1" location="lib/apify/__tests__/socialScrapeBillingRoutes.test.ts:127">
P2: This assertion locks raw provider exception text into the public batch response. Require a generic failure message and update the production error path so provider or backend details are never returned to API clients.
(Based on your team's feedback about redacting internal error details.)</violation>
</file>
Architecture diagram
sequenceDiagram
participant HTTP as HTTP Client
participant Artist as Artist Scrape Route
participant Single as Single Social Route
participant Auth as Auth Validation
participant Batch as scrapeProfileUrlBatch
participant SingleScrape as scrapeProfileUrl
participant Providers as Platform Providers
participant Deduct as deductSocialScrapeCredits
participant Credits as ensureSocialScrapeCredits
participant Runs as upsertApifyScraperRuns
participant DB as Supabase
Note over HTTP,DB: Social Scrape Billing Flow - Discriminated Attempt States
alt POST /api/artist/socials/scrape (batch)
HTTP->>Artist: POST with artist_account_id + optional posts
Artist->>Auth: validateAuthContext()
Auth-->>Artist: accountId + authToken
Artist->>Artist: checkAccountArtistAccess()
Artist->>Artist: selectAccountSocials() for artist
Artist->>Batch: scrapeProfileUrlBatch(profiles, posts)
Batch->>SingleScrape: scrapeProfileUrl() per profile
SingleScrape->>Providers: startXProfileScraping(username, posts)
alt Provider returns valid runInfo
Providers-->>SingleScrape: { runId, datasetId }
SingleScrape-->>Batch: { state: "started", runId, datasetId }
else Provider returns null or missing runId
Providers-->>SingleScrape: null/{ runId: "" }
SingleScrape-->>Batch: { state: "failed", error: "Failed to start scrape" }
else Provider throws
Providers--xSingleScrape: Error
SingleScrape-->>Batch: { state: "failed", error: message }
end
alt Platform unsupported
SingleScrape-->>Batch: null (filtered out)
end
Batch-->>Artist: started/failed results with profileUrl
Artist->>Artist: Filter state === "started"
alt At least one started run
Artist->>Credits: ensureSocialScrapeCredits(accountId, totalCost)
Credits-->>Artist: null (sufficient)
Artist->>Deduct: deductSocialScrapeCredits(accountId, startedCount * cost)
Deduct-->>Artist: success
Artist->>Runs: upsertApifyScraperRuns(started runs only)
Runs->>DB: INSERT run records
Artist-->>HTTP: 200 [{ runId, datasetId, error }]
else All runs failed
Artist-->>HTTP: 200 [{ runId: null, datasetId: null, error }]
end
else POST /api/socials/{id}/scrape (single)
HTTP->>Single: POST with posts query param
Single->>Auth: validateAuthContext()
Auth-->>Single: accountId + authToken
Single->>Single: checkAccountArtistAccess()
Single->>Single: selectSocials() for social id
Single->>SingleScrape: scrapeProfileUrl(profileUrl, username, posts)
SingleScrape->>Providers: startXProfileScraping(username, posts)
alt Provider returns valid runInfo
Providers-->>SingleScrape: { runId, datasetId }
SingleScrape-->>Single: { state: "started", runId, datasetId }
Single->>Deduct: deductSocialScrapeCredits(accountId, cost)
Single->>Runs: registerRootApifyRun({ runId })
Runs->>DB: INSERT run record
Single-->>HTTP: 200 { runId, datasetId }
else Provider returns null or throws
Providers-->>SingleScrape: null/Error
SingleScrape-->>Single: { state: "failed", error }
Single-->>HTTP: 500 { status: "error", error }
end
end
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
| expect(response.status).toBe(200); | ||
| expect(await response.json()).toEqual([ | ||
| { runId: null, datasetId: null, error: "Failed to start scrape" }, | ||
| { runId: null, datasetId: null, error: "Provider unavailable" }, |
There was a problem hiding this comment.
P2: This assertion locks raw provider exception text into the public batch response. Require a generic failure message and update the production error path so provider or backend details are never returned to API clients.
(Based on your team's feedback about redacting internal error details.)
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At lib/apify/__tests__/socialScrapeBillingRoutes.test.ts, line 127:
<comment>This assertion locks raw provider exception text into the public batch response. Require a generic failure message and update the production error path so provider or backend details are never returned to API clients.
(Based on your team's feedback about redacting internal error details.) </comment>
<file context>
@@ -0,0 +1,177 @@
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual([
+ { runId: null, datasetId: null, error: "Failed to start scrape" },
+ { runId: null, datasetId: null, error: "Provider unavailable" },
+ { runId: null, datasetId: null, error: "Failed to start scrape" },
+ ]);
</file context>
Social scrape results currently allow a run ID and failure to coexist, so callers infer chargeability from nullable fields and persistence needs a cast. This change makes attempts explicitly
startedorfailed, bills and registers only started runs, and keeps the existing batch and single-social HTTP response shapes, pricing, and debit ordering. A malformed provider result without a run ID now becomes a failed attempt; unsupported platforms still returnnullinternally.The regression gate checks both contradictory type examples and the actual constructors/consumers under strict null checks, scoped to the five changed modules. Route fixtures exercise the real
POSTexports through dispatch and the deduction wrapper with synthetic accounts, providers, and persistence. The full-report parse-failure check also identifies the failed section's runtime and verifies it is never charged.lib/apify/README.mdrecords entry routes, expectations, and fixture limits.Known pre-existing redaction follow-up: Cubic's P2 finding identifies raw provider exception text in the public response. In both baseline
067238daand this PR,scrapeProfileUrlcopieserror.message, the artist batch serializes that error, and the single-social handler returns it. These unchanged paths may expose provider or backend details. This structural refactor deliberately preserves existing response behavior; the fixture assertion documents that behavior and does not endorse it as safe. Replacing public exception text with a generic message, retaining diagnostic detail server-side, and updating both endpoint contracts and tests remains a separate, unresolved redaction follow-up.Validation:
git diff --checkpassed.startedconstructor withrunId: nulleach failed the relevant regression gate; mutations restored.067238dawith matching locked dependencies; no new diagnostics. The focused strict-null gate passes.Local commands ran on Node 23.6.1; CI uses its configured runtime. Fixture checks make no provider requests or real charges and do not verify deployed HTTP authentication or account balances.