albanm/node-libxslt

stylesheet.apply mutates parameters object

Closed this issue · 0 comments

A call to the apply method of the stylesheet object returned from the parse operation modifies the parameters passed as the 2nd argument to enclose strings in double quotes.
The object passed in should not be modified.
Subsequent calls to apply result in the string being enclosed in more double quotes.

const libxslt = require('libxslt');

const xslt = `<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="Param1"/>
  <xsl:template match="/">
    <xsl:value-of select="$Param1"/>
  </xsl:template>
</xsl:stylesheet>`;

libxslt.parse(xslt, (err, stylesheet) =>
{
    let Params = {
        Param1: 'hello'
    };
    stylesheet.apply(`<xml></xml>`, Params, (err, result) =>
    {
        console.log(result);
        console.log(Params);
        console.assert(Params.Param1 === 'hello');
    });
});

gives

<?xml version="1.0"?>
hello

{ Param1: '"hello"' }

assert.js:85
  throw new assert.AssertionError({
  ^
AssertionError: false == true
    at Console.assert (console.js:95:23)
    at stylesheet.apply (/path/to/file.js:20:17)
    at /path/to/node_modules/libxslt/index.js:149:4

Additionally, double quotes within the parameter string are not escaped.

const libxslt = require('libxslt');

const xslt = `<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="Param1"/>
  <xsl:template match="/">
    <xsl:value-of select="$Param1"/>
  </xsl:template>
</xsl:stylesheet>`;

libxslt.parse(xslt, (err, stylesheet) =>
{
    let Params = {
        Param1: 'he"llo'
    };
    stylesheet.apply(`<xml></xml>`, Params, (err, result) =>
    {
     
    });
});

This error is not even sent to the callback but instead causes the application to crash.

XPath error : Invalid expression
"he"llo"
    ^
runtime error
Evaluating user parameter Param1 failed
Segmentation fault