-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (49 loc) · 1.67 KB
/
index.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
#!/usr/bin/env node
const Guardian = require("./guardian");
const errors = require("./errors");
const dangerousRegistries = [
"https://registry.yarnpkg.com",
"https://registry.yarnpkg.com/",
"https://registry.npmjs.org/",
"https://registry.npmjs.org",
];
const npmConfigRegistry = process.env.npm_config_registry;
const isNpmConfigRegistrySet = typeof npmConfigRegistry === "string";
const isNpmConfigRegistryDangerous =
!isNpmConfigRegistrySet || dangerousRegistries.includes(npmConfigRegistry);
const packageName = process.env.npm_package_name;
const isPackageNameSet = typeof packageName === "string";
const packageScope =
isPackageNameSet && packageName[0] === "@"
? packageName.substring(1, packageName.indexOf("/")).replace(/-/g, "_")
: null;
const secureScope = () => {
const scopeRegistry = process.env[`npm_config__${packageScope}_registry`];
const isScopeRegistrySet = typeof scopeRegistry === "string";
const isScopeRegistryDangerous =
isScopeRegistrySet && dangerousRegistries.includes(scopeRegistry);
if (isScopeRegistryDangerous) {
throw new errors.ScopeRegistryDangerousError();
}
if (!isScopeRegistrySet && isNpmConfigRegistryDangerous) {
throw new errors.ScopeNotSetNpmConfigRegistryDangerousError();
}
};
const secureFromDangerousRegistries = () => {
if (isNpmConfigRegistryDangerous) {
throw new errors.NpmConfigRegistryDangerousError();
}
};
const securePublish = ({ useScope }) => {
if (useScope) {
secureScope();
} else {
secureFromDangerousRegistries();
}
};
try {
securePublish({ useScope: typeof packageScope === "string" });
} catch (exception) {
Guardian.inspect(exception);
process.exit(1);
}