API library for parsing and validating npm/yarn lockfiles to detect security issues and enforce security policies
npx @tessl/cli install tessl/npm-lockfile-lint-api@5.9.0Programmatic API library for linting npm and yarn lockfiles to analyze and detect security issues. Provides parser and validator classes to enforce security policies and prevent malicious package injection attacks.
const { ValidateHost, ParseLockfile } = require('lockfile-lint-api');
// Parse and validate
const parser = new ParseLockfile({ lockfilePath: './package-lock.json' });
const lockfile = parser.parseSync();
const validator = new ValidateHost({ packages: lockfile.object });
const result = validator.validate(['npm']);
if (result.type === 'success') {
console.log('All packages are from allowed hosts');
} else {
console.error('Validation failed:', result.errors);
}lockfile-lint-apinpm install lockfile-lint-apiParseLockfile - Parses npm and yarn lockfiles into unified formatValidateHost - Validates package sources come from whitelisted hostsValidateHttps - Ensures all packages use HTTPS protocolValidatePackageNames - Detects name confusion attacksValidateScheme - Validates URI schemes (protocols)ValidateUrl - Validates exact URL whitelistingValidateIntegrity - Validates SHA-512 integrity hashesconst {
ParseLockfile,
ValidateHost,
ValidateHttps,
ValidatePackageNames,
ValidateScheme,
ValidateUrl,
ValidateIntegrity
} = require('lockfile-lint-api');The library has two main functional areas:
ParseLockfile reads and parses lockfiles (npm v1/v2/v3, yarn classic, Yarn Berry) into a unified formatAll validators follow a consistent pattern: accept a packages object in constructor, expose a validate() method returning a result object.
interface ValidationResult {
type: 'success' | 'error';
errors: ValidationError[];
}
interface ValidationError {
message: string;
package: string;
}interface ParseResult {
type: 'success';
object: {
[packageKey: string]: PackageMetadata;
};
}
interface PackageMetadata {
version: string;
resolved?: string;
integrity?: string;
requires?: Object;
}ParsingError exceptions for invalid arguments or parsing failuresresolved field are automatically skipped by URL-based validatorsValidateHost supports shortcuts:
'npm' → 'registry.npmjs.org''yarn' → 'registry.yarnpkg.com''verdaccio' → 'registry.verdaccio.org'| Class | Purpose | Key Methods |
|---|---|---|
ParseLockfile | Parse lockfiles | parseSync() |
ValidateHost | Validate hostnames | validate(hosts[], options?), validateSingle() |
ValidateHttps | Validate HTTPS protocol | validate() |
ValidatePackageNames | Validate name consistency | validate(aliases?) |
ValidateScheme | Validate URI schemes | validate(schemes[]) |
ValidateUrl | Validate exact URLs | validate(urls[], options?), validateSingle() |
ValidateIntegrity | Validate hash algorithms | validate(options?), validateSingle() |
new ParseLockfile({ lockfilePath }) → parseSync()new ValidateXxx({ packages: lockfile.object })validator.validate(...args)result.type === 'success' or handle result.errorsSee Real-World Scenarios for complete examples including:
ParsingError types:
NO_OPTIONS - Missing options objectNO_LOCKFILE - Missing lockfile path or textNO_PARSER_FOR_TYPE - Unsupported lockfile typeNO_PARSER_FOR_PATH - Cannot determine lockfile typeREAD_FAILED - File read failurePARSE_NPMLOCKFILE_FAILED - npm parsing failurePARSE_YARNLOCKFILE_FAILED - yarn parsing failure