gkouziik / eslint-plugin-security-node

ESLint security plugin for Node.js
101 stars 13 forks source link

TypeError: Cannot read properties of null (reading 'name') #77

Open abcfy2 opened 1 year ago

abcfy2 commented 1 year ago

Here is the sample:

async function createAttestationKeyHandler(
  this: FastifyInstance,
  request: FastifyRequest,
  reply: FastifyReply
) {
  const dbService: DbService = this.diContainer.resolve('dbService');
  const {keypairName, privateKey} = request.body as {
    keypairName: string;
    privateKey: string;
  };
  const apiKey = getApiKey(request);

  if (
    await findAttestationKeyByProjectAndName(dbService, apiKey, keypairName)
  ) {
    return reply.status(400).send({error: 'key already exists'});
  }

  let wallet;
  let imported = 0;
  if (privateKey) {
    try {
      wallet = new ethers.Wallet(privateKey);
    } catch {
      return reply.status(400).send({error: 'invalid private key'});
    }
    imported = 1;
  } else {
    let entropy: Uint8Array = crypto.randomBytes(16);
    entropy = arrayify(
      hexDataSlice(keccak256(concat([entropy, crypto.randomBytes(16)])), 0, 16)
    );
    const mnemonic = entropyToMnemonic(entropy);
    wallet = ethers.Wallet.fromMnemonic(mnemonic);
  }

  const iv = createIvByKeyName(keypairName);
  const sk = encrypt(env.KEY_FOR_DB_CRYPTO, iv, wallet.privateKey);

  const result = await createAttestationKeyRecord(
    dbService,
    apiKey,
    keypairName,
    wallet.publicKey,
    sk,
    0,
    imported
  );

  return reply.status(201).send(result);
}

And here is the error:

❯ npx eslint .

Oops! Something went wrong! :(

ESLint: 8.45.0

TypeError: Cannot read properties of null (reading 'name')
Occurred while linting /home/fengyu/projects/AlphaWallet/common-api/backend/src/handlers/attestationKeysActions.ts:93
Rule: "security-node/detect-unhandled-async-errors"
    at isTryCatchStatement (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint-plugin-security-node/lib/rules/detect-unhandled-async-errors.js:42:73)
    at FunctionDeclaration (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint-plugin-security-node/lib/rules/detect-unhandled-async-errors.js:99:29)
    at ruleErrorHandler (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/linter.js:1050:28)
    at /home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
    at Object.emit (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/node-event-generator.js:297:26)
    at NodeEventGenerator.applySelectors (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/node-event-generator.js:326:22)
    at NodeEventGenerator.enterNode (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/node-event-generator.js:340:14)
    at CodePathAnalyzer.enterNode (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/code-path-analysis/code-path-analyzer.js:795:23)
weyert commented 10 months ago

You to change the try..catch to } catch (err: unknown) { to get rid of the error

yatki commented 5 months ago

I am having the same issue with following syntax. In the code below, initServer type signature is (method) mybutton.initServer(): Promise<void | Error>. So we know for sure if the err is defined it's an Error object but still getting the same issue.

The current work around is to create a new Error instance.

mybutton.initServer().then((err) => {
      if (err) {
        /**
         * Calling this.myEvents.emit('error', err)
         * resulting eslint "security-node/detect-unhandled-event-errors" rule
         * to crash and throw an error. Seems like this is a bug in eslint rule.
         *
         * */
        this.myEvents.emit('error', new Error(err.message))
      }
    })

I would expect passing err object to work just fine.

Could you please take a look 🙏🏻 I can help to fix this once I have time as well.

Cheers 🖖🏻