From 85f4723b1bcc3fd051657d470d1d785e8942de0b Mon Sep 17 00:00:00 2001 From: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> Date: Tue, 27 Feb 2024 16:23:01 +0100 Subject: [PATCH 1/2] fix!: avoid binary file corruption in fetch action The `.toString()` caused all non utf-8 data to be corrupted on disk. This caused issues when downloading binary files such as zip archives. Fixes #22899 Signed-off-by: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> --- .changeset/eighty-suits-admire.md | 5 ++++ .../scaffolder-node/src/actions/fetch.test.ts | 26 +++++++++++++++++-- plugins/scaffolder-node/src/actions/fetch.ts | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 .changeset/eighty-suits-admire.md diff --git a/.changeset/eighty-suits-admire.md b/.changeset/eighty-suits-admire.md new file mode 100644 index 0000000000..5e4b065b2a --- /dev/null +++ b/.changeset/eighty-suits-admire.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-node': minor +--- + +**BREAKING** Fixed file corruption for non utf-8 data in fetch contents diff --git a/plugins/scaffolder-node/src/actions/fetch.test.ts b/plugins/scaffolder-node/src/actions/fetch.test.ts index 42f75d02b4..ea7f458fde 100644 --- a/plugins/scaffolder-node/src/actions/fetch.test.ts +++ b/plugins/scaffolder-node/src/actions/fetch.test.ts @@ -210,7 +210,26 @@ describe('fetchContents helper', () => { fetchUrl: 'https://github.com/backstage/foo', }); expect(fs.ensureDir).toHaveBeenCalledWith('.'); - expect(fs.outputFile).toHaveBeenCalledWith('foo', 'test'); + expect(fs.outputFile).toHaveBeenCalledWith( + 'foo', + Buffer.from([116, 101, 115, 116]), + ); + }); + + it('should fetch binary content from url', async () => { + readUrl.mockResolvedValue({ + buffer: () => Buffer.from([0, 1, 2, 3, 255, 254, 253, 252]), + }); + await fetchFile({ + ...options, + outputPath: 'foo', + fetchUrl: 'https://github.com/backstage/foo', + }); + expect(fs.ensureDir).toHaveBeenCalledWith('.'); + expect(fs.outputFile).toHaveBeenCalledWith( + 'foo', + Buffer.from([0, 1, 2, 3, 255, 254, 253, 252]), + ); }); it('should fetch content from url into directory', async () => { @@ -223,7 +242,10 @@ describe('fetchContents helper', () => { fetchUrl: 'https://github.com/backstage/foo', }); expect(fs.ensureDir).toHaveBeenCalledWith('mydir'); - expect(fs.outputFile).toHaveBeenCalledWith('mydir/foo', 'test'); + expect(fs.outputFile).toHaveBeenCalledWith( + 'mydir/foo', + Buffer.from([116, 101, 115, 116]), + ); }); it('should pass through the token provided through to the URL reader', async () => { diff --git a/plugins/scaffolder-node/src/actions/fetch.ts b/plugins/scaffolder-node/src/actions/fetch.ts index 2ddc2b1ae6..6b3e08bc7e 100644 --- a/plugins/scaffolder-node/src/actions/fetch.ts +++ b/plugins/scaffolder-node/src/actions/fetch.ts @@ -95,7 +95,7 @@ export async function fetchFile(options: { const res = await reader.readUrl(readUrl, { token }); await fs.ensureDir(path.dirname(outputPath)); const buffer = await res.buffer(); - await fs.outputFile(outputPath, buffer.toString()); + await fs.outputFile(outputPath, buffer); } } From 90cf6de034ba1da9573b60404a7c13953630ccf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Tue, 27 Feb 2024 16:39:32 +0100 Subject: [PATCH 2/2] Update .changeset/eighty-suits-admire.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/eighty-suits-admire.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/eighty-suits-admire.md b/.changeset/eighty-suits-admire.md index 5e4b065b2a..5b889c8087 100644 --- a/.changeset/eighty-suits-admire.md +++ b/.changeset/eighty-suits-admire.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-scaffolder-node': minor +'@backstage/plugin-scaffolder-node': patch --- -**BREAKING** Fixed file corruption for non utf-8 data in fetch contents +Fixed file corruption for non UTF-8 data in fetch contents