From 7a5cb137b599094a019904ad6053031829dfb18c Mon Sep 17 00:00:00 2001 From: Kenneth Feng Date: Fri, 22 Oct 2021 21:45:04 -0400 Subject: [PATCH] Always use base64 encoding to prevent interpreting binary files as utf-8 text files Signed-off-by: Kenneth Feng --- .../builtin/publish/githubPullRequest.test.ts | 16 ++++++++-------- .../actions/builtin/publish/githubPullRequest.ts | 14 +++++++++++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.test.ts index 1de9a7e268..27c6f1e59d 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.test.ts @@ -99,8 +99,8 @@ describe('createPublishGithubPullRequestAction', () => { commit: 'Create my new app', files: { 'file.txt': { - encoding: 'utf-8', - content: 'Hello there!', + content: Buffer.from('Hello there!').toString('base64'), + encoding: 'base64', mode: '100644', }, }, @@ -172,8 +172,8 @@ describe('createPublishGithubPullRequestAction', () => { commit: 'Create my new app', files: { 'foo.txt': { - content: 'Hello there!', - encoding: 'utf-8', + content: Buffer.from('Hello there!').toString('base64'), + encoding: 'base64', mode: '100644', }, }, @@ -230,8 +230,8 @@ describe('createPublishGithubPullRequestAction', () => { commit: 'Create my new app', files: { 'file.txt': { - content: 'Hello there!', - encoding: 'utf-8', + content: Buffer.from('Hello there!').toString('base64'), + encoding: 'base64', mode: '100644', }, }, @@ -298,8 +298,8 @@ describe('createPublishGithubPullRequestAction', () => { commit: 'Create my new app', files: { 'file.txt': { - content: 'Hello there!', - encoding: 'utf-8', + content: Buffer.from('Hello there!').toString('base64'), + encoding: 'base64', mode: '100755', }, }, diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.ts index aec59c86fd..21361072ef 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.ts @@ -196,15 +196,23 @@ export const createPublishGithubPullRequestAction = ({ const fileContents = await Promise.all( localFilePaths.map(filePath => { const absPath = path.resolve(fileRoot, filePath); - const content = fs.readFileSync(absPath).toString(); + const base64EncodedContent = fs + .readFileSync(absPath) + .toString('base64'); const fileStat = fs.statSync(absPath); const isExecutable = fileStat.mode === 33277; // aka. 100755 // See the properties of tree items // in https://docs.github.com/en/rest/reference/git#trees const githubTreeItemMode = isExecutable ? '100755' : '100644'; + // Always use base64 encoding to avoid doubling a binary file in size + // due to interpreting a binary file as utf-8 and sending github + // the utf-8 encoded content. + // + // For example, the original gradle-wrapper.jar is 57.8k in https://github.com/kennethzfeng/pull-request-test/pull/5/files. + // Its size could be doubled to 98.3K (See https://github.com/kennethzfeng/pull-request-test/pull/4/files) return { - encoding: 'utf-8', - content: content, + encoding: 'base64', + content: base64EncodedContent, mode: githubTreeItemMode, }; }),