-
Notifications
You must be signed in to change notification settings - Fork 662
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
socket-mode(fix): redact ephemeral tokens and secrets from debug logs #1832
base: main
Are you sure you want to change the base?
socket-mode(fix): redact ephemeral tokens and secrets from debug logs #1832
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1832 +/- ##
==========================================
+ Coverage 81.85% 82.05% +0.19%
==========================================
Files 35 35
Lines 7782 7829 +47
Branches 318 331 +13
==========================================
+ Hits 6370 6424 +54
+ Misses 1400 1393 -7
Partials 12 12
Flags with carried forward coverage won't be shown. Click here to find out more. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this! You can continue the review with @filmaj tomorrow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some comments and things to try out to try to avoid type casting - I would like to stop doing that moving forward in our TS libraries.
* @returns the same object with redacted values. | ||
*/ | ||
private static redact(body?: Record<string, unknown>): Record<string, unknown> | unknown[] | undefined { | ||
if (body === undefined || body === null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type of the body
parameter and the values we are checking here don't line up. Should the parameter be optional? Since this is a private method, it's up to us; I would imagine we want to ensure this method is always called with something. Additionally, since we are checking for null
here, should body
be assignable to null
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great callout with the optional body?
- that was poor typing on my part! Also a nice catch with null
. That shouldn't be possible and I was guarding to much... undefined
is possible - via ack()
- and seems to work well as is, but the rest was fixed!
return body; | ||
} | ||
const record = Object.create(body); | ||
if (Array.isArray(body)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question as above: should body
be assignable to an Array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 blocks
or actions
or inputs
.
if (Array.isArray(body)) { | ||
return body.map((item) => ( | ||
(typeof item === 'object' && item !== null) ? | ||
SocketModeClient.redact(item as Record<string, unknown>) : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you may require type casting here because my previous comment is unaddressed: if body
is assignable to an array (of, presumably, Record<string, unknown>
), then the map
here could infer that item
is a Record<string, unknown>
, so then you may not need to type cast anymore.
I know that in a variety of places within node-slack-sdk and bolt-js today we use type casting liberally. In general (not that I'm a TS expert) my understanding is type casting is a sign that the types of variables are not thorough enough. Moving forward with the node SDK and bolt-js, I would like for us to, when possible, consider type casting as a last resort and avoid them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the PR that fixes some coverups caused by blunt casting! slackapi/bolt-js#1806
I am wary of casting and ought to take this lesson to heart more 😳
* @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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What may help in this function is to define a recursive utility type that encapsulates Record<string, unknown>
; something that signifies "an array of objects that itself can contain objects (..and so on), or an object that itself can contain objects (..and so on)." I think that could help you eliminate the type casting below. Perhaps something like:
type Something = Record<string, Something> | Something[];
In theory, in my head, this could be helpful to hint to TS how to derive types as you dissect the thing to redact. I have hit problems with recursive types in TS before, but give it a shot and see if that helps!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted this to work... Found that recursive types sometimes won't work? It might be caused by a certain version of TS cause it seemed to have worked in some earlier version but not in this version 😢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@filmaj great callouts with the typings! Made a few updates here but still found some troubles with casting one last one... Tested this and things are working though!
* Recursive definiton for possible JSON object values. | ||
* | ||
* FIXME: Prefer using a circular reference if allowed: | ||
* Record<string, NestedRecord> | NestedRecord[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately this was tossing an error in the editor... Some prior art was tried but without much luck...
Type alias 'NestedRecord' circularly references itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, don't sweat it! We do some seriously nasty gymnastics in deno-slack-sdk to avoid this; I do not recommend it, though. Roll with the punches and move on!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am oddly impressed with this magic. I wish this was magic tsc
took care of though and I didn't have to learn the tricks 😉 🪄
Object.keys(body).forEach((key: string) => { | ||
const value = body[key]; | ||
if (typeof value === 'object' && value !== null) { | ||
record[key] = SocketModeClient.redact(value as NestedRecord); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cast seems needed even with the recursive definition above... 🤔 I fumbled a bit with it without too much luck.
Argument of type 'object' is not assignable to parameter of type 'NestedRecord'.
return body; | ||
} | ||
const record = Object.create(body); | ||
if (Array.isArray(body)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 blocks
or actions
or inputs
.
Summary
This PR replaces values that should be redacted in debug logs - such as the ephemeral
bot_access_token
used in custom functions andinteractor.secret
- with[[REDACTED]]
.Preview
Example output differences are found towards the end:
Reviewers
These changes resemble those found in #1831 but with updates to match refactors from
socket-mode@v2
. At this time it's not so quick to test with Bolt but the test cases will hopefully give some confidence 🙏Notes
socket-mode@latest
onmain
and the[email protected]
branch to backport for Bolt JS.Requirements