Skip to content

Commit 6eff549

Browse files
ADFA-5241: Close the response-splitting hole the charset parameter left open
safeType refused a value carrying a control character, but the refusal was read back out of its return value -- OCTET_STREAM with no charset -- and a control character inside the charset parameter defeated that: declaredCharset re-parsed the original string, found a charset, so "refused" was never true, and headerValue appended the CRLF-bearing value verbatim. WebServer writes the result with println, so text/html; charset=x<CR><LF><CR><LF><script>alert(1)</script> split the reply into two HTTP responses with an attacker-chosen body -- the one thing this class exists to prevent. The two existing "refused, not repaired" tests put their control character in the type segment or a note= parameter, so neither could see it. Refusal is asked now, not inferred: one isUntrustworthy() consulted by both typeAndCharset and headerValue. That also stops a legitimately stored application/octet-stream; name=file.bin from being mistaken for a refusal and losing its parameters. The charset is the one value that skipped quoteIfNeeded, so a stored charset="utf-8; x=y" -- whose quotes parameters() strips -- came back out as a charset plus a smuggled second parameter. It goes through the same quoting as every other parameter value now. Four tests, three of which fail against the previous logic. 84 common tests pass. Found in review of PR #1725.
1 parent 8769776 commit 6eff549

2 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎common/src/main/java/com/itsaky/androidide/utils/ContentTypeHeaders.kt‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,28 @@ object ContentTypeHeaders {
6464
* both transports instead of only this one.
6565
*/
6666
internal fun typeAndCharset(mimeType: String): Pair<String, String?> {
67+
// Refusal has to be asked, not inferred. This used to read the answer out of safeType's return
68+
// value -- OCTET_STREAM with no charset -- and a control character inside the charset parameter
69+
// defeated it: safeType refused the type, declaredCharset re-parsed the *original* string and
70+
// found a charset, so "refused" was never true and the CRLF went into the header verbatim.
71+
// text/html; charset=x<CR><LF><CR><LF><script>... split the response in two, which is the one
72+
// thing this class exists to prevent.
73+
if (isUntrustworthy(mimeType)) {
74+
return OCTET_STREAM to null
75+
}
6776
val type = safeType(mimeType)
6877
return type to (declaredCharset(mimeType) ?: defaultCharsetFor(type))
6978
}
7079

80+
/**
81+
* Whether the stored value cannot be trusted into a header at all.
82+
*
83+
* A control character anywhere in it -- type, parameter name, or parameter value -- means the
84+
* whole string is refused rather than repaired. `ContentTypes.value` comes from a database a
85+
* debug build swaps in from shared storage, and `WebServer` writes the result with `println`.
86+
*/
87+
private fun isUntrustworthy(mimeType: String): Boolean = mimeType.any { it.isISOControl() }
88+
7189
/**
7290
* [mimeType]'s media type, normalized and safe to put in a header.
7391
*
@@ -205,20 +223,25 @@ object ContentTypeHeaders {
205223
* were, and exactly one charset -- the same one [typeAndCharset] hands the other transport.
206224
*/
207225
fun headerValue(mimeType: String): String {
208-
val (type, charset) = typeAndCharset(mimeType)
209226
// Nothing from a refused value is re-emitted: its parameters are exactly where the control
210-
// characters would have been.
211-
if (type == OCTET_STREAM && charset == null) {
227+
// characters would have been. Asked directly rather than deduced from the pair below, which
228+
// also stops a legitimately stored `application/octet-stream; name=file.bin` from being read
229+
// as a refusal and losing its parameters.
230+
if (isUntrustworthy(mimeType)) {
212231
return OCTET_STREAM
213232
}
233+
val (type, charset) = typeAndCharset(mimeType)
214234
return buildString {
215235
append(type)
216236
for ((name, value) in parameters(mimeType)) {
217237
if (name.equals("charset", ignoreCase = true)) continue
218238
append("; ").append(name)
219239
if (value != null) append('=').append(quoteIfNeeded(value))
220240
}
221-
if (charset != null) append("; charset=").append(charset)
241+
// quoteIfNeeded, like every other parameter value: `parameters` strips the quotes from a
242+
// stored charset="utf-8; x=y" and returned `utf-8; x=y`, which appended raw became a
243+
// charset plus a smuggled second parameter.
244+
if (charset != null) append("; charset=").append(quoteIfNeeded(charset))
222245
}
223246
}
224247

‎common/src/test/java/com/itsaky/androidide/utils/ContentTypeHeadersTest.kt‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,45 @@ class ContentTypeHeadersTest {
136136
assertThat(ContentTypeHeaders.typeAndCharset("text/html; charset=iso-8859-1"))
137137
.isEqualTo("text/html" to "iso-8859-1")
138138
}
139+
140+
// The hole the previous revision left: safeType refused the type, but the charset parameter was
141+
// re-parsed from the original string, so a CRLF inside it reached the header anyway. WebServer
142+
// writes this with println, so that is a split response with an attacker-chosen body.
143+
@Test
144+
fun `a control character in the charset parameter refuses the whole value`() {
145+
val split = "text/html; charset=x\r\n\r\n<script>alert(1)</script>"
146+
147+
val header = ContentTypeHeaders.headerValue(split)
148+
149+
assertThat(header).isEqualTo("application/octet-stream")
150+
assertThat(header).doesNotContain("\r")
151+
assertThat(header).doesNotContain("\n")
152+
assertThat(ContentTypeHeaders.typeAndCharset(split).second).isNull()
153+
}
154+
155+
@Test
156+
fun `a control character in a parameter name refuses the whole value`() {
157+
val header = ContentTypeHeaders.headerValue("text/html; x\r\nX-Injected: y=1")
158+
159+
assertThat(header).isEqualTo("application/octet-stream")
160+
}
161+
162+
// parameters() strips the quotes, so appending the value raw turned one charset into a charset
163+
// plus a second parameter the client would honour.
164+
@Test
165+
fun `a quoted charset cannot smuggle a second parameter`() {
166+
val header = ContentTypeHeaders.headerValue("""text/html; charset="utf-8; x=y"""")
167+
168+
// Quoted, so the recipient reads one charset whose value happens to contain a semicolon --
169+
// not a charset plus a second parameter. Appended raw it was the latter.
170+
assertThat(header).isEqualTo("""text/html; charset="utf-8; x=y"""")
171+
}
172+
173+
// Refusal used to be read off the returned string, so a genuine octet-stream looked refused.
174+
@Test
175+
fun `a stored octet-stream keeps its own parameters`() {
176+
val header = ContentTypeHeaders.headerValue("application/octet-stream; name=file.bin")
177+
178+
assertThat(header).contains("name=file.bin")
179+
}
139180
}

0 commit comments

Comments
 (0)