409.index.js 32.8 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
exports.id = 409;
exports.ids = [409];
exports.modules = {

/***/ 46103:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getVersion = void 0;
const debugModule = __webpack_require__(15158);
const debug = debugModule('snyk');
const child_process_1 = __webpack_require__(63129);
function npm(method, packages, live, cwd, flags) {
    flags = flags || [];
    if (!packages) {
        packages = [];
    }
    if (!Array.isArray(packages)) {
        packages = [packages];
    }
    // only if we have packages, then always save, otherwise the command might
    // be something like `npm shrinkwrap'
    if (packages.length && !flags.length) {
        flags.push('--save');
    }
    method += ' ' + flags.join(' ');
    return new Promise((resolve, reject) => {
        const cmd = 'npm ' + method + ' ' + packages.join(' ');
        if (!cwd) {
            cwd = process.cwd();
        }
        debug('%s$ %s', cwd, cmd);
        if (!live) {
            debug('[skipping - dry run]');
            return resolve();
        }
        child_process_1.exec(cmd, {
            cwd,
        }, (error, stdout, stderr) => {
            if (error) {
                return reject(error);
            }
            if (stderr.indexOf('ERR!') !== -1) {
                console.error(stderr.trim());
                const e = new Error('npm update issues: ' + stderr.trim());
                e.code = 'FAIL_UPDATE';
                return reject(e);
            }
            debug('npm %s complete', method);
            resolve();
        });
    });
}
exports.default = npm;
function getVersion() {
    return new Promise((resolve, reject) => {
        child_process_1.exec('npm --version', {
            cwd: process.cwd(),
        }, (error, stdout) => {
            if (error) {
                return reject(error);
            }
            return resolve(stdout);
        });
    });
}
exports.getVersion = getVersion;


/***/ }),

/***/ 17240:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
const fs = __webpack_require__(35747);
const analytics = __webpack_require__(82744);
const debugModule = __webpack_require__(15158);
const request_1 = __webpack_require__(52050);
const debug = debugModule('snyk:fetch-patch');
async function getPatchFile(patchUrl, patchFilename) {
    try {
        const response = await request_1.makeRequest({ url: patchUrl });
        if (!response ||
            !response.res ||
            !response.body ||
            response.res.statusCode !== 200) {
            throw response;
        }
        fs.writeFileSync(patchFilename, response.body);
        debug(`Fetched patch from ${patchUrl} to ${patchFilename}, patch size ${response.body.length} bytes`);
    }
    catch (error) {
        const errorMessage = `Failed to fetch patch from ${patchUrl} to ${patchFilename}`;
        debug(errorMessage, error);
        analytics.add('patch-fetch-fail', {
            message: (error && error.message) || errorMessage,
            code: error && error.res && error.res.statusCode,
            patchFilename,
            patchUrl,
        });
        throw new Error(errorMessage);
    }
    return patchFilename;
}
exports.default = getPatchFile;


/***/ }),

/***/ 96284:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.yarn = void 0;
const Debug = __webpack_require__(15158);
const child_process_1 = __webpack_require__(63129);
const errors_1 = __webpack_require__(55191);
const debug = Debug('snyk');
function yarn(method, packages, live, cwd, flags) {
    flags = flags || [];
    if (!packages) {
        packages = [];
    }
    if (!Array.isArray(packages)) {
        packages = [packages];
    }
    method += ' ' + flags.join(' ');
    return new Promise((resolve, reject) => {
        const cmd = 'yarn ' + method + ' ' + packages.join(' ');
        if (!cwd) {
            cwd = process.cwd();
        }
        debug('%s$ %s', cwd, cmd);
        if (!live) {
            debug('[skipping - dry run]');
            return resolve();
        }
        child_process_1.exec(cmd, {
            cwd,
        }, (error, stdout, stderr) => {
            if (error) {
                return reject(error);
            }
            if (stderr.indexOf('ERR!') !== -1) {
                console.error(stderr.trim());
                const e = new errors_1.CustomError('Yarn update issues: ' + stderr.trim());
                e.strCode = 'FAIL_UPDATE';
                e.code = 422;
                return reject(e);
            }
            debug('yarn %s complete', method);
            resolve();
        });
    });
}
exports.yarn = yarn;


