3

I'm trying to address the root element with the namespace and providing a reference to the library xml-crypto.

I'm not giving the path correctly, please advise. Objective is to sign the document so the signature can be inserted right after the tag <samlp:Response

<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ID="efedb3b0-909f-4b39-b8c0-57427ee8dc83" Version="2.0" IssueInstant="2019-11-08T15:34:51.272Z">

    <saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">http://www.example.com</saml:Issuer>

</samlp:Response>

nodeJS code

var SignedXml = require('xml-crypto').SignedXml, fs = require('fs');
var sig = new SignedXml();

sig.addReference("//*[local-name(.)='samlp:Response']");

sig.signingKey = fs.readFileSync(__dirname + "/client.pem");
sig.computeSignature(xml);
fs.writeFileSync(__dirname + "/signed.xml", sig.getSignedXml());

Attempts

sig.addReference("//samlp:Response");

Error: Cannot resolve QName samlp

it worked fine at https://www.freeformatter.com/xpath-tester.html though

AppDeveloper
  • 1,816
  • 7
  • 24
  • 49

1 Answers1

2

If you wish to defeat/bypass namespaces, then change

sig.addReference("//*[local-name(.)='samlp:Response']");

to

sig.addReference("//*[local-name()='Response']");

because the namespace prefix, samlp, is not part of the local name, Response.

For a comprehensive answer on namespaces in XPath, see How does XPath deal with XML namespaces?

kjhughes
  • 106,133
  • 27
  • 181
  • 240