Skip to content

kotlin-sdk: JNI string arguments are decoded lossily, so malformed UTF-16 is rewritten instead of rejected #6154

Done

Description

@HashEngineering

Problem

rs-unified-sdk-jni reads Kotlin String arguments with env.get_string(s).into(). In the pinned jni 0.21.1, that conversion decodes Java's modified UTF-8 and, when decoding fails, falls back to String::from_utf8_lossy. A string containing an unpaired UTF-16 surrogate therefore reaches Rust as different text, with U+FFFD in place of the surrogate, and the call goes ahead with it.

For a paid memo (shieldedShieldToRecipient, shieldedShieldFromIdentityToRecipient) or a token public note, the recipient can receive text the caller never supplied, as long as the rewritten text still fits the length limit.

A few helpers also treat an unreadable string as absent instead of failing:

  • tokens.rs and wallet_manager.rs read_cstring_opt drop a note, memo, label, user agent or devnet name;
  • queries.rs opt_c_string drops a query filter or cursor, so the query runs without it;
  • sdk.rs to_c_string drops a DAPI address list or quorum URL, so the default is used;
  • the vote path drops contenderIdentityId.

Fix direction

One strict helper in support.rs that every string argument goes through. It copies the UTF-16 code units and decodes them with String::from_utf16, so an unpaired surrogate is an error and supplementary characters still cross intact, independent of how the VM encodes modified UTF-8. Call sites throw DashSDKException (code 1) on a malformed or unreadable string instead of substituting or dropping it.

Tests

  • Rust unit tests for the decoder: well-formed text including a supplementary character and an embedded NUL; lone high, lone low, trailing high and reversed surrogates rejected.
  • Instrumented binding test: a memo with an unpaired surrogate is refused at the JNI boundary; a 32-byte memo of supplementary characters reaches the FFI.

Context

Raised by the automated review on #5896 (read_cstring_opt in funding.rs, used for the recipient memo). The same conversion is used in 13 files of the JNI crate, so it is fixed in one place rather than in that PR. Swift is not affected: it passes UTF-8 C strings.

Activity

  1. added 4 commits that reference this issue on Nov 25, 2026
    234a43e
    5ac93c8
    56b3c3c
    753cbff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions