File size: 1,679 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
 * Helper function to safely log sensitive data when debug mode is enabled
 * @param obj - Object to stringify
 * @param maxLength - Maximum length of the stringified output
 * @returns Stringified object with sensitive data masked
 */
export function safeStringify(obj: unknown, maxLength = 1000): string {
  try {
    const str = JSON.stringify(obj, (key, value) => {
      // Mask sensitive values
      if (
        key === 'client_secret' ||
        key === 'Authorization' ||
        key.toLowerCase().includes('token') ||
        key.toLowerCase().includes('password')
      ) {
        return typeof value === 'string' && value.length > 6
          ? `${value.substring(0, 3)}...${value.substring(value.length - 3)}`
          : '***MASKED***';
      }
      return value;
    });

    if (str && str.length > maxLength) {
      return `${str.substring(0, maxLength)}... (truncated)`;
    }
    return str;
  } catch (error) {
    return `[Error stringifying object: ${(error as Error).message}]`;
  }
}

/**
 * Helper to log headers without revealing sensitive information
 * @param headers - Headers object to log
 * @returns Stringified headers with sensitive data masked
 */
export function logHeaders(headers: Headers | undefined | null): string {
  const headerObj: Record<string, string> = {};
  if (!headers || typeof headers.entries !== 'function') {
    return 'No headers available';
  }
  for (const [key, value] of headers.entries()) {
    if (key.toLowerCase() === 'authorization' || key.toLowerCase().includes('secret')) {
      headerObj[key] = '***MASKED***';
    } else {
      headerObj[key] = value;
    }
  }
  return safeStringify(headerObj);
}