Skip to content

Twilio Event Streams: mask helpseeker data in every event type before sending to Datadog - #4881

Open
janorivera wants to merge 5 commits into
masterfrom
alejandro-c/twilio_event_streams_pii_filter
Open

janorivera wants to merge 5 commits into
masterfrom
alejandro-c/twilio_event_streams_pii_filter

Conversation

@janorivera

@janorivera janorivera commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Bug fix (privacy). The twilioEventStreams Lambda forwards Twilio Event Streams events to Datadog (service:twilio-event-stream). It only masked a fixed list of keys in TaskRouter task_attributes, so helpseeker phone numbers, IPs, emails, names and message text were reaching Datadog through:

  • TaskRouter events: name_old (caller SIP address with carrier IP), outbound_to, conversations.external_contact, which weren't on the list (~21k events/day, 11 helplines)
  • Error-log events (com.twilio.error-logs.error.logged): Functions INFO console output (Task attribute: from +1…, full TaskRouter event dumps), Studio INFO "Send interaction to Flex via TaskRouter" logs, logged webhook requests (message Body, WhatsApp ProfileName/WaId), and error messages such as 81014, 32021, 13227 (~36k/day)
  • Studio executions: contact_channel_address (~9k/day)
  • Messaging and Conversations events: from, to, Author, participant addresses and attributes, and the message text

attributeFilterer.ts is renamed to sensitiveDataFilter.ts. Its filterSensitiveData(event) now walks every field of every event type:

  1. By field name, at any depth, any case
    • Fully masked: message text, names and locations (Body/body, ProfileName, CallerName, Latitude/Longitude, city/state/zip parameters)
    • Last 4 characters kept: contact addresses (from, to, Author, Address, contact_channel_address, WaId, ForwardedFrom, …)
    • Numbers with 7+ digits under phone-like keys (phone, number, caller, msisdn, waid)
  2. Attributes JSON (task attributes, Conversations Attributes)
    • customers (keeping external_id), memory and preEngagementData are removed as before
    • Sensitive keys are masked at any depth, including number values. This adds name_old, outbound_to, external_contact, contactIdentifier, phone, address, and fixes the friendyName typo.
    • Names (customerName, firstName, friendlyName) are always fully masked
  3. Strings holding JSON (error-log payloads, logged webhook requests): parsed and masked field by field, so the JSON stays valid. They are only rewritten when something was masked, which keeps formatting and large-number precision.
  4. Free text (console output, error messages): masked by pattern
    • phone numbers: +/%2B, sip: and long numbers
    • channel IDs: modica:, messenger:, line:U…
    • numbers right after contact keys
    • IPs and emails
    • message text in URL-encoded or (escaped) JSON form, including text cut off mid-message

Numbers and addresses keep the existing XXXXX + last 4 characters format (XXXXX1234, XXXXX0.40) so events can still be told apart. A value whose last 4 characters contain letters (a name, street address or email) becomes XXXXX.

What stays readable:

  • worker_name, for routing debugging
  • A failing webhook's response body (the helpline server's own reply), which gets pattern masking only
  • SIDs, error codes, timestamps, short codes and stack-trace locations

Other changes in index.ts:

  • Masking runs only after the Twilio signature is validated, and only on the event sent to Datadog.
  • Masking is inside the Datadog try block. If it fails, the event is skipped (never sent unmasked), the error is logged with the account SID, and the request still returns 200, so Twilio doesn't retry.
  • A body that isn't an array now returns 400.

Regexes stay linear: 200 KB adversarial inputs take ≤ 10 ms.

Not changed:

  • Only body[0] is sent to Datadog. This is intended, because sending every event caused duplicates.
  • SNS still receives the raw body.

Checklist

  • Corresponding issue has been opened
  • New tests added
  • Feature flags added
  • Strings are localized
  • Tested for chat contacts
  • Tested for call contacts

Other Related Issues

None

