Skip to content

fix: bound backslash scan to the base64 string in JSONReaderUTF8.readBase64 (issue #7967) - #7972

Open
divine-punishment wants to merge 1 commit into
alibaba:mainfrom
divine-punishment:fix/base64-slash-scan-bounds
Open

divine-punishment wants to merge 1 commit into
alibaba:mainfrom
divine-punishment:fix/base64-slash-scan-bounds

Conversation

@divine-punishment

Copy link
Copy Markdown

What this PR does / why we need it?

Fixes #7967.

With Base64StringAsByteArray enabled (the default in the fastjson 1.x compatibility package), deserializing a byte[] field whose base64 value is followed by escaped content elsewhere in the document failed with invalid base64 string.

Root cause: JSONReaderUTF8.readBase64 scans for a backslash to reject invalid escaped content, but searched from the value start to the end of the document instead of up to the closing quote of the value. A backslash belonging to a later field (e.g. a nested JSON string containing \" in a sibling field) was therefore treated as if it were part of the base64 value.

Summary of your change

  • JSONReaderUTF8.readBase64: only reject when the backslash lies within this string (before the closing quote, slashIndex < index), consistent with how JSONReaderASCII.readFieldNameHashCode bounds the same scan. Scanning to the end is kept for the vectorized indexOfSlash fast path.
  • A backslash inside the base64 string itself is still rejected (legal base64 alphabet never contains \).
  • New regression test Issue7967 covering both the byte[] and String parse paths of the issue scenario, plus the still-rejected backslash-inside-string case.

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed. (Bug fix only, no API change)
中文说明

修复内容

Fixes #7967:Base64StringAsByteArray 开启时(fastjson 1.x 兼容包默认开启),byte[] 字段的 base64 值之后文档中只要存在转义内容(如兄弟字段嵌套 JSON 字符串中的 \"),反序列化即报 invalid base64 string。

根因:JSONReaderUTF8.readBase64 扫描反斜杠以拒绝非法转义内容,但范围从值起点直到整个文档结尾,而非当前值的结束引号;后续字段的转义反斜杠被误判为 base64 值的一部分。

修改方式

  • 仅当反斜杠位于本字符串内部(slashIndex < index,结束引号之前)才拒绝;与 JSONReaderASCII.readFieldNameHashCode 对同一函数的边界用法保持一致。扫到文档结尾保留,维持 indexOfSlash 向量化快路径
  • base64 字符串内部的反斜杠仍然拒绝(合法 base64 字母表不含 \)
  • 新增回归测试 Issue7967:issue 场景的 byte[]/String 双路径 + 字符串内反斜杠仍拒绝

…Base64

With Base64StringAsByteArray enabled (the default in the fastjson 1.x
compatibility package), deserializing a byte[] field whose base64 value is
followed by escaped content elsewhere in the document failed with
'invalid base64 string' (issue alibaba#7967).

readBase64 scans for a backslash to reject invalid escaped content, but
searched from the value start to the END of the document instead of up to
the closing quote. A backslash belonging to a later field (e.g. a nested
JSON string containing \" in a sibling field) was therefore treated as
part of the base64 value.

Only reject when the backslash lies within this string (before the closing
quote), consistent with how JSONReaderASCII bounds the same scan in
readFieldNameHashCode. Scanning to the end is kept for the vectorized
indexOfSlash fast path.

A backslash inside the base64 string itself is still rejected.
@divine-punishment
divine-punishment force-pushed the fix/base64-slash-scan-bounds branch from dada094 to d1cc358 Compare November 27, 2026 06:33

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] byte[] 字段之后存在转义字符(如嵌套 JSON 字符串中的 \")时,反序列化报 invalid base64 string

1 participant