You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider reordering reading arguments to avoid copies #422
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?)
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:
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.
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.
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.
Activity
twiss commentedon Jan 27, 2026
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:
I.e. only Chrome implements this per spec. So if we want to change this it's likely to be web-compatible.
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.
davidben commentedon Jan 27, 2026
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.
twiss commentedon Jan 27, 2026
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.
annevk commentedon Jan 28, 2026
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.