]> git.kaiwu.me - njs.git/commit
WebCrypto: fix use after free of BufferSource arguments master
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 13 Nov 2026 00:49:01 +0000 (16:49 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 3 Dec 2026 17:38:00 +0000 (09:38 -0800)
commit7dffbf53cf9b35433d527c31f8c45d850d9f9cec
treefb2b71b9562288a35b9c741de86276f44e32d5d4
parent82dd8d90e60a4ab0c62a75ad386bfc8a3c630580
WebCrypto: fix use after free of BufferSource arguments

Previously, the QuickJS WebCrypto methods took borrowed pointers to
the bytes of BufferSource arguments and algorithm members, and then
read further algorithm members.  Getters and valueOf() of those
members could detach or resize the underlying ArrayBuffer, or drop the
last reference to a temporary buffer, so OpenSSL read freed memory.
This affected encrypt(), decrypt(), sign(), verify(), deriveBits(),
deriveKey(), importKey(), wrapKey() and unwrapKey().

Now all algorithm members are read before the bytes of the data
argument are taken, and no JavaScript code runs while the bytes are
in use, avoiding copies of inputs of up to 2 GiB.  BufferSource
algorithm members (iv, counter, additionalData, salt, info) are
copied when read, and members are read in lexicographic order, as
WebIDL dictionary conversion does.  Detached buffers are rejected
with TypeError, matching other njs modules.

The issue was introduced in 75ca26f8 (0.8.10).
external/qjs_webcrypto_module.c
test/webcrypto/buffer_lifetime.t.mjs [new file with mode: 0644]