Skip to content

json_encode: Escape U+2028 and U+2029. #1701

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

Closed
wants to merge 1 commit into from
Closed
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 ext/json/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ static PHP_MINIT_FUNCTION(json)
PHP_JSON_REGISTER_CONSTANT("JSON_UNESCAPED_UNICODE", PHP_JSON_UNESCAPED_UNICODE);
PHP_JSON_REGISTER_CONSTANT("JSON_PARTIAL_OUTPUT_ON_ERROR", PHP_JSON_PARTIAL_OUTPUT_ON_ERROR);
PHP_JSON_REGISTER_CONSTANT("JSON_PRESERVE_ZERO_FRACTION", PHP_JSON_PRESERVE_ZERO_FRACTION);
PHP_JSON_REGISTER_CONSTANT("JSON_UNESCAPED_LINE_TERMINATORS", PHP_JSON_UNESCAPED_LINE_TERMINATORS);

/* options for json_decode */
PHP_JSON_REGISTER_CONSTANT("JSON_OBJECT_AS_ARRAY", PHP_JSON_OBJECT_AS_ARRAY);
Expand Down
11 changes: 10 additions & 1 deletion ext/json/json_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ static void php_json_escape_string(smart_str *buf, char *s, size_t len, int opti

do {
us = (unsigned char)s[pos];
if (us >= 0x80 && !(options & PHP_JSON_UNESCAPED_UNICODE)) {
if (us >= 0x80 && (!(options & PHP_JSON_UNESCAPED_UNICODE) || us == 0xE2)) {
/* UTF-8 character */
us = php_next_utf8_char((const unsigned char *)s, len, &pos, &status);
if (status != SUCCESS) {
Expand All @@ -332,6 +332,15 @@ static void php_json_escape_string(smart_str *buf, char *s, size_t len, int opti
smart_str_appendl(buf, "null", 4);
return;
}
/* Escape U+2028/U+2029 line terminators, UNLESS both
JSON_UNESCAPED_UNICODE and
JSON_UNESCAPED_LINE_TERMINATORS were provided */
if ((options & PHP_JSON_UNESCAPED_UNICODE)
&& ((options & PHP_JSON_UNESCAPED_LINE_TERMINATORS)
|| us < 0x2028 || us > 0x2029)) {
smart_str_appendl(buf, &s[pos - 3], 3);
continue;
}
/* From http://en.wikipedia.org/wiki/UTF16 */
if (us >= 0x10000) {
unsigned int next_us;
Expand Down
1 change: 1 addition & 0 deletions ext/json/php_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typedef enum {
#define PHP_JSON_UNESCAPED_UNICODE (1<<8)
#define PHP_JSON_PARTIAL_OUTPUT_ON_ERROR (1<<9)
#define PHP_JSON_PRESERVE_ZERO_FRACTION (1<<10)
#define PHP_JSON_UNESCAPED_LINE_TERMINATORS (1<<11)

/* json_decode() options */
#define PHP_JSON_OBJECT_AS_ARRAY (1<<0)
Expand Down
36 changes: 36 additions & 0 deletions ext/json/tests/json_encode_u2028_u2029.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
json_encode() tests for U+2028, U+2029
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php
var_dump(json_encode(array("a\xC3\xA1b")));
var_dump(json_encode(array("a\xC3\xA1b"), JSON_UNESCAPED_UNICODE));
var_dump(json_encode("a\xE2\x80\xA7b"));
var_dump(json_encode("a\xE2\x80\xA7b", JSON_UNESCAPED_UNICODE));
var_dump(json_encode("a\xE2\x80\xA8b"));
var_dump(json_encode("a\xE2\x80\xA8b", JSON_UNESCAPED_UNICODE));
var_dump(json_encode("a\xE2\x80\xA8b", JSON_UNESCAPED_LINE_TERMINATORS));
var_dump(json_encode("a\xE2\x80\xA8b", JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS));
var_dump(json_encode("a\xE2\x80\xA9b"));
var_dump(json_encode("a\xE2\x80\xA9b", JSON_UNESCAPED_UNICODE));
var_dump(json_encode("a\xE2\x80\xA9b", JSON_UNESCAPED_LINE_TERMINATORS));
var_dump(json_encode("a\xE2\x80\xA9b", JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS));
var_dump(json_encode("a\xE2\x80\xAAb"));
var_dump(json_encode("a\xE2\x80\xAAb", JSON_UNESCAPED_UNICODE));
?>
--EXPECT--
string(12) "["a\u00e1b"]"
string(8) "["aáb"]"
string(10) ""a\u2027b""
string(7) ""a‧b""
string(10) ""a\u2028b""
string(10) ""a\u2028b""
string(10) ""a\u2028b""
string(7) ""a
b""
string(10) ""a\u2029b""
string(10) ""a\u2029b""
string(10) ""a\u2029b""
string(7) ""a
b""
string(10) ""a\u202ab""
string(7) ""a‪b""