Skip to content

fix(api): attribute v2 workspace keys to billing actor - #6376

Open
TheodoreSpeaks wants to merge 2 commits into
improvement/v2-platformsfrom
improvement/api-key-attribution
Open

fix(api): attribute v2 workspace keys to billing actor#6376
TheodoreSpeaks wants to merge 2 commits into
improvement/v2-platformsfrom
improvement/api-key-attribution

Conversation

@TheodoreSpeaks

Copy link
Copy Markdown
Collaborator

Summary

  • attribute v2 workspace-key requests, billing, quotas, and audits to the workspace billing actor
  • keep key creators as continuation owners while blocking personal-secret access from workspace keys
  • freeze billing attribution across multipart uploads, imports, and workflow execution

Type of Change

  • Bug fix

Testing

  • 122 focused tests passed
  • whole-workspace type-check passed
  • lint, API validation, OpenAPI, boundaries, and all CI audits passed

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Aug 7, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Aug 7, 2026 5:32pm

Request Review

@cursor

cursor Bot commented Aug 7, 2026

Copy link
Copy Markdown

PR Summary

High Risk
Changes authentication, authorization, billing attribution, and quota accounting across the entire v2 API surface; mis-attribution or permission bugs could affect billing, access control, or audit trails.

Overview
v2 workspace API keys are now split into a key creator (principal) and a workspace billing actor. Rate limits, usage gates, rollout (v2-api gate with org id), and billing snapshots use the frozen workspace payer; authorization and visibility use the creator’s workspace permission (invalidated if the creator leaves the workspace).

Across v2 routes, handlers distinguish who may act (principalUserId for table access, credentials, upload ownership, workflow auth) from who is attributed (userId / billing actor for writes, audits, execution, and file registration). Multipart uploads and table/knowledge imports store attribution in session metadata so completion does not re-resolve billing. Workspace keys cannot access personal secrets (PERSONAL_KEY_REQUIRED).

Infrastructure: authenticateV2ApiKey / checkV2RateLimit, keyId on auth, request context enriched with apiKeyId/apiKeyType for logs and audit metadata (overriding untrusted caller metadata).

Reviewed by Cursor Bugbot for commit 622ccb8. Bugbot is set up for automated code reviews on this repo. Configure here.

@greptile-apps

greptile-apps Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR separates workspace-key authorization from billing attribution, preserving the key creator as the authorization principal while using the workspace billing actor for quotas, audits, and persisted operations.

  • Freezes creator permission and billing attribution during v2 workspace-key admission.
  • Carries attribution snapshots through uploads, imports, and workflow execution.
  • Prevents workspace keys from accessing personal secrets.
  • Updates table, workflow, credential, and secret routes to authorize against the creator principal.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
apps/sim/app/api/v1/middleware.ts Introduces the workspace-key principal/actor split and enforces the creator's effective workspace permission during authorization.
apps/sim/app/api/public-api-route-handler.ts Applies organization-aware v2 gating and propagates API-key identity through request context.
apps/sim/app/api/v2/workflows/[id]/execute/route.ts Authorizes workflow execution as the key creator while preserving the billing actor and attribution snapshot through execution.
apps/sim/app/api/v2/secrets/[name]/route.ts Uses the creator for secret authorization, attributes workspace writes to the billing actor, and rejects personal-secret access from workspace keys.
apps/sim/app/api/v2/knowledge/[id]/documents/uploads/utils.ts Restores multipart billing attribution from session metadata so completion retries retain the admission-time payer.
apps/sim/lib/table/orchestration/import-resource.ts Carries frozen billing attribution through table-import orchestration.

Sequence Diagram

sequenceDiagram
  participant Client
  participant Auth as V2 API Authentication
  participant Authz as Workspace Authorization
  participant Billing as Billing and Quotas
  participant Operation as Route or Background Operation
  Client->>Auth: Request with workspace API key
  Auth->>Auth: Resolve key creator and current permission
  Auth->>Billing: Resolve workspace billing actor and payer snapshot
  Auth-->>Authz: Creator principal and frozen permission
  Auth-->>Operation: Billing actor and attribution snapshot
  Authz->>Authz: Enforce creator permission
  Billing->>Billing: Apply payer quota and billing status
  Operation->>Operation: Persist or execute with frozen attribution
Loading

Reviews (2): Last reviewed commit: "fix(api): preserve workspace key permiss..." | Re-trigger Greptile

Comment thread apps/sim/app/api/v1/middleware.ts
Comment thread apps/sim/app/api/v2/files/uploads/[uploadId]/complete/route.ts
Comment thread apps/sim/app/api/public-api-route-handler.ts
Comment thread apps/sim/lib/table/orchestration/import-resource.ts
@TheodoreSpeaks

Copy link
Copy Markdown
Collaborator Author

@greptile

@TheodoreSpeaks

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 622ccb8. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant