-
Notifications
You must be signed in to change notification settings - Fork 676
socket-mode(fix): redact ephemeral tokens and secrets from debug logs #1832
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
d097cd6
b4718d8
e7a1166
3147e08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -284,8 +284,6 @@ export class SocketModeClient extends EventEmitter { | |
| this.logger.debug('Unexpected binary message received, ignoring.'); | ||
| return; | ||
| } | ||
| const payload = data.toString(); | ||
| this.logger.debug(`Received a message on the WebSocket: ${payload}`); | ||
|
|
||
| // Parse message into slack event | ||
| let event: { | ||
|
|
@@ -299,10 +297,13 @@ export class SocketModeClient extends EventEmitter { | |
| accepts_response_payload?: boolean; // type: events_api, slash_commands, interactive | ||
| }; | ||
|
|
||
| const payload = data?.toString(); | ||
| try { | ||
| event = JSON.parse(payload); | ||
| this.logger.debug(`Received a message on the WebSocket: ${JSON.stringify(SocketModeClient.redact(event))}`); | ||
| } catch (parseError) { | ||
| // Prevent application from crashing on a bad message, but log an error to bring attention | ||
| this.logger.debug(`Received a message on the WebSocket: ${payload}`); | ||
| this.logger.debug( | ||
| `Unable to parse an incoming WebSocket message (will ignore): ${parseError}, ${payload}`, | ||
| ); | ||
|
|
@@ -325,7 +326,7 @@ export class SocketModeClient extends EventEmitter { | |
| // Define Ack, a helper method for acknowledging events incoming from Slack | ||
| const ack = async (response: Record<string, unknown>): Promise<void> => { | ||
| if (this.logger.getLevel() === LogLevel.DEBUG) { | ||
| this.logger.debug(`Calling ack() - type: ${event.type}, envelope_id: ${event.envelope_id}, data: ${JSON.stringify(response)}`); | ||
| this.logger.debug(`Calling ack() - type: ${event.type}, envelope_id: ${event.envelope_id}, data: ${JSON.stringify(SocketModeClient.redact(response))}`); | ||
| } | ||
| await this.send(event.envelope_id, response); | ||
| }; | ||
|
|
@@ -386,9 +387,8 @@ export class SocketModeClient extends EventEmitter { | |
| } else { | ||
| this.emit('outgoing_message', message); | ||
|
|
||
| const flatMessage = JSON.stringify(message); | ||
| this.logger.debug(`Sending a WebSocket message: ${flatMessage}`); | ||
| this.websocket.send(flatMessage, (error) => { | ||
| this.logger.debug(`Sending a WebSocket message: ${JSON.stringify(SocketModeClient.redact(message))}`); | ||
| this.websocket.send(JSON.stringify(message), (error) => { | ||
| if (error) { | ||
| this.logger.error(`Failed to send a WebSocket message (error: ${error})`); | ||
| return reject(websocketErrorWithOriginal(error)); | ||
|
|
@@ -398,6 +398,37 @@ export class SocketModeClient extends EventEmitter { | |
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Removes secrets and tokens from socket request and response objects | ||
| * before logging. | ||
| * @param body - the object with values for redaction. | ||
| * @returns the same object with redacted values. | ||
| */ | ||
| private static redact(body?: Record<string, unknown>): Record<string, unknown> | unknown[] | undefined { | ||
|
||
| if (body === undefined || body === null) { | ||
|
||
| return body; | ||
| } | ||
| const records = Object.create(body); | ||
zimeg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (Array.isArray(body)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question as above: should
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not directly, but it's possible that keys somewhere within the body contain an Array, which would be hit in some recursive case. Possibly with details from |
||
| return body.map((item) => ( | ||
| (typeof item === 'object' && item !== null) ? | ||
| SocketModeClient.redact(item as Record<string, unknown>) : | ||
|
||
| item | ||
| )); | ||
| } | ||
| Object.keys(body).forEach((key: string) => { | ||
| const value = body[key]; | ||
| if (typeof value === 'object' && value !== null) { | ||
| records[key] = SocketModeClient.redact(value as Record<string, unknown>); | ||
| } else if (key.match(/.*token.*/) !== null || key.match(/secret/)) { | ||
| records[key] = '[[REDACTED]]'; | ||
| } else { | ||
| records[key] = value; | ||
| } | ||
| }); | ||
| return records; | ||
| } | ||
| } | ||
|
|
||
| /* Instrumentation */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.