-
-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command) prevent commands run as root user
refs #47 - adds a `checkRootUser` fn to utils, which prevents any command to be run as `root` unless, it is set with the `allowRoot` property - adds test
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const chalk = require('chalk'); | ||
|
||
function checkRootUser() { | ||
if (process.getuid() === 0) { | ||
console.error(`${chalk.yellow('Can\'t run command as \'root\' user.')} | ||
Please create a new user with regular account privileges and use this user to run the command. | ||
See ${chalk.underline.blue('https://docs.ghost.org/docs/install#section-create-a-new-user')} for more information`); | ||
|
||
process.exit(1); | ||
} | ||
} | ||
|
||
module.exports = checkRootUser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
const expect = require('chai').expect; | ||
const sinon = require('sinon'); | ||
const checkRootUser = require('../../../lib/utils/check-root-user'); | ||
|
||
describe('checkRootUser', function () { | ||
const sandbox = sinon.sandbox.create(); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}) | ||
|
||
it('throws error command run with root', function () { | ||
const processStub = sandbox.stub(process, 'getuid').returns(0); | ||
const exitStub = sandbox.stub(process, 'exit').throws(); | ||
const errorStub = sandbox.stub(console, 'error'); | ||
|
||
try { | ||
checkRootUser('test'); | ||
throw new Error('should not be thrown'); | ||
} catch (e) { | ||
expect(e.message).to.not.equal('should not be thrown'); | ||
expect(processStub.calledOnce).to.be.true; | ||
expect(errorStub.calledOnce).to.be.true; | ||
expect(exitStub.calledOnce).to.be.true; | ||
expect(errorStub.args[0][0]).to.match(/Can't run command as 'root' user/); | ||
} | ||
}); | ||
|
||
it('doesn\'t do anything if command run as non root user', function () { | ||
const processStub = sandbox.stub(process, 'getuid').returns(501); | ||
const exitStub = sandbox.stub(process, 'exit').throws(); | ||
const errorStub = sandbox.stub(console, 'error'); | ||
|
||
checkRootUser('test'); | ||
expect(processStub.calledOnce).to.be.true; | ||
expect(errorStub.calledOnce).to.be.false; | ||
expect(exitStub.calledOnce).to.be.false; | ||
}); | ||
}); |