
Security News
pnpm 11.5 Adds Support for Recognizing npm Staged Publishes
pnpm 11.5 now recognizes npm staged publish approvals in release metadata, preventing those releases from being mistaken for lower-trust package publishes.
@mitre/hdf-parsers
Advanced tools
Parse and load Heimdall Data Format (HDF) documents with validation. Provides a simple, type-safe API for reading HDF Results and Baselines from JSON with automatic schema validation.
hdf-parsers provides validated parsing of HDF documents:
| hdf-parsers | hdf-validators |
|---|---|
| Parse JSON → typed objects | Validate JSON against schema |
| "Load and validate this HDF file" | "Is this valid HDF?" |
| Returns typed HdfResults/HdfBaseline | Returns validation errors |
| One-step parse + validate | Schema validation only |
| Used by CLI commands and tools | Used internally by parsers |
Example:
validateResults(data) → { valid: true, errors: [] } (validators - just validates)parseResults(json) → { success: true, data: HdfResults } (parsers - validates AND parses)npm install @mitre/hdf-parsers
import { parseResults, parseBaseline, parse } from '@mitre/hdf-parsers';
// Parse HDF Results
const json = '{"baselines":[...],"targets":[],"statistics":{}}';
const result = parseResults(json);
if (result.success) {
console.log('Parsed HDF Results:', result.data);
console.log('Number of baselines:', result.data.baselines?.length);
} else {
console.error('Parse failed:', result.error);
}
// Parse HDF Baseline
const baselineJson = '{"name":"My Baseline","requirements":[...],...}';
const baselineResult = parseBaseline(baselineJson);
if (baselineResult.success) {
console.log('Baseline name:', baselineResult.data.name);
console.log('Requirements:', baselineResult.data.requirements.length);
}
// Auto-detect document type
const unknownJson = '...'; // Could be Results or Baseline
const autoResult = parse(unknownJson);
if (autoResult.success) {
console.log('Document type:', autoResult.type); // "results" or "baseline"
console.log('Parsed data:', autoResult.data);
}
// Parse from Uint8Array (e.g., file reads)
import { readFileSync } from 'fs';
const bytes = readFileSync('scan-results.json');
const result = parseResults(bytes);
package main
import (
"fmt"
"os"
parsers "github.com/mitre/hdf-libs/hdf-parsers/go/v3"
)
func main() {
// Read HDF file
data, err := os.ReadFile("results.json")
if err != nil {
panic(err)
}
// Parse HDF Results
result := parsers.ParseResults(data)
if result.Success {
fmt.Println("✓ Parsed HDF Results")
fmt.Printf("Baselines: %d\n", len(result.Data.Baselines))
} else {
fmt.Println("✗ Parse failed:")
fmt.Println(result.Error)
os.Exit(1)
}
}
// Parse HDF Baseline
result := parsers.ParseBaseline(baselineData)
if result.Success {
fmt.Println("Baseline name:", result.Data.Name)
fmt.Printf("Requirements: %d\n", len(result.Data.Requirements))
}
// Auto-detect document type
result := parsers.Parse(data)
if result.Success {
fmt.Println("Document type:", result.Type) // "results" or "baseline"
}
parseResults(input: string | Uint8Array): ParseResult<HdfResults>Parse HDF Results document from JSON string or bytes.
input - JSON string or Uint8Array to parseParseResult<HdfResults> with parsed data or errorparseBaseline(input: string | Uint8Array): ParseResult<HdfBaseline>Parse HDF Baseline document from JSON string or bytes.
input - JSON string or Uint8Array to parseParseResult<HdfBaseline> with parsed data or errorparse(input: string | Uint8Array): ParseResult<HdfResults | HdfBaseline>Parse HDF document with auto-detection of type (Results vs Baseline).
input - JSON string or Uint8Array to parseParseResult with parsed data, type indicator, or errorParseResult<T>interface ParseResult<T> {
success: boolean; // True if parsing succeeded
data?: T; // Parsed data (undefined if failed)
error?: string; // Error message (undefined if succeeded)
type?: 'results' | 'baseline'; // Document type (only for parse())
}
ParseResults(input []byte) ResultsParseResultParse HDF Results document from JSON bytes.
input - JSON bytes to parseResultsParseResult with parsed data or errorParseBaseline(input []byte) BaselineParseResultParse HDF Baseline document from JSON bytes.
input - JSON bytes to parseBaselineParseResult with parsed data or errorParse(input []byte) ParseResultParse HDF document with auto-detection of type.
input - JSON bytes to parseParseResult with parsed data, type indicator, or errortype ResultsParseResult struct {
Success bool `json:"success"`
Data *hdf.HDFResults `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}
type BaselineParseResult struct {
Success bool `json:"success"`
Data *hdf.HDFBaseline `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}
type ParseResult struct {
Success bool `json:"success"`
Data interface{} `json:"data,omitempty"`
Error string `json:"error,omitempty"`
Type string `json:"type,omitempty"` // "results" or "baseline"
}
error: "Invalid JSON: Unexpected token } in JSON at position 42"
Ensure the input is valid JSON. Check for:
error: "Schema validation failed: baselines: is required"
The JSON is valid but doesn't match the HDF schema. Common issues:
error: "Input is empty"
Provide non-empty JSON content.
error: "Invalid JSON: unexpected trailing data after end of object"
The JSON has extra characters after the closing brace. Remove any trailing content.
Parse HDF files for CLI operations:
import { parseResults } from '@mitre/hdf-parsers';
import { readFileSync } from 'fs';
const data = readFileSync(inputFile, 'utf-8');
const result = parseResults(data);
if (!result.success) {
console.error(`Failed to parse ${inputFile}: ${result.error}`);
process.exit(1);
}
// Process the validated HDF data
processResults(result.data);
Validate HDF input before conversion:
import { parseResults } from '@mitre/hdf-parsers';
export function convertHdfToCsv(hdfJson: string): string {
const result = parseResults(hdfJson);
if (!result.success) {
throw new Error(`Invalid HDF input: ${result.error}`);
}
// Convert validated data
return buildCsv(result.data);
}
Parse and validate HDF uploads:
app.post('/api/upload', async (req, res) => {
const result = parseResults(req.body);
if (!result.success) {
return res.status(400).json({
error: 'Invalid HDF document',
details: result.error
});
}
// Store validated HDF data
await storeResults(result.data);
res.json({ success: true });
});
Get type-safe HDF objects:
import { parseResults } from '@mitre/hdf-parsers';
import type { HdfResults } from '@mitre/hdf-schema';
function processResults(data: HdfResults) {
// TypeScript knows the exact structure
for (const baseline of data.baselines ?? []) {
console.log(`Baseline: ${baseline.name}`);
for (const req of baseline.requirements ?? []) {
console.log(` Requirement ${req.id}: ${req.results?.length ?? 0} results`);
}
}
}
const result = parseResults(jsonData);
if (result.success) {
processResults(result.data); // Type-safe!
}
const result = parseResults(data);
if (!result.success) {
// Handle error - data is undefined here
console.error(result.error);
return;
}
// TypeScript knows data exists here
console.log(result.data.baselines);
const result = parseResults(userInput);
if (!result.success) {
if (result.error.includes('JSON')) {
console.error('File contains invalid JSON syntax');
} else if (result.error.includes('Schema validation')) {
console.error('File does not match HDF format');
} else {
console.error('Failed to parse HDF file');
}
console.error('Details:', result.error);
}
import { parseResults } from '@mitre/hdf-parsers';
import { validateResults } from '@mitre/hdf-validators';
// For detailed debugging, use validator directly
const validationResult = validateResults(jsonData);
if (!validationResult.valid) {
console.error('Validation errors:');
for (const error of validationResult.errors) {
console.error(` ${error.field}: ${error.message}`);
}
}
// For normal use, parser is simpler
const parseResult = parseResults(jsonData);
# Install dependencies
pnpm install
# Run TypeScript tests
pnpm test:ts
# Run Go tests
pnpm test:go
# Run all tests
pnpm test
# Run tests with coverage
pnpm test:coverage
# Build TypeScript package
pnpm build
# Lint code
pnpm lint
Both TypeScript and Go implementations maintain >95% test coverage with comprehensive parsing tests. Run pnpm test:coverage to view current coverage report.
Apache-2.0 © MITRE Corporation
FAQs
Parse and load HDF documents with validation
We found that @mitre/hdf-parsers demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
pnpm 11.5 now recognizes npm staged publish approvals in release metadata, preventing those releases from being mistaken for lower-trust package publishes.

Security News
Federal audit finds NIST lacked a plan to clear the NVD backlog, wasted funds on duplicate work, and delayed use of CISA data.

Research
/Security News
A mini Shai-Hulud campaign compromised Red Hat Cloud Services npm packages to steal developer and CI/CD secrets during installation.