Skip to content

refactor(cli/extract.integrity): compare object if integrity is not matching #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import dotenv from "dotenv";
dotenv.config();
dotenv.config({ quiet: true });

// Import Node.js Dependencies
import path from "node:path";
Expand Down Expand Up @@ -131,7 +131,6 @@ prog
prog
.command("extract integrity [spec]")
.describe(i18n.getTokenSync("cli.commands.extractIntegrity.desc"))
.option("-t, --token", i18n.getTokenSync("cli.commands.extractIntegrity.option_token"))
.action(commands.extractIntegrity.main);

prog.parse(process.argv);
Expand Down
4 changes: 1 addition & 3 deletions docs/cli/extract-integrity.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ $ nsecure extract integrity [spec]

## ⚙️ Available Options

| Name | Shortcut | Default Value | Description |
|---|---|---|---|
| `--token` | `-t` | undefined | NPM token. |
NONE
3 changes: 1 addition & 2 deletions i18n/english.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ const cli = {
cleared: "Cache cleared successfully!"
},
extractIntegrity: {
desc: "Extract the integrity of a package from its manifest and tarball and compare the two integrities if different from one another.",
option_token: "NPM token"
desc: "Extract the integrity of a package from its manifest and tarball and compare the two integrities if different from one another."
}
},
startHttp: {
Expand Down
3 changes: 1 addition & 2 deletions i18n/french.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ const cli = {
cleared: "Cache nettoyé avec succès !"
},
extractIntegrity: {
desc: "Extraire l'intégrité d'un paquet à partir de son manifeste et du tarball et comparer les deux intégrités si elles sont différentes.",
option_token: "Jeton NPM"
desc: "Extraire l'intégrité d'un paquet à partir de son manifeste et du tarball et comparer les deux intégrités si elles sont différentes."
}
},
startHttp: {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@
"dependencies": {
"@nodesecure/documentation-ui": "^1.3.0",
"@nodesecure/flags": "^3.0.3",
"@nodesecure/i18n": "^4.0.1",
"@nodesecure/i18n": "^4.0.2",
"@nodesecure/js-x-ray": "^9.2.0",
"@nodesecure/licenses-conformance": "^2.1.0",
"@nodesecure/npm-registry-sdk": "^3.0.0",
"@nodesecure/ossf-scorecard-sdk": "^3.2.1",
"@nodesecure/rc": "^5.0.0",
"@nodesecure/report": "^3.0.0",
"@nodesecure/scanner": "^6.9.0",
"@nodesecure/scanner": "^6.12.0",
"@nodesecure/utils": "^2.2.0",
"@nodesecure/vulnera": "^2.0.1",
"@openally/result": "^1.3.0",
Expand All @@ -108,12 +108,12 @@
"@topcli/spinner": "^3.0.0",
"cacache": "^19.0.1",
"chokidar": "^4.0.3",
"diff": "^8.0.2",
"dotenv": "^17.0.0",
"filenamify": "^6.0.0",
"glob": "^11.0.1",
"highlightjs-line-numbers.js": "^2.8.0",
"ini": "^5.0.0",
"json-diff-ts": "^4.8.1",
"kleur": "^4.1.5",
"ms": "^2.1.3",
"open": "^10.1.0",
Expand Down
77 changes: 49 additions & 28 deletions src/commands/extract-integrity.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,58 @@
// Import Node.js Dependencies
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";

// Import Third-party Dependencies
import kleur from "kleur";
import { diffChars } from "diff";
import { packumentVersion } from "@nodesecure/npm-registry-sdk";
import * as npmRegistrySDK from "@nodesecure/npm-registry-sdk";
import { diff } from "json-diff-ts";
import { tarball } from "@nodesecure/scanner";
import {
parseNpmSpec,
packageJSONIntegrityHash
} from "@nodesecure/mama";

export async function main(spec, options) {
const [pkgName, pkgVersion] = spec.split("@");
const { dist: { tarball: location, shasum: manifestIntegrity } } = await packumentVersion(pkgName, pkgVersion, {
token: options.token
});
const manifestManager = await tarball.extractAndResolve(location, {
spec
});
const tarballIntegrity = manifestManager.integrity;
if (manifestIntegrity === tarballIntegrity) {
console.log(`integrity: ${manifestIntegrity}`);

return;
export async function main(
npmPackageSpec
) {
const parsedPackageSpec = parseNpmSpec(npmPackageSpec);
if (!parsedPackageSpec) {
throw new Error(`Invalid npm spec: ${npmPackageSpec}`);
}

console.log(`manifest integrity: ${manifestIntegrity}`);
console.log(`tarball integrity: ${tarballIntegrity}`);
process.stdout.write("integrity diff: ");
for (const { added, removed, value } of diffChars(manifestIntegrity, tarballIntegrity)) {
if (added) {
process.stdout.write(kleur.green().bold(`+${value}`));
}
else if (removed) {
process.stdout.write(kleur.red().bold(`-${value}`));
const packumentVersion = await npmRegistrySDK.packumentVersion(
parsedPackageSpec.name,
parsedPackageSpec.semver,
{
token: process.env.NODE_SECURE_TOKEN
}
else {
process.stdout.write(value);
);
const remote = packageJSONIntegrityHash(
packumentVersion,
{ isFromRemoteRegistry: true }
);

const extractionDirectory = await fs.mkdtemp(
path.join(os.tmpdir(), "nodesecure-tarball-integrity-")
);

try {
const mama = await tarball.extractAndResolve(extractionDirectory, {
spec: npmPackageSpec
});
const local = packageJSONIntegrityHash(mama.document);

if (local.integrity === remote.integrity) {
console.log("no integrity diff found");

return;
}

const diffs = diff(local.object, remote.object);
console.log("integrity diff found:");
console.log(JSON.stringify(diffs, null, 2));
}
finally {
await fs.rm(extractionDirectory, { recursive: true, force: true });
}
console.log("\n");
}
Loading