Skip to content

[Bug]: SHA2 truncates LONGTEXT input to 65,535 bytes #28103

Closed

Description

@XuPeng-SH

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

HEAD (Docker image matrixorigin/matrixone:latest)

Commit ID

d239386

Other Environment Information

- MatrixOne version: v4.2.1
- Docker image ID: sha256:c95d1b468e3fc5f4f2b377da14f4601e7efefa6eb143758636d60114a5bbb436
- Image creation time: 2026-09-03T14:57:40Z
- Client: mysql CLI over the MySQL protocol

Actual Behavior

SHA2(payload, 256) hashes only the first 65,535 bytes when payload is a
160,000-byte LONGTEXT value. The returned digest is exactly the digest of
LEFT(payload, 65535), not the digest of the complete value.

Observed result:

payload_bytes:                  160000
SHA2(payload, 256):             09ab7495d3e61a76f0deb12cb0306f0696cbb17ffc12131368c7a939f12f56d3
SHA2(LEFT(payload,65535), 256): 09ab7495d3e61a76f0deb12cb0306f0696cbb17ffc12131368c7a939f12f56d3
SHA-256 over all 160000 bytes:  e5dcc6668e15a3a785d911afb58f0d038c1412806a2c54d4c6253a6a7060f356

CAST(payload AS TEXT) preserves all 160,000 bytes, but
SHA2(CAST(payload AS TEXT), 256) still returns the 65,535-byte-prefix digest.

Expected Behavior

SHA2(expr, 256) should hash the complete byte sequence represented by
expr. For this input it should return:

e5dcc6668e15a3a785d911afb58f0d038c1412806a2c54d4c6253a6a7060f356

Steps to Reproduce

DROP TABLE IF EXISTS sha2_longtext_repro;
CREATE TABLE sha2_longtext_repro (payload LONGTEXT NOT NULL);

INSERT INTO sha2_longtext_repro
VALUES (CONCAT(
  REPEAT('x', 40000),
  REPEAT('x', 40000),
  REPEAT('x', 40000),
  REPEAT('x', 40000)
));

SELECT
  OCTET_LENGTH(payload) AS payload_bytes,
  LOWER(SHA2(payload, 256)) AS actual_full_digest,
  LOWER(SHA2(LEFT(payload, 65535), 256)) AS prefix_digest,
  OCTET_LENGTH(CAST(payload AS TEXT)) AS cast_text_bytes,
  LOWER(SHA2(CAST(payload AS TEXT), 256)) AS cast_text_digest
FROM sha2_longtext_repro;

Independent expected digest:

python3 -c 'import hashlib; print(hashlib.sha256(b"x" * 160000).hexdigest())'

Additional information

This can silently invalidate integrity checks for persisted JSON, manifests,
WAL payloads, or other values larger than 65,535 bytes. OCTET_LENGTH and a
raw client read both report the full value, so the stored LONGTEXT itself is
not truncated; the truncation occurs in the SHA2 evaluation path.

For comparison, CAST(payload AS CHAR) has length 65,535 while
CAST(payload AS TEXT) has length 160,000 on the same row. The SHA2 result
behaves as if the input had passed through the bounded CHAR representation.

Activity

  1. added
    kind/bug
    on Sep 4, 2026
  2. added a commit that references this issue on Sep 4, 2026
    a6f3988
  3. assigned and unassigned on Sep 4, 2026
  4. slyunnn commented on Sep 4, 2026

    @slyunnn

    Regression and extended verification for #28103 (2026-09-04)

    Verification Environment

    • MatrixOne source: latest origin/main at 8a83e3e31e4297ea14e58afd8c14837513a6cf54 — perf(analyze): publish table-wide cardinality stats (#28067).
    • Related fix: PR #28104, merge commit a6f3988587bdba967a75015a0a12f9bf3a2c9bac — fix(function): preserve wide strings in SHA2; it is included in the verified origin/main.
    • Executed topology: locally built LOG + TN + one CN; the CN MySQL endpoint was 127.0.0.1:26001. SQL used the MySQL/TCP protocol through MOTR.
    • Standard multi-CN endpoints 127.0.0.1:16001, 127.0.0.1:16002, and Proxy 127.0.0.1:6001 were not covered. An existing non-baseline Docker service on 6001 was intentionally not used.
    • Focused CGo unit test passed: TestSHA2PreservesWideStringInputs, TestSHA2StillFormatsScalarInput, and TestSHA2PreservesEveryMySQLStringDomain.

    What Was Covered

    • OK — Original reproduction: a 160000-byte LONGTEXT returns the full SHA2(payload, 256) digest e5dcc6668e15a3a785d911afb58f0d038c1412806a2c54d4c6253a6a7060f356, rather than the LEFT(payload, 65535) digest; full_not_prefix is 1.
    • OK — Happy path and compatibility: CAST(payload AS TEXT) retains 160000 bytes and produces the same full SHA-256 digest; CAST(payload AS CHAR) remains the expected 65535-byte control behavior.
    • OK — Boundary: a 65536-byte MEDIUMTEXT input returns a full SHA-512 digest distinct from LEFT(payload, 65535), covering the first byte beyond the truncation boundary.
    • OK — Binary input: BLOB bytes 0x000102FF are hashed without string truncation or binary-byte loss.
    • OK — Unhappy path: SHA2(NULL, 256) returns NULL.
    • OK — Unit coverage: CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT domains, scalar formatting, wide strings, and binary bytes passed focused CGo tests.
    • Not covered — Multi-CN routing through 127.0.0.1:16001 and 127.0.0.1:16002, or Proxy routing through 127.0.0.1:6001; the defect is a local function-input width conversion issue and does not require those paths for reproduction.

    Findings

    The fix retains the original string domain when evaluating SHA2, so inputs larger than 65535 bytes are no longer coerced to a bounded VARCHAR representation. The original LONGTEXT reproduction and the immediate MEDIUMTEXT boundary both return full-input digests, while scalar, text, binary, and NULL controls remain correct.

    Large-data regression is not needed. This defect depends on the 65535-byte string-width boundary rather than row count, block/batch boundaries, memory or spill, concurrency, data skew, network transfer, or resource scale. The stable 65536-byte boundary payload and the original 160000-byte payload directly cover the relevant execution boundary.

    MOTR / BVT Coverage

    • Added suites/14_issue_regression/t/issue_28103_sha2_longtext.test and suites/14_issue_regression/r/issue_28103_sha2_longtext.result.
    • Recorded and replayed 14_issue_regression.issue_28103_sha2_longtext successfully against the locally built latest-main CN: 13/13 statements passed in both runs.
    • Independent MOTR branch: test/issue-28103-sha2-longtext; commit 159c230 (test: add MOTR regression for #28103), pushed by SSH.
    • The commit was added to the daily MOTR regression PR #142 as cherry-pick 4f4043d. No issue-specific PR was created.

    Final Conclusion

    Fixed. Latest origin/main contains the related fix and passes the original reproduction plus targeted happy-path, unhappy-path, boundary, binary compatibility, and focused unit coverage. Large-data regression is not required for the reasons above.

  5. added 3 commits that reference this issue on Sep 17, 2026
    99823e4
    67e2173
    f8b4981
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugphase/testing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions