From d283eeb535601414baf42fd37aa8ec517e187507 Mon Sep 17 00:00:00 2001 From: Oleg Bespalov Date: Wed, 17 Apr 2024 16:53:04 +0200 Subject: [PATCH] refactor: DeriveBits moving variables to isolate state and check for the nil --- webcrypto/subtle_crypto.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/webcrypto/subtle_crypto.go b/webcrypto/subtle_crypto.go index 661a4e9..31983e0 100644 --- a/webcrypto/subtle_crypto.go +++ b/webcrypto/subtle_crypto.go @@ -7,6 +7,7 @@ import ( "hash" "github.com/dop251/goja" + "go.k6.io/k6/js/common" "go.k6.io/k6/js/modules" "go.k6.io/k6/js/promises" ) @@ -634,8 +635,10 @@ func (sc *SubtleCrypto) DeriveKey( func (sc *SubtleCrypto) DeriveBits(algorithm goja.Value, baseKey goja.Value, length int) *goja.Promise { rt := sc.vu.Runtime() - var publicKey, privateKey CryptoKey - var algName string + var ( + publicKey, privateKey CryptoKey + deriver bitsDeriver + ) err := func() error { if err := rt.ExportTo(baseKey, &privateKey); err != nil { @@ -647,18 +650,25 @@ func (sc *SubtleCrypto) DeriveBits(algorithm goja.Value, baseKey goja.Value, len } alg := algorithm.ToObject(rt) + if common.IsNullish(alg) { + return NewError(InvalidAccessError, "algorithm is not an object") + } pcValue := alg.Get("public") if err := rt.ExportTo(pcValue, &publicKey); err != nil { return NewError(InvalidAccessError, "algorithm's public is not a valid CryptoKey") } - algName = alg.Get("name").String() - if publicKey.Type != PublicCryptoKeyType { return NewError(InvalidAccessError, "algorithm's public key is not a public key") } + var err error + deriver, err = newBitsDeriver(alg.Get("name").String()) + if err != nil { + return err + } + return nil }() @@ -671,11 +681,6 @@ func (sc *SubtleCrypto) DeriveBits(algorithm goja.Value, baseKey goja.Value, len callback := sc.vu.RegisterCallback() go func() { result, err := func() ([]byte, error) { - deriver, err := newBitsDeriver(algName) - if err != nil { - return nil, err - } - b, err := deriver(privateKey, publicKey) if err != nil { return nil, NewError(OperationError, err.Error())