All files / server/src/utils scrubIpAddress.ts

100% Statements 8/8
100% Branches 4/4
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32          7x                     7x 7x 2x     7x 1x   4x         7x    
import { type ErrorEvent } from "@sentry/node";
 
// Request headers that carry the client IP address (PII). The requestData
// integration includes request headers by default while excluding user.ip_address,
// so these have to be scrubbed manually before sending the event to Sentry
const ipHeaders = new Set([
  "x-forwarded-for",
  "x-real-ip",
  "forwarded",
  "cf-connecting-ip",
  "true-client-ip",
  "x-client-ip",
  "x-cluster-client-ip",
]);
 
// Don't send the user's IP address to Sentry, it's Personally Identifiable Information (PII)
export const scrubIpAddress = (event: ErrorEvent): ErrorEvent => {
  if (event.user) {
    delete event.user.ip_address;
  }
 
  if (event.request?.headers) {
    event.request.headers = Object.fromEntries(
      Object.entries(event.request.headers).filter(
        ([header]) => !ipHeaders.has(header.toLowerCase()),
      ),
    );
  }
 
  return event;
};