From 4123256f03884b5cdd78ef85bb71c42d1e604346 Mon Sep 17 00:00:00 2001 From: Justin Williamson Date: Tue, 10 Mar 2020 16:31:55 -0500 Subject: [PATCH] escape single parenthesis when generating fdf files --- index.js | 17 +++++++++++++++-- pretest.js | 1 + test/files/form.escape.fdf | 24 ++++++++++++++++++++++++ test/fillForm.spec.js | 17 +++++++++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 test/files/form.escape.fdf diff --git a/index.js b/index.js index e3995a7..da0f359 100644 --- a/index.js +++ b/index.js @@ -177,6 +177,19 @@ class PdfTk { return new Buffer(str, encoding); } + /** + * Sanitizes fdf input + * @statuc + * @public + * @param {String} str - String to sanitize + * @returns {String} Sanitized string + */ + static sanitizeForFdf(str) { + return str + .replace(/\(/g, '\\(') + .replace(/\)/g, '\\)'); + } + /** * Creates fdf file from JSON input. * Converts input values to binary buffer, which seems to allow PdfTk to render utf-8 characters. @@ -208,7 +221,7 @@ class PdfTk { ]); body = Buffer.concat([ body, - PdfTk.stringToBuffer(prop.toString(), 'binary'), + PdfTk.stringToBuffer(PdfTk.sanitizeForFdf(prop.toString()), 'binary'), ]); body = Buffer.concat([ body, @@ -216,7 +229,7 @@ class PdfTk { ]); body = Buffer.concat([ body, - PdfTk.stringToBuffer(data[prop].toString(), 'binary'), + PdfTk.stringToBuffer(PdfTk.sanitizeForFdf(data[prop].toString()), 'binary'), ]); body = Buffer.concat([ body, diff --git a/pretest.js b/pretest.js index e468699..3ec1d65 100644 --- a/pretest.js +++ b/pretest.js @@ -24,6 +24,7 @@ const commands = [ 'test/files/form.pdf fill_form test/files/formwithnumber.fdf output test/files/filledformwithnumber.temp.pdf', 'test/files/form.pdf fill_form test/files/form.fdf output test/files/filledformflat.temp.pdf flatten', 'test/files/form.pdf fill_form test/files/form.blank.fdf output test/files/filledformempty.temp.pdf flatten', + 'test/files/form.pdf fill_form test/files/form.escape.fdf output test/files/filledformescape.temp.pdf flatten', 'test/files/form.pdf generate_fdf output test/files/form.temp.fdf', 'test/files/form.pdf stamp test/files/logo.pdf output test/files/stamp.temp.pdf', 'test/files/form.pdf multistamp test/files/logo.pdf output test/files/multistamp.temp.pdf', diff --git a/test/files/form.escape.fdf b/test/files/form.escape.fdf new file mode 100644 index 0000000..19505e1 --- /dev/null +++ b/test/files/form.escape.fdf @@ -0,0 +1,24 @@ +%FDF-1.2 +%���� +1 0 obj +<< +/FDF +<< +/Fields [ +<< +/V (test@email.com) +/T (email) +>> +<< +/V (John Doe\)) +/T (name) +>>] +>> +>> +endobj +trailer + +<< +/Root 1 0 R +>> +%%EOF diff --git a/test/fillForm.spec.js b/test/fillForm.spec.js index c8bf0a8..80e76ff 100644 --- a/test/fillForm.spec.js +++ b/test/fillForm.spec.js @@ -135,6 +135,23 @@ describe('fillForm', function () { .catch(err => expect(err).to.be.null); }); + it('should escape single parenthesis', function () { + + const testFile = fs.readFileSync(path.join(__dirname, './files/filledformescape.temp.pdf')); + const input = path.join(__dirname, './files/form.pdf'); + + return pdftk + .input(input) + .fillForm({ + name: 'John Doe)', + email: 'test@email.com', + }) + .flatten() + .output() + .then(buffer => expect(buffer.equals(testFile)).to.be.true) + .catch(err => expect(err).to.be.null); + }); + it('should catch an error if given a bad input path', function () { const input = path.join(__dirname, './files/doesnotexist.pdf');