Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize "api_key" from verbose log #3558

Merged
merged 3 commits into from
Apr 4, 2024
Merged
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
8 changes: 8 additions & 0 deletions packages/cli-kit/src/public/node/monorail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ describe('monorail', () => {
headers: expectedHeaders,
})
})

test('sanitizes the api_key from the debug log', async () => {
const outputMock = mockAndCaptureOutput()
const res = await publishMonorailEvent('fake_schema/0.0', {api_key: 'some-api-key'}, {baz: 'abc'})
expect(res.type).toEqual('ok')
expect(outputMock.debug()).toContain('"api_key": "****"')
theodoretan marked this conversation as resolved.
Show resolved Hide resolved
expect(outputMock.debug()).not.toContain('some-api-key')
})
})
17 changes: 16 additions & 1 deletion packages/cli-kit/src/public/node/monorail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export async function publishMonorailEvent<TSchemaId extends keyof Schemas, TPay
const response = await fetch(url, {method: 'POST', body, headers})

if (response.status === 200) {
outputDebug(outputContent`Analytics event sent: ${outputToken.json(payload)}`)
outputDebug(outputContent`Analytics event sent: ${outputToken.json(sanitizePayload(payload))}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the only place where we were printing the api_key?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I believe it is

return {type: 'ok'}
} else {
outputDebug(`Failed to report usage analytics: ${response.statusText}`)
Expand All @@ -215,6 +215,21 @@ export async function publishMonorailEvent<TSchemaId extends keyof Schemas, TPay
}
}

/**
* Sanitizies the api_key from the payload and returns a new hash.
*
* @param payload - The public and sensitive data.
* @returns A copy of the payload with the api_key sanitized.
*/
function sanitizePayload<T extends object>(payload: T): T {
const result = {...payload}
if ('api_key' in result) {
result.api_key = '****'
}

return result
}
theodoretan marked this conversation as resolved.
Show resolved Hide resolved

const buildHeaders = (currentTime: number) => {
return {
'Content-Type': 'application/json; charset=utf-8',
Expand Down