Always use base64 encoding to prevent interpreting binary files as utf-8 text files

Signed-off-by: Kenneth Feng <kenneth.feng@onepeloton.com>
This commit is contained in:
Kenneth Feng
2021-10-22 21:45:04 -04:00
parent b960209976
commit 7a5cb137b5
2 changed files with 19 additions and 11 deletions
@@ -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',
},
},
@@ -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,
};
}),