/***/ }),

/***/ 64029:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

const { v4: uuidv4 } = __webpack_require__(42277);
const debug = __webpack_require__(15158)('snyk');
const diff = __webpack_require__(88507);
const exec = __webpack_require__(63129).exec;
const path = __webpack_require__(85622);
const fs = __webpack_require__(35747);
const semver = __webpack_require__(36625);
const { addDataAndSend } = __webpack_require__(82744);

function applyPatch(patchFileName, vuln, live, patchUrl) {
  let cwd = vuln.source;

  return new Promise((resolve, reject) => {
    if (!cwd) {
      cwd = process.cwd();
    }

    const relative = path.relative(process.cwd(), cwd);
    debug('DRY RUN: relative: %s', relative);

    try {
      let pkg = {};
      const packageJsonPath = path.resolve(relative, 'package.json');
      try {
        const packageJson = fs.readFileSync(packageJsonPath);
        pkg = JSON.parse(packageJson);
        debug('package at patch target location: %s@%s', pkg.name, pkg.version);
      } catch (err) {
        debug(
          'Failed loading package.json at %s. Skipping patch!',
          packageJsonPath,
          err,
        );
        return resolve();
      }

      const versionOfPackageToPatch = pkg.version;
      const patchableVersionsRange = vuln.patches.version;

      const isSemverMatch = semver.satisfies(
        versionOfPackageToPatch,
        patchableVersionsRange,
      );

      const isVersionMatch = semver.satisfies(
        versionOfPackageToPatch,
        semver.valid(semver.coerce(vuln.patches.version)),
      );

      if (isSemverMatch || isVersionMatch) {
        debug(
          'Patch version range %s matches package version %s',
          patchableVersionsRange,
          versionOfPackageToPatch,
        );
      } else {
        debug(
          'Patch version range %s does not match package version %s. Skipping patch!',
          patchableVersionsRange,
          versionOfPackageToPatch,
        );
        return resolve();
      }

      const patchContent = fs.readFileSync(
        path.resolve(relative, patchFileName),
        'utf8',
      );

      jsDiff(patchContent, relative, live).then(() => {
        debug('patch succeed');
        resolve();
      });
    } catch (error) {
      debug('patch command failed', relative, error);
      patchError(error, relative, vuln, patchUrl).catch(reject);
    }
  });
}

function jsDiff(patchContent, relative, live) {
  const patchedFiles = {};
  return new Promise((resolve, reject) => {
    diff.applyPatches(patchContent, {
      loadFile: function(index, callback) {
        try {
          const fileName = trimUpToFirstSlash(index.oldFileName);
          if (patchedFiles[fileName]) {
            return callback(null, patchedFiles[fileName]);
          }

          const filePath = path.resolve(relative, fileName);
          const content = fs.readFileSync(filePath, 'utf8');

          // create an `.orig` copy of the file prior to patching it
          // used in case we need to revert a patch
          const origFilePath = filePath + '.orig';
          fs.writeFileSync(origFilePath, content);

          callback(null, content);
        } catch (err) {
          // collect patch metadata for error analysis
          err.patchIssue = JSON.stringify(index);
          callback(err);
        }
      },
      patched: function(index, content, callback) {
        try {
          if (content === false) {
            // `false` means the patch does not match the original content.
            const error = new Error('Found a mismatching patch');
            error.patchIssue = JSON.stringify(index);
            throw error;
          }
          const newFileName = trimUpToFirstSlash(index.newFileName);
          const oldFileName = trimUpToFirstSlash(index.oldFileName);
          if (newFileName !== oldFileName) {
            patchedFiles[oldFileName] = null;
          }
          patchedFiles[newFileName] = content;
          callback();
        } catch (err) {
          callback(err);
        }
      },
      compareLine: function(_, line, operation, patchContent) {
        if (operation === ' ') {
          // Ignore when no patch operators as GNU patch does
          return true;
        }
        return line === patchContent;
      },
      complete: function(error) {
        if (error) {
          return reject(error);
        }
        if (!live) {
          return resolve();
        }
        try {
          // write patched files back to disk, unlink files completely removed by patching
          for (const fileName in patchedFiles) {
            if (typeof patchedFiles[fileName] === 'string') {
              fs.writeFileSync(
                path.resolve(relative, fileName),
                patchedFiles[fileName],
              );
            } else {
              fs.unlinkSync(path.resolve(relative, fileName));
            }
          }
          resolve();
        } catch (err) {
          reject(err);
        }
      },
    });
  });
}

