0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

fix(ext/crypto): various cleanup in JWK imports (#13092)

This aligns all of the error messages, and makes falsey comparisons
more strict.
This commit is contained in:
Luca Casonato 2021-12-15 15:46:29 +01:00 committed by GitHub
parent ee49cce726
commit ec7d90666f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1869,10 +1869,11 @@
case "jwk": { case "jwk": {
// 1. // 1.
const jwk = keyData; const jwk = keyData;
// 2. // 2.
if (jwk.kty !== "oct") { if (jwk.kty !== "oct") {
throw new DOMException( throw new DOMException(
"`kty` member of JsonWebKey must be `oct`", "'kty' property of JsonWebKey must be 'oct'",
"DataError", "DataError",
); );
} }
@ -1880,7 +1881,7 @@
// Section 6.4.1 of RFC7518 // Section 6.4.1 of RFC7518
if (jwk.k === undefined) { if (jwk.k === undefined) {
throw new DOMException( throw new DOMException(
"`k` member of JsonWebKey must be present", "'k' property of JsonWebKey must be present",
"DataError", "DataError",
); );
} }
@ -1927,13 +1928,15 @@
} }
// 6. // 6.
if (keyUsages.length > 0 && jwk.use && jwk.use !== "enc") { if (
keyUsages.length > 0 && jwk.use !== undefined && jwk.use !== "enc"
) {
throw new DOMException("Invalid key usages", "DataError"); throw new DOMException("Invalid key usages", "DataError");
} }
// 7. // 7.
// Section 4.3 of RFC7517 // Section 4.3 of RFC7517
if (jwk.key_ops) { if (jwk.key_ops !== undefined) {
if ( if (
ArrayPrototypeFind( ArrayPrototypeFind(
jwk.key_ops, jwk.key_ops,
@ -1941,7 +1944,7 @@
) !== undefined ) !== undefined
) { ) {
throw new DOMException( throw new DOMException(
"`key_ops` member of JsonWebKey is invalid", "'key_ops' property of JsonWebKey is invalid",
"DataError", "DataError",
); );
} }
@ -1953,16 +1956,16 @@
) )
) { ) {
throw new DOMException( throw new DOMException(
"`key_ops` member of JsonWebKey is invalid", "'key_ops' property of JsonWebKey is invalid",
"DataError", "DataError",
); );
} }
} }
// 8. // 8.
if (jwk.ext === false && extractable == true) { if (jwk.ext === false && extractable === true) {
throw new DOMException( throw new DOMException(
"`ext` member of JsonWebKey is invalid", "'ext' property of JsonWebKey must not be false if extractable is true",
"DataError", "DataError",
); );
} }
@ -2026,21 +2029,20 @@
break; break;
} }
case "jwk": { case "jwk": {
// TODO(@littledivy): Why does the spec validate JWK twice?
const jwk = keyData; const jwk = keyData;
// 2. // 2.
if (jwk.kty !== "oct") { if (jwk.kty !== "oct") {
throw new DOMException( throw new DOMException(
"`kty` member of JsonWebKey must be `oct`", "'kty' property of JsonWebKey must be 'oct'",
"DataError", "DataError",
); );
} }
// Section 6.4.1 of RFC7518 // Section 6.4.1 of RFC7518
if (!jwk.k) { if (jwk.k === undefined) {
throw new DOMException( throw new DOMException(
"`k` member of JsonWebKey must be present", "'k' property of JsonWebKey must be present",
"DataError", "DataError",
); );
} }
@ -2061,7 +2063,7 @@
case "SHA-1": { case "SHA-1": {
if (jwk.alg !== undefined && jwk.alg !== "HS1") { if (jwk.alg !== undefined && jwk.alg !== "HS1") {
throw new DOMException( throw new DOMException(
"`alg` member of JsonWebKey must be `HS1`", "'alg' property of JsonWebKey must be 'HS1'",
"DataError", "DataError",
); );
} }
@ -2070,7 +2072,7 @@
case "SHA-256": { case "SHA-256": {
if (jwk.alg !== undefined && jwk.alg !== "HS256") { if (jwk.alg !== undefined && jwk.alg !== "HS256") {
throw new DOMException( throw new DOMException(
"`alg` member of JsonWebKey must be `HS256`", "'alg' property of JsonWebKey must be 'HS256'",
"DataError", "DataError",
); );
} }
@ -2079,7 +2081,7 @@
case "SHA-384": { case "SHA-384": {
if (jwk.alg !== undefined && jwk.alg !== "HS384") { if (jwk.alg !== undefined && jwk.alg !== "HS384") {
throw new DOMException( throw new DOMException(
"`alg` member of JsonWebKey must be `HS384`", "'alg' property of JsonWebKey must be 'HS384'",
"DataError", "DataError",
); );
} }
@ -2088,7 +2090,7 @@
case "SHA-512": { case "SHA-512": {
if (jwk.alg !== undefined && jwk.alg !== "HS512") { if (jwk.alg !== undefined && jwk.alg !== "HS512") {
throw new DOMException( throw new DOMException(
"`alg` member of JsonWebKey must be `HS512`", "'alg' property of JsonWebKey must be 'HS512'",
"DataError", "DataError",
); );
} }
@ -2099,16 +2101,18 @@
} }
// 7. // 7.
if (keyUsages.length > 0 && jwk.use && jwk.use !== "sign") { if (
keyUsages.length > 0 && jwk.use !== undefined && jwk.use !== "sign"
) {
throw new DOMException( throw new DOMException(
"`use` member of JsonWebKey must be `sign`", "'use' property of JsonWebKey must be 'sign'",
"DataError", "DataError",
); );
} }
// 8. // 8.
// Section 4.3 of RFC7517 // Section 4.3 of RFC7517
if (jwk.key_ops) { if (jwk.key_ops !== undefined) {
if ( if (
ArrayPrototypeFind( ArrayPrototypeFind(
jwk.key_ops, jwk.key_ops,
@ -2116,7 +2120,7 @@
) !== undefined ) !== undefined
) { ) {
throw new DOMException( throw new DOMException(
"`key_ops` member of JsonWebKey is invalid", "'key_ops' property of JsonWebKey is invalid",
"DataError", "DataError",
); );
} }
@ -2128,16 +2132,16 @@
) )
) { ) {
throw new DOMException( throw new DOMException(
"`key_ops` member of JsonWebKey is invalid", "'key_ops' property of JsonWebKey is invalid",
"DataError", "DataError",
); );
} }
} }
// 9. // 9.
if (jwk.ext === false && extractable == true) { if (jwk.ext === false && extractable === true) {
throw new DOMException( throw new DOMException(
"`ext` member of JsonWebKey is invalid", "'ext' property of JsonWebKey must not be false if extractable is true",
"DataError", "DataError",
); );
} }
@ -2390,19 +2394,17 @@
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usages", "SyntaxError");
} }
} else { } else if (
if ( ArrayPrototypeFind(
ArrayPrototypeFind( keyUsages,
keyUsages, (u) =>
(u) => !ArrayPrototypeIncludes(
!ArrayPrototypeIncludes( SUPPORTED_RSA_KEY_USAGES[normalizedAlgorithm.name].public,
SUPPORTED_RSA_KEY_USAGES[normalizedAlgorithm.name].public, u,
u, ),
), ) !== undefined
) !== undefined ) {
) { throw new DOMException("Invalid key usages", "SyntaxError");
throw new DOMException("Invalid key usages", "SyntaxError");
}
} }
// 3. // 3.