-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
194 lines (167 loc) · 5.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var sanitizeHtml = require('sanitize-html'),
fs = require('fs-extra'),
pluginData = require('./plugin.json'),
path = require('path'),
async = require('async'),
extend = require('extend'),
$ = require('jquery'),
winston = module.parent.require('winston'),
meta = require.main.require('./src/meta'),
Plugin;
pluginData.nbbId = pluginData.id.replace(/nodebb-plugin-/, '');
Plugin = {
settings: async function(settings) {
if (settings) {
await meta.settings.set(pluginData.nbbId, settings);
} else {
await meta.settings.get(pluginData.nbbId)
Plugin.config = extend(true, {}, Plugin.config, config);
}
},
config: {
textFilter: function (text) {
return Plugin.unescapeHtml(text);
}
},
onLoad: async function (params) {
var app = params.router, middleware = params.middleware;
function render(req, res, next) {
res.render('admin/plugins/' + pluginData.nbbId, pluginData);
}
app.get('/admin/plugins/' + pluginData.nbbId, middleware.applyCSRF, middleware.admin.buildHeader, render);
app.get('/api/admin/plugins/' + pluginData.nbbId, middleware.applyCSRF, render);
await Plugin.init();
},
init: async function() {
// Load saved config
const defaults = pluginData.defaultSettings;
const fields = Object.keys(defaults);
const options = await meta.settings.get(pluginData.nbbId)
fields.forEach(function(field) {
const savedValue = options[field];
const defaultValue = defaults[field];
let obj;
if (field !== 'parseAgain') {
obj = !savedValue ? field === 'allowedAttributes' ? '{}' : '[]' : savedValue;
if (obj && typeof obj == 'string') {
try {
obj = JSON.parse(obj);
} catch (e) {
winston.warn('[plugins/' + pluginData.nbbId + '] e1: ' + e + ' can\'t JSON.parse option: ' + field + ' value: ' + obj + ' falling back to default.');
obj = JSON.parse(defaultValue);
}
}
} else {
var noop = function (c) {
return c;
};
try {
// Function.apply(context, args (csv string), function-code (string))
obj = Function.apply(Plugin, ['content, $', (savedValue ? savedValue : defaultValue) + '\nreturn content;' ]);
} catch (e) {
winston.warn('[plugins/' + pluginData.nbbId + '] e2: ' + e + ' can\'t parse function "' + field + '" falling back to noop.');
obj = noop;
}
// let's see if it doesn't crash
try {
obj("test", $, "1", "2", "3");
} catch (e) {
// if it did, then too bad, you had a good run, but no thanks
winston.warn('[plugins/' + pluginData.nbbId + '] e3: ' + e + ' | parseAgain code:[start] ' + obj + ' [end] has error, falling back to noop.');
obj = noop;
}
}
Plugin.config[field] = obj;
});
},
sanitize: function (content) {
return Plugin.config.parseAgain(sanitizeHtml(Plugin.unescapeHtml(content || ''), Plugin.config), $);
},
sanitizeSave: function (post) {
if (post && post.content) {
post.content = Plugin.sanitize(post.content);
}
return post
},
sanitizePost: function (data) {
if (data && data.postData && data.postData.content) {
data.postData.content = Plugin.sanitize(data.postData.content);
}
return data
},
sanitizeRaw: function (raw) {
return raw ? Plugin.sanitize(raw) : raw
},
sanitizeSignature: function (data) {
if (data && data.userData && data.userData.signature) {
data.userData.signature = Plugin.sanitize(data.userData.signature);
}
return data
},
unescapeHtml: function (unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, "\"")
.replace(/'/g, "'");
},
renderHelp: function (helpContent) {
helpContent += "<h2>Sanitized HTML</h2>"
+ "<p>You can still use safe HTML, provided by <a href='https://github.com/akhoury/nodebb-plugin-sanitizehtml'>nodebb-plugin-sanitizehtml</a></p>"
+ (function () {
var allowedTags = "<h4> Allowed Tags</h4>";
allowedTags += "<p class='text-muted'> ";
Plugin.config.allowedTags.forEach(function (tag, i) {
if (i > 0) {
allowedTags += ", ";
}
allowedTags += tag
});
allowedTags += !Plugin.config.allowedTags.length ? "No HTML tags allowed" : "";
allowedTags += "</p>";
return allowedTags;
})()
+ (function () {
var allowedAttributes = "<h4> Allowed Attributes</h4>";
allowedAttributes += "<p class='text-muted'> ";
var keys = Object.keys(Plugin.config.allowedAttributes || {});
keys.forEach(function (tagName, i) {
if (i > 0) {
allowedAttributes += ", ";
}
var tag = Plugin.config.allowedAttributes[tagName];
tag.forEach(function (attr, j) {
if (j > 0) {
allowedAttributes += ", ";
}
allowedAttributes += tagName + "." + attr;
});
});
allowedAttributes += "</p>";
return allowedAttributes;
})()
+ "<p class='text-warning'> Eveything else will be sanitized</p>";
return helpContent
},
admin: {
menu: function (custom_header) {
custom_header.plugins.push({
"route": '/plugins/' + pluginData.nbbId,
"icon": pluginData.faIcon,
"name": pluginData.name
});
return custom_header
},
activate: function (id) {
if (id === 'nodebb-plugin-' + pluginData.nbbId) {
var defaults = pluginData.defaultSettings;
async.each(defaults, function (optObj, next) {
meta.settings.setOnEmpty(pluginData.nbbId, optObj.field, optObj.value, next);
});
}
}
}
};
Plugin.init();
module.exports = Plugin;