Skip to content

Commit 468d674

Browse files
authored
Remove the default for getKeyInfoContent forcing a consumer to choose (#411)
1 parent 741240f commit 468d674

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ When verifying a xml document you can pass the following options to the `SignedX
116116
- `publicCert` - **[optional]** your certificate as a string, a string of multiple certs in PEM format, or a Buffer
117117
- `privateKey` - **[optional]** your private key as a string or a Buffer - used for verifying symmetrical signatures (HMAC)
118118

119-
The certificate that will be used to check the signature will first be determined by calling `.getCertFromKeyInfo()`, which function you can customize as you see fit. If that returns `null`, then `publicCert` is used. If that is `null`, then `privateKey` is used (for symmetrical signing applications). If you do not want to trust any embedded `<KeyInfo />` node, preferring to validate the signature using a provided `publicCert`, you can set `getCertFromKeyInfo` to return `null`.
119+
The certificate that will be used to check the signature will first be determined by calling `this.getCertFromKeyInfo()`, which function you can customize as you see fit. If that returns `null`, then `publicCert` is used. If that is `null`, then `privateKey` is used (for symmetrical signing applications).
120120

121121
Example:
122122

@@ -246,7 +246,7 @@ The `SignedXml` constructor provides an abstraction for sign and verify xml docu
246246
- `inclusiveNamespacesPrefixList` - string - default `null` - a list of namespace prefixes to include during canonicalization
247247
- `implicitTransforms` - string[] - default `[]` - a list of implicit transforms to use during verification
248248
- `keyInfoAttributes` - object - default `{}` - a hash of attributes and values `attrName: value` to add to the KeyInfo node
249-
- `getKeyInfoContent` - function - default `SignedXml.geTKeyInfoContent` - a function that returns the content of the KeyInfo node
249+
- `getKeyInfoContent` - function - default `noop` - a function that returns the content of the KeyInfo node
250250
- `getCertFromKeyInfo` - function - default `SignedXml.getCertFromKeyInfo` - a function that returns the certificate from the `<KeyInfo />` node
251251

252252
#### API
@@ -290,8 +290,8 @@ var SignedXml = require("xml-crypto").SignedXml,
290290
Now define the extension point you want to implement. You can choose one or more.
291291

292292
To determine the inclusion and contents of a `<KeyInfo />` element, the function
293-
`getKeyInfoContent()` is called. There is a default implementation of this. If you wish to change
294-
this implementation, provide your own function assigned to the property `.getKeyInfoContent`. If
293+
`this.getKeyInfoContent()` is called. There is a default implementation of this. If you wish to change
294+
this implementation, provide your own function assigned to the property `this.getKeyInfoContent`. If you prefer to use the default implementation, assign `SignedXml.getKeyInfoContent` to `this.getKeyInfoContent` If
295295
there are no attributes and no contents to the `<KeyInfo />` element, it won't be included in the
296296
generated XML.
297297

src/signed-xml.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ export class SignedXml {
111111
ds: "http://www.w3.org/2000/09/xmldsig#",
112112
};
113113

114+
static noop = () => null;
115+
114116
/**
115117
* The SignedXml constructor provides an abstraction for sign and verify xml documents. The object is constructed using
116118
* @param options {@link SignedXmlOptions}
@@ -147,7 +149,7 @@ export class SignedXml {
147149
}
148150
this.implicitTransforms = implicitTransforms ?? this.implicitTransforms;
149151
this.keyInfoAttributes = keyInfoAttributes ?? this.keyInfoAttributes;
150-
this.getKeyInfoContent = getKeyInfoContent ?? this.getKeyInfoContent;
152+
this.getKeyInfoContent = getKeyInfoContent ?? SignedXml.noop;
151153
this.getCertFromKeyInfo = getCertFromKeyInfo ?? this.getCertFromKeyInfo;
152154
this.CanonicalizationAlgorithms;
153155
this.HashAlgorithms;
@@ -163,7 +165,7 @@ export class SignedXml {
163165
this.SignatureAlgorithms = {
164166
"http://www.w3.org/2000/09/xmldsig#hmac-sha1": signatureAlgorithms.HmacSha1,
165167
};
166-
this.getKeyInfoContent = () => null;
168+
this.getKeyInfoContent = SignedXml.noop;
167169
}
168170

169171
/**

test/key-info-tests.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe("KeyInfo tests", function () {
1313
sig.publicCert = fs.readFileSync("./test/static/client_public.pem");
1414
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
1515
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
16+
sig.getKeyInfoContent = SignedXml.getKeyInfoContent;
1617
sig.computeSignature(xml);
1718
const signedXml = sig.getSignedXml();
1819
const doc = new xmldom.DOMParser().parseFromString(signedXml);

test/signature-unit-tests.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ describe("Signature unit tests", function () {
534534
sig.signatureAlgorithm = "http://dummySignatureAlgorithm";
535535
sig.canonicalizationAlgorithm = "http://DummyCanonicalization";
536536
sig.privateKey = "";
537+
sig.getKeyInfoContent = SignedXml.getKeyInfoContent;
537538

538539
sig.addReference({
539540
xpath: "//*[local-name(.)='x']",
@@ -1236,6 +1237,7 @@ describe("Signature unit tests", function () {
12361237
sig.publicCert = pemBuffer;
12371238
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
12381239
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
1240+
sig.getKeyInfoContent = SignedXml.getKeyInfoContent;
12391241
sig.computeSignature(xml);
12401242
const signedXml = sig.getSignedXml();
12411243

0 commit comments

Comments
 (0)