// diff data compares the same file with a dummy path (a/path/to/real.file vs b/path/to/real.file)
// skipping the dummy folder name by trimming up to the first slash
function trimUpToFirstSlash(fileName) {
  return fileName && fileName.replace(/^[^/]+\//, '');
}

function patchError(error, dir, vuln, patchUrl) {
  if (error && error.code === 'ENOENT') {
    error.message =
      'Failed to patch: the target could not be found (' + error.message + ').';
    return Promise.reject(error);
  }

  return new Promise((resolve, reject) => {
    const id = vuln.id;

    exec(
      'npm -v',
      {
        env: process.env,
      },
      (npmVError, versions) => {
        // stderr is ignored
        const npmVersion = versions && versions.split('\n').shift();
        const referenceId = uuidv4();

        // this is a general "patch failed", since we already check if the
        // patch was applied via a flag, this means something else went
        // wrong, so we'll ask the user for help to diagnose.
        const filename = path.relative(process.cwd(), dir);

        // post metadata to help diagnose
        addDataAndSend({
          command: 'patch-fail',
          metadata: {
            from: vuln.from.slice(1),
            vulnId: id,
            packageName: vuln.name,
            packageVersion: vuln.version,
            package: vuln.name + '@' + vuln.version,
            patchError: Object.assign(
              {},
              {
                message: error.message,
                stack: error.stack,
                name: error.name,
              },
              error,
            ),
            'npm-version': npmVersion,
            referenceId: referenceId,
            patchUrl: patchUrl,
            filename: filename,
          },
        });

        const msg =
          id +
          ' on ' +
          vuln.name +
          '@' +
          vuln.version +
          ' at "' +
          filename +
          '"\n' +
          error +
          ', ' +
          'reference ID: ' +
          referenceId +
          '\n';

        error = new Error(msg);
        error.code = 'FAIL_PATCH';

        reject(error);
      },
    );
  });
}

module.exports = applyPatch;


/***/ }),

/***/ 53417:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

module.exports = dedupe;

const debug = __webpack_require__(15158)('snyk:patch');
const patchesForPackage = __webpack_require__(87553);

function dedupe(source) {
  const removed = [];

  const names = source.reduce((acc, vuln) => {
    if (Array.isArray(vuln.patches)) {
      // strip down to the only paches that can be applied
      vuln.patches = patchesForPackage(vuln);
    }

    const key = vuln.name + vuln.version + vuln.from.join('>');

    const other = acc[key];
    if (other) {
      debug('dupe found on %s & %s', vuln.id, other.id);
      if (vuln.publicationTime > other.publicationTime) {
        debug('stripping %s', other.id);
        removed.push(other);
        acc[key] = vuln;
      } else {
        removed.push(vuln);
      }
    } else {
      acc[key] = vuln;
    }

    return acc;
  }, {});

  // turn back into an array
  const packages = Object.keys(names).map((key) => {
    return names[key];
  });

  return {
    packages: packages,
    removed: removed,
  };
}


/***/ }),

/***/ 82645:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

module.exports = getVulnSource;

const debug = __webpack_require__(15158)('snyk');
const resolve = __webpack_require__(7320);
const path = __webpack_require__(85622);
const statSync = __webpack_require__(35747).statSync;
const { parsePackageString: moduleToObject } = __webpack_require__(60390);

