Skip to content

Commit dde6bf0

Browse files
aileenacburdine
authored andcommitted
feat(doctor): prevent installations with ghost user
refs #47 - Adds a check to the `install` category - Check is disabled for local installs - Check will prevent user from installation, if the `ghost` user exists and user is logged in as such - Adds tests. Still 💯
1 parent cdc9c31 commit dde6bf0

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

lib/commands/doctor/checks/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const nodeVersion = require('./node-version');
3+
const loggedInUser = require('./logged-in-user');
34
const loggedInGhostUser = require('./logged-in-ghost-user');
45
const loggedInUserOwner = require('./logged-in-user-owner');
56
const installFolderPermissions = require('./install-folder-permissions');
@@ -13,6 +14,7 @@ const checkMemory = require('./check-memory');
1314

1415
module.exports = [
1516
nodeVersion,
17+
loggedInUser,
1618
loggedInGhostUser,
1719
loggedInUserOwner,
1820
installFolderPermissions,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
const errors = require('../../../errors');
3+
const chalk = require('chalk');
4+
const ghostUser = require('../../../utils/use-ghost-user');
5+
6+
const taskTitle = 'Checking logged in user';
7+
8+
function loggedInUser() {
9+
const uid = process.getuid();
10+
const ghostStats = ghostUser.getGhostUid();
11+
12+
if (ghostStats && ghostStats.uid === uid) {
13+
throw new errors.SystemError({
14+
message: 'You can\'t install Ghost with a user called `ghost`. Please use a different user(name).',
15+
help: `${chalk.green('https://docs.ghost.org/docs/install#section-create-a-new-user')}`,
16+
task: taskTitle
17+
});
18+
}
19+
20+
return;
21+
}
22+
23+
module.exports = {
24+
title: taskTitle,
25+
task: loggedInUser,
26+
enabled: (ctx) => !ctx.local && !(ctx.instance && ctx.instance.process.name === 'local') && ctx.system.platform.linux && !(ctx.argv && ctx.argv.process === 'local'),
27+
category: ['install']
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'use strict';
2+
const expect = require('chai').expect;
3+
const sinon = require('sinon');
4+
const errors = require('../../../../../lib/errors');
5+
const ghostUser = require('../../../../../lib/utils/use-ghost-user');
6+
7+
const loggedInUser = require('../../../../../lib/commands/doctor/checks/logged-in-user');
8+
9+
describe('Unit: Doctor Checks > loggedInUser', function () {
10+
const sandbox = sinon.sandbox.create();
11+
12+
afterEach(() => {
13+
sandbox.restore();
14+
});
15+
16+
it('enabled works', function () {
17+
expect(loggedInUser.enabled({
18+
local: true,
19+
system: {platform: {linux: true}},
20+
argv: {}
21+
}), 'false if local is true').to.be.false;
22+
expect(loggedInUser.enabled({
23+
local: false,
24+
instance: {process: {name: 'local'}},
25+
system: {platform: {linux: false}}
26+
}), 'false if local is false and process name is local').to.be.false;
27+
expect(loggedInUser.enabled({
28+
local: false,
29+
instance: {process: {name: 'systemd'}},
30+
system: {platform: {linux: false}}
31+
}), 'false if local is false and process name is not local and platform is not linux').to.be.false;
32+
expect(loggedInUser.enabled({
33+
local: false,
34+
instance: {process: {name: 'systemd'}},
35+
system: {platform: {linux: true}}
36+
}), 'true if local is false and process name is not local and platform is linux').to.be.true;
37+
expect(loggedInUser.enabled({
38+
local: false,
39+
instance: {process: {name: 'systemd'}},
40+
system: {platform: {linux: true}},
41+
argv: {process: 'local'}
42+
}), 'false if local is false and process name is not local and platform is linux, but argv local is given').to.be.false;
43+
});
44+
45+
it('rejects if user name is ghost', function () {
46+
const processStub = sandbox.stub(process, 'getuid').returns(501);
47+
const ghostUserStub = sandbox.stub(ghostUser, 'getGhostUid').returns({uid: 501, guid: 501});
48+
49+
try {
50+
loggedInUser.task();
51+
} catch (error) {
52+
expect(error).to.exist;
53+
expect(error).to.be.an.instanceof(errors.SystemError);
54+
expect(processStub.calledOnce).to.be.true;
55+
expect(ghostUserStub.calledOnce).to.be.true;
56+
}
57+
});
58+
59+
it('passes if user name is not ghost', function () {
60+
const processStub = sandbox.stub(process, 'getuid').returns(1000);
61+
const ghostUserStub = sandbox.stub(ghostUser, 'getGhostUid').returns(false);
62+
63+
try {
64+
loggedInUser.task();
65+
expect(processStub.calledOnce).to.be.true;
66+
expect(ghostUserStub.calledOnce).to.be.true;
67+
} catch (error) {
68+
expect(error).to.not.exist;
69+
}
70+
});
71+
72+
it('passes if ghost user exists but not currently used', function () {
73+
const processStub = sandbox.stub(process, 'getuid').returns(1000);
74+
const ghostUserStub = sandbox.stub(ghostUser, 'getGhostUid').returns({uid: 501, guid: 501});
75+
76+
try {
77+
loggedInUser.task();
78+
expect(processStub.calledOnce).to.be.true;
79+
expect(ghostUserStub.calledOnce).to.be.true;
80+
} catch (error) {
81+
expect(error).to.not.exist;
82+
}
83+
});
84+
});

0 commit comments

Comments
 (0)