Skip to content
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
24 changes: 14 additions & 10 deletions java/src/json/ext/StringEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,29 +273,33 @@ void encodeBasic(ByteList src) throws IOException {
}
}

protected boolean isUnsafeScriptTag(byte[] ptrBytes, int pos, int len) {
protected boolean isUnsafeScriptTag(byte[] ptrBytes, int pos, int len, int ptr) {
int remaining = len - pos;

if ((remaining >= 4 && ByteBuffer.wrap(ptrBytes, pos, 4).equals(HTML_COMMENT))) {
if (remaining < 4) {
return false;
}

if (ByteBuffer.wrap(ptrBytes, ptr + pos, 4).equals(HTML_COMMENT)) {
return true;
}

pos++;
remaining--;

if (ptrBytes[pos] == '/') {
if (ptrBytes[ptr + pos] == '/') {
pos++;
remaining--;
}

return (
remaining >= 6 &&
(ptrBytes[pos + 0] | 0x20) == 's' &&
(ptrBytes[pos + 1] | 0x20) == 'c' &&
(ptrBytes[pos + 2] | 0x20) == 'r' &&
(ptrBytes[pos + 3] | 0x20) == 'i' &&
(ptrBytes[pos + 4] | 0x20) == 'p' &&
(ptrBytes[pos + 5] | 0x20) == 't'
(ptrBytes[ptr + pos + 0] | 0x20) == 's' &&
(ptrBytes[ptr + pos + 1] | 0x20) == 'c' &&
(ptrBytes[ptr + pos + 2] | 0x20) == 'r' &&
(ptrBytes[ptr + pos + 3] | 0x20) == 'i' &&
(ptrBytes[ptr + pos + 4] | 0x20) == 'p' &&
(ptrBytes[ptr + pos + 5] | 0x20) == 't'
);
}

Expand Down Expand Up @@ -332,7 +336,7 @@ void encode(ByteList src) throws IOException {
break;
}
case 25: {
if (isUnsafeScriptTag(ptrBytes, pos, len)) {
if (isUnsafeScriptTag(ptrBytes, pos, len, ptr)) {
beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1);
escapeAscii(ch, scratch, hexdig);
} else {
Expand Down
2 changes: 1 addition & 1 deletion java/src/json/ext/StringEncoderAsciiOnly.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void encode(ByteList src) throws IOException {
break;
}
case 25: {
if (isUnsafeScriptTag(ptrBytes, pos, len)) {
if (isUnsafeScriptTag(ptrBytes, pos, len, ptr)) {
beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1);
escapeAscii(ch, scratch, hexdig);
} else {
Expand Down
4 changes: 4 additions & 0 deletions test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,10 @@ def run_script_safe_test(ascii_only)
data = "<a href=\"/\">\n</script>"
json = '"<a href=\\"/\\">\\n\\u003c/script>"'
assert_equal json, generate(data, script_safe: true, ascii_only: ascii_only)

data = ("a" * 100 + "</script>")[100..] # JRuby shared string.
json = '"\\u003c/script>"'
assert_equal json, generate(data, script_safe: true, ascii_only: ascii_only)
end

def test_backslash
Expand Down
Loading