function getVulnSource(vuln, live) {
  const from = vuln.from.slice(1).map((pkg) => {
    return moduleToObject(pkg).name;
  });

  const viaPath = path.resolve(
    process.cwd(),
    'node_modules',
    from.join('/node_modules/'),
  );

  let source = vuln.__filename ? path.dirname(vuln.__filename) : viaPath;

  // try to stat the directory, if it throws, it doesn't exist...
  try {
    statSync(source);
  } catch (e) {
    // ...which means the package is located in a parent path (from an
    // npm dedupe process), so we remove the module name from the path
    // and use the `resolve` package to navigate the node_modules up
    // through parent directories.
    try {
      source = resolve.sync(from.slice(-1).pop(), viaPath);
    } catch (e) {
      let adaptedError = e;
      if (e.code === 'NO_PACKAGE_FOUND') {
        adaptedError =
          'Error: `' +
          e.message +
          "`\nWe can't patch without " +
          'dependencies installed. Please run `npm ' +
          'install` or  `yarn install` first.';
      }
      if (live) {
        throw adaptedError;
      }

      // otherwise this is a dry run so we don't mind that it won't be
      // able to patch - likely a scenario run, so it's fine that the
      // patch target won't be found
    }
    debug('found better source for package: %s', source);
  }

  return source;
}


/***/ }),

/***/ 6346:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

module.exports = ignore;

const debug = __webpack_require__(15158)('snyk');
const stripVersions = __webpack_require__(15177);
const oneDay = 1000 * 60 * 60 * 24;

function ignore(data) {
  return new Promise((resolve) => {
    const config = {};
    config.ignore = data
      .map((res) => {
        const vuln = res.vuln;
        const days = res.meta.days || 30;
        const ignoreRule = {};
        ignoreRule[stripVersions(vuln.from.slice(1)).join(' > ')] = {
          reason: res.meta.reason,
          expires: new Date(Date.now() + oneDay * days).toJSON(),
        };
        ignoreRule.vulnId = vuln.id;
        return ignoreRule;
      })
      .reduce((acc, curr) => {
        if (!acc[curr.vulnId]) {
          acc[curr.vulnId] = [];
        }

        const id = curr.vulnId;
        delete curr.vulnId;
        acc[id].push(curr);

        return acc;
      }, {});

    // final format looks like test/fixtures/protect-interactive-config.json
    debug('ignore config', config);

    resolve(config);
  });
}


/***/ }),

/***/ 5409:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

const protect = (module.exports = {
  ignore: __webpack_require__(6346),
  update: __webpack_require__(37466)/* .update */ .Vx,
  install: __webpack_require__(37466)/* .install */ .N9,
  installDev: __webpack_require__(37466)/* .installDev */ .V7,
  patch: __webpack_require__(39609),
  patchesForPackage: __webpack_require__(87553),
  generatePolicy: generatePolicy,
});

const debug = __webpack_require__(15158)('snyk');

const flattenDeep = __webpack_require__(44779);
const merge = __webpack_require__(72378);

function generatePolicy(policy, tasks, live, packageManager) {
  const promises = ['ignore', 'update', 'patch']
    .filter((task) => {
      return tasks[task].length;
    })
    .map((task) => {
      return protect[task](tasks[task], live, packageManager);
    });

  return Promise.all(promises).then((res) => {
    // we're squashing the arrays of arrays into a flat structure
    // with only non-false values
    const results = flattenDeep(res).filter(Boolean);

    // then we merge the configs together using the original config
    // as the baseline (this lets us retain the user's existing config)
    results.unshift(policy);
    const newPolicy = merge(...results);

    debug(JSON.stringify(newPolicy, '', 2));

    return newPolicy;
  });
}


/***/ }),

/***/ 39609:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

module.exports = patch;

const now = new Date();

const debug = __webpack_require__(15158)('snyk');
const chalk = __webpack_require__(32589);
const glob = __webpack_require__(12884);
const tempy = __webpack_require__(30488);
const fs = __webpack_require__(35747);
const path = __webpack_require__(85622);
const flatten = __webpack_require__(5800);
const cloneDeep = __webpack_require__(83465);
const { contactSupportMessage } = __webpack_require__(70527);
const applyPatch = __webpack_require__(64029);
const stripVersions = __webpack_require__(15177);
const getVulnSource = __webpack_require__(82645);
const dedupe = __webpack_require__(53417);
const writePatchFlag = __webpack_require__(87666);
const { spinner } = __webpack_require__(86766);
const errors = __webpack_require__(79407);
const analytics = __webpack_require__(82744);
const { default: getPatchFile } = __webpack_require__(17240);

