Skip to content

Commit d96143c

Browse files
committed
Throw error for unsupported key algorithms.
Throw error from `AsymmetricKey.getAlgorithm()` if an unsupported encoded key algorithm is found. This may be indirectly called from the constructor and other functions.
1 parent deab0b9 commit d96143c

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# webkms-client ChangeLog
22

3+
## 14.1.2 - 2024-xx-xx
4+
5+
### Changed
6+
- Throw error from `AsymmetricKey.getAlgorithm()` if an unsupported encoded key
7+
algorithm is found. This may be indirectly called from the constructor and
8+
other functions.
9+
310
## 14.1.1 - 2024-07-10
411

512
### Added

lib/AsymmetricKey.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ export class AsymmetricKey {
130130
}
131131

132132
/**
133-
* Gets the JOSE algorithm from a key description.
133+
* Gets the JOSE algorithm from a key description. Throws error if the
134+
* encoded key algorithm is not supported.
134135
*
135136
* @param {object} options - The options to use.
136137
* @param {object} options.keyDescription - A key description.
@@ -142,7 +143,12 @@ export class AsymmetricKey {
142143
// presently all supported key types will have a base58-multikey-header
143144
// value that is only 4 characters long
144145
const prefix = publicKeyMultibase?.slice(0, 4);
145-
return BASE58_MULTIKEY_HEADER_TO_JOSE.get(prefix);
146+
const algorithm = BASE58_MULTIKEY_HEADER_TO_JOSE.get(prefix);
147+
if(!algorithm) {
148+
throw new Error(
149+
`Unsupported key description public key prefix: "${prefix}".`);
150+
}
151+
return algorithm;
146152
}
147153

148154
/**

tests/20-AsymmetricKey.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ const keys = new Map([
1818
['secp256k1', 'did:key:zQ3shokFTS3brHcDQrn82RUDfCZESWL1ZdCEJwekUDPQiYBme']
1919
]);
2020

21+
const badKeys = new Map([
22+
// key from a test with unkonwn 'zUC6' prefix
23+
// eslint-disable-next-line
24+
['Bls12381G2', 'did:key:zUC6zwkczByHEDfap8UJdBwLDeiTYn2xUBq5AhYDnH3Actf9RgdvVF3Rqc2DaYh8j6JysZ6HLidVxM2Y2AhTtM7a5GefA2DGv6JJuSaTJ7ov1jtCnQLmAFYJoovhdzj2kivX9ev'],
25+
]);
26+
2127
describe('AsymmetricKey API', () => {
2228
describe('should create key from keyDescription', () => {
2329
for(const [keyType, did] of keys) {
@@ -38,4 +44,24 @@ describe('AsymmetricKey API', () => {
3844
});
3945
}
4046
});
47+
describe('should fail for bad keys', () => {
48+
for(const [keyType, did] of badKeys) {
49+
it(`key type ${keyType}`, async () => {
50+
let error;
51+
let key;
52+
try {
53+
const keyDescription = {
54+
id: did,
55+
type: keyType,
56+
publicKeyMultibase: did.slice(8)
57+
};
58+
key = new AsymmetricKey({keyDescription});
59+
} catch(e) {
60+
error = e;
61+
}
62+
should.exist(error, 'Expected error to exist');
63+
should.not.exist(key, 'Expected key to not exist');
64+
});
65+
}
66+
});
4167
});

0 commit comments

Comments
 (0)