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

isObject helper fails in jest #178

Closed
maurolucc opened this issue Apr 13, 2021 · 8 comments
Closed

isObject helper fails in jest #178

maurolucc opened this issue Apr 13, 2021 · 8 comments
Labels
bug Something isn't working

Comments

@maurolucc
Copy link

maurolucc commented Apr 13, 2021

Describe the bug

fromKeyLike does not return an object (JWK). If you use the output of this function in the latest version of node you get the following error:

TypeError: JWK must be an object

To Reproduce

Use Node v15.14.0.

  const key = crypto.generateKeyPairSync("ec", {
    namedCurve: "secp256k1",
  });
  const privateJwk = await fromKeyLike(key.privateKey);
     // ^^^^^^^^^^ this is not an object
  const privateKey = await parseJwk(privateJwk, "ES256K");                                  
  // TypeError: JWK must be an object

Expected behaviour
Get proper object so as to be able to use JWK.

Environment:

  • jose version: 3.11.4
  • Typescript v4.2.4
  • affected runtime is: Node.js v15.14.0

Additional context
If anyone faces the same issue, there are two tricks that can save you:

  • Do not use privateJwk in the next function as input but {...privateJwk }
    or
  • After getting privateJwk set jwk.constructor = Object;
@panva
Copy link
Owner

panva commented Apr 13, 2021

Hi @maurolucc, I am unable to reproduce your issue. Can you provide more details?

const crypto = require('crypto');

const { fromKeyLike } = require('jose/jwk/from_key_like');
const { parseJwk } = require('jose/jwk/parse');

const key = crypto.generateKeyPairSync("ec", {
  namedCurve: "secp256k1",
});

(async () => {
  const privateJwk = await fromKeyLike(key.privateKey);
  console.log(privateJwk.constructor)
  const privateKey = await parseJwk(privateJwk, "ES256K");
  console.log('works');
})();
❯ node -v                         
v15.14.0

❯ cat package.json | jq '.version'
"3.11.4"

❯ node some.js                    
[Function: Object]
works

@panva panva added the waiting for feedback The OP is asked for feedback or a proposal label Apr 13, 2021
@maurolucc
Copy link
Author

Excuse me, I forgot to mention I'm using Typescript v4.2.4. Let me add it in the environment information.

@panva
Copy link
Owner

panva commented Apr 13, 2021

I would need the actual steps and project to reproduce then, from the top of my head i don't see how TS is relevant to this issue. As is, i'm not able to reproduce. Can you create a gist with the necessary source, configuration files, and a readme to reproduce?

panva added a commit that referenced this issue Apr 13, 2021
@maurolucc
Copy link
Author

@panva
Copy link
Owner

panva commented Apr 13, 2021

@maurolucc that is of no help unfortunately. I have no clue what the package.json looks like, how you execute this, nor what the typescript configuration is.

I'm looking for a reproduction, e.g. such as that i can clone the gist, run npm i and then execute whatever command that reproduces the issue.

@maurolucc
Copy link
Author

Alright. I've updated the gist I shared with you. Now you should be able to clone it and run:

nvm use 15.14.0
yarn
jest

You'll see that the test fails for this node version but not for older. Notice that to make this test pass in my project I fix it with the trick found in AuxTest.ts, swtiching how I return JWK:

  // returning directly privateJwk provoke getting TypeError: JWK must be an object error
  /* return {
    hexPrivateKey,
    jwk: privateJwk,
    hexPublicKey,
  }; */
  // The code below makes the trick
  return {
    hexPrivateKey,
    jwk: { ...privateJwk }, 
    hexPublicKey,
  }; 

@panva
Copy link
Owner

panva commented Apr 13, 2021

Thank you for the reproduction steps, very helpful!

Well, apparently under jest the global.Object comparison with constructor fails, eventho they both log as [Function: Object].

I'll update the isObject helper in to account for different domains when i have the time, unless you feel like opening a PR and researching a better way. Or possibly just making sure typeof is object and it's not null or Array, so return typeof input === 'object' && !!input && !Array.isArray(input)

@panva panva added bug Something isn't working and removed triage waiting for feedback The OP is asked for feedback or a proposal labels Apr 13, 2021
@panva panva changed the title fromKeyLike does not return JWK object isObject helper fails in jest Apr 13, 2021
@panva panva closed this as completed in 7819df7 Apr 13, 2021
@maurolucc
Copy link
Author

maurolucc commented Apr 14, 2021

@panva Awesome! You fix it already. Good job. Thanks. 😄

@github-actions github-actions bot locked and limited conversation to collaborators Jul 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants