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

Bun bun bun!! #579

Closed
2 tasks done
hiteshjoshi opened this issue Sep 22, 2023 · 17 comments
Closed
2 tasks done

Bun bun bun!! #579

hiteshjoshi opened this issue Sep 22, 2023 · 17 comments

Comments

@hiteshjoshi
Copy link

What happened?

Duuddee!
Why import bun as a browser module?

"bun": "./dist/browser/index.js",

I am using bun on the server because it's so sexy with typescript.

I wasted 2 hours thinking something was wrong with jwks-rsa auth0/node-jwks-rsa#373

Version

doesnt matter!

Runtime

Other (I will specify below)

Runtime Details

bun bun bun

Code to reproduce

https://github.com/panva/jose/blob/22c05ceeaecb850c6933d4ef5bc0935a8acf6854/package.json#L80

Required

  • I have searched the issues tracker and discussions for similar topics and couldn't find anything related.
  • I agree to follow this project's Code of Conduct
@panva
Copy link
Owner

panva commented Sep 22, 2023

Why import bun as a browser module?

Because bun is Web Platform API first and node's crypto module compatibility is not guaranteed and will never be a 100%.

@panva panva closed this as not planned Won't fix, can't repro, duplicate, stale Sep 22, 2023
@panva panva removed the triage label Sep 22, 2023
@panva
Copy link
Owner

panva commented Sep 22, 2023

I wasted 2 hours thinking something was wrong with jwks-rsa auth0/node-jwks-rsa/issues/373

There isn't anything wrong with jwks-rsa, but it's a node module, not one made for other runtimes.

@hiteshjoshi
Copy link
Author

Mention it on docs. Please?

@panva
Copy link
Owner

panva commented Sep 22, 2023

jwks-rsa has a package.json engines entry marking it node-only.

@hiteshjoshi
Copy link
Author

BTW, the crypto works fine if I point bun to the node package of jose.

@panva
Copy link
Owner

panva commented Sep 22, 2023

BTW, the crypto works fine if I point bun to the node package of jose.

It might for you, it might not for others. Open an issue with bun on how they resolve node-first module dependencies. Yours is not an isolated issue, i've seen them popping up and it's a runtime issue.

@panva
Copy link
Owner

panva commented Sep 22, 2023

FYI auth0/node-jwks-rsa#374 give this branch a shot and let me know if there's anything else.

@BRAVO68WEB
Copy link

Hey @panva its me again.

Here is my code.

import JwksClient from "jwks-rsa";
import JsonWebToken, { JwtHeader } from "jsonwebtoken";

const client = JwksClient({
    jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`
})

const keySets: any = await client.getKeys();

export const certToPEM = (cert: string) => {
    cert = cert.match(/.{1,64}/g)!.join('\n')
    cert = `-----BEGIN CERTIFICATE-----\n${cert}\n-----END CERTIFICATE-----\n`;
    return cert;
}

export const verifySession = (token: string) => {
    const decoded = JsonWebToken.decode(token, { complete: true }) as { header: JwtHeader, payload: any };
    const kid = decoded.header.kid;

    const keySet = keySets.find((key: { kid: string; }) => key.kid === kid);

    if (!keySet) {
        throw new Error("No key set");
    }

    const signingKey = certToPEM(keySet.x5c[0]);
    return JsonWebToken.verify(token, signingKey);
}

Here is my error

115 | 
116 |     if (!hasSignature && !options.algorithms) {
117 |       return done(new JsonWebTokenError('please specify "none" in "algorithms" to verify unsigned tokens'));
118 |     }
119 | 
120 |     if (secretOrPublicKey != null && !(secretOrPublicKey instanceof KeyObject)) {
                                          ^
TypeError: Right hand side of instanceof is not an object
      at /home/xxxxxxxxxx/hono-bun/node_modules/jsonwebtoken/verify.js:120:39

@panva
Copy link
Owner

panva commented Oct 4, 2023

https://bun.sh/docs/runtime/nodejs-apis#node-crypto Bun does not implement those APIs. Use jose, not jsonwebtoken.

You can replace both jwks-rsa and jsonwebtoken with jose.

const JWKS = jose.createRemoteJWKSet(new URL('https://www.googleapis.com/oauth2/v3/certs'))

const { payload, protectedHeader } = await jose.jwtVerify(jwt, JWKS, {
  issuer: 'urn:example:issuer',
  audience: 'urn:example:audience',
})
console.log(protectedHeader)
console.log(payload)

@BRAVO68WEB
Copy link

This was the code that got me segmentation fault

@panva
Copy link
Owner

panva commented Oct 4, 2023

Then create a reproduction sample and open a Bun issue please, there's nothing I can do about those.

@BRAVO68WEB
Copy link

Sure man, thx you for helping me out.

@panva
Copy link
Owner

panva commented Oct 4, 2023

To explain, it is far more likely the segmentation fault is related to some other code and just presents itself when the jose code yields control. So it seems like jose is triggering it but it's not. The whole jose test suite gets executed using Bun without segmentation faults.

@BRAVO68WEB
Copy link

BRAVO68WEB commented Oct 4, 2023

Hmm
One question!

I am using Auth0 to do openid stuff. My JWKS does not contain publicKey directly. I use x5c to create it. Can this be the reason?

PS: Also the jwks-rsa is your custom branch from github not from npm

@BRAVO68WEB
Copy link

The OP is correct. Every thing is fine in node

@panva
Copy link
Owner

panva commented Oct 4, 2023

My JWKS does not contain publicKey directly

JWKs are the public keys, just in a JWK format

@BRAVO68WEB
Copy link

Ohhh got

@github-actions github-actions bot locked and limited conversation to collaborators Jan 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants