From e5952f082ec3115bee2e931c7afaadfdbda84733 Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Sun, 22 May 2022 17:59:27 +0900 Subject: [PATCH] fix buffer parse error message of evaluateNodeProperty --- packages/node_modules/@node-red/util/lib/util.js | 7 ++++++- test/unit/@node-red/util/lib/util_spec.js | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/node_modules/@node-red/util/lib/util.js b/packages/node_modules/@node-red/util/lib/util.js index a6fec8f98..d82de0d1d 100644 --- a/packages/node_modules/@node-red/util/lib/util.js +++ b/packages/node_modules/@node-red/util/lib/util.js @@ -641,7 +641,12 @@ function evaluateNodeProperty(value, type, node, msg, callback) { result = Date.now(); } else if (type === 'bin') { var data = JSON.parse(value); - result = Buffer.from(data); + if (Array.isArray(data) || (typeof(data) === "string")) { + result = Buffer.from(data); + } + else { + throw createError("INVALID_BUFFER_DATA", "Not string or array"); + } } else if (type === 'msg' && msg) { try { result = getMessageProperty(msg,value); diff --git a/test/unit/@node-red/util/lib/util_spec.js b/test/unit/@node-red/util/lib/util_spec.js index 2013f143e..3bab2739a 100644 --- a/test/unit/@node-red/util/lib/util_spec.js +++ b/test/unit/@node-red/util/lib/util_spec.js @@ -388,6 +388,19 @@ describe("@node-red/util/util", function() { result[0].should.eql(1); result[1].should.eql(2); }); + it('throws an error if buffer data is not array or string', function (done) { + try { + var result = util.evaluateNodeProperty('12','bin'); + done("should throw an error"); + } catch (err) { + if (err.code === "INVALID_BUFFER_DATA") { + done(); + } + else { + done("should throw an error"); + } + } + }); it('returns msg property',function() { var result = util.evaluateNodeProperty('foo.bar','msg',{},{foo:{bar:"123"}}); result.should.eql("123");