Skip to content

Escape quotes in curl share #1211

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

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Please add your entries according to this format.

* Fixed activity still asking for notification permission when notifications are disabled [#1165]
* Fixed Gson issue, when using Chucker with ProGuard [#1183]
* Fixed share of curl when header values contain quotes [#1211]

### Added

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ internal class TransactionCurlCommandSharable(
if (isCompressed(header)) {
compressed = true
}
writeUtf8(" -H \"${header.name}: ${header.value}\"")
val headerValue = escapeHeaderValue(header.value)
writeUtf8(" -H \"${header.name}: ${headerValue}\"")
}

val requestBody = transaction.requestBody
Expand All @@ -37,4 +38,9 @@ internal class TransactionCurlCommandSharable(
"br".contains(header.value, ignoreCase = true)
)
}

private fun escapeHeaderValue(value: String): String {
// escape double quotes from header value to prevent getting an invalid curl
return value.replace("\"", "\\\"")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ internal class TransactionCurlCommandSharableTest {
}
}

@Test
fun `create cURL command with quote value in header`() {
val headers =
listOf(
HttpHeader("X-JSON-Object", "{\"name\": \"value\"}"),
HttpHeader("Etag", "W/\"0815\""),
)
val convertedHeaders = JsonConverter.instance.toJson(headers)

requestMethods.forEach { method ->
val transaction =
TestTransactionFactory.createTransaction(method).apply {
requestHeaders = convertedHeaders
}
val sharableTransaction = TransactionCurlCommandSharable(transaction)
val expectedCurlCommand =
"""
curl -X $method -H "X-JSON-Object: {\"name\": \"value\"}" -H "Etag: W/\"0815\"" http://localhost/getUsers
""".trimIndent()

val sharedContent = sharableTransaction.toSharableUtf8Content(context)

assertThat(sharedContent).isEqualTo(expectedCurlCommand)
}
}

@Test
fun `create cURL command with request bodies for PUT and POST methods`() {
requestMethods.filter { it in listOf("POST", "PUT") }.forEach { method ->
Expand Down Expand Up @@ -85,7 +111,8 @@ internal class TransactionCurlCommandSharableTest {

val sharedContent = sharableTransaction.toSharableUtf8Content(context)

val expected = "curl -X $method -H \"Accept-Encoding: gzip\" --compressed http://localhost/getUsers"
val expected =
"curl -X $method -H \"Accept-Encoding: gzip\" --compressed http://localhost/getUsers"

assertThat(sharedContent).isEqualTo(expected)
}
Expand Down