Skip to content

Commit fec8010

Browse files
committed
[java] Reject unescaped control characters in JSON strings
RFC 8259 §7 requires characters U+0000..U+001F to be escaped inside JSON strings. readString() previously appended them silently, so inputs containing a literal newline / tab / etc. inside quotes were accepted.
1 parent 3e8e318 commit fec8010

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

java/src/org/openqa/selenium/json/JsonInput.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,12 @@ private String readString() {
585585
readEscape(builder);
586586
break;
587587
default:
588+
// RFC 8259 §7: characters U+0000..U+001F MUST be escaped.
589+
if (c < 0x20) {
590+
throw new JsonException(
591+
String.format(
592+
"Illegal unescaped control character U+%04X in string. %s", c, input));
593+
}
588594
builder.append((char) c);
589595
}
590596
}

java/test/org/openqa/selenium/json/JsonInputTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,22 @@ void shouldReadU_FFFF_AsALiteralCharacterAndNotEndOfInput() {
309309
}
310310
}
311311

312+
@Test
313+
void shouldRejectUnescapedControlCharactersInStrings() {
314+
// RFC 8259 §7: characters U+0000..U+001F MUST be escaped in JSON strings.
315+
// A literal newline / tab / etc. inside quotes is not valid JSON.
316+
try (JsonInput input = newInput("\"a\nb\"")) {
317+
assertThatExceptionOfType(JsonException.class)
318+
.isThrownBy(input::nextString)
319+
.withMessageStartingWith("Illegal unescaped control character");
320+
}
321+
322+
// Escaped equivalents are still fine.
323+
try (JsonInput input = newInput("\"a\\nb\"")) {
324+
assertThat(input.nextString()).isEqualTo("a\nb");
325+
}
326+
}
327+
312328
@Test
313329
void nullInputsShouldCoerceAsNullValues() throws IOException {
314330
try (InputStream is = new ByteArrayInputStream(new byte[0]);

0 commit comments

Comments
 (0)