Skip to content

improvement(utils): add shared utility functions and replace inline patterns#4214

Merged
waleedlatif1 merged 8 commits intostagingfrom
waleedlatif1/vienna-v3
Apr 17, 2026
Merged

improvement(utils): add shared utility functions and replace inline patterns#4214
waleedlatif1 merged 8 commits intostagingfrom
waleedlatif1/vienna-v3

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

Summary

  • Add sleep, toError, safeJsonParse, isNonNull to @/lib/core/utils/helpers
  • Add invariant, assertNever to @/lib/core/utils/asserts
  • Replace all inline new Promise(resolve => setTimeout(resolve, ms)) with sleep(ms) across 29 files
  • Replace all inline e instanceof Error ? e.message : String(e) with toError(e).message across ~290 files
  • Document utility patterns in CLAUDE.md, .claude/rules/global.md, .cursor/rules/global.mdc
  • Zero behavioral changes — all replacements are 1:1 equivalent

Type of Change

  • Improvement (code cleanup / consolidation)

Testing

  • bun run lint passes
  • bun run type-check passes (sim package)
  • Verified toError fallback uses String(value) to match original inline behavior exactly

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
Copy link
Copy Markdown

vercel bot commented Apr 17, 2026

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

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview, Comment Apr 17, 2026 8:32pm

Request Review

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 17, 2026

PR Summary

Medium Risk
Broad refactor touches many request handlers and background jobs, so regressions would be widespread if toError/sleep behavior differs in edge cases; additional redirect/URL validation and the new Agiloft fetch endpoint also affect security-sensitive request flows.

Overview
Standardizes common patterns by adding @/lib/core/utils/helpers (e.g. sleep, toError, safeJsonParse, isNonNull) and @/lib/core/utils/asserts (e.g. invariant, assertNever), then updates many API routes, background tasks, and UI hooks to use these helpers instead of inline setTimeout promises and instanceof Error ? ... message normalization.

Adds a few security/validation hardenings: validates Shopify returnUrl with isSameOrigin, validates user-supplied OIDC endpoints in SSO registration via validateUrlWithDNS before discovery, and sanitizes/validates Supabase projectId when constructing Storage URLs.

Adds a new internal POST /api/tools/agiloft/retrieve endpoint to authenticate to Agiloft, download an attachment, and return it as base64 (with SSRF validation and logout cleanup).

Reviewed by Cursor Bugbot for commit 4b89bd6. Configure here.

Comment thread apps/sim/app/api/files/presigned/route.ts Outdated
Comment thread apps/sim/lib/core/utils/helpers.ts Outdated
@waleedlatif1 waleedlatif1 force-pushed the waleedlatif1/vienna-v3 branch from bd8b16d to db54596 Compare April 17, 2026 19:34
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Copy link
Copy Markdown

@cursor cursor bot left a comment

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 db54596. Configure here.

@waleedlatif1 waleedlatif1 force-pushed the waleedlatif1/vienna-v3 branch from db54596 to 6925e53 Compare April 17, 2026 19:41
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 17, 2026

Greptile Summary

This PR extracts five common inline patterns — sleep, toError, safeJsonParse, isNonNull, invariant/assertNever — into shared utility modules and applies them across the codebase (~320 files). It also introduces supabase/utils.ts with supabaseBaseUrl(), which replaces bare template-literal URL construction with validated project ID input, preventing SSRF via fragment injection.

The toError/sleep replacements are verified 1:1 equivalent. The Supabase URL changes are a security improvement (not truly zero behavioral change), but are well-tested and intentional.

Confidence Score: 5/5

Safe to merge — all replacements are verified equivalent, and the Supabase validation is a net security improvement.

