GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — module: report unreadable package.json · nodejs/node@eb5d66e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • node_modules.cc
  • test/parallel
    • test-module-unreadable-package-json.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node_modules.cc‎

Copy file name to clipboardExpand all lines: src/node_modules.cc
+17-1Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,23 @@ 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+
// No file at this path, a path component that is not a directory, or a
117+
// "package.json" that is itself a directory all mean there is no package
118+
// config here. Any other failure means a package.json is present but
119+
// could not be read. Treating that as absent silently drops fields such
120+
// as "exports" and "type", which can resolve a specifier to a different
121+
// file, so surface the read error instead of continuing.
122+
if (read_error != UV_ENOENT && read_error != UV_ENOTDIR &&
123+
read_error != UV_EISDIR) {
124+
THROW_ERR_INVALID_PACKAGE_CONFIG(realm->isolate(),
125+
"Cannot read package config %s: %s.",
126+
path.data(),
127+
uv_strerror(read_error));
128+
return nullptr;
129+
}
130+
115131
// Add `nullopt` to the package config cache so that we don't
116132
// need to open and attempt to read this path again
117133
binding_data->package_configs_.insert({std::string(path), std::nullopt});
Collapse file

‎test/parallel/test-module-unreadable-package-json.js‎

Copy file name to clipboard
+57Lines 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.stdout.trim(), 'real');
44+
assert.strictEqual(child.status, 0, child.stderr);
45+
}
46+
47+
fs.chmodSync(depPackageJson, 0o000);
48+
49+
try {
50+
const child = spawnSync(process.execPath, [entry], { encoding: 'utf8' });
51+
// The read failure must be reported rather than resolving to index.js.
52+
assert.doesNotMatch(child.stdout, /decoy/);
53+
assert.match(child.stderr, /Cannot read package config/);
54+
assert.notStrictEqual(child.status, 0);
55+
} finally {
56+
fs.chmodSync(depPackageJson, 0o644);
57+
}

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.