Skip to content

Respect idAttribute when generating signatures. #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/signed-xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ export class SignedXml {
id,
);
} else {
node.setAttribute("Id", id);
node.setAttribute(this.idAttributes[0], id);
}

return id;
Expand Down
10 changes: 7 additions & 3 deletions test/signature-unit-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ describe("Signature unit tests", function () {
expect(node.length, `xpath ${xpathArg} not found`).to.equal(1);
}

function verifyAddsId(mode, nsMode) {
function verifyAddsId(mode, nsMode, idAttribute:string|undefined = undefined) {
const xml = '<root><x xmlns="ns"></x><y attr="value"></y><z><w></w></z></root>';
const sig = new SignedXml({ idMode: mode });
const sig = new SignedXml({ idMode: mode, idAttribute });
sig.privateKey = fs.readFileSync("./test/static/client.pem");

sig.addReference({
Expand All @@ -46,14 +46,18 @@ describe("Signature unit tests", function () {

const op = nsMode === "equal" ? "=" : "!=";

const xpathArg = `//*[local-name(.)='{elem}' and '_{id}' = @*[local-name(.)='Id' and namespace-uri(.)${op}'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd']]`;
const xpathArg = `//*[local-name(.)='{elem}' and '_{id}' = @*[local-name(.)='${idAttribute || 'Id'}' and namespace-uri(.)${op}'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd']]`;

//verify each of the signed nodes now has an "Id" attribute with the right value
nodeExists(doc, xpathArg.replace("{id}", "0").replace("{elem}", "x"));
nodeExists(doc, xpathArg.replace("{id}", "1").replace("{elem}", "y"));
nodeExists(doc, xpathArg.replace("{id}", "2").replace("{elem}", "w"));
}

it("signer adds increasing different id attributes to elements with custom idAttribute", function () {
verifyAddsId(null, "different", 'myIdAttribute');
});

it("signer adds increasing different id attributes to elements", function () {
verifyAddsId(null, "different");
});
Expand Down