function patch(vulns, live) {
  const lbl = 'Applying patches...';
  const errorList = [];

  return (
    spinner(lbl)
      .then(() => {
        // the target directory where our module name will live
        vulns.forEach((vuln) => (vuln.source = getVulnSource(vuln, live)));

        const deduped = dedupe(vulns);
        debug('patching %s vulns after dedupe', deduped.packages.length);

        // find the patches, pull them down off the web, save them in a temp file
        // then apply each individual patch - but do it one at a time (via reduce)
        const promises = deduped.packages.reduce((acc, vuln) => {
          return acc.then((res) => {
            const patches = vuln.patches; // this is also deduped in `dedupe`

            if (patches === null) {
              debug('no patch available for ' + vuln.id);
              analytics.add('no-patch', vuln.from.slice(1).join(' > '));
              return res;
            }

            analytics.add('patch', vuln.from.slice(1).join(' > '));
            debug(`Patching vuln: ${vuln.id} ${vuln.from}`);

            // the colon doesn't like Windows, ref: https://git.io/vw2iO
            const fileSafeId = vuln.id.replace(/:/g, '-');
            const flag = path.resolve(
              vuln.source,
              '.snyk-' + fileSafeId + '.flag',
            );
            const oldFlag = path.resolve(
              vuln.source,
              '.snyk-' + vuln.id + '.flag',
            );

            // get the patches on the local fs
            const promises = patches.urls.map((url) => {
              const filename = tempy.file({
                extension: '.' + fileSafeId + '.snyk-patch',
              });
              return getPatchFile(url, filename)
                .then((patch) => {
                  // check whether there's a trace of us having patched before
                  return Promise.resolve(fs.existsSync(flag))
                    .then((exists) => {
                      // if the file doesn't exist, look for the old style filename
                      // in case and for backwards compatability
                      return exists || fs.existsSync(oldFlag);
                    })
                    .then((exists) => {
                      if (!exists) {
                        return patch;
                      }
                      debug(
                        'Previous flag found = ' +
                          exists +
                          ' | Restoring file back to original to apply the patch again',
                      );
                      // else revert the patch
                      return new Promise((resolve, reject) => {
                        // find all backup files that do not belong to transitive deps
                        glob(
                          '**/*.orig',
                          { cwd: vuln.source, ignore: '**/node_modules/**' },
                          (error, files) => {
                            if (error) {
                              return reject(error);
                            }

                            // copy '.orig' backups over the patched files
                            for (const file of files) {
                              const backupFile = path.resolve(
                                vuln.source,
                                file,
                              );
                              const sourceFile = backupFile.slice(
                                0,
                                -'.orig'.length,
                              );
                              debug('restoring', backupFile, sourceFile);
                              fs.renameSync(backupFile, sourceFile);
                            }

                            resolve(patch);
                          },
                        );
                      });
                    });
                })
                .then((patch) => {
                  if (patch === false) {
                    debug('already patched %s', vuln.id);
                    return vuln;
                  }

                  debug(
                    'applying patch file for %s: \n%s\n%s',
                    vuln.id,
                    url,
                    patch,
                  );

                  return applyPatch(patch, vuln, live, url)
                    .then(
                      () => {
                        return true;
                      },
                      (e) => {
                        errorList.push(e);
                        return false;
                      },
                    )
                    .then(writePatchFlag(now, vuln))
                    .then((ok) => {
                      return ok ? vuln : false;
                    });
                });
            });

            return Promise.all(promises).then((result) => {
              res.push(result);
              return res; // this is what makes the waterfall reduce chain work
            });
          });
        }, Promise.resolve(deduped.removed));

        const promise = promises
          .then((res) => {
            const patched = flatten(res).filter(Boolean);

            if (!live) {
              debug('[skipping - dry run]');
              return patched;
            }
            return Promise.all(patched);
          })
          .then((patched) => {
            const config = {};

            // this reduce function will look to see if the patch actually resolves
            // more than one vulnerability, and if it does, it'll replicate the
            // patch rule against the *other* vuln.ids. This will happen when the user
            // runs the wizard and selects to apply a patch that fixes more than one
            // vuln.
            const mapped = patched.map(patchRule).reduce((acc, curr, i) => {
              const vuln = patched[i];
              if (vuln.grouped && vuln.grouped.includes) {
                vuln.grouped.includes.forEach((id) => {
                  const rule = cloneDeep(curr);
                  rule.vulnId = id;
                  acc.push(rule);
                });
              }

              acc.push(curr);

              return acc;
            }, []);

            config.patch = mapped.reduce((acc, curr) => {
              if (!acc[curr.vulnId]) {
                acc[curr.vulnId] = [];
              }

              const id = curr.vulnId;
              delete curr.vulnId;
              acc[id].push(curr);

              return acc;
            }, {});

            debug('patched', config);

            return config;
          });

        return promise;
      })
      // clear spinner in case of success or failure
      .then(spinner.clear(lbl))
      .catch((error) => {
        spinner.clear(lbl)();
        throw error;
      })
      .then((res) => {
        if (errorList.length) {
          errorList.forEach((error) => {
            console.log(chalk.red(errors.message(error)));
            debug(error.stack);
          });
          throw new Error(contactSupportMessage);
        }

        return res;
      })
  );
}

