|
| 1 | +import fs from 'node:fs' |
| 2 | +import os from "node:os"; |
| 3 | +import path from 'node:path' |
| 4 | +import { PackageURL } from 'packageurl-js' |
| 5 | + |
| 6 | +import { getCustomPath, invokeCommand } from "../tools.js"; |
| 7 | +import Sbom from '../sbom.js' |
| 8 | + |
| 9 | +/** @typedef {import('../provider.js').Provider} */ |
| 10 | + |
| 11 | +/** @typedef {import('../provider.js').Provided} Provided */ |
| 12 | + |
| 13 | +const ecosystem = 'npm' |
| 14 | +const defaultVersion = 'v0.0.0' |
| 15 | + |
| 16 | +export default class Base_javascript { |
| 17 | + |
| 18 | + // Resolved cmd to use |
| 19 | + #cmd; |
| 20 | + |
| 21 | + /** |
| 22 | + * @returns {string} the name of the lock file name for the specific implementation |
| 23 | + */ |
| 24 | + _lockFileName() { |
| 25 | + throw new TypeError("_lockFileName must be implemented"); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @returns {string} the command name to use for the specific JS package manager |
| 30 | + */ |
| 31 | + _cmdName() { |
| 32 | + throw new TypeError("_cmdName must be implemented"); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @returns {Array<string>} |
| 37 | + */ |
| 38 | + _listCmdArgs() { |
| 39 | + throw new TypeError("_listCmdArgs must be implemented"); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @returns {Array<string>} |
| 44 | + */ |
| 45 | + _updateLockFileCmdArgs() { |
| 46 | + throw new TypeError("_updateLockFileCmdArgs must be implemented"); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param {string} manifestName - the subject manifest name-type |
| 51 | + * @returns {boolean} - return true if `pom.xml` is the manifest name-type |
| 52 | + */ |
| 53 | + isSupported(manifestName) { |
| 54 | + return 'package.json' === manifestName; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Checks if a required lock file exists in the same path as the manifest |
| 59 | + * |
| 60 | + * @param {string} manifestDir - The base directory where the manifest is located |
| 61 | + * @returns {boolean} - True if the lock file exists |
| 62 | + */ |
| 63 | + validateLockFile(manifestDir) { |
| 64 | + const lock = path.join(manifestDir, this._lockFileName()); |
| 65 | + return fs.existsSync(lock); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Provide content and content type for maven-maven stack analysis. |
| 70 | + * @param {string} manifest - the manifest path or name |
| 71 | + * @param {{}} [opts={}] - optional various options to pass along the application |
| 72 | + * @returns {Provided} |
| 73 | + */ |
| 74 | + provideStack(manifest, opts = {}) { |
| 75 | + return { |
| 76 | + ecosystem, |
| 77 | + content: this.#getSBOM(manifest, opts, true), |
| 78 | + contentType: 'application/vnd.cyclonedx+json' |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Provide content and content type for maven-maven component analysis. |
| 84 | + * @param {string} manifest - path to pom.xml for component report |
| 85 | + * @param {{}} [opts={}] - optional various options to pass along the application |
| 86 | + * @returns {Provided} |
| 87 | + */ |
| 88 | + provideComponent(manifest, opts = {}) { |
| 89 | + return { |
| 90 | + ecosystem, |
| 91 | + content: this.#getSBOM(manifest, opts, false), |
| 92 | + contentType: 'application/vnd.cyclonedx+json' |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Utility function for creating Purl String |
| 98 | + * @param name the name of the artifact, can include a namespace(group) or not - namespace/artifactName. |
| 99 | + * @param version the version of the artifact |
| 100 | + * @returns {PackageURL|null} PackageUrl Object ready to be used in SBOM |
| 101 | + */ |
| 102 | + #toPurl(name, version) { |
| 103 | + let parts = name.split("/"); |
| 104 | + var purlNs, purlName; |
| 105 | + if (parts.length === 2) { |
| 106 | + purlNs = parts[0]; |
| 107 | + purlName = parts[1]; |
| 108 | + } else { |
| 109 | + purlName = parts[0]; |
| 110 | + } |
| 111 | + return new PackageURL('npm', purlNs, purlName, version, undefined, undefined); |
| 112 | + } |
| 113 | + |
| 114 | + _buildDependencyTree(includeTransitive, manifest) { |
| 115 | + this.#version(); |
| 116 | + let manifestDir = path.dirname(manifest) |
| 117 | + this.#createLockFile(manifestDir); |
| 118 | + |
| 119 | + let npmOutput = this.#executeListCmd(includeTransitive, manifestDir); |
| 120 | + return JSON.parse(npmOutput); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Create SBOM json string for npm Package. |
| 125 | + * @param {string} manifest - path for package.json |
| 126 | + * @param {{}} [opts={}] - optional various options to pass along the application |
| 127 | + * @returns {string} the SBOM json content |
| 128 | + * @private |
| 129 | + */ |
| 130 | + #getSBOM(manifest, opts = {}, includeTransitive) { |
| 131 | + this.#cmd = getCustomPath(this._cmdName(), opts); |
| 132 | + const depsObject = this._buildDependencyTree(includeTransitive, manifest, opts); |
| 133 | + let rootName = depsObject["name"] |
| 134 | + let rootVersion = depsObject["version"] |
| 135 | + if (!rootVersion) { |
| 136 | + rootVersion = defaultVersion |
| 137 | + } |
| 138 | + let mainComponent = this.#toPurl(rootName, rootVersion); |
| 139 | + |
| 140 | + let sbom = new Sbom(); |
| 141 | + sbom.addRoot(mainComponent) |
| 142 | + |
| 143 | + let dependencies = depsObject["dependencies"] || {}; |
| 144 | + this.#addAllDependencies(sbom, sbom.getRoot(), dependencies) |
| 145 | + let packageJson = fs.readFileSync(manifest).toString() |
| 146 | + let packageJsonObject = JSON.parse(packageJson); |
| 147 | + if (packageJsonObject.exhortignore !== undefined) { |
| 148 | + let ignoredDeps = Array.from(packageJsonObject.exhortignore); |
| 149 | + sbom.filterIgnoredDeps(ignoredDeps) |
| 150 | + } |
| 151 | + return sbom.getAsJsonString(opts) |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * This function recursively build the Sbom from the JSON that npm listing returns |
| 156 | + * @param sbom this is the sbom object |
| 157 | + * @param from this is the current component in bom (Should start with root/main component of SBOM) for which we want to add all its dependencies. |
| 158 | + * @param dependencies the current dependency list (initially it's the list of the root component) |
| 159 | + * @private |
| 160 | + */ |
| 161 | + #addAllDependencies(sbom, from, dependencies) { |
| 162 | + Object.entries(dependencies) |
| 163 | + .filter(entry => entry[1].version !== undefined) |
| 164 | + .forEach(entry => { |
| 165 | + let [name, artifact] = entry; |
| 166 | + let purl = this.#toPurl(name, artifact.version); |
| 167 | + sbom.addDependency(from, purl) |
| 168 | + let transitiveDeps = artifact.dependencies |
| 169 | + if (transitiveDeps !== undefined) { |
| 170 | + this.#addAllDependencies(sbom, sbom.purlToComponent(purl), transitiveDeps) |
| 171 | + } |
| 172 | + }); |
| 173 | + } |
| 174 | + |
| 175 | + #executeListCmd(includeTransitive, manifestDir) { |
| 176 | + const listArgs = this._listCmdArgs(includeTransitive, manifestDir); |
| 177 | + return this.#invokeCommand(listArgs); |
| 178 | + } |
| 179 | + |
| 180 | + #version() { |
| 181 | + this.#invokeCommand(['--version'], { stdio: 'ignore' }); |
| 182 | + } |
| 183 | + |
| 184 | + #createLockFile(manifestDir) { |
| 185 | + // in windows os, --prefix flag doesn't work, it behaves really weird , instead of installing the package.json fromm the prefix folder, |
| 186 | + // it's installing package.json (placed in current working directory of process) into prefix directory, so |
| 187 | + let originalDir = process.cwd() |
| 188 | + if (os.platform() === 'win32') { |
| 189 | + process.chdir(manifestDir) |
| 190 | + } |
| 191 | + const args = this._updateLockFileCmdArgs(manifestDir); |
| 192 | + try { |
| 193 | + this.#invokeCommand(args); |
| 194 | + } finally { |
| 195 | + if (os.platform() === 'win32') { |
| 196 | + process.chdir(originalDir) |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + #invokeCommand(args, opts = {}) { |
| 202 | + try { |
| 203 | + return invokeCommand(this.#cmd, args, opts); |
| 204 | + } catch (error) { |
| 205 | + if (error.code === 'ENOENT') { |
| 206 | + throw new Error(`${this.#cmd} is not accessible`); |
| 207 | + } |
| 208 | + throw new Error(`failed to execute ${this.#cmd} ${args}`, { cause: error }) |
| 209 | + } |
| 210 | + } |
| 211 | +} |
| 212 | + |
0 commit comments