node-argon2 icon indicating copy to clipboard operation
node-argon2 copied to clipboard

Typescript declared type "Options.type?: 0 | 2 | 1 | undefined" doesn't match dynamic check in index.js serialize()

Open tiagoj opened this issue 3 years ago • 5 comments

This is not a show stopper for me, but I thought I would mention it.

Steps to reproduce

"argon2": "^0.29.1"

I'm using this code segment in Typescript

let options = {
    memoryCost: 15 * 2**10, // 15MiB, memoryCost is in KiB
    timeCost: 2,
    parallelism: 1,
    hashLength: 32, // default
    type: argon2.argon2id, // default
    raw: false
  }
 let hash = await argon2.hash(newUser.psw, options)

Note: That because the 'type' I was using is the default anyway, just deleting the 'type' line avoids the bug.

You don't really need to reproduce the issue, just looking at the source shows the mismatch.

Expected behaviour

Typescript declared types should agree with internal dynamic check.

Actual behaviour

If I set id to an integer as required by the typescript type, it results in a runtime error when the type check fails in the serialize() function in index.js

If I set it to a string, the Typescipt compiler issues an error TypeError: id must be a string at serialize (/Users/james/GitRepos/WebMisc/RmaSite/dev/dbApi/node_modules/@phc/format/index.js:59:11) at Module.hash (/Users/james/GitRepos/WebMisc/RmaSite/dev/dbApi/node_modules/argon2/argon2.js:68:10) ...

The odd thing, is that even with the error, using commonJs modules, it seemed to work, but when switching to use EJS (for other reasons), it started catching the error.

Environment

MAC

node: v16.13.0

typescript: Version 4.7.4

tiagoj avatar Sep 22 '22 18:09 tiagoj

This also happens on Windows 11.

const password = 'mypassword';
const hash = await argon2.hash(
    password, 
    {
        type: argon2.argon2id,
    }
);

If I remove type: argon2.argon2id, option, it runs fine.

ve3 avatar Jan 31 '23 04:01 ve3

@ve3 what version of the library are you using? It was solved in the past and now it passes function name to @phc/format

ranisalt avatar Jan 31 '23 09:01 ranisalt

It is newly installed.

"argon2": "^0.30.3"

Node 18.12.1

ve3 avatar Jan 31 '23 09:01 ve3

I can reproduce it on Node.JS v18.10.0 and "argon2": "^0.30.3".

seia-soto avatar Mar 31 '23 07:03 seia-soto

if set type with number, it runs fine

const hash = await argon2.hash('password', {
  type: argon2.argon2d // error
})

const hash2 = await argon2.hash('password', {
  type: 0 // success
})

Ttou avatar Aug 24 '23 07:08 Ttou