function patchRule(vuln) {
  const rule = {
    vulnId: vuln.id,
  };
  rule[stripVersions(vuln.from.slice(1)).join(' > ')] = {
    patched: now.toJSON(),
  };
  return rule;
}


/***/ }),

/***/ 87553:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

module.exports = patchesForPackage;

const semver = __webpack_require__(36625);

function patchesForPackage(vuln) {
  return (
    vuln.patches.filter((patch) => {
      if (semver.satisfies(vuln.version, patch.version)) {
        return (patch.urls || []).length ? patch : false;
      }
      return false;
    })[0] || null
  );
}


/***/ }),

/***/ 15177:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

module.exports = stripVersions;

const { parsePackageString: moduleToObject } = __webpack_require__(60390);

function stripVersions(packages) {
  return packages.map((pkg) => {
    return moduleToObject(pkg).name;
  });
}


/***/ }),

/***/ 37466:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

module.exports.Vx = update;
module.exports.N9 = install;
module.exports.V7 = installDev;

const debug = __webpack_require__(15158)('snyk');
const chalk = __webpack_require__(32589);
const uniq = __webpack_require__(97644);
const isEmpty = __webpack_require__(99245);
const { parsePackageString: moduleToObject } = __webpack_require__(60390);
const semver = __webpack_require__(36625);
const errors = __webpack_require__(79407);
const { default: npm } = __webpack_require__(46103);
const { yarn } = __webpack_require__(96284);
const { spinner } = __webpack_require__(86766);
const analytics = __webpack_require__(82744);

function update(packages, live, pkgManager) {
  pkgManager = pkgManager || 'npm';
  const lbl = 'Applying updates using ' + pkgManager + '...';
  let error = false;

  return (
    spinner(lbl)
      .then(() => {
        const upgrade = packages
          .map((vuln) => {
            const remediation = vuln.upgradePath && vuln.upgradePath[1];
            if (!remediation) {
              // this vuln holds an unreachable upgrade path - send this to analytics
              // and return an empty object to be filtered
              analytics.add('bad-upgrade-path', vuln);
              return null;
            }

            return {
              remediation: remediation,
              type: vuln.parentDepType || 'prod',
            };
          })
          .filter(Boolean)
          .reduce((ups, vuln) => {
            if (!ups[vuln.type]) {
              ups[vuln.type] = [];
            }
            ups[vuln.type].push(vuln.remediation);
            return ups;
          }, {});

        debug('to upgrade', upgrade);

        if (upgrade.length === 0) {
          return;
        }

        // warn if extraneous packages were selected for update
        if (upgrade.extraneous) {
          console.error(
            chalk.yellow(
              'Extraneous packages were selected for ' +
                'update, but will be skipped. These dependencies introduce ' +
                'vulnerabilities. Please remove the dependencies with `npm prune`, ' +
                'or install properly as prod or dev dependencies:',
              upgrade.extraneous.join(', '),
            ),
          );
        }

        const promise = Promise.resolve()
          .then(() => {
            // create list of unique package names _without versions_ for uninstall
            // skip extraneous packages, if any
            const prodToUninstall =
              (upgrade.prod && upgrade.prod.map(stripVersion)) || [];
            const devToUninstall =
              (upgrade.dev && upgrade.dev.map(stripVersion)) || [];
            const toUninstall = uniq(prodToUninstall.concat(devToUninstall));
            debug('to uninstall', toUninstall);

            if (!isEmpty(toUninstall)) {
              return uninstall(pkgManager, toUninstall, live);
            }
          })
          .then(() => {
            const prodUpdate = (upgrade.prod
              ? install(pkgManager, findUpgrades(upgrade.prod), live)
              : Promise.resolve(true)
            ).catch((e) => {
              error = e;
              return false;
            });
            const devUpdate = (upgrade.dev
              ? installDev(pkgManager, findUpgrades(upgrade.dev), live)
              : Promise.resolve(true)
            ).catch((e) => {
              error = e;
              return false;
            });
            return Promise.all([prodUpdate, devUpdate]).then((results) => {
              return results[0] && results[1];
            });
          });
        return promise;
      })
      // clear spinner in case of success or failure
      .then(spinner.clear(lbl))
      .catch((error) => {
        spinner.clear(lbl)();
        throw error;
      })
      .then((res) => {
        if (error) {
          console.error(chalk.red(errors.message(error)));
          debug(error.stack);
        }
        return res;
      })
  );
}

