Skip to content

Callback invoked twice on unhandled exception #527

@adamjmcgrath

Description

@adamjmcgrath

The callback handler is invoked twice when there is an unhandled exception in the callback, eg:

const { SignedXml } = require("xml-crypto");
const crypto = require("crypto");

const xml = `<x xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' Id='_1'></x>`;
const sig = new SignedXml();
sig.privateKey = crypto.generateKeyPairSync("rsa", {
  modulusLength: 2048,
  privateKeyEncoding: {
    type: "pkcs1",
    format: "pem"
  }
}).privateKey;
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml, (err, result) => {
  console.log("Callback invoked", !!err, !!result);
  throw new Error('Error Thrown');
});

// Results in:
// Callback invoked false true
// Callback invoked true false

// Error: Error Thrown

The callback should only be invoked a single time, eg:

require('fs').readdir(".", (err, files) => {
  console.log("Callback invoked", !!err, !!files);
  throw new Error('Error Thrown');
});

// Results in:
// Callback invoked false true

// Error: Error Thrown

I believe the problem is the synchronous execution of the callback in https://github.com/node-saml/xml-crypto/blob/master/src/types.ts#L259

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions