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

createSign doesn't emit an error when the secret is undefined #62

Closed
zbinlin opened this issue Nov 14, 2016 · 6 comments
Closed

createSign doesn't emit an error when the secret is undefined #62

zbinlin opened this issue Nov 14, 2016 · 6 comments

Comments

@zbinlin
Copy link

zbinlin commented Nov 14, 2016

jws.createSign({
  header: { alg: 'HS256' },
  secret: undefined,
  payload: 'hhhh'
}).on('error', function (err) {
  console.error(err);
}).on('done', function(signature) {
  console.log(signature);
});
@cbaron
Copy link

cbaron commented Nov 30, 2016

I am experiencing the same thing. Please contact me for more information. I thought you addressed this months ago?

@robertrossmann
Copy link

robertrossmann commented Dec 22, 2016

I found the root cause, but I am currently too busy providing a PR with a fix (you know, Christmas & all that stuff).

When you provide a secret which is null or undefined (falsy value), it gets passed in here to the DataStream:
https://github.com/brianloveswords/node-jws/blob/master/lib/sign-stream.js#L29

Later on, the SignStream attaches a single "close" event listener to the DataStream object which, when fired, will mark the SignStream as finished:
https://github.com/brianloveswords/node-jws/blob/master/lib/sign-stream.js#L35

The problem:
The DataStream implementation checks for the secret to be truthy, but if it is not, it will simply return itself and will not emit any event:
https://github.com/brianloveswords/node-jws/blob/master/lib/data-stream.js#L12

This leaves the SignStream hanging and patiently waiting for the "close" event to be fired, but at this point it is quite certain it will never happen.

I am not certain what the correct behaviour should be in this case:

  • emit the "close" event even when no input has been provided
  • emit an "error" event
  • throw an error

If someone can make a decision what the correct behaviour is I will attempt to fix this and send a PR next year. 😄 (I mean January, of course). Unless someone beats me to it, now that you know how to fix it...

Thanks for your input and help!

@willm
Copy link

willm commented Apr 11, 2017

Hi, I just hunted down a bug due to this. I think the safest thing to do here is to emit an error event. It seems to me that attempting to sign with an empty key is a programmer error. I think there is also an issue where the DataStream constructor ends synchronously in this case, but async in the others (by registering process.nextTick or piping the stream. This is causing the callback in the calling package I'm using (jsonwebtoken) to never get called.

@borromeotlhs
Copy link

I made pr #65 , but have to work to make it emit an error in order to satisfy the automated build tests. . .

@willm
Copy link

willm commented May 4, 2017

I feel like the only way to change this is to make a breaking change to not allow a DataStream to be created without any data. I'm not sure what implications that has though?

@borromeotlhs
Copy link

borromeotlhs commented May 4, 2017 via email

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

No branches or pull requests

5 participants