From 3e1baad7d6f7a71b3e8e1bb23d34a6b8107559b9 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sat, 21 Nov 2026 09:29:52 +0530 Subject: [PATCH] Check export support before extractable in exportKey and wrapKey exportKey() and wrapKey() check that the key is extractable before anything else, and the NotSupportedError for an algorithm with no export only comes from the default CryptoKey::Impl::exportKey() after that. HKDF and PBKDF2 keys can only be imported as non-extractable, so they always got InvalidAccessError. The spec checks export support first (exportKey step 6, wrapKey step 11), and the WPT export-key-error-order.https.any.js expects NotSupportedError. In this patch CryptoKey::Impl gets a supportsExport() virtual, false for HKDF and PBKDF2, and a requireExportable() helper that runs both checks in spec order for exportKey() and wrapKey(). Only calls that already fail change, and only in the error type, so there is no compat flag. --- src/workerd/api/crypto/crypto.c++ | 8 ++------ src/workerd/api/crypto/hkdf.c++ | 4 ++++ src/workerd/api/crypto/impl.c++ | 9 +++++++++ src/workerd/api/crypto/impl.h | 11 +++++++++++ src/workerd/api/crypto/pbkdf2.c++ | 8 ++++++-- src/wpt/WebCryptoAPI-test.ts | 11 +---------- 6 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/workerd/api/crypto/crypto.c++ b/src/workerd/api/crypto/crypto.c++ index 870a6e8c67c..1e07da1c732 100644 --- a/src/workerd/api/crypto/crypto.c++ +++ b/src/workerd/api/crypto/crypto.c++ @@ -885,8 +885,7 @@ jsg::Promise> SubtleCrypto::wrapKey(jsg::Lock& js size_t minimumLength; }; - JSG_REQUIRE(key.getExtractable(), DOMInvalidAccessError, "Attempt to export non-extractable ", - key.getAlgorithmName(), " key."); + key.impl->requireExportable(); auto exportedKey = key.impl->exportKey(js, kj::mv(format)); @@ -1027,10 +1026,7 @@ jsg::Promise SubtleCrypto::exportKey( return js.evalNow([&] { requireModernKeyFormat(js, format); - // TODO(someday): Throw a NotSupportedError? The Web Crypto API spec says InvalidAccessError, - // but Web IDL says that's deprecated. - JSG_REQUIRE(key.getExtractable(), DOMInvalidAccessError, "Attempt to export non-extractable ", - key.getAlgorithmName(), " key."); + key.impl->requireExportable(); return key.impl->exportKey(js, format); }); diff --git a/src/workerd/api/crypto/hkdf.c++ b/src/workerd/api/crypto/hkdf.c++ index 1190d820831..e3f4ab307a8 100644 --- a/src/workerd/api/crypto/hkdf.c++ +++ b/src/workerd/api/crypto/hkdf.c++ @@ -64,6 +64,10 @@ class HkdfKey final: public CryptoKey::Impl { DOMOperationError, "HKDF deriveBits failed."); } + bool supportsExport() const override { + return false; + } + kj::StringPtr getAlgorithmName() const override { return "HKDF"; } diff --git a/src/workerd/api/crypto/impl.c++ b/src/workerd/api/crypto/impl.c++ index f7710a8f2d1..3c54016ca19 100644 --- a/src/workerd/api/crypto/impl.c++ +++ b/src/workerd/api/crypto/impl.c++ @@ -231,6 +231,15 @@ kj::EncodingResult> decodeBase64Url(kj::String text) { return kj::decodeBase64(text); } +void CryptoKey::Impl::requireExportable() const { + JSG_REQUIRE(supportsExport(), DOMNotSupportedError, "Unrecognized or unsupported export of \"", + getAlgorithmName(), "\" requested."); + // TODO(someday): Throw a NotSupportedError? The Web Crypto API spec says InvalidAccessError, but + // Web IDL says that's deprecated. + JSG_REQUIRE(isExtractable(), DOMInvalidAccessError, "Attempt to export non-extractable ", + getAlgorithmName(), " key."); +} + bool CryptoKey::Impl::equals(const kj::Array&) const { return false; } diff --git a/src/workerd/api/crypto/impl.h b/src/workerd/api/crypto/impl.h index fa54ee215ab..00e14dbddbe 100644 --- a/src/workerd/api/crypto/impl.h +++ b/src/workerd/api/crypto/impl.h @@ -187,6 +187,11 @@ class CryptoKey::Impl { bool isExtractable() const { return extractable; } + + // Throws the errors exportKey() and wrapKey() raise for a key they can't export, in the order + // the spec checks them: NotSupportedError, then InvalidAccessError. + void requireExportable() const; + CryptoKeyUsageSet getUsages() const { return usages; } @@ -259,6 +264,12 @@ class CryptoKey::Impl { return decrypt(js, kj::mv(algorithm), wrappedKey); } + // Whether the algorithm supports the Web Crypto export key operation. Override it to return + // false in any Impl that doesn't override exportKey(). + virtual bool supportsExport() const { + return true; + } + virtual SubtleCrypto::ExportKeyData exportKey(jsg::Lock& js, kj::StringPtr format) const { JSG_FAIL_REQUIRE(DOMNotSupportedError, "Unrecognized or unsupported export of \"", getAlgorithmName(), "\" requested."); diff --git a/src/workerd/api/crypto/pbkdf2.c++ b/src/workerd/api/crypto/pbkdf2.c++ index 8563828d5ee..80b9a016cdf 100644 --- a/src/workerd/api/crypto/pbkdf2.c++ +++ b/src/workerd/api/crypto/pbkdf2.c++ @@ -76,14 +76,18 @@ class Pbkdf2Key final: public CryptoKey::Impl { // algorithms in exportKey(). Later, the allow list was removed, instead assuming that any // algorithm which implemented this method must be allowed. To maintain exactly the // preexisting behavior, then, this implementation had to be commented out. If disallowing this - // was a mistake, we can un-comment this method, but we would need to make sure to add tests - // when we do. + // was a mistake, we can un-comment this method and remove supportsExport() below, but we + // would need to make sure to add tests when we do. // SubtleCrypto::ExportKeyData exportKey(kj::StringPtr format) const override { // JSG_REQUIRE(format == "raw", DOMNotSupportedError, // "Unimplemented key export format \"", format, "\"."); // return kj::heapArray(keyData.asPtr()); // } + bool supportsExport() const override { + return false; + } + kj::StringPtr getAlgorithmName() const override { return "PBKDF2"; } diff --git a/src/wpt/WebCryptoAPI-test.ts b/src/wpt/WebCryptoAPI-test.ts index 349c6a01b5e..4f8cad7e014 100644 --- a/src/wpt/WebCryptoAPI-test.ts +++ b/src/wpt/WebCryptoAPI-test.ts @@ -86,16 +86,7 @@ export default { 'encrypt_decrypt/rsa.js': supportFile, 'encrypt_decrypt/rsa_oaep.https.any.js': {}, 'encrypt_decrypt/rsa_vectors.js': {}, - 'export-key-error-order.https.any.js': { - comment: - 'exportKey() and wrapKey() check extractable before export support, so HKDF and PBKDF2 keys throw InvalidAccessError instead of NotSupportedError', - expectedFailures: [ - 'exportKey() with a HKDF key throws NotSupportedError', - 'wrapKey() with a HKDF key throws NotSupportedError', - 'exportKey() with a PBKDF2 key throws NotSupportedError', - 'wrapKey() with a PBKDF2 key throws NotSupportedError', - ], - }, + 'export-key-error-order.https.any.js': {}, 'generateKey/algorithm_registry.js': {}, 'generateKey/failures.js': {}, 'generateKey/failures_AES-CBC.https.any.js': {},