Skip to content

Commit 31f17cd

Browse files
committed
Add plugin loader access permission check.
1 parent 91f9a1e commit 31f17cd

File tree

1 file changed

+59
-45
lines changed

1 file changed

+59
-45
lines changed

lib/plugin_loader.js

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
var _ = require('lodash');
2+
var fs = require('fs');
23
var path = require('path');
34
var util = require('util');
45
var glob = require('glob');
56

67
var app = {};
78

89
function PluginLoader(_app) {
9-
var self = this;
10+
var self = this;
1011

11-
// global variables
12-
app = _app;
12+
// global variables
13+
app = _app;
1314

14-
// class variables
15-
self.plugins = {};
15+
// class variables
16+
self.plugins = {};
1617

17-
// Try to load global apidoc-plugins (if apidoc is installed locally it tries only local)
18-
this.detectPugins(__dirname);
18+
// Try to load global apidoc-plugins (if apidoc is installed locally it tries only local)
19+
this.detectPugins(__dirname);
1920

20-
// Try to load local apidoc-plugins
21-
this.detectPugins( path.join(process.cwd(), '/node_modules') );
21+
// Try to load local apidoc-plugins
22+
this.detectPugins( path.join(process.cwd(), '/node_modules') );
2223

23-
if (Object.keys(this.plugins).length === 0)
24-
app.log.debug('No plugins found.');
24+
if (Object.keys(this.plugins).length === 0) {
25+
app.log.debug('No plugins found.');
26+
}
2527

26-
this.loadPlugins();
28+
this.loadPlugins();
2729
}
2830
/**
2931
* Inherit
@@ -40,52 +42,64 @@ module.exports = PluginLoader;
4042
* Search up to root until found a plugin.
4143
*/
4244
PluginLoader.prototype.detectPugins = function(dir) {
43-
var self = this;
44-
45-
// Search from the given dir up to root.
46-
// Every dir start with "apidoc-plugin-", because for the tests of apidoc-plugin-test.
47-
var plugins = glob.sync(dir + '/apidoc-plugin-*');
48-
if (plugins.length === 0) {
49-
dir = path.join(dir, '..');
50-
if (dir === '/' || dir.substr(1) === ':\\')
51-
return;
52-
return this.detectPugins(dir);
45+
var self = this;
46+
47+
try {
48+
if (dir === '/') {
49+
fs.accessSync(dir + '.')
50+
} else {
51+
fs.accessSync(dir + '/.')
52+
}
53+
} catch (e) {
54+
app.log.warn(e);
55+
return;
56+
}
57+
58+
// Every dir start with "apidoc-plugin-", because for the tests of apidoc-plugin-test.
59+
var plugins = glob.sync(dir + '/apidoc-plugin-*');
60+
if (plugins.length === 0) {
61+
dir = path.join(dir, '..');
62+
if (dir === '/' || dir.substr(1) === ':\\') {
63+
return;
5364
}
65+
return this.detectPugins(dir);
66+
}
5467

55-
var offset = dir.length + 1;
56-
plugins.forEach( function(plugin) {
57-
var name = plugin.substr(offset);
58-
var filename = path.relative(__dirname, plugin);
59-
app.log.debug('add plugin: ' + name + ', ' + filename);
60-
self.addPlugin(name, plugin);
61-
});
68+
var offset = dir.length + 1;
69+
plugins.forEach( function(plugin) {
70+
var name = plugin.substr(offset);
71+
var filename = path.relative(__dirname, plugin);
72+
app.log.debug('add plugin: ' + name + ', ' + filename);
73+
self.addPlugin(name, plugin);
74+
});
6275
};
6376

6477
/**
6578
* Add Plugin to plugin list.
6679
*/
6780
PluginLoader.prototype.addPlugin = function(name, filename) {
68-
if (this.plugins[name])
69-
app.log.debug('overwrite plugin: ' + name + ', ' + this.plugins[name]);
81+
if (this.plugins[name]) {
82+
app.log.debug('overwrite plugin: ' + name + ', ' + this.plugins[name]);
83+
}
7084

71-
this.plugins[name] = filename;
85+
this.plugins[name] = filename;
7286
};
7387

7488
/**
7589
* Load and initialize Plugins.
7690
*/
7791
PluginLoader.prototype.loadPlugins = function() {
78-
_.forEach(this.plugins, function(filename, name) {
79-
app.log.debug('load plugin: ' + name + ', ' + filename);
80-
var plugin;
81-
try {
82-
plugin = require(filename);
83-
} catch(e) {
84-
}
85-
if (plugin && plugin.init) {
86-
plugin.init(app);
87-
} else {
88-
app.log.debug('Ignored, no init function found.');
89-
}
90-
});
92+
_.forEach(this.plugins, function(filename, name) {
93+
app.log.debug('load plugin: ' + name + ', ' + filename);
94+
var plugin;
95+
try {
96+
plugin = require(filename);
97+
} catch(e) {
98+
}
99+
if (plugin && plugin.init) {
100+
plugin.init(app);
101+
} else {
102+
app.log.debug('Ignored, no init function found.');
103+
}
104+
});
91105
};

0 commit comments

Comments
 (0)