Skip to content

Commit

Permalink
fix(logging-sensitive-data) Filter out logs with sensitive data.
Browse files Browse the repository at this point in the history
* Fix logs from the client printing Authorization token
* Fix logic printing the full message with token and text
* Remove logging SQL queries
  • Loading branch information
spoonman01 committed Sep 18, 2024
1 parent d751746 commit 3012ae8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
kotlin("jvm") version "1.5.30"
application
distribution
id("net.nemerosa.versioning") version "2.14.0"
id("net.nemerosa.versioning") version "3.1.0"
}

group = "com.wire.bots.polls"
Expand Down
21 changes: 19 additions & 2 deletions src/main/kotlin/com/wire/bots/polls/dto/roman/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,17 @@ data class Message(
* Type of the file
*/
val mimeType: String?,

) {
data class Text(
val data: String,
val mentions: List<Mention>?
)

) {
override fun toString(): String {
return "Text(mentions=$mentions)"
}
}

/**
* Poll representation for the proxy.
Expand All @@ -97,7 +103,18 @@ data class Message(
* Id of the button when it was clicked on.
*/
val offset: Int?
)
) {
override fun toString(): String {
return "PollObjectMessage(id='$id', buttons=$buttons, offset=$offset)"
}
}

/**
* Avoid printing out the token by mistake if object is printed.
*/
override fun toString(): String {
return "Message(botId='$botId', userId=$userId, conversationId=$conversationId, type='$type', messageId=$messageId, text=$text, refMessageId=$refMessageId, reaction=$reaction, image=$image, handle=$handle, locale=$locale, poll=$poll, mimeType=$mimeType)"
}
}

/* JSON from the swagger
Expand Down
36 changes: 14 additions & 22 deletions src/main/kotlin/com/wire/bots/polls/setup/HttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,12 @@ package com.wire.bots.polls.setup
import com.wire.bots.polls.utils.ClientRequestMetric
import com.wire.bots.polls.utils.createLogger
import com.wire.bots.polls.utils.httpCall
import io.ktor.client.HttpClient
import io.ktor.client.engine.apache.Apache
import io.ktor.client.features.json.JacksonSerializer
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.features.logging.LogLevel
import io.ktor.client.features.logging.Logger
import io.ktor.client.features.logging.Logging
import io.ktor.client.*
import io.ktor.client.engine.apache.*
import io.ktor.client.features.json.*
import io.ktor.client.features.logging.*
import io.micrometer.core.instrument.MeterRegistry


/**
* Prepares HTTP Client.
*/
fun createHttpClient(meterRegistry: MeterRegistry) =
HttpClient(Apache) {
install(JsonFeature) {
Expand All @@ -32,23 +25,22 @@ fun createHttpClient(meterRegistry: MeterRegistry) =
}
}

/**
* Debug logger for HTTP Requests.
*/
private val Logger.Companion.DEBUG: Logger
get() = object : Logger, org.slf4j.Logger by createLogger("DebugHttpClient") {
override fun log(message: String) {
debug(message)
}
}


/**
* Trace logger for HTTP Requests.
*
* Logs request/response bodies, params and headers.
* Avoids logging lines containing sensitive data
*/
private val Logger.Companion.TRACE: Logger
get() = object : Logger, org.slf4j.Logger by createLogger("TraceHttpClient") {
override fun log(message: String) {
for (blockedWord in blockedWordList) {
if (message.contains(blockedWord, ignoreCase = true)) {
return
}
}
trace(message)
}
}

private val blockedWordList = listOf("Authorization", "token", "Bearer", "text")
1 change: 1 addition & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

<logger name="org.eclipse.jetty" level="INFO"/>
<logger name="io.netty" level="INFO"/>
<logger name="org.jetbrains.exposed" level="INFO"/>

<logger name="com.wire" level="TRACE"/>

Expand Down

0 comments on commit 3012ae8

Please sign in to comment.