mod cyclonedx;
mod detection;
mod spdx;
mod spdx3;
pub mod streaming;
mod traits;
pub use cyclonedx::CycloneDxParser;
pub use detection::{DetectionResult, FormatDetector, MIN_CONFIDENCE_THRESHOLD, ParserKind};
pub use spdx::SpdxParser;
pub use spdx3::Spdx3Parser;
pub use streaming::{ParseEvent, ParseProgress, ParsedMetadata, StreamingConfig, StreamingParser};
pub use traits::{FormatConfidence, FormatDetection, ParseError, SbomParser};
use crate::model::NormalizedSbom;
use std::path::Path;
#[cfg_attr(feature = "ffi", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct DetectedFormat {
pub format_name: String,
pub confidence: f32,
pub variant: Option<String>,
pub version: Option<String>,
pub warnings: Vec<String>,
}
#[must_use]
pub fn detect_format(content: &str) -> Option<DetectedFormat> {
let detector = FormatDetector::new();
let result = detector.detect_from_content(content);
if result.can_parse() {
Some(DetectedFormat {
format_name: result
.parser
.map(|p| p.name().to_string())
.unwrap_or_default(),
confidence: result.confidence.value(),
variant: result.variant,
version: result.version,
warnings: result.warnings,
})
} else {
None
}
}
pub(crate) const MAX_SBOM_FILE_SIZE: u64 = 512 * 1024 * 1024;
pub const DECLARED_NO_DEPENDENCIES_PROPERTY: &str = "sbom-tools:declared-no-dependencies";
pub(crate) fn strip_bom(content: &str) -> &str {
content.strip_prefix('\u{FEFF}').unwrap_or(content)
}
pub(crate) fn contains_json_key(content: &str, key: &str) -> bool {
let quoted = format!("\"{key}\"");
let mut search_from = 0;
while let Some(rel) = content[search_from..].find(quoted.as_str()) {
let after = search_from + rel + quoted.len();
if content[after..].trim_start().starts_with(':') {
return true;
}
search_from = after;
}
false
}
pub(crate) const MAX_VULN_DESCRIPTION_BYTES: usize = 64 * 1024;
pub(crate) fn capped_description(desc: &Option<String>) -> Option<String> {
desc.as_ref().map(|d| {
if d.len() <= MAX_VULN_DESCRIPTION_BYTES {
return d.clone();
}
let mut end = MAX_VULN_DESCRIPTION_BYTES;
while end > 0 && !d.is_char_boundary(end) {
end -= 1;
}
let mut truncated = d[..end].to_string();
truncated.push_str("… [truncated]");
truncated
})
}
pub fn parse_sbom(path: &Path) -> Result<NormalizedSbom, ParseError> {
let metadata = std::fs::metadata(path).map_err(|e| ParseError::IoError(e.to_string()))?;
if metadata.len() > MAX_SBOM_FILE_SIZE {
return Err(ParseError::IoError(format!(
"SBOM file is {} MB, exceeding the {} MB limit. Split the document or filter it (e.g. `sbom-tools tailor`) before processing.",
metadata.len() / (1024 * 1024),
MAX_SBOM_FILE_SIZE / (1024 * 1024),
)));
}
let content = std::fs::read_to_string(path).map_err(|e| ParseError::IoError(e.to_string()))?;
parse_sbom_str(&content)
}
pub fn parse_sbom_str(content: &str) -> Result<NormalizedSbom, ParseError> {
let detector = FormatDetector::new();
detector.parse_str(content)
}
#[deprecated(
since = "0.2.0",
note = "Use detect_format() or CycloneDxParser::detect() instead"
)]
#[must_use]
pub fn is_cyclonedx(content: &str) -> bool {
CycloneDxParser::new().can_parse(content)
}
#[deprecated(
since = "0.2.0",
note = "Use detect_format() or SpdxParser::detect() instead"
)]
#[must_use]
pub fn is_spdx(content: &str) -> bool {
SpdxParser::new().can_parse(content)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn capped_description_truncates_on_char_boundary() {
assert_eq!(capped_description(&None), None);
assert_eq!(
capped_description(&Some("short".to_string())).as_deref(),
Some("short")
);
let big = "é".repeat(MAX_VULN_DESCRIPTION_BYTES); let out = capped_description(&Some(big)).unwrap();
assert!(out.len() <= MAX_VULN_DESCRIPTION_BYTES + 16);
assert!(out.ends_with("… [truncated]"));
assert!(std::str::from_utf8(out.as_bytes()).is_ok());
}
#[test]
fn strip_bom_removes_only_a_leading_marker() {
assert_eq!(strip_bom("\u{FEFF}{\"a\":1}"), "{\"a\":1}");
assert_eq!(strip_bom("{\"a\":1}"), "{\"a\":1}");
assert_eq!(strip_bom("{\"a\":\"\u{FEFF}\"}"), "{\"a\":\"\u{FEFF}\"}");
}
#[test]
fn contains_json_key_requires_key_position() {
assert!(contains_json_key(
r#"{"spdxVersion":"SPDX-2.3"}"#,
"spdxVersion"
));
assert!(contains_json_key(
r#"{"spdxVersion" : "SPDX-2.3"}"#,
"spdxVersion"
));
assert!(!contains_json_key(
r#"{"name":"spdxVersion","other":1}"#,
"spdxVersion"
));
assert!(!contains_json_key("no markers here", "spdxVersion"));
}
#[test]
fn bom_prefixed_document_parses_end_to_end() {
let content = "\u{FEFF}{\"bomFormat\": \"CycloneDX\", \"specVersion\": \"1.5\", \
\"components\": [{\"type\": \"library\", \"name\": \"lib\", \"version\": \"1.0\"}]}";
let sbom = parse_sbom_str(content).expect("BOM-prefixed document must parse");
assert_eq!(sbom.component_count(), 1);
}
#[test]
fn test_detect_cyclonedx_json() {
let content = r#"{"bomFormat": "CycloneDX", "specVersion": "1.5"}"#;
let detected = detect_format(content).expect("Should detect format");
assert_eq!(detected.format_name, "CycloneDX");
assert!(detected.confidence >= 0.75);
assert_eq!(detected.variant, Some("JSON".to_string()));
assert_eq!(detected.version, Some("1.5".to_string()));
}
#[test]
fn test_detect_cyclonedx_xml() {
let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<bom xmlns="http://cyclonedx.org/schema/bom/1.5" version="1">
<components/>
</bom>"#;
let detected = detect_format(content).expect("Should detect format");
assert_eq!(detected.format_name, "CycloneDX");
assert!(detected.confidence >= 0.75);
assert_eq!(detected.variant, Some("XML".to_string()));
}
#[test]
fn test_detect_spdx_json() {
let content = r#"{"spdxVersion": "SPDX-2.3", "SPDXID": "SPDXRef-DOCUMENT"}"#;
let detected = detect_format(content).expect("Should detect format");
assert_eq!(detected.format_name, "SPDX");
assert!(detected.confidence >= 0.75);
assert_eq!(detected.variant, Some("JSON".to_string()));
assert_eq!(detected.version, Some("2.3".to_string()));
}
#[test]
fn test_detect_spdx_tag_value() {
let content = "SPDXVersion: SPDX-2.3\nDataLicense: CC0-1.0\nSPDXID: SPDXRef-DOCUMENT";
let detected = detect_format(content).expect("Should detect format");
assert_eq!(detected.format_name, "SPDX");
assert!(detected.confidence >= 0.75);
assert_eq!(detected.variant, Some("tag-value".to_string()));
assert_eq!(detected.version, Some("2.3".to_string()));
}
#[test]
fn test_detect_unknown_format() {
let content = r#"{"some": "random", "json": "content"}"#;
let detected = detect_format(content);
assert!(detected.is_none());
}
#[test]
fn test_detect_spdx3_json_ld() {
let content = r#"{"@context": "https://spdx.org/rdf/3.0.1/spdx-context.jsonld", "type": "SpdxDocument", "spdxId": "urn:spdx:doc:test", "creationInfo": {"specVersion": "3.0.1"}}"#;
let detected = detect_format(content).expect("Should detect SPDX 3.0 format");
assert_eq!(detected.format_name, "SPDX");
assert!(detected.confidence >= 0.9);
assert_eq!(detected.variant, Some("JSON-LD".to_string()));
}
#[test]
fn test_confidence_based_selection() {
let cdx_content = r#"{"bomFormat": "CycloneDX", "specVersion": "1.6", "components": []}"#;
let cdx_parser = CycloneDxParser::new();
let spdx_parser = SpdxParser::new();
let cdx_conf = cdx_parser.confidence(cdx_content);
let spdx_conf = spdx_parser.confidence(cdx_content);
assert!(cdx_conf.value() > spdx_conf.value());
}
}