Skip to content

Commit 7e8ad9b

Browse files
committed
policy: add dependencies map for resources
Adds a "dependencies" field to resources in policy manifest files. In order to ease development and testing while using manifests, wildcard values for both "dependencies" and "integrity" have been added using the boolean value "true" in the policy manifest. PR-URL: #28767 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 499533f commit 7e8ad9b

19 files changed

+411
-63
lines changed

doc/api/errors.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,13 @@ An attempt was made to load a resource, but the resource did not match the
14321432
integrity defined by the policy manifest. See the documentation for [policy]
14331433
manifests for more information.
14341434

1435+
<a id="ERR_MANIFEST_DEPENDENCY_MISSING"></a>
1436+
### ERR_MANIFEST_DEPENDENCY_MISSING
1437+
1438+
An attempt was made to load a resource, but the resource was not listed as a
1439+
dependency from the location that attempted to load it. See the documentation
1440+
for [policy] manifests for more information.
1441+
14351442
<a id="ERR_MANIFEST_INTEGRITY_MISMATCH"></a>
14361443
### ERR_MANIFEST_INTEGRITY_MISMATCH
14371444

@@ -1440,6 +1447,13 @@ entries for a resource which did not match each other. Update the manifest
14401447
entries to match in order to resolve this error. See the documentation for
14411448
[policy] manifests for more information.
14421449

1450+
<a id="ERR_MANIFEST_INVALID_RESOURCE_FIELD"></a>
1451+
### ERR_MANIFEST_INVALID_RESOURCE_FIELD
1452+
1453+
A policy manifest resource had an invalid value for one of its fields. Update
1454+
the manifest entry to match in order to resolve this error. See the
1455+
documentation for [policy] manifests for more information.
1456+
14431457
<a id="ERR_MANIFEST_PARSE_POLICY"></a>
14441458
### ERR_MANIFEST_PARSE_POLICY
14451459

doc/api/policy.md

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ node --experimental-policy=policy.json app.js
3838
The policy manifest will be used to enforce constraints on code loaded by
3939
Node.js.
4040

41-
In order to mitigate tampering with policy files on disk, an integrity for
41+
To mitigate tampering with policy files on disk, an integrity for
4242
the policy file itself may be provided via `--policy-integrity`.
4343
This allows running `node` and asserting the policy file contents
4444
even if the file is changed on disk.
@@ -105,9 +105,83 @@ When loading resources the entire URL must match including search parameters
105105
and hash fragment. `./a.js?b` will not be used when attempting to load
106106
`./a.js` and vice versa.
107107

108-
In order to generate integrity strings, a script such as
108+
To generate integrity strings, a script such as
109109
`printf "sha384-$(cat checked.js | openssl dgst -sha384 -binary | base64)"`
110110
can be used.
111111

112+
Integrity can be specified as the boolean value `true` to accept any
113+
body for the resource which can be useful for local development. It is not
114+
recommended in production since it would allow unexpected alteration of
115+
resources to be considered valid.
116+
117+
### Dependency Redirection
118+
119+
An application may need to ship patched versions of modules or to prevent
120+
modules from allowing all modules access to all other modules. Redirection
121+
can be used by intercepting attempts to load the modules wishing to be
122+
replaced.
123+
124+
```json
125+
{
126+
"builtins": [],
127+
"resources": {
128+
"./app/checked.js": {
129+
"dependencies": {
130+
"fs": true,
131+
"os": "./app/node_modules/alt-os"
132+
}
133+
}
134+
}
135+
}
136+
```
137+
138+
The dependencies are keyed by the requested string specifier and have values
139+
of either `true` or a string pointing to a module that will be resolved.
140+
141+
The specifier string does not perform any searching and must match exactly
142+
what is provided to the `require()`. Therefore, multiple specifiers may be
143+
needed in the policy if `require()` uses multiple different strings to point
144+
to the same module (such as excluding the extension).
145+
146+
If the value of the redirection is `true` the default searching algorithms will
147+
be used to find the module.
148+
149+
If the value of the redirection is a string, it will be resolved relative to
150+
the manifest and then immediately be used without searching.
151+
152+
Any specifier string that is `require()`ed and not listed in the dependencies
153+
will result in an error according to the policy.
154+
155+
Redirection will not prevent access to APIs through means such as direct access
156+
to `require.cache` and/or through `module.constructor` which allow access to
157+
loading modules. Policy redirection only affect specifiers to `require()`.
158+
Other means such as to prevent undesired access to APIs through variables are
159+
necessary to lock down that path of loading modules.
160+
161+
A boolean value of `true` for the dependencies map can be specified to allow a
162+
module to load any specifier without redirection. This can be useful for local
163+
development and may have some valid usage in production, but should be used
164+
only with care after auditing a module to ensure its behavior is valid.
165+
166+
#### Example: Patched Dependency
167+
168+
Since a dependency can be redirected, you can provide attenuated or modified
169+
forms of dependencies as fits your application. For example, you could log
170+
data about timing of function durations by wrapping the original:
171+
172+
```js
173+
const original = require('fn');
174+
module.exports = function fn(...args) {
175+
console.time();
176+
try {
177+
return new.target ?
178+
Reflect.construct(original, args) :
179+
Reflect.apply(original, this, args);
180+
} finally {
181+
console.timeEnd();
182+
}
183+
};
184+
```
185+
112186

