Skip to content

Commit b4271b7

Browse files
committed
module: report unreadable package.json
A package.json that exists but cannot be read was treated the same as one that is not there: the read failure returned no config and resolution continued as if the package had none. Fields such as "exports" and "type" silently disappear, so a specifier can resolve to a different file than the package declares, while an unparsable package.json already throws ERR_INVALID_PACKAGE_CONFIG. Keep treating ENOENT and ENOTDIR as "no package config here", and report any other read failure with the underlying error. Fixes: #65220 Signed-off-by: Paul Bouchon <mail@bitpshr.net>
1 parent 4a5eb1c commit b4271b7

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

src/node_modules.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,22 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON(
111111
PackageConfig package_config{};
112112
package_config.file_path = path;
113113
// No need to exclude BOM since simdjson will skip it.
114-
if (ReadFileSync(&package_config.raw_json, path.data()) < 0) {
114+
int read_error = ReadFileSync(&package_config.raw_json, path.data());
115+
if (read_error < 0) {
116+
// A file that is not there, or a path that turns out not to be a
117+
// directory, simply means there is no package config here. Any other
118+
// failure means a package.json is present but could not be read.
119+
// Treating that as absent silently drops fields such as "exports" and
120+
// "type", which can resolve a specifier to a different file, so surface
121+
// the read error instead of continuing.
122+
if (read_error != UV_ENOENT && read_error != UV_ENOTDIR) {
123+
THROW_ERR_INVALID_PACKAGE_CONFIG(realm->isolate(),
124+
"Cannot read package config %s: %s.",
125+
path.data(),
126+
uv_strerror(read_error));
127+
return nullptr;
128+
}
129+
115130
// Add `nullopt` to the package config cache so that we don't
116131
// need to open and attempt to read this path again
117132
binding_data->package_configs_.insert({std::string(path), std::nullopt});
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
// A package.json that exists but cannot be read must not be treated as
4+
// absent. Doing so silently drops fields such as "exports", which can resolve
5+
// a specifier to a different file than the one the package declares.
6+
// Refs: https://github.com/nodejs/node/issues/65220
7+
8+
const common = require('../common');
9+
10+
if (common.isWindows) {
11+
common.skip('chmod does not restrict reads on Windows');
12+
}
13+
if (process.getuid?.() === 0) {
14+
common.skip('cannot make a file unreadable as root');
15+
}
16+
17+
const assert = require('assert');
18+
const fs = require('fs');
19+
const path = require('path');
20+
const { spawnSync } = require('child_process');
21+
const tmpdir = require('../common/tmpdir');
22+
23+
tmpdir.refresh();
24+
25+
const depDir = tmpdir.resolve('node_modules/dep');
26+
fs.mkdirSync(path.join(depDir, 'lib'), { recursive: true });
27+
const depPackageJson = path.join(depDir, 'package.json');
28+
fs.writeFileSync(
29+
depPackageJson,
30+
'{"name":"dep","exports":{".":"./lib/real.js"}}',
31+
);
32+
fs.writeFileSync(path.join(depDir, 'lib', 'real.js'), 'export const which = "real";');
33+
// If the package config is ignored, resolution falls back to this file.
34+
fs.writeFileSync(path.join(depDir, 'index.js'), 'export const which = "decoy";');
35+
36+
fs.writeFileSync(tmpdir.resolve('package.json'), '{"type":"module"}');
37+
const entry = tmpdir.resolve('main.mjs');
38+
fs.writeFileSync(entry, 'import { which } from "dep"; console.log(which);');
39+
40+
// Sanity check: the export resolves while the package config is readable.
41+
{
42+
const child = spawnSync(process.execPath, [entry], { encoding: 'utf8' });
43+
assert.strictEqual(child.status, 0, child.stderr);
44+
assert.strictEqual(child.stdout.trim(), 'real');
45+
}
46+
47+
fs.chmodSync(depPackageJson, 0o000);
48+
49+
{
50+
const child = spawnSync(process.execPath, [entry], { encoding: 'utf8' });
51+
// The read failure must be reported rather than resolving to index.js.
52+
assert.notStrictEqual(child.status, 0);
53+
assert.doesNotMatch(child.stdout, /decoy/);
54+
assert.match(child.stderr, /Cannot read package config/);
55+
}
56+
57+
fs.chmodSync(depPackageJson, 0o644);

0 commit comments

Comments
 (0)