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

Don't log bot and voice tokens in Gateway implementations (trace level) #671

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

lukellmann
Copy link
Member

@lukellmann lukellmann commented Aug 16, 2022

DefaultGateway logs the JSON payloads it sends when trace logging is enabled. It also hides the bot token in case of an Identify command. But it didn't do this for Resume commands, which also includes the bot token.

Same goes for DefaultVoiceGateway, which didn't hide the voice token for Resume commands.

This PR fixes this and also updates some toString implementations of data classes that contain a bot or voice token.

DefaultVoiceGateway also hides secret_key for SessionDescription events, this is also added for voice tokens in VoiceServerUpdate events that DefaultGateway receives.

The PR also makes the Json instance used by DefaultGateway and DefaultVoiceGateway a property of the companion object since it can be shared safely.

@lukellmann
Copy link
Member Author

Should we do this for voice gateway too @lost-illusi0n?

@lost-illusi0n
Copy link
Contributor

I'm not against it. Might as well be consistent across the entirety of Kord.

@lukellmann
Copy link
Member Author

Alright, just asking cause I don't know how sensitive voice tokens are.

@lost-illusi0n
Copy link
Contributor

Probably sensitive enough, it is a token after all. I believe someone with access to it would be able to hijack the voice connection for a server (within a certain timeframe), though I haven't tried it :p

Comment on lines 186 to 284
defaultGatewayLogger.trace {
val credentialFreeJson = when (event) {

is VoiceServerUpdate -> {
when (val payload = GatewayJson.parseToJsonElement(json)) {
is JsonObject -> {
val payloadCopy = buildJsonObject {
for ((k, v) in payload) put(k, v)

val data = payload["d"]
if (data is JsonObject) putJsonObject("d") {
for ((k, v) in data) put(k, v)
put("token", "hunter2")
}
}
payloadCopy.toString()
}
else -> json
}
}

else -> json
}

"Gateway <<< $credentialFreeJson"
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is quite heavy for just not logging the token, voice does it like this to hide secret_key, should we do it like that too instead?

@lukellmann lukellmann changed the title Don't log token when sending Resume Don't log bot and voice tokens in Gateway implementations Aug 16, 2022
@lukellmann
Copy link
Member Author

Other token types this PR didn't touch yet are interaction and webhook tokens, what about them?

@MrPowerGamerBR
Copy link
Contributor

MrPowerGamerBR commented Aug 16, 2022

Other token types this PR didn't touch yet are interaction and webhook tokens, what about them?

I think that, for consistency, it would be nice to hide them too, because they are sensitive too, because with interaction/webhook tokens you can send whatever message you want.

By the way, wouldn't it be better to filter the sensitive data on the logger end, instead of replacing all entities with a custom toString() implementation? While replacing the toString() implemention works, someone could forget to update it if new variables are added to the entities. Filtering on the logger side would make it easier to filter in other places were the token might get accidentally printed to the console, however you do have the disadvantage that it may filter something that it shouldn't be filtered AND how the tokens are filtered would vary depending on what logger the user is using (here's a logback example: https://www.baeldung.com/logback-mask-sensitive-data)

@lukellmann
Copy link
Member Author

lukellmann commented Aug 16, 2022

Hm, how would we filter it on the logger end without a logger implementation (kord doesn't provide one)?

@MrPowerGamerBR
Copy link
Contributor

Hm, how would we filter it on the logger end without a logger implementation (kord doesn't provide one)?

Yeah, that's one of the issues with this approach: The user would need to configure their logger implementation (logback, log4j, etc) to filter tokens

@lost-illusi0n
Copy link
Contributor

Not quite, Kord depends on kotlin-logging for logging, which simply wraps around the SLF4J api. I'm aware of the log4j api having an AbstractLogger class that can be implemented, which would then strip out the token(s) from any outgoing log message, and delegate it to the actual logger. I assume this can similarly be done with the slf4j abstract logger. Might be worth taking a look at.

@lukellmann lukellmann marked this pull request as draft August 18, 2022 13:09
@lukellmann lukellmann changed the title Don't log bot and voice tokens in Gateway implementations Don't log bot and voice tokens in Gateway implementations (trace level) Aug 21, 2022
@lukellmann
Copy link
Member Author

Mentioned trace level in title to not scare people.

@lukellmann lukellmann force-pushed the fix/resume-token-leak branch 2 times, most recently from abe99f9 to 1214b95 Compare March 9, 2023 22:33
@lukellmann lukellmann changed the base branch from 0.8.x to 0.9.x March 25, 2023 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants