From 03fe3b6a0db14ce32a0640dc48a95de94a1e6038 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 17 May 2024 14:38:46 +0100 Subject: [PATCH] Reject invalid types for puter.fs.upload() items or .write() data Previously, this would crash in upload() when trying to iterate the `entries` array, which is undefined when the `items` parameter is an unsupported type. --- .../puter-js/src/modules/FileSystem/operations/upload.js | 6 +++++- .../puter-js/src/modules/FileSystem/operations/write.js | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/puter-js/src/modules/FileSystem/operations/upload.js b/packages/puter-js/src/modules/FileSystem/operations/upload.js index b32da230..ab8778ea 100644 --- a/packages/puter-js/src/modules/FileSystem/operations/upload.js +++ b/packages/puter-js/src/modules/FileSystem/operations/upload.js @@ -122,7 +122,11 @@ const upload = async function(items, dirPath, options = {}){ entries[i].filepath = entries[i].name; entries[i].fullPath = entries[i].name; } - } + } + // Anything else is invalid + else { + return error({ code: 'field_invalid', message: 'upload() items parameter is an invalid type' }); + } // Will hold directories and files to be uploaded let dirs = []; diff --git a/packages/puter-js/src/modules/FileSystem/operations/write.js b/packages/puter-js/src/modules/FileSystem/operations/write.js index 6ed2fd1a..c1319ad4 100644 --- a/packages/puter-js/src/modules/FileSystem/operations/write.js +++ b/packages/puter-js/src/modules/FileSystem/operations/write.js @@ -46,6 +46,11 @@ const write = async function (targetPath, data, options = {}) { if(!data) data = new File([data ?? ''], filename); + // data should be a File now. If it's not, it's an unsupported type + if (!(data instanceof File)) { + throw new Error({ code: 'field_invalid', message: 'write() data parameter is an invalid type' }); + } + // perform upload return this.upload(data, parent, options); }