Skip to content

Commit d4aa6ad

Browse files
committed
refactor(sdk): extract header sanitization to shared utility
Eliminates duplication of SENSITIVE_HEADERS and sanitizeHeaders() function. - Created src/utils/header-sanitization.ts as single source of truth - Updated http-client.ts to import shared utility (removed 39 lines) - Updated file-upload.ts to import shared utility (removed 39 lines) Moved sanitizeHeaders import to be grouped with other module imports, improving code organization and readability.
1 parent c5769e1 commit d4aa6ad

File tree

3 files changed

+47
-78
lines changed

3 files changed

+47
-78
lines changed

src/file-upload.ts

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Readable } from 'node:stream'
77
import { normalizePath } from '@socketsecurity/lib/paths/normalize'
88

99
import { getHttpModule, getResponse } from './http-client'
10+
import { sanitizeHeaders } from './utils/header-sanitization'
1011

1112
import type { RequestOptions, SocketSdkOptions } from './types'
1213
import type { ReadStream } from 'node:fs'
@@ -16,45 +17,6 @@ import type { RequestOptions as HttpsRequestOptions } from 'node:https'
1617
/**
1718
* Array of sensitive header names that should be redacted in logs
1819
*/
19-
const SENSITIVE_HEADERS = [
20-
'authorization',
21-
'cookie',
22-
'set-cookie',
23-
'proxy-authorization',
24-
'www-authenticate',
25-
'proxy-authenticate',
26-
]
27-
28-
/**
29-
* Sanitize headers for logging by redacting sensitive values.
30-
*/
31-
function sanitizeHeaders(
32-
headers: Record<string, unknown> | readonly string[] | undefined,
33-
): Record<string, string> | undefined {
34-
if (!headers) {
35-
return undefined
36-
}
37-
38-
// Handle readonly string[] case - this shouldn't normally happen for headers
39-
if (Array.isArray(headers)) {
40-
return { headers: headers.join(', ') }
41-
}
42-
43-
const sanitized: Record<string, string> = {}
44-
45-
// Plain object iteration works for both HeadersRecord and IncomingHttpHeaders
46-
for (const [key, value] of Object.entries(headers)) {
47-
const keyLower = key.toLowerCase()
48-
if (SENSITIVE_HEADERS.includes(keyLower)) {
49-
sanitized[key] = '[REDACTED]'
50-
} else {
51-
// Handle both string and string[] values
52-
sanitized[key] = Array.isArray(value) ? value.join(', ') : String(value)
53-
}
54-
}
55-
56-
return sanitized
57-
}
5820

5921
/**
6022
* Create multipart form-data body parts for file uploads.

src/http-client.ts

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { jsonParse } from '@socketsecurity/lib/json'
1111
import { perfTimer } from '@socketsecurity/lib/performance'
1212

1313
import { MAX_RESPONSE_SIZE } from './constants'
14+
import { sanitizeHeaders } from './utils/header-sanitization'
1415

1516
import type {
1617
RequestOptions,
@@ -24,45 +25,6 @@ import type { ClientRequest, IncomingMessage } from 'node:http'
2425
/**
2526
* Array of sensitive header names that should be redacted in logs
2627
*/
27-
const SENSITIVE_HEADERS = [
28-
'authorization',
29-
'cookie',
30-
'set-cookie',
31-
'proxy-authorization',
32-
'www-authenticate',
33-
'proxy-authenticate',
34-
]
35-
36-
/**
37-
* Sanitize headers for logging by redacting sensitive values.
38-
*/
39-
function sanitizeHeaders(
40-
headers: Record<string, unknown> | readonly string[] | undefined,
41-
): Record<string, string> | undefined {
42-
if (!headers) {
43-
return undefined
44-
}
45-
46-
// Handle readonly string[] case - this shouldn't normally happen for headers
47-
if (Array.isArray(headers)) {
48-
return { headers: headers.join(', ') }
49-
}
50-
51-
const sanitized: Record<string, string> = {}
52-
53-
// Plain object iteration works for both HeadersRecord and IncomingHttpHeaders
54-
for (const [key, value] of Object.entries(headers)) {
55-
const keyLower = key.toLowerCase()
56-
if (SENSITIVE_HEADERS.includes(keyLower)) {
57-
sanitized[key] = '[REDACTED]'
58-
} else {
59-
// Handle both string and string[] values
60-
sanitized[key] = Array.isArray(value) ? value.join(', ') : String(value)
61-
}
62-
}
63-
64-
return sanitized
65-
}
6628

6729
/**
6830
* HTTP response error for Socket API requests.

src/utils/header-sanitization.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* List of sensitive HTTP headers that should be redacted in logs.
3+
*/
4+
export const SENSITIVE_HEADERS: readonly string[] = [
5+
'authorization',
6+
'cookie',
7+
'set-cookie',
8+
'proxy-authorization',
9+
'www-authenticate',
10+
'proxy-authenticate',
11+
]
12+
13+
/**
14+
* Sanitize headers for logging by redacting sensitive values.
15+
*
16+
* @param headers - Headers to sanitize (object or array)
17+
* @returns Sanitized headers with sensitive values redacted
18+
*/
19+
export function sanitizeHeaders(
20+
headers: Record<string, unknown> | readonly string[] | undefined,
21+
): Record<string, string> | undefined {
22+
if (!headers) {
23+
return undefined
24+
}
25+
26+
// Handle readonly string[] case - this shouldn't normally happen for headers.
27+
if (Array.isArray(headers)) {
28+
return { headers: headers.join(', ') }
29+
}
30+
31+
const sanitized: Record<string, string> = {}
32+
33+
// Plain object iteration works for both HeadersRecord and IncomingHttpHeaders.
34+
for (const [key, value] of Object.entries(headers)) {
35+
const keyLower = key.toLowerCase()
36+
if (SENSITIVE_HEADERS.includes(keyLower)) {
37+
sanitized[key] = '[REDACTED]'
38+
} else {
39+
// Handle both string and string[] values.
40+
sanitized[key] = Array.isArray(value) ? value.join(', ') : String(value)
41+
}
42+
}
43+
44+
return sanitized
45+
}

0 commit comments

Comments
 (0)