Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit eb5f70e

Browse files
committed
fix(fs-git): fix command injection
1 parent 50b33e3 commit eb5f70e

File tree

5 files changed

+3045
-2575
lines changed

5 files changed

+3045
-2575
lines changed

lib/index.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
"use strict";
2-
3-
// if you use Node.js 0.10, you need exec `require("es6-promise").polyfill();`
4-
51
import * as child_process from "child_process";
62

73
export function open(path: string, ref?: string): Promise<FSGit> {
8-
"use strict";
9-
104
return Promise.resolve(new FSGit(path, ref));
115
}
126

@@ -35,17 +29,17 @@ export class FSGit {
3529
showRef(): Promise<RefInfo[]> {
3630
let command = this._buildCommand("show-ref");
3731
return new Promise((resolve: (value: RefInfo[]) => void, reject: (error: any) => void) => {
38-
child_process.exec(command, { maxBuffer: maxBuffer }, (error, stdout, stderr) => {
32+
child_process.execFile(command.base, command.args, { encoding: "buffer", maxBuffer: maxBuffer }, (error, stdout, stderr) => {
3933
if (error) {
4034
reject(error);
4135
} else {
4236
let list = stdout.toString("utf8").split("\n").filter(line => !!line);
43-
let resultList: RefInfo[] = list.map(str=> {
37+
let resultList: RefInfo[] = list.map(str => {
4438
let columns = str.split(" ", 2);
4539
return {
4640
gitDir: this.path,
4741
ref: columns[0],
48-
name: columns[1]
42+
name: columns[1],
4943
};
5044
});
5145
resolve(resultList);
@@ -61,7 +55,7 @@ export class FSGit {
6155
readFile(path: string, opts?: { encoding: string; }): Promise<any> {
6256
let command = this._buildCommand("show", this.ref + ":" + path);
6357
return new Promise((resolve: (value: any) => void, reject: (error: any) => void) => {
64-
child_process.exec(command, { maxBuffer: maxBuffer }, (error, stdout, stderr) => {
58+
child_process.execFile(command.base, command.args, { encoding: "buffer", maxBuffer: maxBuffer }, (error, stdout, stderr) => {
6559
if (error) {
6660
reject(error);
6761
} else {
@@ -76,14 +70,14 @@ export class FSGit {
7670
}
7771

7872
exists(path: string): Promise<boolean> {
79-
return this.fileList().then(list=> list.some(data => data.path === path));
73+
return this.fileList().then(list => list.some(data => data.path === path));
8074
}
8175

8276
revParse(ref: string): Promise<string> {
8377
let command = this._buildCommand("rev-parse", ref);
8478

8579
return new Promise((resolve: (value?: any) => void, reject: (error: any) => void) => {
86-
child_process.exec(command, { maxBuffer: maxBuffer }, (error, stdout, stderr) => {
80+
child_process.execFile(command.base, command.args, { encoding: "buffer", maxBuffer: maxBuffer }, (error, stdout, stderr) => {
8781
if (error) {
8882
console.log(command);
8983
reject(error);
@@ -96,23 +90,23 @@ export class FSGit {
9690
}
9791

9892
_lsTree(ref = this.ref, path = "."): Promise<FileInfo[]> {
99-
return this.revParse(ref).then(ref=> {
93+
return this.revParse(ref).then(ref => {
10094
let command = this._buildCommand("ls-tree", "-r", "-z", "--full-name", ref, path);
10195
return new Promise((resolve: (value: FileInfo[]) => void, reject: (error: any) => void) => {
102-
child_process.exec(command, { maxBuffer: maxBuffer }, (error, stdout, stderr) => {
96+
child_process.execFile(command.base, command.args, { encoding: "buffer", maxBuffer: maxBuffer }, (error, stdout, stderr) => {
10397
if (error) {
10498
reject(error);
10599
} else {
106100
let list = stdout.toString("utf8").split("\0").filter(str => str.length !== 0);
107-
let resultList: FileInfo[] = list.map(str=> {
101+
let resultList: FileInfo[] = list.map(str => {
108102
let matches = str.match(/^([0-9]+)\s([^\s]+)\s([0-9a-f]+)\t(.+)$/);
109103
return {
110104
gitDir: this.path,
111105
ref: ref,
112106
permission: matches[1],
113107
type: matches[2],
114108
hash: matches[3],
115-
path: matches[4]
109+
path: matches[4],
116110
};
117111
});
118112
resolve(resultList);
@@ -122,8 +116,11 @@ export class FSGit {
122116
});
123117
}
124118

125-
_buildCommand(...args: string[]): string {
126-
return `git --git-dir=${this.path} ${args.join(" ") }`;
119+
_buildCommand(...args: string[]): { base: string; args: string[]; } {
120+
return {
121+
base: "git",
122+
args: [`--git-dir=${this.path}`, ...args],
123+
};
127124
}
128125
}
129126

0 commit comments

Comments
 (0)