Skip to content

bug: native verifyBytes reads mutated message after invocation #464

Open

Description

@harrshita123

Summary

On the native backend, HMAC, ECDSA, RSA-PSS, and RSASSA-PKCS1-v1_5 verifyBytes pass the caller-owned message to Stream.value. The stream is consumed asynchronously, so changing the message immediately after the call can change the verification result. Browser verification copies the bytes when invoked.

Reproduction

final key = await HmacSecretKey.importRawKey(Uint8List(16), Hash.sha256);
final original = Uint8List.fromList([1, 2, 3]);
final signature = await key.signBytes(original);

final message = Uint8List.fromList(original);
final pending = key.verifyBytes(signature, message);
message[0] ^= 0xff;

assert(await pending); // Fails on the native backend.

The reverse mutation also matters: starting verification with an invalid message and changing it to the signed message can return true.

Expected behavior

verifyBytes should verify the message supplied when the method was called, independently of later buffer reuse. This is an input-timing bug; it does not permit forgery without mutation of the caller-owned buffer.

Suggested fix

Snapshot the message before passing it to the native stream verifier. Keep verifyStream consuming chunks as they are delivered. Add regression coverage for both mutation directions across the four algorithms.

Related: #421 and #438 cover mutation of the signature input, rather than the message input.

Activity

  1. jonasfj commented on Nov 6, 2026

    @jonasfj
    Member

    I'm unsure if this, #421 and #438 are bugs?

    If you pass a Uint8List to an async function is it really fair to assume that you can safely mutate the bytes while function is running.

    I'm not sure it is. Because all it really does is force the function to make an unnecessary copy of the bytes -- before it does any await statements.

    We could look into what other APIs in Dart world do. But I think this is a mistake.

  2. harrshita123 commented on Nov 6, 2026

    @harrshita123
    ContributorAuthor

    The Web Crypto specification explicitly copies both signature and data when verify() is called, which is why I expected the same behavior here. The regression test confirms that native verifyBytes can instead use later mutations of data.

    But I overstated the conclusion: verifyBytes does not explicitly promise an input snapshot, and copying is not a general Dart rule. For example, IOSink.add tells callers not to modify a byte list after passing it in. Copying large messages also has a cost.

    I agree this is an API contract decision, not an established security bug. If callers are expected to keep the input unchanged until the Future completes, I’m happy to close the issue and withdraw the change. It would help to document that expectation.

  3. HamdaanAliQuatil commented on Nov 7, 2026

    @HamdaanAliQuatil
    Collaborator

    i’d lean towards requiring inputs to stay unchanged until the future completes, rather than guaranteeing a snapshot.

    this also affects copies we’ve already merged, not just the change proposed here. once we agree on the rule, i’ll go through those and put together a cleanup PR with the docs and tests updated too. i’ll check the IV/counter copies separately so we don’t remove buffers the implementation needs for its own state.

  4. harrshita123 commented on Nov 7, 2026

    @harrshita123
    ContributorAuthor

    i’d lean towards requiring inputs to stay unchanged until the future completes, rather than guaranteeing a snapshot.

    this also affects copies we’ve already merged, not just the change proposed here. once we agree on the rule, i’ll go through those and put together a cleanup PR with the docs and tests updated too. i’ll check the IV/counter copies separately so we don’t remove buffers the implementation needs for its own state.

    I’m comfortable with the rule that callers keep mutable inputs unchanged until the Future completes, provided it is documented for the byte APIs. I agree IV and counter copies should be assessed separately where the implementation needs its own state. I’ll hold off on the proposed verifyBytes change while the contract is settled.

  5. harrshita123 commented on Nov 9, 2026

    @harrshita123
    ContributorAuthor

    @jonasfj I would like to make sure we handle the earlier changes in the way you prefer. Would you want callers to keep mutable byte inputs unchanged until the Future completes, with that rule documented in the API?
    If so, I can help review the copies already merged and coordinate with @HamdaanAliQuatil on a single cleanup PR. We will check IV and counter copies separately, since some may still be needed for internal state.

  6. jonasfj commented on Nov 9, 2026

    @jonasfj
    Member

    If in the future I'm proven wrong here, I'm willing to go back and revisit this 🤣

    But if today we start promising to always copy, then someone might rely on it, and it'll be extremely hard to walk back.

    So I propose that we don't copy.

    We will check IV and counter copies separately, since some may still be needed for internal state.

    Yes, and in most cases these are small and copied into an FFI allocated buffer anyways.

    But if I pass in a HUGE buffer of payload data, I also expect that it'll be copied into FFI allocated buffers chunk by chunk, because if someone gives us 1GB payload, we don't want to allocate a copy.


    The one case where I think there could be something about this Stream<List<int>> I think in package:tar the Stream<List<int>> doesn't actually allocate a new buffer for each chunk it gives you. So it's possible that when we are processing a byte stream Stream<List<int>> we should be copying bytes from the buffer we got from the stream into an FFI buffer in 4096 byte chunks, and then we get to end of the buffer we got from the stream we copy what is remaining into the temporary buffer.

    Such that we don't access the buffer from byte stream outside the message handler (and inside the message handler we handle it sync).

    But I'll also admit that the reuse of the Stream<List<int>> chunk buffers in package:tar have bit me more than once, and probably it's a mistake for a stream to pass buffers that stream listeners are not allowed to hold on to.

  7. HamdaanAliQuatil commented on Nov 10, 2026

    @HamdaanAliQuatil
    Collaborator

    @harrshita123 i've created a fix in #467. In the meantime would you like to pick up #468?

  8. harrshita123 commented on Nov 10, 2026

    @harrshita123
    ContributorAuthor

    @harrshita123 i've created a fix in #467. In the meantime would you like to pick up #468?

    Thanks for handling #467. I can take #468 and will verify the iOS tests on a supported runner.

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