Skip to content

Commit

Permalink
fix: Command Injection Vuln
Browse files Browse the repository at this point in the history
  • Loading branch information
commenthol committed Feb 19, 2023
1 parent 1f61c21 commit 2ca1288
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
62 changes: 37 additions & 25 deletions lib/gitfn.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
'use strict'

var child = require('child_process')

function GitFn (version, options) {
this._version = version
this._options = {
cwd: options.dir,
env: process.env,
setsid: false,
stdio: [0, 1, 2]
const child = require('child_process')
const semver = require('semver')

const assertVersionValid = version => {
if (!semver.valid(version)) {
throw new Error('version is invalid')
}
}
module.exports = GitFn

GitFn.prototype = {
tag: function (cb) {
var cmd = ['git', 'tag', 'v' + this._version].join(' ')
this._exec(cmd, cb)
},
untag: function (cb) {
var cmd = ['git', 'tag', '-d', 'v' + this._version].join(' ')
this._exec(cmd, cb)
},
commit: function (cb) {
var cmd = ['git', 'commit', '-am', '"' + this._version + '"'].join(' ')
this._exec(cmd, cb)
},
_exec: function (cmd, cb) {
child.exec(cmd, this._options, cb)
const exec = (cmd, options, cb) => child.exec(cmd, options, cb)

class GitFn {
constructor (version, options) {
this._version = version
this._options = {
cwd: options.dir,
env: process.env,
setsid: false,
stdio: [0, 1, 2]
}
}

tag (cb) {
assertVersionValid(this._version)
const cmd = ['git', 'tag', 'v' + this._version].join(' ')
exec(cmd, this._options, cb)
}

untag (cb) {
assertVersionValid(this._version)
const cmd = ['git', 'tag', '-d', 'v' + this._version].join(' ')
exec(cmd, this._options, cb)
}

commit (cb) {
assertVersionValid(this._version)
const cmd = ['git', 'commit', '-am', '"' + this._version + '"'].join(' ')
exec(cmd, this._options, cb)
}
}

module.exports = GitFn
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,14 @@ describe('change multiple files', function () {
done()
})
})

it('shall throw if not a valid version', function (done) {
const gitFn = new Version._.GitFn('& touch newFile', { dir: './' })
try {
gitFn.tag(done)
} catch (e) {
assert.strictEqual(e.message, 'version is invalid')
done()
}
})
})

0 comments on commit 2ca1288

Please sign in to comment.