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

Can validate an invalid signature... #343

Open
wellcaffeinated opened this issue Feb 3, 2022 · 2 comments
Open

Can validate an invalid signature... #343

wellcaffeinated opened this issue Feb 3, 2022 · 2 comments

Comments

@wellcaffeinated
Copy link

Am I mistaken, or is this not right?

Affected version: 2.0.0

async function test() {
  const payload = Uint8Array.from([0, 3, 4, 2])
  const key = await jose.JWK.createKey('EC', 'P-256')
  const pk = await jose.JWK.asKey(key.toJSON())
  console.log(pk)
  const sig = await jose.JWS.createSign({ format: 'compact' }, key)
    .update(payload)
    .final()
  console.log(sig)
  const res = await jose.JWS.createVerify(pk).verify(sig+'12') // APPEND GARBAGE DATA TO SIGNATURE
  // still get result.
  console.log(
    res,
    res.payload.toString('hex'),
    Buffer.from(payload).toString('hex')
  )
}

test()
@zkwzk
Copy link

zkwzk commented Jul 22, 2022

+1, we came across the same issue, if we add extra charater at the end of the signature, it's still be verified

@Jefferson111
Copy link

That's because the library doesn't check if the signature length matches the actual digest. You can add checks if you want it to fail, for example in hmac.js, you can update the logic from:

  function compare(len, expected, actual) {
    len = (len || CONSTANTS.HASHLENGTH[hash]) / 8;
    var valid = true;
    for (var idx = 0; len > idx; idx++) {
      valid = valid && (expected[idx] === actual[idx]);
    }
    return valid;
  }

to

  function compare(len, expected, actual) {
    len = (len || CONSTANTS.HASHLENGTH[hash]) / 8;
    if (expected.length !== actual.length) { // just add a check here
        return false;
    }
    var valid = true;
    for (var idx = 0; len > idx; idx++) {
      valid = valid && (expected[idx] === actual[idx]);
    }
    return valid;
  }

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

3 participants