All files / src executor.ts

31.13% Statements 33/106
100% Branches 4/4
20% Functions 1/5
31.13% Lines 33/106

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1071x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x                                                   1x                                                           1x 1x                               1x 1x 3x 3x 2x 2x 3x 1x 1x 3x 1x 1x 3x 3x  
import { hideSecrets } from "./util/misc.js";
import {
  SpawnSyncOptions,
  SpawnSyncOptionsWithStringEncoding,
} from "child_process";
import spawn from "cross-spawn";
import { Maybe } from "purify-ts";
import { green } from "ansis";
import { createLogger } from "./util/log.js";
import { name } from "./info.js";
import { cleanDir } from "./util/fs.js";
import { setEnv } from "./util/proc.js";
 
const log = createLogger(`${name} Analysis`);
 
function logCommandExecution(executable: string, cmdArguments: string[]) {
  const command = `${executable} ${hideSecrets(cmdArguments.join(" "))}`;
  log.info("Running command:", command);
}
 
function executeVersionCheck(executable: string) {
  const versionCmdArguments = ["--version"];
  logCommandExecution(executable, versionCmdArguments);

  const versionSpawnOpts: SpawnSyncOptionsWithStringEncoding = {
    shell: false,
    encoding: "utf-8",
  };

  const versionSpawn = spawn.sync(
    executable,
    versionCmdArguments,
    versionSpawnOpts,
  );
  if (versionSpawn.error) {
    throw versionSpawn.error;
  }
  if (versionSpawn.status === null) {
    throw new Error("Version check did not complete with status code.");
  }
  if (versionSpawn.status !== 0) {
    throw new Error(versionSpawn.stderr);
  }
  log.info(versionSpawn.stdout.trimEnd());
}
 
function executeAnalysis(
  executable: string,
  cmdArguments: string[],
  proxyUrl: Maybe<URL>,
  hideOwaspOutput: boolean,
) {
  setEnv("JAVA_OPTS", proxyUrl.map(buildJavaToolOptions), true, log);

  const dependencyCheckSpawnOpts: SpawnSyncOptions = {
    shell: false,
    stdio: hideOwaspOutput ? "ignore" : "inherit",
  };

  logCommandExecution(executable, cmdArguments);
  const dependencyCheckSpawn = spawn.sync(
    executable,
    cmdArguments,
    dependencyCheckSpawnOpts,
  );
  if (dependencyCheckSpawn.error) {
    throw dependencyCheckSpawn.error;
  }
  if (dependencyCheckSpawn.status === null) {
    throw new Error("Analysis did not complete with status code.");
  }

  log.info(green`Done.`);
  return dependencyCheckSpawn.status;
}
 
export function executeDependencyCheck(
  executable: string,
  cmdArguments: string[],
  outDir: string,
  proxyUrl: Maybe<URL>,
  hideOwaspOutput: boolean,
  javaBinary: Maybe<string>,
) {
  log.info("Dependency-Check Core path:", executable);
  cleanDir(outDir, log);

  setEnv("JAVACMD", javaBinary, false, log);

  executeVersionCheck(executable);
  return executeAnalysis(executable, cmdArguments, proxyUrl, hideOwaspOutput);
}
 
export function buildJavaToolOptions(proxyUrl: URL) {
  let javaToolOptions = `-Dhttps.proxyHost=${proxyUrl.hostname}`;
  if (proxyUrl.port) {
    javaToolOptions += ` -Dhttps.proxyPort=${proxyUrl.port}`;
  }
  if (proxyUrl.username) {
    javaToolOptions += ` -Dhttps.proxyUser=${proxyUrl.username}`;
  }
  if (proxyUrl.password) {
    javaToolOptions += ` -Dhttps.proxyPassword=${proxyUrl.password}`;
  }
  return javaToolOptions;
}