-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Merge webcrypto into k6 #4188
Merge webcrypto into k6 #4188
Conversation
Define module API to match WebCrypto's
The Web Crypto API specification has a pretty loose set of expectations towards errors. However, making our implementation pass the Web Platform Tests outlined that they're expected to be in a quite specific format. In order to provide a good support for errors in the Web Crypto API, we define its expected errors as custom errors.
Implement support for the crypto.getRandomValues operation
Implement support for the crypto.randomUUID operation
Implement support for the `crypto.subtle.digest` operation
The previous implementation of the normalization function was really thorough, complex, and close to the specification. Up to a level where the complexity was not necessarily worth it compared to the sought end result. This commit reduces the algorithm normalization to its bare minimum requirements.
The pre-existing CryptoKey type was intended as a contract-type declaration of type matching the specification's. It was not bound to any specific implementation, and as a result, not battle tested. This commit realigns its definition with the real-world use-case brought by the `subtle.crypto.generateKey` operation's implementation. The two main changes are the switch from a generic type to a `any` type for the `CryptoKey.handle` and `Cryptoattribute. This attribute is not exposed to the user, and is heavily contextual. We will in practice know from the context what type it is supposed to be castable to. The second change is to define a dedicated generic type for the algorithm field. There are a variety of Algorithm object types defined by the specs, and we need to be able to distinguish and export them to dedicated objects easily for them to be convinient to use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reviewed as much as I could, but the PR is pretty large, so it wasn't a thorough review. I also found it a little unclear what was moved or added newly in this PR.
> [!IMPORTANT] | ||
> We do run the tests based on these examples, that's why we have a simple convention for each example: | ||
> | ||
> * It should do any `console.log`. Since we try to detect that output (log) contain `INFO` keyword. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does "do" mean in this sentence: "It should do any console.log
." I'm curious if it means "do" or "do not."
> * It should do any `console.log`. Since we try to detect that output (log) contain `INFO` keyword. | |
> * It should do any `console.log`. Since we try to detect that output (log) contains the `INFO` keyword. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that contains that mean perform
, like it's expected to have at least one console.log
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could fix this after merge if needed
+ // promise_test(function(test) { | ||
+ // return subtle.deriveBits({name: "ECDH", public: publicKeys[namedCurve]}, privateKeys[namedCurve], 8 * sizes[namedCurve] - 11) | ||
+ // .then(function(derivation) { | ||
+ // assert_true(equalBuffers(derivation, derivations[namedCurve], 8 * sizes[namedCurve] - 11), "Derived correct bits"); | ||
+ // }, function(err) { | ||
+ // assert_unreached("deriveBits failed with error " + err.name + ": " + err.message); | ||
+ // }); | ||
+ // }, namedCurve + " non-multiple of 8 bits"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we consider removing that if it's not needed? I noticed there are some other parts like this commented out, but I didn't highlight all of them before understanding our reasons for keeping the code in the comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's needed, it's commented because comments easier to uncomment once support will be there
@inancgumus yeah, it's big, but don't worry from many aspects it's the code that has been already reviewed and this PR same as the PR of moving browser code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
> [!IMPORTANT] | ||
> We do run the tests based on these examples, that's why we have a simple convention for each example: | ||
> | ||
> * It should do any `console.log`. Since we try to detect that output (log) contain `INFO` keyword. | ||
> * It should NOT `try/catch` exceptions. Since we try to detect if keywords like `"Uncaught"` and `"ERRO"` should not appear in the output (logs). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on your comment, I'd suggest the following for clarity, but I have no strong opinion. The current text is also fine, except for the typo.
> [!IMPORTANT] | |
> We do run the tests based on these examples, that's why we have a simple convention for each example: | |
> | |
> * It should do any `console.log`. Since we try to detect that output (log) contain `INFO` keyword. | |
> * It should NOT `try/catch` exceptions. Since we try to detect if keywords like `"Uncaught"` and `"ERRO"` should not appear in the output (logs). | |
> [!IMPORTANT] | |
> We do run the tests based on these examples; that's why we have a simple convention for each example: | |
> | |
> * Success condition: Example SHOULD output an `INFO` keyword using `console.log` at least once. | |
> * Failure condition: Example SHOULD NOT `try/catch` exceptions so that we can detect failures. The log output SHOULD NOT contain any keywords like `"Uncaught"` and `"ERRO"`. |
@@ -18,9 +18,16 @@ jobs: | |||
with: | |||
go-version: 1.23.x | |||
check-latest: true | |||
- name: Run tests | |||
# TODO: combine WebPlatform tests checkout & patch into the singe step |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# TODO: combine WebPlatform tests checkout & patch into the singe step | |
# TODO: combine WebPlatform tests checkout & patch into the single step |
@@ -34,3 +34,6 @@ script.js | |||
|
|||
/packaging/.env | |||
/packaging/*.gpg | |||
|
|||
# web platform tests | |||
js/modules/k6/webcrypto/tests/wpt/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit; I'd move this line next to the other one related with the WPT tests.
What?
Move xk6-webcrypto inside the k6 codebase
Why?
k6's WebCrypto soon will be promoted out of experimental, and as the team we decided that the codebase should be located in the k6
Checklist
make lint
) and all checks pass.make tests
) and all tests pass.Related PR(s)/Issue(s)
https://github.com/grafana/xk6-webcrypto/
Part of #3154