function install(pkgManager, upgrades, live) {
  return pkgManager === 'yarn'
    ? yarn('add', upgrades, live)
    : npm('install', upgrades, live);
}

function installDev(pkgManager, upgrades, live) {
  return pkgManager === 'yarn'
    ? yarn('add', upgrades, live, null, ['--dev'])
    : npm('install', upgrades, live, null, ['--save-dev']);
}

function uninstall(pkgManager, toUninstall, live) {
  return pkgManager === 'yarn'
    ? yarn('remove', toUninstall, live)
    : npm('uninstall', toUninstall, live);
}

function findUpgrades(packages) {
  return packages
    .map(moduleToObject)
    .reduce((acc, curr) => {
      const have = acc
        .filter((pkg) => {
          return pkg.name === curr.name;
        })
        .pop();

      if (have) {
        if (semver.gt(curr.version, have.version)) {
          have.version = curr.version;
        }
      } else {
        acc.push(curr);
      }

      return acc;
    }, [])
    .map((pkg) => {
      return pkg.name + '@' + pkg.version;
    });
}

function stripVersion(pkg) {
  if (!pkg) {
    return;
  }

  // scoped packages like @snyk/module@1.0.0
  if (pkg.startsWith('@')) {
    return '@' + pkg.split('@')[1];
  }

  // non-scoped packages like snyk@1.2.3
  if (pkg.indexOf('@') > 0) {
    return pkg.split('@').shift();
  }

  // versionless packages like tap
  return pkg;
}


/***/ }),

/***/ 87666:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

module.exports = writePatchFlag;

const debug = __webpack_require__(15158)('snyk');
const fs = __webpack_require__(35747);
const path = __webpack_require__(85622);

function writePatchFlag(now, vuln) {
  if (!vuln) {
    vuln = now;
    now = new Date();
  }

  debug('writing flag for %s', vuln.id);
  let promise;
  // the colon doesn't like Windows, ref: https://git.io/vw2iO
  const fileSafeId = vuln.id.replace(/:/g, '-');
  const flag = path.resolve(vuln.source, '.snyk-' + fileSafeId + '.flag');
  if (vuln.grouped && vuln.grouped.includes) {
    debug('found addition vulns to write flag files for');
    const writePromises = [];
    fs.writeFileSync(flag, now.toJSON(), 'utf8');
    vuln.grouped.includes.forEach(() => {
      const fileSafeId = vuln.id.replace(/:/g, '-');
      const flag = path.resolve(vuln.source, '.snyk-' + fileSafeId + '.flag');
      debug('Writing flag for grouped vulns', flag);
      writePromises.push();
      fs.writeFileSync(flag, now.toJSON(), 'utf8');
    });
    promise = Promise.all(writePromises);
  } else {
    debug('Writing flag for single vuln', flag);
    /* TODO:
      This piece is actually swallowing fs.writeFile errors!
      See the `promise.then` construct below.
      This should be refactored and tests should be updated.
    */
    promise = new Promise((r) => fs.writeFile(flag, now.toJSON(), 'utf8', r));
  }
  return promise.then(() => {
    return vuln;
  });
}


/***/ })

};
;
//# sourceMappingURL=409.index.js.map