-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (39 loc) · 1.72 KB
/
Copy pathindex.js
File metadata and controls
44 lines (39 loc) · 1.72 KB
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
'use strict';
const assert = require('assert').strict;
const {createLogger} = require('ep_plugin_helpers/logger');
const logger = createLogger('ep_disable_imports');
let allow = new Set();
let deny = new Set();
class PolyfillImportError extends Error {
constructor(ignored, ...args) {
super(...args);
if (Error.captureStackTrace) Error.captureStackTrace(this, PolyfillImportError);
}
}
exports.import = async (hookName, {fileEnding, ImportError = PolyfillImportError}) => {
if (deny.has(fileEnding) || (allow != null && !allow.has(fileEnding))) {
logger.error(`denying import of file with extension ${JSON.stringify(fileEnding)}`);
throw new ImportError('permission', `forbidden file extension: ${JSON.stringify(fileEnding)}`);
}
};
exports.loadSettings = async (hookName, {settings: {ep_disable_imports: settings}}) => {
if (settings == null) settings = {};
assert.equal(typeof settings, 'object');
if (settings.allow == null && settings.deny == null) settings.allow = [];
if (settings.deny == null) settings.deny = [];
assert(settings.allow == null || Array.isArray(settings.allow));
assert(Array.isArray(settings.deny));
const fixExtension = (ext) => {
if (!ext.startsWith('.')) ext = `.${ext}`;
return ext.toLowerCase();
};
deny = new Set(settings.deny.map(fixExtension));
allow = settings.allow == null
? null : new Set(settings.allow.map(fixExtension).filter((x) => !deny.has(x)));
const displayExts = (exts) => [...exts].sort().map(JSON.stringify).join(', ');
const humanReadable =
allow == null ? `all${deny.size === 0 ? '' : ` except ${displayExts(deny)}`}`
: allow.size === 0 ? 'none'
: displayExts(allow);
logger.info(`allowed extensions: ${humanReadable}`);
};