Skip to content

Commit 0817e46

Browse files
committed
Accept DOM when calling checkSignature
1 parent b07c42d commit 0817e46

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/signed-xml.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,26 +245,25 @@ export class SignedXml {
245245
* @returns `true` if the signature is valid
246246
* @throws Error if no key info resolver is provided.
247247
*/
248-
checkSignature(xml: string): boolean;
248+
checkSignature(xml: Document | string): boolean;
249249
/**
250250
* Validates the signature of the provided XML document synchronously using the configured key info provider.
251251
*
252252
* @param xml The XML document containing the signature to be validated.
253253
* @param callback Callback function to handle the validation result asynchronously.
254254
* @throws Error if the last parameter is provided and is not a function, or if no key info resolver is provided.
255255
*/
256-
checkSignature(xml: string, callback: (error: Error | null, isValid?: boolean) => void): void;
256+
checkSignature(xml: Document | string, callback: (error: Error | null, isValid?: boolean) => void): void;
257257
checkSignature(
258-
xml: string,
258+
xml: Document | string,
259259
callback?: (error: Error | null, isValid?: boolean) => void,
260260
): unknown {
261261
if (callback != null && typeof callback !== "function") {
262262
throw new Error("Last parameter must be a callback function");
263263
}
264264

265-
this.signedXml = xml;
266-
267-
const doc = new xmldom.DOMParser().parseFromString(xml);
265+
const doc = typeof xml === "string" ? new xmldom.DOMParser().parseFromString(xml) : xml;
266+
this.signedXml = doc.toString();
268267

269268
// Reset the references as only references from our re-parsed signedInfo node can be trusted
270269
this.references = [];
@@ -347,7 +346,7 @@ export class SignedXml {
347346

348347
// Check the signature verification to know whether to reset signature value or not.
349348
const sigRes = signer.verifySignature(unverifiedSignedInfoCanon, key, this.signatureValue);
350-
if (sigRes === true) {
349+
if (sigRes) {
351350
if (callback) {
352351
callback(null, true);
353352
} else {

test/signature-unit-tests.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ describe("Signature unit tests", function () {
873873
return fs.readFileSync("./test/static/client.pem", "latin1");
874874
};
875875

876-
const checkedSignature = sig.checkSignature(xml);
876+
const checkedSignature = sig.checkSignature(toString ? doc.toString() : doc );
877877
expect(checkedSignature).to.be.true;
878878

879879
/* eslint-disable-next-line deprecation/deprecation */
@@ -931,7 +931,6 @@ describe("Signature unit tests", function () {
931931
const sig = new SignedXml({ idMode });
932932
sig.publicCert = fs.readFileSync("./test/static/client_public.pem");
933933
sig.loadSignature(node);
934-
935934
return sig;
936935
}
937936

@@ -955,8 +954,9 @@ describe("Signature unit tests", function () {
955954
function throwsValidatingSignature(file: string, idMode?: "wssecurity") {
956955
const xml = fs.readFileSync(file).toString();
957956
const sig = loadSignature(xml, idMode);
957+
const doc = new xmldom.DOMParser().parseFromString(xml);
958958
expect(
959-
() => sig.checkSignature(xml),
959+
() => sig.checkSignature(doc),
960960
"expected an error to be thrown because signatures couldn't be checked for validity",
961961
).to.throw();
962962
expect(sig.getSignedReferences().length).to.equal(0);

0 commit comments

Comments
 (0)