Skip to content

Commit 241a014

Browse files
committed
feat(okhttp): add redacted headers & parameters to the HTTP instrumentation tests
1 parent f1975e1 commit 241a014

3 files changed

Lines changed: 31 additions & 10 deletions

File tree

bugsnag-plugin-android-okhttp/src/main/java/com/bugsnag/android/okhttp/EventRequestHelper.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ internal fun Event.setHttpInfo(
2222
)
2323

2424
request?.apply {
25-
bodyLength = bodyLengthOf(instrumentedRequest.request)
25+
val okReq = instrumentedRequest.request
26+
bodyLength = bodyLengthOf(okReq)
2627
body = instrumentedRequest.reportedRequestBody
27-
instrumentedRequest.request.headers.forEach { (name, value) ->
28+
okReq.headers.forEach { (name, value) ->
2829
addHeader(name, value)
2930
}
31+
32+
val queryParams = okReq.url.queryParameterNames
33+
queryParams.forEach { queryKey ->
34+
addQueryParameter(queryKey, okReq.url.queryParameter(queryKey))
35+
}
3036
}
3137

3238
val okResp = instrumentedResponse?.response

features/fixtures/mazerunner/jvm-scenarios/src/main/java/com/bugsnag/android/mazerunner/scenarios/OkHttpInstrumentationScenario.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import okhttp3.OkHttpClient
1010
import okhttp3.Request
1111
import okhttp3.RequestBody.Companion.toRequestBody
1212
import org.json.JSONObject
13+
import java.util.regex.Pattern
1314
import kotlin.concurrent.thread
1415

1516
private const val MAX_CAPTURE_BYTES = 32L
@@ -30,13 +31,21 @@ class OkHttpInstrumentationScenario(
3031
.addInterceptor(instrumentation.createInterceptor())
3132
.build()
3233

33-
private fun reflectionUrl(status: Int): String {
34+
private fun reflectionUrl(status: Int): Uri {
3435
return Uri.parse(config.endpoints.notify)
3536
.buildUpon()
3637
.path("/reflect")
3738
.appendQueryParameter("status", status.toString())
39+
.appendQueryParameter("password", "secret")
3840
.build()
39-
.toString()
41+
}
42+
43+
init {
44+
config.redactedKeys = setOf(
45+
"Cookie".toPattern(Pattern.LITERAL or Pattern.CASE_INSENSITIVE),
46+
"Authorization".toPattern(Pattern.LITERAL or Pattern.CASE_INSENSITIVE),
47+
".*password.*".toPattern(Pattern.CASE_INSENSITIVE)
48+
)
4049
}
4150

4251
private fun requestType(): Pair<String, Int> {
@@ -55,12 +64,16 @@ class OkHttpInstrumentationScenario(
5564

5665
val payload = JSONObject()
5766
payload.put("padding", "this is a string, and it goes on and on until it stops...here")
58-
payload.put("url", reflectionUrl)
67+
// we expect the output URL to not have a query string, so we help the scenario feature
68+
// out by removing it in the reflection payload (allowing an "equals" match)
69+
payload.put("url", reflectionUrl.buildUpon().clearQuery().build())
70+
payload.put("status", status)
5971

6072
val body = payload.toString().toRequestBody(JSON)
6173

6274
val requestBuilder = Request.Builder()
63-
.url(reflectionUrl)
75+
.url(reflectionUrl.toString())
76+
.header("Authorization", "Bearer OpenSesame")
6477
.method(method, body.takeIf { method != "GET" })
6578

6679
log("Sending request to $reflectionUrl")

features/full_tests/okhttp_instrumentation.feature

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ Feature: Capturing network breadcrumbs
4141
And the event "request.httpVersion" is not null
4242
And the event "request.bodyLength" is greater than 64
4343
And the error payload field "events.0.request.body" equals "{\"padding\":\"this is a string, an"
44-
And the error payload field "request.url" equals the stored value "expectedUrl"
44+
And the error payload field "events.0.request.url" equals the stored value "expectedUrl"
45+
And the error payload field "events.0.request.headers.Authorization" equals "[REDACTED]"
46+
And the error payload field "events.0.request.params.password" equals "[REDACTED]"
4547

4648
# Validate response fields
4749
And the event "response.statusCode" equals 400
@@ -58,12 +60,12 @@ Feature: Capturing network breadcrumbs
5860
And the exception "message" matches "500: http://.+"
5961
And the event "context" matches "GET .+"
6062

61-
And the reflection payload field "url" is stored as the value "expectedUrl"
62-
6363
# Validate request fields
6464
And the event "request.httpMethod" equals "GET"
6565
And the event "request.httpVersion" is not null
66-
And the error payload field "request.url" equals the stored value "expectedUrl"
66+
And the event "request.url" matches "^https?\:\/\/.+"
67+
And the error payload field "events.0.request.headers.Authorization" equals "[REDACTED]"
68+
And the error payload field "events.0.request.params.password" equals "[REDACTED]"
6769

6870
# Validate response fields
6971
And the event "response.statusCode" equals 500

0 commit comments

Comments
 (0)