@gkiely/safe-install 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +92 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +175 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Grant Kiely
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# safe-install
|
|
2
|
+
|
|
3
|
+
Run npm installs with dependency lifecycle scripts disabled by default, then
|
|
4
|
+
rebuild only the packages you explicitly trust.
|
|
5
|
+
|
|
6
|
+
`safe-install` is for npm projects that want trusted dependency installs without
|
|
7
|
+
switching package managers.
|
|
8
|
+
|
|
9
|
+
## Why
|
|
10
|
+
|
|
11
|
+
npm lifecycle scripts can run arbitrary code during install. Setting
|
|
12
|
+
`ignore-scripts=true` blocks that whole class of install-time execution, but it
|
|
13
|
+
also breaks packages that legitimately need `postinstall`, `install`, or
|
|
14
|
+
`preinstall` scripts to build native bindings, download binaries, or finish
|
|
15
|
+
setup.
|
|
16
|
+
|
|
17
|
+
This package keeps the default install locked down and moves script execution
|
|
18
|
+
behind a reviewed allowlist in `package.json`.
|
|
19
|
+
|
|
20
|
+
## Setup
|
|
21
|
+
|
|
22
|
+
1. Add this to `.npmrc`:
|
|
23
|
+
|
|
24
|
+
```txt
|
|
25
|
+
ignore-scripts=true
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
2. Install `safe-install` without running dependency scripts:
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
npm i --ignore-scripts -D safe-install
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
3. Add scripts to `package.json`:
|
|
35
|
+
|
|
36
|
+
```json
|
|
37
|
+
{
|
|
38
|
+
"scripts": {
|
|
39
|
+
"safe-install": "safe-install"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
4. Find dependencies that declare install-time scripts:
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
npm run safe-install -- find
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
5. Review the output, then add trusted packages to `package.json`. You can also
|
|
51
|
+
enable `blockExoticSubDeps` to fail installs when transitive dependencies point
|
|
52
|
+
outside the npm registry with `git:`, `file:`, `link:`, or remote tarball URL
|
|
53
|
+
specifiers.
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"blockExoticSubDeps": true,
|
|
58
|
+
"trustedDependencies": [
|
|
59
|
+
"esbuild",
|
|
60
|
+
"sharp"
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
6. Use `safe-install` for future installs:
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
npm run safe-install
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## What `safe-install` does
|
|
72
|
+
|
|
73
|
+
`safe-install` runs npm install with scripts blocked, then runs install scripts only for packages listed in
|
|
74
|
+
`trustedDependencies`.
|
|
75
|
+
|
|
76
|
+
If `blockExoticSubDeps` is set to `true` in `package.json`, `safe-install` also
|
|
77
|
+
fails the install before rebuilding trusted dependencies when a transitive
|
|
78
|
+
dependency points outside the npm registry with a `git:`, `file:`, `link:`, or
|
|
79
|
+
remote tarball URL specifier.
|
|
80
|
+
|
|
81
|
+
Equivalent manual flow:
|
|
82
|
+
|
|
83
|
+
```sh
|
|
84
|
+
npm install --ignore-scripts
|
|
85
|
+
npm rebuild --ignore-scripts=false esbuild sharp
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Notes
|
|
89
|
+
|
|
90
|
+
Only add a package to `trustedDependencies` after reviewing why it needs an
|
|
91
|
+
install script. This does not make dependency scripts safe; it makes the trust
|
|
92
|
+
decision explicit and version-controlled.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
type PackageJson = {
|
|
3
|
+
blockExoticSubDeps?: unknown;
|
|
4
|
+
trustedDependencies?: unknown;
|
|
5
|
+
};
|
|
6
|
+
type LockPackage = {
|
|
7
|
+
dependencies?: Record<string, string>;
|
|
8
|
+
hasInstallScript?: boolean;
|
|
9
|
+
link?: boolean;
|
|
10
|
+
name?: string;
|
|
11
|
+
scripts?: Record<string, unknown>;
|
|
12
|
+
};
|
|
13
|
+
type PackageLock = {
|
|
14
|
+
packages?: Record<string, LockPackage>;
|
|
15
|
+
};
|
|
16
|
+
export declare function getTrustedDependencies(pkg: PackageJson): string[];
|
|
17
|
+
export declare function findInstallScriptDependencies(packageLock: PackageLock, trustedDependencies?: readonly string[]): string[];
|
|
18
|
+
type SafeInstallConfig = {
|
|
19
|
+
blockExoticSubdeps: boolean;
|
|
20
|
+
};
|
|
21
|
+
export declare function getSafeInstallConfig(pkg: PackageJson): SafeInstallConfig;
|
|
22
|
+
export declare function assertNoBlockedExoticSubdeps(config: SafeInstallConfig, packageLock: PackageLock): void;
|
|
23
|
+
export declare function findCommand(): void;
|
|
24
|
+
export declare function installCommand(): void;
|
|
25
|
+
export declare function main(args?: string[]): void;
|
|
26
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
const installScriptNames = ["preinstall", "install", "postinstall"];
|
|
6
|
+
const exoticSpecifiers = [
|
|
7
|
+
"file:",
|
|
8
|
+
"git:",
|
|
9
|
+
"http:",
|
|
10
|
+
"https:",
|
|
11
|
+
"link:",
|
|
12
|
+
];
|
|
13
|
+
export function getTrustedDependencies(pkg) {
|
|
14
|
+
if (pkg.trustedDependencies === undefined) {
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
if (!Array.isArray(pkg.trustedDependencies)) {
|
|
18
|
+
throw new Error("package.json trustedDependencies must be an array.");
|
|
19
|
+
}
|
|
20
|
+
return pkg.trustedDependencies.map((dependency) => {
|
|
21
|
+
if (typeof dependency !== "string" || dependency.length === 0) {
|
|
22
|
+
throw new Error("package.json trustedDependencies must contain package names.");
|
|
23
|
+
}
|
|
24
|
+
return dependency;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function packageNameFromPath(path) {
|
|
28
|
+
if (!path.startsWith("node_modules/")) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
const parts = path.split("/");
|
|
32
|
+
if (parts[1]?.startsWith("@")) {
|
|
33
|
+
return parts.length >= 3 ? `${parts[1]}/${parts[2]}` : undefined;
|
|
34
|
+
}
|
|
35
|
+
return parts[1] || undefined;
|
|
36
|
+
}
|
|
37
|
+
export function findInstallScriptDependencies(packageLock, trustedDependencies = []) {
|
|
38
|
+
const trusted = new Set(trustedDependencies);
|
|
39
|
+
const found = new Set();
|
|
40
|
+
for (const [path, pkg] of Object.entries(packageLock.packages ?? {})) {
|
|
41
|
+
if (pkg.link) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const name = pkg.name ?? packageNameFromPath(path);
|
|
45
|
+
if (!name || trusted.has(name)) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const hasInstallScript = pkg.hasInstallScript === true ||
|
|
49
|
+
installScriptNames.some((scriptName) => typeof pkg.scripts?.[scriptName] === "string");
|
|
50
|
+
if (hasInstallScript) {
|
|
51
|
+
found.add(name);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return [...found].sort((a, b) => a.localeCompare(b));
|
|
55
|
+
}
|
|
56
|
+
export function getSafeInstallConfig(pkg) {
|
|
57
|
+
if (pkg.blockExoticSubDeps === undefined) {
|
|
58
|
+
return { blockExoticSubdeps: false };
|
|
59
|
+
}
|
|
60
|
+
if (typeof pkg.blockExoticSubDeps !== "boolean") {
|
|
61
|
+
throw new Error("package.json blockExoticSubDeps must be a boolean.");
|
|
62
|
+
}
|
|
63
|
+
return { blockExoticSubdeps: pkg.blockExoticSubDeps };
|
|
64
|
+
}
|
|
65
|
+
function findExoticSubdependencies(packageLock) {
|
|
66
|
+
const found = [];
|
|
67
|
+
for (const [path, pkg] of Object.entries(packageLock.packages ?? {})) {
|
|
68
|
+
if (path === "" || pkg.link) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const from = pkg.name ?? packageNameFromPath(path) ?? path;
|
|
72
|
+
for (const [dependency, specifier] of Object.entries(pkg.dependencies ?? {})) {
|
|
73
|
+
if (exoticSpecifiers.some((prefix) => specifier.startsWith(prefix))) {
|
|
74
|
+
found.push({ dependency, from, specifier });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return found.sort((a, b) => {
|
|
79
|
+
const byFrom = a.from.localeCompare(b.from);
|
|
80
|
+
return byFrom === 0 ? a.dependency.localeCompare(b.dependency) : byFrom;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
export function assertNoBlockedExoticSubdeps(config, packageLock) {
|
|
84
|
+
if (!config.blockExoticSubdeps)
|
|
85
|
+
return;
|
|
86
|
+
const exoticSubdeps = findExoticSubdependencies(packageLock);
|
|
87
|
+
if (exoticSubdeps.length === 0)
|
|
88
|
+
return;
|
|
89
|
+
const lines = exoticSubdeps
|
|
90
|
+
.map(({ dependency, from, specifier }) => ` ${from} -> ${dependency}: ${specifier}`)
|
|
91
|
+
.join("\n");
|
|
92
|
+
throw new Error(`Blocked exotic subdependencies:\n${lines}`);
|
|
93
|
+
}
|
|
94
|
+
function readJsonFile(path) {
|
|
95
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
96
|
+
}
|
|
97
|
+
function readPackageLock() {
|
|
98
|
+
if (!existsSync("package-lock.json")) {
|
|
99
|
+
throw new Error("package-lock.json not found. Run npm install once with scripts disabled first.");
|
|
100
|
+
}
|
|
101
|
+
return readJsonFile("package-lock.json");
|
|
102
|
+
}
|
|
103
|
+
function readPackageJson() {
|
|
104
|
+
return existsSync("package.json") ? readJsonFile("package.json") : {};
|
|
105
|
+
}
|
|
106
|
+
function run(command, args) {
|
|
107
|
+
const result = spawnSync(command, args, {
|
|
108
|
+
stdio: "inherit",
|
|
109
|
+
shell: process.platform === "win32",
|
|
110
|
+
});
|
|
111
|
+
if (result.error) {
|
|
112
|
+
throw result.error;
|
|
113
|
+
}
|
|
114
|
+
if (result.status !== 0) {
|
|
115
|
+
process.exit(result.status ?? 1);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function printHelp() {
|
|
119
|
+
console.log(`safe-install
|
|
120
|
+
|
|
121
|
+
Usage:
|
|
122
|
+
safe-install Run npm install with scripts disabled, then rebuild trusted dependencies
|
|
123
|
+
safe-install find List dependencies that declare install-time scripts
|
|
124
|
+
`);
|
|
125
|
+
}
|
|
126
|
+
export function findCommand() {
|
|
127
|
+
const dependencies = findInstallScriptDependencies(readPackageLock(), getTrustedDependencies(readPackageJson()));
|
|
128
|
+
if (dependencies.length === 0) {
|
|
129
|
+
console.log("No untrusted dependencies with install-time scripts found.");
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
console.log("Dependencies with install-time scripts:");
|
|
133
|
+
for (const dependency of dependencies) {
|
|
134
|
+
console.log(` ${dependency}`);
|
|
135
|
+
}
|
|
136
|
+
console.log("");
|
|
137
|
+
console.log("Review these packages before adding them to trustedDependencies.");
|
|
138
|
+
}
|
|
139
|
+
export function installCommand() {
|
|
140
|
+
const pkg = readPackageJson();
|
|
141
|
+
const config = getSafeInstallConfig(pkg);
|
|
142
|
+
const trustedDependencies = getTrustedDependencies(pkg);
|
|
143
|
+
run("npm", ["install", "--ignore-scripts"]);
|
|
144
|
+
if (existsSync("package-lock.json")) {
|
|
145
|
+
assertNoBlockedExoticSubdeps(config, readPackageLock());
|
|
146
|
+
}
|
|
147
|
+
if (trustedDependencies.length > 0) {
|
|
148
|
+
run("npm", ["rebuild", "--ignore-scripts=false", ...trustedDependencies]);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
export function main(args = process.argv.slice(2)) {
|
|
152
|
+
const [command] = args;
|
|
153
|
+
if (command === undefined) {
|
|
154
|
+
installCommand();
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (command === "find") {
|
|
158
|
+
findCommand();
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (command === "--help" || command === "-h") {
|
|
162
|
+
printHelp();
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
throw new Error(`Unknown command: ${command}`);
|
|
166
|
+
}
|
|
167
|
+
if (process.argv[1] && realpathSync(fileURLToPath(import.meta.url)) === realpathSync(process.argv[1])) {
|
|
168
|
+
try {
|
|
169
|
+
main();
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
console.error(error instanceof Error ? error.message : error);
|
|
173
|
+
process.exit(1);
|
|
174
|
+
}
|
|
175
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gkiely/safe-install",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Run npm installs with lifecycle scripts disabled, then rebuild explicitly trusted dependencies.",
|
|
5
|
+
"author": "Grant Kiely <grant@youneedawiki.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"npm",
|
|
10
|
+
"install",
|
|
11
|
+
"security",
|
|
12
|
+
"supply-chain",
|
|
13
|
+
"trusted-dependencies"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=16"
|
|
17
|
+
},
|
|
18
|
+
"packageManager": "npm@11.9.0",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+ssh://git@github.com/gkiely/safe-install.git"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/gkiely/safe-install/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/gkiely/safe-install#readme",
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"blockExoticSubDeps": true,
|
|
31
|
+
"bin": {
|
|
32
|
+
"safe-install": "dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"import": "./dist/index.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"README.md"
|
|
43
|
+
],
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsc -p tsconfig.build.json",
|
|
46
|
+
"prepack": "npm run build",
|
|
47
|
+
"prepublishOnly": "npm run typecheck && npm test",
|
|
48
|
+
"test": "node --test",
|
|
49
|
+
"typecheck": "tsc --noEmit"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/node": "^25.7.0",
|
|
53
|
+
"typescript": "latest"
|
|
54
|
+
}
|
|
55
|
+
}
|