113187
[relative url string]: https://url.spec.whatwg.org/#relative-url-with-fragment-string

lib/internal/errors.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,9 +1033,15 @@ E('ERR_MANIFEST_ASSERT_INTEGRITY',
10331033
}
10341034
return msg;
10351035
}, Error);
1036+
E('ERR_MANIFEST_DEPENDENCY_MISSING',
1037+
'Manifest resource %s does not list %s as a dependency specifier',
1038+
Error);
10361039
E('ERR_MANIFEST_INTEGRITY_MISMATCH',
10371040
'Manifest resource %s has multiple entries but integrity lists do not match',
10381041
SyntaxError);
1042+
E('ERR_MANIFEST_INVALID_RESOURCE_FIELD',
1043+
'Manifest resource %s has invalid property value for %s',
1044+
TypeError);
10391045
E('ERR_MANIFEST_TDZ', 'Manifest initialization has not yet run', Error);
10401046
E('ERR_MANIFEST_UNKNOWN_ONERROR',
10411047
'Manifest specified unknown error behavior "%s".',

lib/internal/modules/cjs/helpers.js

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,72 @@
11
'use strict';
22

33
const { Object } = primordials;
4+
const {
5+
ERR_MANIFEST_DEPENDENCY_MISSING,
6+
ERR_UNKNOWN_BUILTIN_MODULE
7+
} = require('internal/errors').codes;
8+
const { NativeModule } = require('internal/bootstrap/loaders');
9+
const { getOptionValue } = require('internal/options');
10+
const experimentalModules = getOptionValue('--experimental-modules');
411

512
const { validateString } = require('internal/validators');
613
const path = require('path');
7-
const { pathToFileURL } = require('internal/url');
14+
const { pathToFileURL, fileURLToPath } = require('internal/url');
815
const { URL } = require('url');
916

17+
const debug = require('internal/util/debuglog').debuglog('module');
18+
19+
function loadNativeModule(filename, request, experimentalModules) {
20+
const mod = NativeModule.map.get(filename);
21+
if (mod) {
22+
debug('load native module %s', request);
23+
mod.compileForPublicLoader(experimentalModules);
24+
return mod;
25+
}
26+
}
27+
1028
// Invoke with makeRequireFunction(module) where |module| is the Module object
1129
// to use as the context for the require() function.
12-
function makeRequireFunction(mod) {
30+
// Use redirects to set up a mapping from a policy and restrict dependencies
31+
function makeRequireFunction(mod, redirects) {
1332
const Module = mod.constructor;
1433

15-
function require(path) {
16-
return mod.require(path);
34+
let require;
35+
if (redirects) {
36+
const { map, reaction } = redirects;
37+
const id = mod.filename || mod.id;
38+
require = function require(path) {
39+
let missing = true;
40+
if (map === true) {
41+
missing = false;
42+
} else if (map.has(path)) {
43+
const redirect = map.get(path);
44+
if (redirect === true) {
45+
missing = false;
46+
} else if (typeof redirect === 'string') {
47+
const parsed = new URL(redirect);
48+
if (parsed.protocol === 'node:') {
49+
const specifier = parsed.pathname;
50+
const mod = loadNativeModule(
51+
specifier,
52+
redirect,
53+
experimentalModules);
54+
if (mod && mod.canBeRequiredByUsers) return mod.exports;
55+
throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier);
56+
} else if (parsed.protocol === 'file:') {
57+
return mod.require(fileURLToPath(parsed));
58+
}
59+
}
60+
}
61+
if (missing) {
62+
reaction(new ERR_MANIFEST_DEPENDENCY_MISSING(id, path));
63+
}
64+
return mod.require(path);
65+
};
66+
} else {
67+
require = function require(path) {
68+
return mod.require(path);
69+
};
1770
}
1871

1972
function resolve(request, options) {
@@ -114,6 +167,7 @@ function normalizeReferrerURL(referrer) {
114167
module.exports = {
115168
addBuiltinLibsToObject,
116169
builtinLibs,
170+
loadNativeModule,
117171
makeRequireFunction,
118172
normalizeReferrerURL,
119173
stripBOM,

lib/internal/modules/cjs/loader.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const {
4646
makeRequireFunction,
4747
normalizeReferrerURL,
4848
stripBOM,
49+
loadNativeModule
4950
} = require('internal/modules/cjs/helpers');
5051
const { getOptionValue } = require('internal/options');
5152
const preserveSymlinks = getOptionValue('--preserve-symlinks');
@@ -618,11 +619,8 @@ Module._load = function(request, parent, isMain) {
618619
return cachedModule.exports;
619620
}
620621

621-
const mod = NativeModule.map.get(filename);
622-
if (mod && mod.canBeRequiredByUsers) {
623-
debug('load native module %s', request);
624-
return mod.compileForPublicLoader(experimentalModules);
625-
}
622+
const mod = loadNativeModule(filename, request, experimentalModules);
623+
if (mod && mod.canBeRequiredByUsers) return mod.exports;
626624

627625
// Don't call updateChildren(), Module constructor already does.
628626
const module = new Module(filename, parent);
@@ -828,8 +826,11 @@ function wrapSafe(filename, content) {
828826
// the file.
829827
// Returns exception, if any.
830828
Module.prototype._compile = function(content, filename) {
829+
let moduleURL;
830+
let redirects;
831831
if (manifest) {
832-
const moduleURL = pathToFileURL(filename);
832+
moduleURL = pathToFileURL(filename);
833+
redirects = manifest.getRedirects(moduleURL);
833834
manifest.assertIntegrity(moduleURL, content);
834835
}
835836

@@ -853,7 +854,7 @@ Module.prototype._compile = function(content, filename) {
853854
}
854855
}
855856
const dirname = path.dirname(filename);
856-
const require = makeRequireFunction(this);
857+
const require = makeRequireFunction(this, redirects);
857858
var result;
858859
const exports = this.exports;
859860
const thisValue = exports;
@@ -942,7 +943,7 @@ function createRequireFromPath(filename) {
942943
m.filename = proxyPath;
943944

944945
m.paths = Module._nodeModulePaths(m.path);
945-
return makeRequireFunction(m);
946+
return makeRequireFunction(m, null);
946947
}
947948

948949
Module.createRequireFromPath = deprecate(

lib/internal/modules/esm/translators.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const {
99
StringPrototype
1010
} = primordials;
1111

12-
const { NativeModule } = require('internal/bootstrap/loaders');
1312
const {
14-
stripBOM
13+
stripBOM,
14+
loadNativeModule
1515
} = require('internal/modules/cjs/helpers');
1616
const CJSModule = require('internal/modules/cjs/loader').Module;
1717
const internalURLModule = require('internal/url');
@@ -93,11 +93,10 @@ translators.set('builtin', async function builtinStrategy(url) {
9393
debug(`Translating BuiltinModule ${url}`);
9494
// Slice 'node:' scheme
9595
const id = url.slice(5);
96-
const module = NativeModule.map.get(id);
96+
const module = loadNativeModule(id, url, true);
9797
if (!module) {
9898
throw new ERR_UNKNOWN_BUILTIN_MODULE(id);
9999
}
100-
module.compileForPublicLoader(true);
101100
return createDynamicModule(
102101
[], [...module.exportKeys, 'default'], url, (reflect) => {
103102
debug(`Loading BuiltinModule ${url}`);

0 commit comments

Comments
 (0)