-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsensitive-module-hook.js
233 lines (207 loc) · 7.91 KB
/
sensitive-module-hook.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* @license
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
/**
* @fileoverview
* Checks that modules that are trying to require sensitive modules are allowed.
*
* See documentation for the config parameter below.
*/
// SENSITIVE - Gates access to modules that are known to be prone to misuse
// in ways that have security consequences.
// GUARANTEE - the output of makeModuleHook only returns a module
// identifier M if M will not resolve to a sensitive module or if the
// requestor is on that module's whitelist or that sensitive module is
// configured in "mode":"report-only".
// This guarantee should survive in the face of an attacker who can
// create files including symlinks and hardlinks but makes no
// guarantee in the face of race conditions related to renaming
// ancestor directories, or unmounting file systems.
// This may assume that ./innocuous.js is not a sensitive module.
// eslint-disable-next-line no-use-before-define
exports.makeHook = makeHook;
const { setTimeout, Set } = global;
const { isArray, prototype: { filter }, from: arrayFrom } = Array;
const { stringify } = JSON;
const { create, entries } = Object;
const { apply } = Reflect;
const { warn } = console;
const { has: setHas } = Set.prototype;
const { replace, toLowerCase } = String.prototype;
const { basename, join, relative, resolve, sep } = require('path');
const process = require('process');
const { isBuiltinModuleId } = require('../builtin-module-ids.js');
const { isProduction } = require('../is-prod.js');
const isFileSystemCaseSensitive = (() => {
try {
require.resolve(`./${ basename(__filename).toUpperCase() }`);
return true;
} catch (exc) {
return false;
}
})();
/**
* Returns a hook compatible with `node --cjs-loader`.
*
* @param {!Object} sensitiveModuleConfig maps ids (resolved relative to base path)
* of sensitive modules to arrays of ids of modules allowed to import the
* sensitive module.
* If the id maps to an object instead, it should look like
* {
* "ids": [ "id0", "id1", "id2" ], // Allowed to import
* "mode": "enforce", // enforce (default) or report-only
* "advice": "Dumped to console to suggest to developer alternatives or how to get approved"
* }
* @param {string} basePath path to project root.
* @param {boolean} reportOnly true to not provide an innocuous replacement
* for unauthorized requests to a sensitive module.
* @return {!Function} a CJS module hook.
*/
function makeHook(sensitiveModuleConfig, basePath, reportOnly = false) {
const missingConfig = !sensitiveModuleConfig;
basePath = resolve(basePath);
const moduleid = relative(basePath, __filename);
// eslint-disable-next-line no-underscore-dangle
const rootResolvePaths = [ basePath, ...module.constructor._nodeModulePaths(basePath) ];
function rootResolve(x) {
return require.resolve(x, { paths: rootResolvePaths });
}
function toModuleKey(id, resolveModuleId = rootResolve) {
if (isBuiltinModuleId(id)) {
return id;
}
let moduleKey = relative(basePath, resolveModuleId(id));
if (isFileSystemCaseSensitive) {
// TODO: just because one physical device is case-insensitive does not
// mean all are. Is there a reliably way to canonicalize
moduleKey = apply(toLowerCase, moduleKey, []);
}
return moduleKey;
}
function fileToModuleKey(filePath) {
let moduleKey = filePath;
if (sep === '\\') {
moduleKey = apply(replace, moduleKey, [ /\\/g, '/' ]);
}
if (isFileSystemCaseSensitive) {
// TODO: just because one physical device is case-insensitive does not
// mean all are. Is there a reliably way to canonicalize
moduleKey = apply(toLowerCase, moduleKey, []);
}
return moduleKey;
}
// eslint-disable-next-line complexity
function unpackConfig() {
const sensitiveModules = new Map();
const moduleAdvice = new Map();
const reportOnlyModules = new Set();
for (const [ src, val ] of entries(sensitiveModuleConfig || {})) {
let advice = null;
let mode = null;
let dests = null;
const key = toModuleKey(src);
if (typeof val === 'object' && val && !isArray(val)) {
({ ids: dests, advice, mode } = val);
} else {
dests = val;
}
if (typeof dests === 'string' && dests) {
dests = [ dests ];
}
if (isArray(dests)) {
dests = [ ...dests ];
} else {
dests = [];
}
dests = apply(filter, dests, [ (x) => typeof x === 'string' ]);
if (typeof advice === 'string') {
moduleAdvice.set(key, advice);
}
if (mode === 'report-only') {
reportOnlyModules.add(key);
}
sensitiveModules.set(
key,
new Set(dests.map((str) => toModuleKey(str))));
}
return { sensitiveModules, moduleAdvice, reportOnlyModules };
}
const { sensitiveModules, moduleAdvice, reportOnlyModules } = unpackConfig();
const isSensitive = sensitiveModules.has.bind(sensitiveModules);
const getImporters = sensitiveModules.get.bind(sensitiveModules);
const getAdvice = moduleAdvice.get.bind(moduleAdvice);
const isModuleReportOnly = reportOnlyModules.has.bind(reportOnlyModules);
let notifyTimeout = null;
let notifications = [];
const advised = new Set();
function notify() {
notifyTimeout = null;
const list = notifications;
notifications = [];
const byModuleKey = create(null);
for (let i = 0, { length } = list; i < length; ++i) {
const element = list[i];
const { moduleKey, requiredId, importingRelFile, enforce } = element;
let group = byModuleKey[moduleKey];
if (!group) {
group = { moduleKey, requiredId, importingRelFiles: new Set(), enforce };
byModuleKey[moduleKey] = group;
}
group.importingRelFiles.add(importingRelFile);
}
for (const moduleKey in byModuleKey) {
const { requiredId, importingRelFiles, enforce } = byModuleKey[moduleKey];
const action = enforce ? 'Blocking' : 'Reporting';
let msg = `${ moduleid }: ${ action } require(${ stringify(requiredId) }) by ${ arrayFrom(importingRelFiles) }`;
if (!isProduction && !advised.has(moduleKey)) {
advised.add(moduleKey);
const advice = getAdvice(moduleKey);
if (advice) {
msg = `${ msg }\n\n\t${ advice }`;
}
}
warn(msg);
}
}
process.on('beforeExit', () => {
if (notifyTimeout !== null) {
notify();
}
});
function sensitiveModuleHook(importingFile, importingId, requiredId, resolveModuleId) {
const moduleKey = toModuleKey(requiredId, resolveModuleId);
if (missingConfig || isSensitive(moduleKey)) {
const importingRelFile = relative(basePath, importingFile);
const importingKey = fileToModuleKey(importingRelFile);
const allowedImporters = getImporters(moduleKey);
if (!apply(setHas, allowedImporters, [ importingKey ])) {
const enforce = reportOnly !== true && !isModuleReportOnly(moduleKey);
notifications[notifications.length] = { moduleKey, requiredId, importingRelFile, importingId, enforce };
if (notifyTimeout === null) {
// eslint-disable-next-line no-magic-numbers
notifyTimeout = setTimeout(notify, 100);
}
if (enforce) {
return join(__dirname, 'innocuous.js');
}
}
}
return requiredId;
}
sensitiveModuleHook.flush = notify;
return sensitiveModuleHook;
}