Skip to content

Commit

Permalink
feat: implement SubtleCrypto.prototype.digest method (#372)
Browse files Browse the repository at this point in the history
Co-authored-by: Trevor Elliott <telliott@fastly.com>
  • Loading branch information
JakeChampion and elliottt committed Jan 24, 2023
1 parent 576a6b3 commit bbe1754
Show file tree
Hide file tree
Showing 20 changed files with 1,424 additions and 349 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependencies.yml
Expand Up @@ -11,5 +11,5 @@ jobs:
steps:
- uses: actions/dependency-review-action@v2.2.0
with:
allow-licenses: Apache-2.0, MIT, BSD-3-Clause, ISC, BSD-2-Clause, MIT OR (CC0-1.0 AND MIT), CC0-1.0 OR MIT OR (CC0-1.0 AND MIT), CC-BY-3.0, CC0-1.0, MIT OR Apache-2.0, MIT AND Apache-2.0, MIT OR WTFPL, BSD-2-Clause OR (MIT OR Apache-2.0), Python-2.0, ISC AND MIT
allow-licenses: Apache-2.0, MIT, BSD-3-Clause, ISC, BSD-2-Clause, MIT OR (CC0-1.0 AND MIT), CC0-1.0 OR MIT OR (CC0-1.0 AND MIT), CC-BY-3.0, CC0-1.0, MIT OR Apache-2.0, MIT AND Apache-2.0, MIT OR WTFPL, BSD-2-Clause OR (MIT OR Apache-2.0), Python-2.0, ISC AND MIT, Apache-2.0 AND MIT
fail-on-scopes: runtime
21 changes: 16 additions & 5 deletions .github/workflows/release-please.yml
Expand Up @@ -43,8 +43,17 @@ jobs:
node-version: 18
cache: 'yarn'

- name: Update yarn lock
run: yarn install --mode=update-lockfile
- run: yarn install --mode=update-lockfile
working-directory: ./documentation
- run: npm run docusaurus docs:version ${{ steps.release.outputs.js-compute--tag_name }}
working-directory: ./documentation
- run: DOCUSAURUS_SSR_CONCURRENCY=1 node --max-old-space-size=20480 ./node_modules/.bin/docusaurus build
working-directory: ./documentation

- run: yarn install --mode=update-lockfile
working-directory: ./documentation/app
- run: npm run build:files
working-directory: ./documentation/app

- name: Committing and push changes
run: |
Expand Down Expand Up @@ -129,14 +138,16 @@ jobs:
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

- run: cd ./docs-app && yarn
- run: yarn
working-directory: ./documentation/app

- name: Set up Fastly CLI
uses: fastly/compute-actions/setup@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
cli_version: '4.3.0'
cli_version: '5.0.0'

- run: cd ./docs-app && yarn deploy
- run: yarn deploy
env:
FASTLY_API_TOKEN: ${{secrets.FASTLY_API_TOKEN}}
working-directory: ./documentation/app
50 changes: 40 additions & 10 deletions c-dependencies/js-compute-runtime/builtins/crypto.cpp
Expand Up @@ -6,6 +6,7 @@
#pragma clang diagnostic pop

#include "crypto.h"
#include "subtle-crypto.h"
#include "xqd.h"

bool is_int_typed_array(JSObject *obj) {
Expand All @@ -24,9 +25,7 @@ namespace builtins {
* of the buffer, but it's far from ideal.
*/
bool Crypto::get_random_values(JSContext *cx, unsigned argc, JS::Value *vp) {
JS::CallArgs args = CallArgsFromVp(argc, vp);
if (!args.requireAtLeast(cx, "crypto.getRandomValues", 1))
return false;
METHOD_HEADER(1)

if (!args[0].isObject() || !is_int_typed_array(&args[0].toObject())) {
JS_ReportErrorUTF8(cx, "crypto.getRandomValues: input must be an integer-typed TypedArray");
Expand Down Expand Up @@ -128,7 +127,7 @@ struct UUID {
Set all the other bits to randomly (or pseudo-randomly) chosen values.
*/
bool Crypto::random_uuid(JSContext *cx, unsigned argc, JS::Value *vp) {
JS::CallArgs args = CallArgsFromVp(argc, vp);
METHOD_HEADER(0)
UUID id;
random_get(reinterpret_cast<int32_t>(&id), sizeof(id));

Expand Down Expand Up @@ -157,19 +156,50 @@ bool Crypto::random_uuid(JSContext *cx, unsigned argc, JS::Value *vp) {
args.rval().setString(str);
return true;
}
JS::PersistentRooted<JSObject *> Crypto::subtle;
JS::PersistentRooted<JSObject *> crypto;

bool Crypto::subtle_get(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(0);
if (self != crypto.get()) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INVALID_INTERFACE, "subtle get",
"Crypto");
return false;
}

args.rval().setObject(*subtle);
return true;
}

const JSFunctionSpec Crypto::methods[] = {
JS_FN("getRandomValues", get_random_values, 1, JSPROP_ENUMERATE),
JS_FN("randomUUID", random_uuid, 0, JSPROP_ENUMERATE), JS_FS_END};

const JSPropertySpec Crypto::properties[] = {JS_PS_END};
const JSPropertySpec Crypto::properties[] = {
JS_PSG("subtle", subtle_get, JSPROP_ENUMERATE),
JS_STRING_SYM_PS(toStringTag, "Crypto", JSPROP_READONLY), JS_PS_END};

bool Crypto::create(JSContext *cx, JS::HandleObject global) {
JS::RootedObject crypto(cx, JS_NewPlainObject(cx));
if (!crypto)
bool Crypto::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_ILLEGAL_CTOR);
return false;
}

bool Crypto::init_class(JSContext *cx, JS::HandleObject global) {
if (!init_class_impl(cx, global)) {
return false;
if (!JS_DefineProperty(cx, global, "crypto", crypto, JSPROP_ENUMERATE))
}

JS::RootedObject cryptoInstance(
cx, JS_NewObjectWithGivenProto(cx, &Crypto::class_, Crypto::proto_obj));
if (!cryptoInstance) {
return false;
return JS_DefineFunctions(cx, crypto, Crypto::methods);
}
crypto.init(cx, cryptoInstance);

JS::RootedObject subtleCrypto(
cx, JS_NewObjectWithGivenProto(cx, &SubtleCrypto::class_, SubtleCrypto::proto_obj));
subtle.init(cx, subtleCrypto);

return JS_DefineProperty(cx, global, "crypto", crypto, JSPROP_ENUMERATE);
}
} // namespace builtins
13 changes: 10 additions & 3 deletions c-dependencies/js-compute-runtime/builtins/crypto.h
Expand Up @@ -5,19 +5,26 @@

namespace builtins {

class Crypto : public BuiltinNoConstructor<Crypto> {
class Crypto : public BuiltinImpl<Crypto> {
private:
public:
static constexpr const char *class_name = "Crypto";
static const int ctor_length = 0;

static JS::PersistentRooted<JSObject *> subtle;

enum Slots { Count };
static const JSFunctionSpec methods[];
static const JSPropertySpec properties[];

static bool subtle_get(JSContext *cx, unsigned argc, JS::Value *vp);
static bool get_random_values(JSContext *cx, unsigned argc, JS::Value *vp);
static bool random_uuid(JSContext *cx, unsigned argc, JS::Value *vp);
static bool create(JSContext *cx, JS::HandleObject global);

static bool constructor(JSContext *cx, unsigned argc, JS::Value *vp);
static bool init_class(JSContext *cx, JS::HandleObject global);
};

} // namespace builtins

#endif
#endif

0 comments on commit bbe1754

Please sign in to comment.