From 813340185d0e350b93419e09a53c27287e142df0 Mon Sep 17 00:00:00 2001 From: Luke Roy Date: Wed, 5 Apr 2023 21:11:11 +0200 Subject: [PATCH 1/4] Add tar.gz support to the nodejs runtime Proxy This new feature will allow for more versatile deployment packages and greater flexibility in handling compressed files. Giving an alternative to zip files. this feature does not impact the current functionality of the current proxy --- core/nodejsActionBase/runner.js | 49 ++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/core/nodejsActionBase/runner.js b/core/nodejsActionBase/runner.js index 6b5759c8..ef576ea9 100644 --- a/core/nodejsActionBase/runner.js +++ b/core/nodejsActionBase/runner.js @@ -27,7 +27,11 @@ const path = require('path'); function initializeActionHandler(message) { if (message.binary) { // The code is a base64-encoded zip file. - return unzipInTmpDir(message.code) + ext = detectFileType(message.code) + if (ext == 'unsupported'){ + return Promise.reject("The Filetype is not supported"); + } + return extractInTmpDir(message.code) .then(moduleDir => { let parts = splitMainHandler(message.main); if (parts === undefined) { @@ -138,21 +142,33 @@ class NodeActionRunner { * Note that this makes heavy use of shell commands because the environment is expected * to provide the required executables. */ -function unzipInTmpDir(zipFileContents) { +function extractInTmpDir(archiveFileContents) { const mkTempCmd = "mktemp -d XXXXXXXX"; return exec(mkTempCmd).then(tmpDir => { return new Promise((resolve, reject) => { - const zipFile = path.join(tmpDir, "action.zip"); - fs.writeFile(zipFile, zipFileContents, "base64", err => { - if (!err) resolve(zipFile); + ext = detectFileType(archiveFileContents) + if (ext == 'unsupported'){ + reject("There was an error Detecting the File type"); + } + const archiveFile = path.join(tmpDir, "action."+ ext); + fs.writeFile(archiveFile, archiveFileContents, "base64", err => { + if (!err) resolve(archiveFile); else reject("There was an error reading the action archive."); }); }); - }).then(zipFile => { + }).then(archiveFile => { return exec(mkTempCmd).then(tmpDir => { - return exec("unzip -qq " + zipFile + " -d " + tmpDir) + if (ext === 'zip') { + return exec("unzip -qq " + archiveFile + " -d " + tmpDir) + .then(res => path.resolve(tmpDir)) + .catch(error => Promise.reject("There was an error uncompressing the action archive.")); + } else if (ext === 'tar.gz') { + return exec("tar -xzf " + archiveFile + " -C " + tmpDir + " > /dev/null") .then(res => path.resolve(tmpDir)) .catch(error => Promise.reject("There was an error uncompressing the action archive.")); + } else { + return Promise.reject("Unsupported archive type."); + } }); }); } @@ -198,3 +214,22 @@ module.exports = { NodeActionRunner, initializeActionHandler }; + +// helper function to detect if base64string is zip or tar.gz +// and returns the file ending +function detectFileType(base64String) { + // Decode the base64 string into binary data + const binaryData = Buffer.from(base64String, 'base64'); + + // Examine the first few bytes of the binary data to determine the file type + const magicNumber = binaryData.slice(0, 4).toString('hex'); + + if (magicNumber === '504b0304') { + return 'zip'; + // GZIP: 1f8b0808 maximum compression level, 1f8b0800 default compression + } else if (magicNumber === '1f8b0808' || magicNumber === '1f8b0800') { + return 'tar.gz'; + } else { + return 'unsupported'; + } + } \ No newline at end of file From 9f9607054f7ce2916de76a74cf83833370837acd Mon Sep 17 00:00:00 2001 From: Luke Roy Date: Wed, 5 Apr 2023 21:25:57 +0200 Subject: [PATCH 2/4] fix whitespace and eol --- core/nodejsActionBase/runner.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/core/nodejsActionBase/runner.js b/core/nodejsActionBase/runner.js index ef576ea9..0667a997 100644 --- a/core/nodejsActionBase/runner.js +++ b/core/nodejsActionBase/runner.js @@ -162,7 +162,7 @@ function extractInTmpDir(archiveFileContents) { return exec("unzip -qq " + archiveFile + " -d " + tmpDir) .then(res => path.resolve(tmpDir)) .catch(error => Promise.reject("There was an error uncompressing the action archive.")); - } else if (ext === 'tar.gz') { + } else if (ext === 'tar.gz') { return exec("tar -xzf " + archiveFile + " -C " + tmpDir + " > /dev/null") .then(res => path.resolve(tmpDir)) .catch(error => Promise.reject("There was an error uncompressing the action archive.")); @@ -220,16 +220,17 @@ module.exports = { function detectFileType(base64String) { // Decode the base64 string into binary data const binaryData = Buffer.from(base64String, 'base64'); - + // Examine the first few bytes of the binary data to determine the file type const magicNumber = binaryData.slice(0, 4).toString('hex'); - + if (magicNumber === '504b0304') { return 'zip'; // GZIP: 1f8b0808 maximum compression level, 1f8b0800 default compression - } else if (magicNumber === '1f8b0808' || magicNumber === '1f8b0800') { + } else if (magicNumber === '1f8b0808' || magicNumber === '1f8b0800') { return 'tar.gz'; } else { return 'unsupported'; } - } \ No newline at end of file + } + \ No newline at end of file From 97bc8604f57e4e09f3390f5b87a468fab203abe4 Mon Sep 17 00:00:00 2001 From: Luke Roy Date: Wed, 5 Apr 2023 21:27:11 +0200 Subject: [PATCH 3/4] whitespace + eol --- core/nodejsActionBase/runner.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/nodejsActionBase/runner.js b/core/nodejsActionBase/runner.js index 0667a997..7579276e 100644 --- a/core/nodejsActionBase/runner.js +++ b/core/nodejsActionBase/runner.js @@ -232,5 +232,4 @@ function detectFileType(base64String) { } else { return 'unsupported'; } - } - \ No newline at end of file +} From 5cacb5356235f422e0a0bd448779fccc96e79140 Mon Sep 17 00:00:00 2001 From: Luke Roy Date: Wed, 5 Apr 2023 22:07:37 +0200 Subject: [PATCH 4/4] Change error message to handle test case There was an error uncompressing the action archive. --- core/nodejsActionBase/runner.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/nodejsActionBase/runner.js b/core/nodejsActionBase/runner.js index 7579276e..5ed211c9 100644 --- a/core/nodejsActionBase/runner.js +++ b/core/nodejsActionBase/runner.js @@ -29,7 +29,7 @@ function initializeActionHandler(message) { // The code is a base64-encoded zip file. ext = detectFileType(message.code) if (ext == 'unsupported'){ - return Promise.reject("The Filetype is not supported"); + return Promise.reject("There was an error uncompressing the action archive."); } return extractInTmpDir(message.code) .then(moduleDir => { @@ -167,7 +167,7 @@ function extractInTmpDir(archiveFileContents) { .then(res => path.resolve(tmpDir)) .catch(error => Promise.reject("There was an error uncompressing the action archive.")); } else { - return Promise.reject("Unsupported archive type."); + return Promise.reject("There was an error uncompressing the action archive."); } }); });