522.index.js
3.96 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"use strict";
exports.id = 522;
exports.ids = [522];
exports.modules = {
/***/ 1522:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getProtectRemovalMessage = void 0;
const debugModule = __webpack_require__(15158);
const snyk = __webpack_require__(9146);
const analytics = __webpack_require__(82744);
const detect = __webpack_require__(45318);
const pm = __webpack_require__(53847);
const errors_1 = __webpack_require__(55191);
const errors = __webpack_require__(55191);
const theme = __webpack_require__(86988);
const protect = __webpack_require__(5409);
const debug = debugModule('snyk');
function getProtectRemovalMessage() {
return theme.color.status.warn(`\n${theme.icon.WARNING} WARNING: Starting from 31 March 2022, the protect command will be removed.\nPlease use the @snyk/protect package instead.\nFor more info: https://snyk.co/ueln7`);
}
exports.getProtectRemovalMessage = getProtectRemovalMessage;
async function protectFunc(options) {
const protectOptions = { ...options };
protectOptions.loose = true; // replace missing policies with empty ones
protectOptions.vulnEndpoint = '/vuln/npm/patches';
// TODO: fix this by providing better patch support for yarn
// yarn hoists packages up a tree so we can't assume their location
// on disk without traversing node_modules
// currently the npm@2 nd npm@3 plugin resolve-deps can do this
// but not the latest node-lockfile-parser
// HACK: if yarn set traverseNodeModules option to
// bypass lockfile test for wizard
protectOptions.traverseNodeModules = true;
try {
const packageManager = detect.detectPackageManager(process.cwd(), protectOptions);
const supportsProtect = pm.PROTECT_SUPPORTED_PACKAGE_MANAGERS.includes(packageManager);
if (!supportsProtect) {
throw new Error('Snyk protect for ' +
pm.SUPPORTED_PACKAGE_MANAGER_NAME[packageManager] +
' projects is not currently supported');
}
}
catch (error) {
return Promise.reject(error);
}
if (protectOptions.interactive) {
// silently fail
return Promise.reject(new Error('Snyk protect interactive mode ' +
'has moved. Please run `snyk wizard`'));
}
if (protectOptions['dry-run']) {
debug('*** dry run ****');
}
else {
debug('~~~~ LIVE RUN ~~~~');
}
try {
const policy = await snyk.policy.load(protectOptions['policy-path']);
const removalMessage = getProtectRemovalMessage();
if (policy.patch) {
const patchOutput = await patch(protectOptions);
return patchOutput + removalMessage;
}
return 'Nothing to do' + removalMessage;
}
catch (e) {
let error;
if (e.code === 'ENOENT') {
error = new errors.PolicyNotFoundError();
}
else {
error = new errors.FailedToLoadPolicyError();
error.innerError = e;
}
throw error;
}
}
exports.default = protectFunc;
async function patch(options) {
try {
const response = (await snyk.test(process.cwd(), options));
// TODO: need to add support for multiple test results being returned
// from test (for example gradle modules)
if (!response.vulnerabilities) {
const e = new errors_1.CustomError('Code is already patched');
e.strCode = 'ALREADY_PATCHED';
e.code = 204;
throw e;
}
await protect.patch(response.vulnerabilities, !options['dry-run']);
analytics.add('success', true);
return 'Successfully applied Snyk patches';
}
catch (e) {
if (e.strCode === 'ALREADY_PATCHED') {
analytics.add('success', true);
return e.message + ', nothing to do';
}
analytics.add('success', false);
throw e;
}
}
/***/ })
};
;
//# sourceMappingURL=522.index.js.map