GeezerVison is showing github.com — crypto: reject duplicate unknown key usages · nodejs/node@fb627ea
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/util.js +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1175,15 +1175,16 @@ function validateKeyOps(keyOps, usagesSet) { | 1175 | 1175 | if (keyOps === undefined) return; | 1176 | 1176 | validateArray(keyOps, 'keyData.key_ops'); | 1177 | 1177 | let keyOpsMask = 0; | | 1178 | + const seen = new SafeSet(); | 1178 | 1179 | for (let n = 0; n < keyOps.length; n++) { | 1179 | 1180 | const op = keyOps[n]; | | 1181 | + if (seen.has(op)) | | 1182 | + throw lazyDOMException('Duplicate key operation', 'DataError'); | | 1183 | + seen.add(op); | 1180 | 1184 | const opMask = kUsageMasks[op]; | 1181 | 1185 | // Skipping unknown key ops | 1182 | 1186 | if (opMask === undefined) | 1183 | 1187 | continue; | 1184 | | - // Have we seen it already? if so, error | 1185 | | - if (keyOpsMask & opMask) | 1186 | | - throw lazyDOMException('Duplicate key operation', 'DataError'); | 1187 | 1188 | keyOpsMask |= opMask; | 1188 | 1189 |
| 1189 | 1190 | // TODO(@jasnell): RFC7517 section 4.3 strong recommends validating | |
Collapse file Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | + | | 7 | +const assert = require('assert'); | | 8 | +const { subtle } = globalThis.crypto; | | 9 | + | | 10 | +(async () => { | | 11 | + const algorithm = { name: 'HMAC', hash: 'SHA-256' }; | | 12 | + const jwk = { kty: 'oct', k: 'AAAAAAAAAAAAAAAAAAAAAA' }; | | 13 | + await subtle.importKey('jwk', { ...jwk, key_ops: ['sign', 'future'] }, | | 14 | + algorithm, false, ['sign']); | | 15 | + for (const key_ops of [['sign', 'sign'], ['sign', 'future', 'future']]) { | | 16 | + await assert.rejects(subtle.importKey('jwk', { ...jwk, key_ops }, | | 17 | + algorithm, false, ['sign']), | | 18 | + { name: 'DataError' }); | | 19 | + } | | 20 | +})().then(common.mustCall()); |
|