Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions packages/cli-kit/src/private/node/api/urls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ describe('sanitizeURL', () => {
'client_secret',
'code',
'token',
'api_key',
'secret',
'password',
'sig',
'signature',
])('sanitizes %s query parameter', (param) => {
// Given
const url = `https://example.com?${param}=secret-value`
Expand All @@ -79,4 +84,48 @@ describe('sanitizeURL', () => {
'https://example.com/?access_token=****&refresh_token=****&device_code=****&subject_token=****&other=keep',
)
})

test('sanitizes query parameters case-insensitively', () => {
// Given
const url = 'https://example.com?TOKEN=abc123&Access_Token=def456'

// When
const sanitizedUrl = sanitizeURL(url)

// Then
expect(sanitizedUrl).toBe('https://example.com/?TOKEN=****&Access_Token=****')
})

test('sanitizes username and password in the URL', () => {
// Given
const url = 'https://user:pass@example.com/path?token=abc123'

// When
const sanitizedUrl = sanitizeURL(url)

// Then
expect(sanitizedUrl).toBe('https://****:****@example.com/path?token=****')
})

test('sanitizes only password in the URL', () => {
// Given
const url = 'https://:pass@example.com/path'

// When
const sanitizedUrl = sanitizeURL(url)

// Then
expect(sanitizedUrl).toBe('https://:****@example.com/path')
})

test('sanitizes only username in the URL', () => {
// Given
const url = 'https://user@example.com/path'

// When
const sanitizedUrl = sanitizeURL(url)

// Then
expect(sanitizedUrl).toBe('https://****@example.com/path')
})
})
21 changes: 18 additions & 3 deletions packages/cli-kit/src/private/node/api/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const SENSITIVE_QUERY_PARAMS = [
'client_secret',
'code',
'token',
'api_key',
'secret',
'password',
'sig',
'signature',
]

/**
Expand All @@ -21,10 +26,20 @@ const SENSITIVE_QUERY_PARAMS = [
*/
export function sanitizeURL(url: string): string {
const parsedUrl = new URL(url)
for (const param of SENSITIVE_QUERY_PARAMS) {
if (parsedUrl.searchParams.has(param)) {
parsedUrl.searchParams.set(param, '****')

if (parsedUrl.username) {
parsedUrl.username = '****'
}
if (parsedUrl.password) {
parsedUrl.password = '****'
}

const keys = Array.from(parsedUrl.searchParams.keys())
for (const key of keys) {
if (SENSITIVE_QUERY_PARAMS.includes(key.toLowerCase())) {
parsedUrl.searchParams.set(key, '****')
}
}

return parsedUrl.toString()
}
Loading