Skip to content

Commit

Permalink
Fix issue in case when namespace has no prefix (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
git9527 authored Jul 8, 2023
1 parent be17c06 commit a31e7ff
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/c14n-canonicalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ C14nCanonicalization.prototype.renderNs = function (
}

p = nsListToRender[a];
res.push(" xmlns:", p.prefix, '="', p.namespaceURI, '"');
res.push(" xmlns", p.prefix ? ":" + p.prefix : "", '="', p.namespaceURI, '"');
}

return { rendered: res.join(""), newDefaultNs: newDefaultNs };
Expand Down
34 changes: 17 additions & 17 deletions lib/signed-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,10 @@ function findAncestorNs(doc, docSubsetXpath, namespaceResolver) {

// Remove namespaces which are already declared in the subset with the same prefix
var returningNs = [];
var subsetAttributes = docSubset[0].attributes;
for (var j = 0; j < ancestorNsWithoutDuplicate.length; j++) {
var isUnique = true;
for (var k = 0; k < subsetAttributes.length; k++) {
var nodeName = subsetAttributes[k].nodeName;
if (nodeName.search(/^xmlns:/) === -1) continue;
var prefix = nodeName.replace(/^xmlns:/, "");
if (ancestorNsWithoutDuplicate[j].prefix === prefix) {
isUnique = false;
break;
}
}

if (isUnique) {
returningNs.push(ancestorNsWithoutDuplicate[j]);
const subsetNsPrefix = findNSPrefix(docSubset[0]);
for (const ancestorNs of ancestorNsWithoutDuplicate) {
if (ancestorNs.prefix !== subsetNsPrefix) {
returningNs.push(ancestorNs);
}
}

Expand All @@ -247,9 +236,9 @@ function collectAncestorNamespaces(node, nsArray) {
if (parent.attributes && parent.attributes.length > 0) {
for (var i = 0; i < parent.attributes.length; i++) {
var attr = parent.attributes[i];
if (attr && attr.nodeName && attr.nodeName.search(/^xmlns:/) !== -1) {
if (attr && attr.nodeName && attr.nodeName.search(/^xmlns:?/) !== -1) {
nsArray.push({
prefix: attr.nodeName.replace(/^xmlns:/, ""),
prefix: attr.nodeName.replace(/^xmlns:?/, ""),
namespaceURI: attr.nodeValue,
});
}
Expand All @@ -259,6 +248,17 @@ function collectAncestorNamespaces(node, nsArray) {
return collectAncestorNamespaces(parent, nsArray);
}

function findNSPrefix(subset) {
const subsetAttributes = subset.attributes;
for (let k = 0; k < subsetAttributes.length; k++) {
const nodeName = subsetAttributes[k].nodeName;
if (nodeName.search(/^xmlns:?/) !== -1) {
return nodeName.replace(/^xmlns:?/, "");
}
}
return subset.prefix || "";
}

/**
* Xml signature implementation
*
Expand Down
26 changes: 26 additions & 0 deletions test/c14n-non-exclusive-unit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ exports[
test_findAncestorNs(test, xml, xpath, expected);
};

exports["findAncestorNs: Should not find namespace when both has no prefix"] = function (test) {
var xml = "<root xmlns='bbb'><child1><child2 xmlns='ddd'></child2></child1></root>";
var xpath = "//*[local-name()='child2']";
var expected = [];

test_findAncestorNs(xml, xpath, expected);
};

exports["findAncestorNs: Should find namespace without prefix"] = function (test) {
var xml =
"<root xmlns='bbb'><child1><ds:child2 xmlns:ds='ddd'><ds:child3></ds:child3></ds:child2></child1></root>";
var xpath = "//*[local-name()='child2']";
var expected = [{ prefix: "", namespaceURI: "bbb" }];

test_findAncestorNs(xml, xpath, expected);
};

exports["findAncestorNs: Ignores namespace declared in the target xpath node"] = function (test) {
var xml = "<root xmlns:aaa='bbb'><child1><child2 xmlns:ccc='ddd'></child2></child1></root>";
var xpath = "/root/child1/child2";
Expand Down Expand Up @@ -197,3 +214,12 @@ exports["C14n: Don't declare an attribute's namespace prefix if in scope from pa

test_C14nCanonicalization(test, xml, xpath, expected);
};

exports["C14n: should not has colon when parent namespace has no prefix"] = function (test) {
var xml =
"<root xmlns='bbb'><child1><cc:child2 xmlns:cc='ddd'><cc:child3></cc:child3></cc:child2></child1></root>";
var xpath = "//*[local-name()='child3']";
var expected = '<cc:child3 xmlns="bbb" xmlns:cc="ddd"></cc:child3>';

test_C14nCanonicalization(test, xml, xpath, expected);
};

0 comments on commit a31e7ff

Please sign in to comment.