Skip to content

SubtleCrypto.verify should return js.Promise[Boolean] #660

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

Closed
armanbilge opened this issue Dec 31, 2021 · 5 comments
Closed

SubtleCrypto.verify should return js.Promise[Boolean] #660

armanbilge opened this issue Dec 31, 2021 · 5 comments

Comments

@armanbilge
Copy link
Member

Reported by @bblfish on Discord.

def verify(algorithm: AlgorithmIdentifier, key: CryptoKey, signature: BufferSource,
data: BufferSource): js.Promise[js.Any] = js.native

https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/verify

There may be other signatures that can be improved in the crypto APIs as well.

@bblfish
Copy link
Contributor

bblfish commented Dec 31, 2021

Thanks for writing this up. Another example is

  def sign(
    algorithm: AlgorithmIdentifier, key: CryptoKey, data: BufferSource
): js.Promise[js.Any] = js.native

that should return a Promise[ArrayBuffer] https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/sign#return_value

@zetashift
Copy link
Contributor

So the spec/idl actually says Promise<any>: https://w3c.github.io/webcrypto/#webidl-451956974
But when reading the description of a few methods, it seems that they are somewhat typed, for example except for verify and sign, there is encrypt: https://www.w3.org/TR/WebCryptoAPI/#SubtleCrypto-method-encrypt

Let ciphertext be the result of performing the encrypt operation specified by normalizedAlgorithm using algorithm and key and with data as plaintext.

Resolve promise with ciphertext.

TypeScript uses ArrayBuffer for a few cases instead of any: https://github.com/microsoft/TypeScript/blob/226dd0b7bf5cb5e5bb4dc34ab0e8e14f408f3e20/lib/lib.webworker.d.ts#L3010

Personally, I think we should type them as TypeScript...but spec is spec :P

@armanbilge
Copy link
Member Author

Thanks for looking into that! You're right, good catch.

Let result be the result of performing the verify operation specified by normalizedAlgorithm using key, algorithm and signature and with data as message.

I guess the fundamental issue is that these methods have to support various cryptography algorithms, including ones that may be introduced in the future.

In theory, the "verify operation" for an algorithm could return something that is not a Boolean. I'm not sure if there are any concrete examples of that, but clearly they are not ruling it out.

IMHO we should leave this as-is. My opinion is that Scala.js DOM's purpose is to facade according to the spec without getting too tangled up in "usability" concerns (see related discussion in #481).

Instead to "fix" these issues the door is open for wrapper libraries to emerge, with idiomatic Scala APIs. e.g. https://github.com/typelevel/bobcats

@armanbilge armanbilge closed this as not planned Won't fix, can't repro, duplicate, stale Nov 2, 2022
@zetashift
Copy link
Contributor

zetashift commented Nov 2, 2022

Understandable, however there is one actual mistake in the signatures as far as I can see, according to the spec deriveBits(it should return an Promise of ArrayBuffer:

Promise<ArrayBuffer> deriveBits(AlgorithmIdentifier algorithm,
CryptoKey baseKey,
unsigned long length);

and in scala-js-dom it's a js.Any:

def deriveBits(algorithm: AlgorithmIdentifier, baseKey: CryptoKey, length: Double): js.Promise[js.Any] = js.native

I can open a PR if you like, should I make a new issue for it?

@armanbilge
Copy link
Member Author

@zetashift great, let's fix that one then!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants