Skip to content

Commit fd6bc4d

Browse files
committed
Remove daemon support
The "daemon" feature contains a security vulnerability [1]. This feature relies on a beta release (e.g. version 0.5.1) of a Node.js module on npm--one that was superseded by a stable (e.g. version 1.0) release published three years ago [2]. Due to a build-time dependency on the long-since deprecated `node-waf` tool, the module at that version can only be built for Node.js versions 0.8 and below. Given this, actual usage of this feature is likely very limited. Remove it completely so the integrity of this module's core functionality can be verified. [1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=809252 [2] The source expects the module to define a property named `deamonize`, which was removed for the 1.0 release: https://github.com/indexzero/daemon.node/tree/v1.0.0
1 parent 9f4c00b commit fd6bc4d

File tree

5 files changed

+4
-212
lines changed

5 files changed

+4
-212
lines changed

README.md

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -39,50 +39,6 @@ cli.withStdinLines(function(lines, newline) {
3939
});
4040
```
4141

42-
### static.js
43-
44-
Let's create a static file server with daemon support to see the opts parser + plugins in use - note: this requires `npm install creationix daemon`
45-
46-
```javascript
47-
var cli = require('cli').enable('daemon', 'status'); //Enable 2 plugins
48-
49-
cli.parse({
50-
log: ['l', 'Enable logging'],
51-
port: ['p', 'Listen on this port', 'number', 8080],
52-
serve: [false, 'Serve static files from PATH', 'path', './public']
53-
});
54-
55-
cli.main(function(args, options) {
56-
var server, middleware = [];
57-
58-
if (options.log) {
59-
this.debug('Enabling logging');
60-
middleware.push(require('creationix/log')());
61-
}
62-
63-
this.debug('Serving files from ' + options.serve);
64-
middleware.push(require('creationix/static')('/', options.serve, 'index.html'));
65-
66-
server = this.createServer(middleware).listen(options.port);
67-
68-
this.ok('Listening on port ' + options.port);
69-
});
70-
```
71-
72-
To output usage information
73-
74-
```bash
75-
$ ./static.js --help
76-
```
77-
78-
To create a daemon that serves files from */tmp*, run
79-
80-
```bash
81-
$ ./static.js -ld --serve=/tmp
82-
```
83-
84-
For more examples, see [./examples](https://github.com/chriso/cli/tree/master/examples)
85-
8642
## Command Line Arguments Parser
8743

8844
cli takes an object as a map for the arguments you wish to parse.
@@ -209,12 +165,6 @@ cli.ok(msg);
209165

210166
Enables glob matching of arguments
211167

212-
**daemon** - *requires* `npm install daemon`
213-
214-
Adds `-d,--daemon ARG` for daemonizing the process and controlling the resulting daemon
215-
216-
`ARG` can be either start (default), stop, restart, pid (outputs the daemon's pid if it's running), or log (output the daemon's stdout+stderr)
217-
218168
**timeout**
219169

220170
Adds `-t,--timeout N` to exit the process after N seconds with an error

cli.js

Lines changed: 3 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var cli = exports,
2828
argv, curr_opt, curr_val, full_opt, is_long,
2929
short_tags = [], opt_list, parsed = {},
3030
usage, argv_parsed, command_list, commands,
31-
daemon, daemon_arg, show_debug;
31+
show_debug;
3232

3333
cli.app = null;
3434
cli.version = null;
@@ -85,7 +85,6 @@ if (process.env.NODE_DISABLE_COLORS || process.env.TERM === 'dumb') {
8585
var enable = {
8686
help: true, //Adds -h, --help
8787
version: false, //Adds -v,--version => gets version by parsing a nearby package.json
88-
daemon: false, //Adds -d,--daemon [ARG] => (see cli.daemon() below)
8988
status: false, //Adds -k,--no-color & --debug => display plain status messages /display debug messages
9089
timeout: false, //Adds -t,--timeout N => timeout the process after N seconds
9190
catchall: false, //Adds -c,--catch => catch and output uncaughtExceptions
@@ -94,16 +93,6 @@ var enable = {
9493
cli.enable = function (/*plugins*/) {
9594
Array.prototype.slice.call(arguments).forEach(function (plugin) {
9695
switch (plugin) {
97-
case 'daemon':
98-
try {
99-
daemon = require('daemon');
100-
if (typeof daemon.daemonize !== 'function') {
101-
throw 'Invalid module';
102-
}
103-
} catch (e) {
104-
cli.fatal('daemon.node not installed. Please run `npm install daemon`');
105-
}
106-
break;
10796
case 'catchall':
10897
process.on('uncaughtException', function (err) {
10998
cli.error('Uncaught exception: ' + (err.msg || err));
@@ -347,9 +336,6 @@ cli.parse = function (opts, command_def) {
347336
console.error(cli.app + ' v' + cli.version);
348337
cli.exit();
349338
break;
350-
} else if (enable.daemon && (o === 'd' || o === 'daemon')) {
351-
daemon_arg = cli.getArrayValue(['start','stop','restart','pid','log'], is_long ? null : 'start');
352-
continue;
353339
} else if (enable.catchall && (o === 'c' || o === 'catch')) {
354340
continue;
355341
} else if (enable.status && (o === 'k' || o === 'no-color')) {
@@ -667,9 +653,6 @@ cli.getUsage = function (code) {
667653
if (enable.catchall && seen_opts.indexOf('c') === -1 && seen_opts.indexOf('catch') === -1) {
668654
console.error(pad(' -c, --catch', switch_pad) + 'Catch unanticipated errors');
669655
}
670-
if (enable.daemon && seen_opts.indexOf('d') === -1 && seen_opts.indexOf('daemon') === -1) {
671-
console.error(pad(' -d, --daemon [ARG]', switch_pad) + 'Daemonize the process. Control the daemon using [start, stop, restart, log, pid]');
672-
}
673656
if (enable.version && seen_opts.indexOf('v') === -1 && seen_opts.indexOf('version') === -1) {
674657
console.error(pad(' -v, --version', switch_pad) + 'Display the current version');
675658
}
@@ -965,102 +948,13 @@ cli.toType = function(obj) {
965948
}
966949

967950
/**
968-
* A method for creating and controlling a daemon.
969-
*
970-
* `arg` can be:
971-
* start = daemonizes the process
972-
* stop = stops the daemon if it is running
973-
* restart = alias for stop -> start
974-
* pid = outputs the daemon's PID if it is running
975-
* log = outputs the daemon's log file (stdout + stderr)
976-
*
977-
* @param {String} arg (Optional - default is 'start')
978-
* @param {Function} callback
979-
* @api public
980-
*/
981-
cli.daemon = function (arg, callback) {
982-
if (typeof daemon === 'undefined') {
983-
cli.fatal('Daemon is not initialized');
984-
}
985-
986-
if (typeof arg === 'function') {
987-
callback = arg;
988-
arg = 'start';
989-
}
990-
991-
var lock_file = '/tmp/' + cli.app + '.pid',
992-
log_file = '/tmp/' + cli.app + '.log';
993-
994-
var start = function () {
995-
daemon.daemonize(log_file, lock_file, function (err) {
996-
if (err) return cli.error('Error starting daemon: ' + err);
997-
callback();
998-
});
999-
};
1000-
1001-
var stop = function () {
1002-
try {
1003-
cli.native.fs.readFileSync(lock_file);
1004-
} catch (e) {
1005-
return cli.error('Daemon is not running');
1006-
}
1007-
daemon.kill(lock_file, function (err, pid) {
1008-
if (err && err.errno === 3) {
1009-
return cli.error('Daemon is not running');
1010-
} else if (err) {
1011-
return cli.error('Error stopping daemon: ' + err.errno);
1012-
}
1013-
cli.ok('Successfully stopped daemon with pid: ' + pid);
1014-
});
1015-
};
1016-
1017-
switch(arg) {
1018-
case 'stop':
1019-
stop();
1020-
break;
1021-
case 'restart':
1022-
daemon.stop(lock_file, function () {
1023-
start();
1024-
});
1025-
break;
1026-
case 'log':
1027-
try {
1028-
cli.native.fs.createReadStream(log_file, {encoding: 'utf8'}).pipe(process.stdout);
1029-
} catch (e) {
1030-
return cli.error('No daemon log file');
1031-
}
1032-
break;
1033-
case 'pid':
1034-
try {
1035-
var pid = cli.native.fs.readFileSync(lock_file, 'utf8');
1036-
cli.native.fs.statSync('/proc/' + pid);
1037-
cli.info(pid);
1038-
} catch (e) {
1039-
return cli.error('Daemon is not running');
1040-
}
1041-
break;
1042-
default:
1043-
start();
1044-
break;
1045-
}
1046-
}
1047-
1048-
/**
1049-
* The main entry method. Calling cli.main() is only necessary in
1050-
* scripts that have daemon support enabled. `callback` receives (args, options)
951+
* The main entry method. `callback` receives (args, options)
1051952
*
1052953
* @param {Function} callback
1053954
* @api public
1054955
*/
1055956
cli.main = function (callback) {
1056-
var after = function () {
1057-
callback.apply(cli, [cli.args, cli.options]);
1058-
};
1059-
if (enable.daemon && daemon_arg) {
1060-
cli.daemon(daemon_arg, after);
1061-
} else {
1062-
after();
1063-
}
957+
callback.call(cli, cli.args, cli.options);
1064958
}
1065959

1066960
/**

examples/static.coffee

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/static.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"description" : "A tool for rapidly building command line apps",
33
"version" : "0.11.3",
44
"homepage" : "http://github.com/node-js-libs/cli",
5-
"keywords" : ["cli","command line","opts","parseopt","opt","args","console","argsparse","optparse","daemon","autocomplete","command","autocompletion"],
5+
"keywords" : ["cli","command line","opts","parseopt","opt","args","console","argsparse","optparse","autocomplete","command","autocompletion"],
66
"author" : "Chris O'Hara <cohara87@gmail.com>",
77
"main" : "cli.js",
88
"bugs": {

0 commit comments

Comments
 (0)