Skip to content

[Bug]: JSON_QUOTE rejects strings containing control characters #28044

Closed

Description

@Ariznawlll

Description

JSON_QUOTE() cannot handle ordinary UTF-8 strings containing control characters such as U+0000, U+0001, or U+001F. It builds a Go-style \\xNN escape, which is not a JSON escape sequence, then fails while parsing its own generated text.

Environment

  • Branch: main
  • Commit: 17c161a31b47a22e71ca222839143f66717ac365
  • Deployment: local single-CN test service
  • Test date: 2026-09-03
  • MySQL control: isolated MySQL 8.0.45

Steps to reproduce

SELECT JSON_QUOTE(CONCAT('A', CHAR(1), 'B'));
SELECT JSON_QUOTE(CONCAT('A', CHAR(0), 'B'));
SELECT JSON_QUOTE(CONCAT('A', CHAR(31), 'B'));

Actual behavior

All three statements return an invalid-JSON error. For example, CHAR(1) returns:

ERROR 20301 (HY000): invalid input: json text "A\\x01B"

The same result reproduces 3/3 times.

Expected behavior

JSON_QUOTE() should always produce a valid JSON string literal. MySQL 8.0.45 returns the following UTF-8 JSON text, shown as hex:

A + U+0000 + B  -> 22415C75303030304222
A + U+0001 + B  -> 22415C75303030314222
A + U+001F + B  -> 22415C75303031664222

Reference: https://dev.mysql.com/doc/refman/8.0/en/json-creation-functions.html#function_json-quote

Stability and controls

  • Reproducer: 3/3 for U+0000, U+0001, and U+001F.
  • MySQL 8.0.45 control returns valid JSON strings for all three characters.
  • MatrixOne control: printable text and standard newline, tab, backslash, and double-quote escapes work.
  • No DDL or DML is involved.

Code analysis

pkg/sql/plan/function/func_unary.go:JsonQuote uses strconv.Quote(str). For arbitrary control bytes, Go emits \\xNN; JSON permits \\u00NN (or the standard short escapes), not \\xNN. The subsequent types.ParseStringToByteJson() therefore rejects the generated string. JSON-specific escaping must replace Go string quoting here.

Regression coverage

After the fix, add deterministic JSON_QUOTE() cases for U+0000, U+0001, U+001F, newline/tab, backslash, double quote, and a UTF-8 non-ASCII character. Verify each result is valid JSON and round-trips through JSON_UNQUOTE().

Activity

  1. added
    kind/bug
    on Sep 3, 2026
  2. added this to the MO-202609 milestone on Sep 3, 2026
  3. added theissue type on Sep 3, 2026
  4. added
    severity/s1
    and removed on Sep 4, 2026
  5. VioletQwQ-0 commented on Sep 8, 2026

    @VioletQwQ-0
    Collaborator

    【QA 交接|PR #28091 已合并】

    关联合并:PR #28091(merge commit 3d46ea9);当前 main=1cfec4c80134adc5640a3bbf1aef7aaa87a45282。

    本次范围:JSON_QUOTE 对控制字符、UTF-8 合法性和 JSON string escaping 的兼容行为,SQL NULL 必须保持 NULL,不能退化为 binary JSON。

    合并后证据:PR required checks 无失败;本地 exact-main CGo 自测因缺少 cgo/libmo.dylib 阻塞,mo-tester 未安装,未宣称本地 PASS。

    请在合并版本连续复验控制字符、非 ASCII/非法 UTF-8、NULL、常量/列值和 prepared rebinding,并覆盖单 CN、多 CN、重启/读回。PASS 标准是字符串内容、字符集/排序规则和转义结果与 MySQL 8 一致;FAIL 时请保留 SQL、main SHA、重复轮次和实际/期望结果。

  6. VioletQwQ-0 commented on Sep 8, 2026

    @VioletQwQ-0
    Collaborator

    补充 pin:远端 main 已推进到 9f87b62。请以该当前 main 复验;此前列出的合并提交、测试范围和 PASS/FAIL 标准保持不变。该更新不代表 QA PASS。

  7. VioletQwQ-0 commented on Sep 8, 2026

    @VioletQwQ-0
    Collaborator

    再次补充 pin:远端 main 已推进到 9cedf6e。请以该当前 main 复验;此前列出的合并提交、测试范围和 PASS/FAIL 标准保持不变。该更新不代表 QA PASS。

  8. VioletQwQ-0 commented on Sep 8, 2026

    @VioletQwQ-0
    Collaborator

    Automation checkpoint: merged PR #28091 is being validated against current upstream/main 6d44c50. This issue remains open in phase/testing; no terminal tester PASS has been recorded in the current handoff. Please report complete version, environment, scoped scenario results, and terminal PASS/FAIL evidence before any closure decision.

  9. Ariznawlll commented on Sep 9, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:f0c31cd4b830be32442cf329e0a3fb08aa9c16c3
    • 部署方式:本地 standalone MatrixOne,单 CN;本地从该 SHA 构建并启动。
    • 测试日期:2026-09-09

    原问题验证

    在 VARCHAR UTF-8 文本域中构造 A + U+0000/U+0001/U+001F + B,分别验证 JSON_QUOTE 的字节结果、JSON_VALID 和 JSON_UNQUOTE 回环。三轮均得到:

    • 410042 -> 22415C75303030304222 -> 410042;
    • 410142 -> 22415C75303030314222 -> 410142;
    • 411F42 -> 22415C75303031664222 -> 411F42。

    三项 JSON_VALID 均为 1,不再生成旧实现的 \\xNN 非 JSON 转义。

    Happy Path 覆盖

    1. 文本列中三种控制字符均可正确 JSON 转义并回环。
    2. PREPARE/EXECUTE 参数路径以 U+001F 复测,结果为 22415C75303031664222、JSON_VALID=1、回环 411F42,连续 3 次一致。
    3. 普通文本、换行/制表符、反斜杠、双引号和 Unicode 文本控制组均保持 JSON 字符串语义。

    Unhappy Path 覆盖

    1. 裸 CHAR(1) 属于 binary domain,不是本 issue 的 UTF-8 text 输入;JSON_QUOTE 按当前契约返回 ERROR 20301: binary data not supported by json_quote。
    2. 该拒绝后重新读取同一张文本表,仍为 3 行,且三行 JSON_VALID(JSON_QUOTE(v)) 的最小/最大值均为 1;失败没有污染已有文本数据。
    3. 非法 UTF-8/binary domain 的函数级拒绝测试已覆盖,文本路径不会把 binary 拒绝误作用于有效 UTF-8 控制字符。

    白盒验证

    pkg/sql/plan/function 的 TestJsonQuote、TestJsonQuoteRejectsInvalidUTF8、TestJsonQuoteRejectsBinaryDomain、TestJsonQuoteReturnType 在该 main 构建上 -count=3 通过。该覆盖分别验证文本转义、非法 UTF-8 拒绝、binary domain 拒绝和返回类型。

    回归测试

    • 已加入 MOTR PR #152,提交 0f6044d006674821281b7a0c6bd485886c6c0aca。
    • 用例:suites/14_issue_regression/t/issue_28044_json_quote_control_utf8.test。
    • ./motr --record --repeat 3 --do-test '14_issue_regression\\.issue_28044_json_quote_control_utf8' -v:3/3 用例、39/39 语句通过;非 record 相同命令再次 3/3 通过。

    测试证据

    MOTR result 文件固定断言三种控制字符的输入/输出十六进制、JSON 有效性、反序列化回环、prepared 执行,以及 binary 拒绝后的表状态。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugphase/testingseverity/s1

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions