sim icon indicating copy to clipboard operation
sim copied to clipboard

fix(teams): webhook notifications crash

Open CodeLoopdroid opened this issue 2 months ago • 2 comments

@waleedlatif1

Summary

This PR fixes a crash in the formatTeamsGraphNotification function in apps/sim/lib/webhooks/utils.server.ts. Previously, the function assumed that body.value always contained at least one element. If an empty array was received, the server would throw a TypeError when accessing notification.changeType.

Fixes #2425

Type of Change

  • [x] Bug fix
  • [ ] New feature
  • [ ] Breaking change
  • [ ] Documentation
  • [ ] Other: ___________

Testing

How has this been tested? What should reviewers focus on?

Tested locally using a reproduction script with payloads like: { "value": [] }

Observed that:

  • The function no longer crashes.
  • A warning is logged -> "Received empty Teams notification body".
  • Returns null as expected. No other functionality was affected.

Checklist

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

CodeLoopdroid avatar Dec 17 '25 13:12 CodeLoopdroid

@CodeLoopdroid is attempting to deploy a commit to the Sim Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Dec 17 '25 13:12 vercel[bot]

Greptile Summary

Added defensive null check for empty body.value arrays in Teams webhook notification handler to prevent TypeError crashes when accessing notification.changeType.

Key changes:

  • Added optional chaining (body.value?.[0]) and null check before processing notification
  • Logs warning and returns null when empty notification array received
  • Fixed whitespace formatting in empty catch block (line 366)
  • Adjusted indentation for Telegram webhook object literals (lines 638-659)

Notes:

  • The guard at line 854 already checks body.value.length > 0 before calling this function, making the new null check redundant for current call path but provides defense-in-depth for future callers
  • Line 121 still uses body.value[0] instead of the extracted notification variable (minor inconsistency)
  • Function parameters use any type, which violates the project's TypeScript style guide

Confidence Score: 4/5

  • Safe to merge - fixes a legitimate crash scenario with defensive coding
  • The core fix correctly prevents the TypeError crash by adding proper null checks. However, score is 4 instead of 5 due to: (1) redundant guard that's already present at call site, (2) minor inconsistency with body.value[0] usage on line 121, (3) unrelated whitespace changes bundled in the PR, and (4) any type usage that violates style guide. These are minor style issues that don't affect functionality.
  • No files require special attention - the fix is straightforward and correct

Important Files Changed

Filename Overview
apps/sim/lib/webhooks/utils.server.ts Added defensive null check for empty Teams notification arrays; includes unrelated whitespace formatting changes

Sequence Diagram

sequenceDiagram
    participant Caller as formatWebhookInput
    participant Guard as Guard Check
    participant Fn as formatTeamsGraphNotification
    participant Logger as Logger
    
    Caller->>Guard: Check body.value exists & length > 0
    alt Array is empty or missing
        Guard-->>Caller: Skip function call
    else Array has elements
        Guard->>Fn: Call with body
        Fn->>Fn: Extract notification = body.value?.[0]
        alt notification is null/undefined
            Fn->>Logger: logger.warn('Received empty Teams notification body')
            Fn-->>Caller: return null
        else notification exists
            Fn->>Fn: Extract changeType, resource, subscriptionId
            Fn->>Fn: Parse chatId & messageId from resource
            Fn->>Fn: Fetch message data from Teams API
            Fn-->>Caller: return formatted notification
        end
    end

greptile-apps[bot] avatar Dec 17 '25 13:12 greptile-apps[bot]