Skip to content

Commit 5bffa31

Browse files
author
Jon
committed
Added downgradeToUser start option to allow safely running SocketCluster as root by downgrading to a user with fewer privileges after binding to ports.
1 parent dcbdd45 commit 5bffa31

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

balancer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ if (cluster.isMaster) {
8484
balancer.setWorkers(m.data);
8585
}
8686
});
87-
}
87+
}

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ SocketCluster.prototype._init = function (options) {
6969
storeController: null,
7070
rebootOnSignal: true,
7171
useSmartBalancing: false,
72+
downgradeToUser: false,
7273
clusterEngine: 'iocluster'
7374
};
7475

@@ -312,6 +313,7 @@ SocketCluster.prototype._initLoadBalancer = function () {
312313
checkStatusTimeout: this.options.connectTimeout * 1000,
313314
statusURL: this._paths.statusURL,
314315
statusCheckInterval: this.options.workerStatusInterval * 1000,
316+
downgradeToUser: this.options.downgradeToUser,
315317
appBalancerControllerPath: this._paths.appBalancerControllerPath
316318
}
317319
});
@@ -536,8 +538,9 @@ SocketCluster.prototype._start = function () {
536538
self._ioCluster = new self._clusterEngine.IOCluster({
537539
stores: self.options.stores,
538540
dataKey: self._dataKey,
539-
appStoreControllerPath: self._paths.appStoreControllerPath,
540-
expiryAccuracy: self._dataExpiryAccuracy
541+
expiryAccuracy: self._dataExpiryAccuracy,
542+
downgradeToUser: self.options.downgradeToUser,
543+
appStoreControllerPath: self._paths.appStoreControllerPath
541544
});
542545

543546
self._ioCluster.on('error', function (err) {

scworker.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ SCWorker.prototype._init = function (options) {
4545
for (var i in options) {
4646
this.options[i] = options[i];
4747
}
48+
49+
if (this.options.downgradeToUser && process.setuid) {
50+
try {
51+
process.setuid(this.options.downgradeToUser);
52+
} catch (err) {
53+
this._errorDomain.emit('error', new Error('Could not downgrade to user "' + this.options.downgradeToUser +
54+
'" - Either this user does not exist or the current process does not have the permission' +
55+
' to switch to it.'));
56+
}
57+
}
4858

4959
if (this.options.dataKey == null) {
5060
this.options.dataKey = crypto.randomBytes(32).toString('hex');
@@ -224,4 +234,4 @@ SCWorker.prototype.noticeHandler = function (notice) {
224234
this.emit(this.EVENT_NOTICE, notice);
225235
};
226236

227-
module.exports = SCWorker;
237+
module.exports = SCWorker;

worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ process.on('message', function (m) {
5050
worker.handleMasterEvent(m.event);
5151
}
5252
}
53-
});
53+
});

0 commit comments

Comments
 (0)