Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the function signature overloads of SubtleCrypto#exportKey more flexible #1593

Merged
merged 11 commits into from
Jul 13, 2023
1 change: 1 addition & 0 deletions baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21839,6 +21839,7 @@ interface SubtleCrypto {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */
exportKey(format: "jwk", key: CryptoKey): Promise<JsonWebKey>;
exportKey(format: Exclude<KeyFormat, "jwk">, key: CryptoKey): Promise<ArrayBuffer>;
exportKey(format: KeyFormat, key: CryptoKey): Promise<ArrayBuffer | JsonWebKey>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */
generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
generateKey(algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKey>;
Expand Down
1 change: 1 addition & 0 deletions baselines/serviceworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5016,6 +5016,7 @@ interface SubtleCrypto {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */
exportKey(format: "jwk", key: CryptoKey): Promise<JsonWebKey>;
exportKey(format: Exclude<KeyFormat, "jwk">, key: CryptoKey): Promise<ArrayBuffer>;
exportKey(format: KeyFormat, key: CryptoKey): Promise<ArrayBuffer | JsonWebKey>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */
generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
generateKey(algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKey>;
Expand Down
1 change: 1 addition & 0 deletions baselines/sharedworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4827,6 +4827,7 @@ interface SubtleCrypto {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */
exportKey(format: "jwk", key: CryptoKey): Promise<JsonWebKey>;
exportKey(format: Exclude<KeyFormat, "jwk">, key: CryptoKey): Promise<ArrayBuffer>;
exportKey(format: KeyFormat, key: CryptoKey): Promise<ArrayBuffer | JsonWebKey>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */
generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
generateKey(algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKey>;
Expand Down
1 change: 1 addition & 0 deletions baselines/webworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5322,6 +5322,7 @@ interface SubtleCrypto {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */
exportKey(format: "jwk", key: CryptoKey): Promise<JsonWebKey>;
exportKey(format: Exclude<KeyFormat, "jwk">, key: CryptoKey): Promise<ArrayBuffer>;
exportKey(format: KeyFormat, key: CryptoKey): Promise<ArrayBuffer | JsonWebKey>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */
generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
generateKey(algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKey>;
Expand Down
7 changes: 4 additions & 3 deletions inputfiles/overridingTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -1572,17 +1572,18 @@
},
"exportKey": {
"additionalSignatures": [
"exportKey(format: \"jwk\", key: CryptoKey): Promise<JsonWebKey>"
"exportKey(format: \"jwk\", key: CryptoKey): Promise<JsonWebKey>",
"exportKey(format: Exclude<KeyFormat, \"jwk\">, key: CryptoKey): Promise<ArrayBuffer>"
],
"signature": {
"0": {
"param": [
{
"name": "format",
"overrideType": "Exclude<KeyFormat, \"jwk\">"
"overrideType": "KeyFormat"
}
],
"overrideType": "Promise<ArrayBuffer>"
"overrideType": "Promise<ArrayBuffer | JsonWebKey>"
}
}
},
Expand Down
9 changes: 9 additions & 0 deletions unittests/files/keyusage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
function assertType<T>(_x: T) {}

const mockKey = {} as CryptoKey;
assertType<Promise<ArrayBuffer | JsonWebKey>>(crypto.subtle.exportKey('' as KeyFormat, mockKey));
Copy link
Contributor

@saschanaz saschanaz Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too loose as either Promise<ArrayBuffer> or Promise<JsonWebKey> will match the type. But not sure how to make it strict enough 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can test whether it works for both as Promise<ArrayBuffer> and as Promise<JsonWebKey>, like:

crypto.subtle.exportKey('' as KeyFormat, mockKey) as Promise<ArrayBuffer>; // type includes ArrayBuffer
crypto.subtle.exportKey('' as KeyFormat, mockKey) as Promise<JsonWebKey>; // type also includes JsonWebKey

Copy link
Contributor Author

@steveluscher steveluscher Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too loose as either Promise or Promise will match the type

Isn't that exactly the point when format is not known to yield one or the other?

How about this?

assertType<Promise<ArrayBuffer | JsonWebKey>>(
  crypto.subtle
    .exportKey("" as KeyFormat, mockKey)
    .then((ambiguousExportedKeyData) =>
      ambiguousExportedKeyData instanceof ArrayBuffer
        ? (ambiguousExportedKeyData satisfies ArrayBuffer)
        : (ambiguousExportedKeyData satisfies JsonWebKey)
    )
);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that exactly the point when format is not known to yield one or the other?

Yes but that'll never fail when the return type regresses to either ArrayBuffer or JsonWebKey.

How about this?

Sounds good!

assertType<Promise<JsonWebKey>>(crypto.subtle.exportKey('jwk', mockKey));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also check what happens if the input is 'jwk' | 'pkcs8'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100%. I'm on it.

assertType<Promise<ArrayBuffer>>(crypto.subtle.exportKey('pkcs8', mockKey));
assertType<Promise<ArrayBuffer>>(crypto.subtle.exportKey('raw', mockKey));
assertType<Promise<ArrayBuffer>>(crypto.subtle.exportKey('spki', mockKey));

const usageInline = crypto.subtle.generateKey({
name: "AES-GCM",
length: 256,
Expand Down