No P0 or P1 issues found. The toError/sleep replacements are 1:1 equivalent to the original inline patterns. The supabaseBaseUrl change adds beneficial input validation with comprehensive tests. Documentation updates are accurate. Prior review concerns have been addressed.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/lib/core/utils/helpers.ts New utility module with sleep, safeJsonParse, isNonNull, and toError — all correctly implemented and 1:1 equivalent to the replaced inline patterns.
apps/sim/lib/core/utils/asserts.ts New assertion utilities: invariant (throws on falsy condition) and assertNever (exhaustive switch guard). Both are correctly typed and implemented.
apps/sim/tools/supabase/utils.ts New supabaseBaseUrl() wraps validateSupabaseProjectId() to enforce lowercase-alphanumeric project IDs before URL construction, preventing SSRF via fragment injection. Correctly implemented and well-guarded.
apps/sim/tools/supabase/utils.test.ts Comprehensive test suite covering valid IDs, empty strings, fragment injection, path traversal, authority injection, uppercase, and too-short IDs. Uses correct vi.mock/static-import pattern.
apps/sim/lib/execution/isolated-vm.ts Replaces inline error-message ternary with toError(error).message in the logger call; the intentional user-facing fallback 'Unknown fetch error' in the return value is preserved unchanged.
apps/sim/executor/execution/engine.ts Two replacements: toError(error) for executionError assignment and toError(error).message for logger — both 1:1 equivalent to the original inline patterns.
apps/sim/app/workspace/[workspaceId]/knowledge/hooks/use-knowledge-upload.ts Local sleep helper removed and replaced with shared sleep import — identical implementation, clean extraction.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Catch block] --> B[toError value]
    B -->|instanceof Error| C[Return error as-is]
    B -->|typeof string| D[new Error with value]
    B -->|other| E[new Error with String value]
    C --> F[.message]
    D --> F
    E --> F

    G[Supabase tool url fn] --> H[supabaseBaseUrl projectId]
    H --> I[validateSupabaseProjectId]
    I -->|invalid| J[throw Error]
    I -->|valid| K[https sanitized .supabase.co]

    L[sleep ms] --> M[setTimeout resolve ms]
Loading

Reviews (4): Last reviewed commit: "chore(utils): remove unused utilities (a..." | Re-trigger Greptile

Comment thread apps/sim/tools/supabase/utils.test.ts Outdated
Comment thread apps/sim/lib/execution/isolated-vm.ts
…atterns

Add sleep, toError, safeJsonParse, isNonNull helpers and invariant/assertNever
assertions. Replace all inline implementations across the codebase with these
shared utilities for consistency. Zero behavioral changes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@waleedlatif1 waleedlatif1 force-pushed the waleedlatif1/vienna-v3 branch from 6925e53 to a05b442 Compare April 17, 2026 19:44
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Copy link
Copy Markdown

@cursor cursor bot left a comment

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 a05b442. Configure here.

…ndle build

Turbopack resolves .server.ts modules even for type-only imports,
pulling dns/promises into client bundles. Define SecureFetchResponse
locally instead.
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

Comment thread apps/sim/app/api/mcp/servers/[id]/refresh/route.ts
The SSRF upgrade to input-validation.server introduced dns/promises
into client bundles via tools/registry.ts. Revert to the original
client-safe validateExternalUrl + fetch. The SSRF DNS-pinning upgrade
for agiloft directExecution should be done via API routes in a
separate PR.
…ished file patterns

Convert retrieve_attachment from directExecution to standard API route
pattern, consistent with Slack download and Google Drive download tools.

- Create /api/tools/agiloft/retrieve with DNS validation, auth lifecycle,
  and base64 file response matching the { file: { name, mimeType, data,
  size } } convention
- Update retrieve_attachment tool to use request/transformResponse
  instead of directExecution, removing the dependency on
  executeAgiloftRequest from the tool definition
- File output type: 'file' enables FileToolProcessor to store downloaded
  files in execution filesystem automatically
waleedlatif1 and others added 2 commits April 17, 2026 13:12
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…xists on tool outputs

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Copy link
Copy Markdown

@cursor cursor bot left a comment

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 4b89bd6. Configure here.

…ull)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@waleedlatif1 waleedlatif1 merged commit e1018f1 into staging Apr 17, 2026
8 of 9 checks passed
@waleedlatif1 waleedlatif1 deleted the waleedlatif1/vienna-v3 branch April 17, 2026 20:29
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