Skip to content

Consider reordering reading arguments to avoid copies #422

Closed

Description

@twiss

The current spec says, in most of its methods, some variant of:

  1. Let data be the result of getting a copy of the bytes held by the data parameter

  2. Let normalizedAlgorithm be the result of normalizing an algorithm, with alg set to the algorithm parameter

which in turn ends up accessing properties of algorithm, which might have getters (which might mutate the data). So, we need to copy the data before doing anything else, which may be inefficient, especially if we want to add a synchronous version of the API (#167).

So, we may want to swap these operations, if that turns out to be web-compatible.


This issue was split out from #167 (comment) and following comments:

The JS object parameters must account for custom getters (which might mutate everything), which means the exact order that parameters are accessed must be specified. As specified, the input buffers are copied before reading the getters, which is another forced copy.

(...) by changing the order of operations. I strongly doubt that any user of the API is modifying the input data in algorithm object getters.

I would also be surprised if anything but (I hope!) WPT is sensitive to this. Though resolving the algorithm first means we tolerate arbitrary modifications to the buffers before we do the operation. I don't know if it's possible for those buffers to somehow stop being buffers or do something extra weird in between the two, such that some check by the IDL layer becomes invalidated.

(I actually don't know. I'm not an expert on JS state and the assumptions made by IDL layers. Probably need to talk to an expert there. What happens if the algorithm object has a custom property that, on access, transfers the underlying buffer to a message port. Does that put the object in some kind of bad state? Or if it messes with the prototype?)

Activity

  1. twiss commented on Jan 27, 2026

    @twiss
    MemberAuthor

    From a cursory glance, WPT does test mutating the input buffers after the function returns (but before the returned promise resolves), but not inside getters of the algorithm object. And, it seems browsers are not consistent here:

    let data = new Uint8Array(1);
    console.log(new Uint8Array(await crypto.subtle.digest({
        get name() {
            data[0] = 1;
            return 'SHA-256';
        }
    }, data)));
    // Chrome: Uint8Array(32) [ 110, 52, 11, 156, 255, 179, 122, 152, 156, 165, … ]
    // Firefox and Safari: Uint8Array(32) [ 75, 245, 18, 47, 52, 69, 84, 197, 59, 222, … ]

    I.e. only Chrome implements this per spec. So if we want to change this it's likely to be web-compatible.

    Though resolving the algorithm first means we tolerate arbitrary modifications to the buffers before we do the operation. I don't know if it's possible for those buffers to somehow stop being buffers or do something extra weird in between the two, such that some check by the IDL layer becomes invalidated.

    AFAICT, the IDL layer checks depend on internal slots of the object, rather than anything modifiable by javascript, so I think we should be good there as well.

  2. davidben commented on Jan 27, 2026

    @davidben
    Collaborator

    NB: This only avoids copies when combined with an implementation that does the cryptography operation on the calling thread immediately, i.e. where the async was pointless. :-)

    If your WebCrypto implementation is actually async, then you need to make a copy, to guard against the buffer changing before you get around to doing the operation.

  3. twiss commented on Jan 27, 2026

    @twiss
    MemberAuthor

    Yeah. At this point another other argument for changing this could just be that it aligns the spec with the majority of implementations :)

    I'll make a PR at some point, if there are no objections.

  4. annevk commented on Jan 28, 2026

    @annevk
    Member

    Oh yeah, if you effectively implement your own bindings (which normalize seems to do) doing that early is very much preferable so anything that can throw or side effect is dealt with.

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