Skip to content

Commit 0d01641

Browse files
authored
Make references accessible only via get/set (#395)
1 parent c2b8cd6 commit 0d01641

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ In order to protect from some attacks we must check the content we want to use i
154154
```javascript
155155
// Roll your own
156156
var elem = xpath.select("/xpath_to_interesting_element", doc);
157-
var uri = sig.references[0].uri; // might not be 0 - depending on the document you verify
157+
var uri = sig.getReferences()[0].uri; // might not be 0 - depending on the document you verify
158158
var id = uri[0] === "#" ? uri.substring(1) : uri;
159159
if (elem.getAttribute("ID") != id && elem.getAttribute("Id") != id && elem.getAttribute("id") != id)
160160
throw new Error("the interesting element was not the one verified by the signature");

src/signed-xml.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class SignedXml {
7070
* Contains the references that were signed.
7171
* @see {@link Reference}
7272
*/
73-
references: Reference[] = [];
73+
private references: Reference[] = [];
7474

7575
/**
7676
* Contains validation errors (if any) after {@link checkSignature} method is called
@@ -253,9 +253,9 @@ export class SignedXml {
253253

254254
const doc = new xmldom.DOMParser().parseFromString(xml);
255255

256-
if (!this.validateReferences(doc)) {
256+
if (!this.getReferences().every((ref) => this.validateReference(ref, doc))) {
257257
if (callback) {
258-
callback(new Error("Could not validate references"));
258+
callback(new Error("Could not validate all references"));
259259
return;
260260
}
261261

@@ -372,7 +372,7 @@ export class SignedXml {
372372
}
373373

374374
validateElementAgainstReferences(elem: Element, doc: Document): Reference {
375-
for (const ref of this.references) {
375+
for (const ref of this.getReferences()) {
376376
const uri = ref.uri?.[0] === "#" ? ref.uri.substring(1) : ref.uri;
377377
let targetElem: xpath.SelectSingleReturnType;
378378

@@ -647,6 +647,10 @@ export class SignedXml {
647647
});
648648
}
649649

650+
getReferences(): Reference[] {
651+
return this.references;
652+
}
653+
650654
/**
651655
* Compute the signature of the given XML (using the already defined settings).
652656
*
@@ -879,7 +883,7 @@ export class SignedXml {
879883
prefix = prefix || "";
880884
prefix = prefix ? `${prefix}:` : prefix;
881885

882-
for (const ref of this.references) {
886+
for (const ref of this.getReferences()) {
883887
const nodes = xpath.selectWithResolver(ref.xpath ?? "", doc, this.namespaceResolver);
884888

885889
if (!utils.isArrayHasLength(nodes)) {

test/signature-unit-tests.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe("Signature unit tests", function () {
6868
const checkedSignature = sig.checkSignature(xml);
6969
expect(checkedSignature).to.be.true;
7070

71-
expect(sig.references.length).to.equal(3);
71+
expect(sig.getReferences().length).to.equal(3);
7272

7373
const digests = [
7474
"b5GCZ2xpP5T7tbLWBTkOl4CYupQ=",
@@ -81,8 +81,8 @@ describe("Signature unit tests", function () {
8181
const matchedReference = sig.validateElementAgainstReferences(firstGrandchild, doc);
8282
expect(matchedReference).to.not.be.false;
8383

84-
for (let i = 0; i < sig.references.length; i++) {
85-
const ref = sig.references[i];
84+
for (let i = 0; i < sig.getReferences().length; i++) {
85+
const ref = sig.getReferences()[i];
8686
const expectedUri = `#_${i}`;
8787
expect(
8888
ref.uri,

0 commit comments

Comments
 (0)