Verification steps

  • cd lambdas/twilioEventStreams && npm run test:unit: 54 tests built from real event shapes (with made-up numbers)
  • Deployed this branch to the staging lambda on 2026-11-26 and sent SMS, webchat and voice test contacts to USCH, USNM, CA, AS and USNC staging:
    • From ~16:31 UTC, task attributes show the new masking (customerName: XXXXX, from: XXXXX4372). Before that they showed the old partial masking (XXXXXmous, XXXXX-sim).
    • A scan of 8,201 staging events (16:27–16:47 UTC) found no unmasked phone numbers, test numbers, IPs, emails, Body, ProfileName or WaId.
    • No "Error masking or posting the event to Datadog" errors.
  • Datadog query: service:twilio-event-stream @account_env:STG @type:com.twilio.taskrouter.task.created. Expand data.payload.task_attributes.
  • After deploying to production, these queries should return no new events (they find the leaks today):
    • service:twilio-event-stream @data.payload:*Task\ attribute\:\ from\ *\+??????????*
    • service:twilio-event-stream @type:com.twilio.studio.flow.execution.started @data.contact_channel_address:\+*
    • service:twilio-event-stream @data.product_name:TaskRouter @data.payload.task_attributes:(*name_old???sip\:\+??????????* OR *external_contact???\+??????????* OR *outbound_to???\+??????????*)
    • service:twilio-event-stream @data.payload:*Send\ interaction\ to\ Flex* @data.payload:*\"name?????\+??????????*
  • Check that TaskRouter, Studio and error-log events still arrive in Datadog with their SIDs, event types and error codes intact.
  • Still to test: Messaging and Conversations events (none came through in staging), and whether Studio step.ended events ever carry widget results.

AFTER YOU MERGE

  1. Cut a release tag using the Github workflow. Wait for it to complete and notify in the #aselo-deploys Slack channel.
  2. Comment on the ticket with the release tag version AND any additional instructions required to configure an environment to test the changes.
  3. Only then move the ticket into the QA column in JIRA

You are responsible for ensuring the above steps are completed. If you move a ticket into QA without advising what version to test, the QA team will assume the latest tag has the changes. If it does not, the following confusion is on you! :-P

🤖 Generated with Claude Code

Alejandro Rivera and others added 5 commits November 19, 2026 13:09
… sending to Datadog

The forwarder only masked a fixed list of TaskRouter task attributes. Phone numbers, IPs
and emails were reaching Datadog through other attributes (name_old, outbound_to,
conversations.external_contact), Studio executions (contact_channel_address), Messaging
events, and error-log events (Functions and Studio console output, error messages).

filterSensitiveData now masks, for every event:
- task attributes by name at any depth, then IPs, emails and phone numbers in any attribute
- contact addresses and message text by field name
- phone numbers, IPs and emails in every field of error-log events
- phone numbers and channel IDs anywhere in the event

Masking runs only after the Twilio signature is validated. Adds unit tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…fore sending to Datadog

- Mask Body/ProfileName at any depth and in free text (URL-encoded and logged JSON)
- Parse JSON inside strings (error log payloads) and mask field by field, keeping it valid
- Mask names entirely; numbers and addresses keep their last 4 characters
- Mask numeric helpseeker attributes, more contact keys, LINE IDs and long numbers
- Full masking (emails, IPs) for every event type, keeping worker_name readable
- Mask inside the Datadog try block so a failure never sends unmasked data or fails the request
- Rename attributeFilterer to sensitiveDataFilter; typed test helpers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…fore sending to Datadog

The previous commit only carried the attributeFilterer -> sensitiveDataFilter rename.

- Mask Body/ProfileName at any depth and in free text (URL-encoded and logged JSON)
- Parse JSON inside strings (error log payloads) and mask field by field, keeping it valid
- Mask names entirely; numbers and addresses keep their last 4 characters
- Mask numeric helpseeker attributes, more contact keys, LINE IDs and long numbers
- Full masking (emails, IPs) for every event type, keeping worker_name readable
- Mask inside the Datadog try block so a failure never sends unmasked data or fails the request
- Typed test helpers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Body/ProfileName/CallerName in logged JSON: handle escaped quotes, line breaks and accents; mask to the end when cut short (also keeps it linear)
- Mask lowercase body everywhere except a webhook response body
- Mask WaId, ForwardedFrom, CallerName, location and city/state/zip parameters; match field names case-insensitively
- Mask numbers under phone-like keys (phone, number, caller, msisdn, waid) when they have 7+ digits
- Names always fully masked; values keep their last 4 characters only when those have no letters
- Only rewrite JSON strings when something was masked (keeps number precision and formatting)
- from/to numbers in free text only after a key separator, so timestamps in sentences are kept
- Log the account and say masking or posting when the Datadog step fails

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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