-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdx-xml-to-json.ts
More file actions
170 lines (151 loc) · 4.82 KB
/
cdx-xml-to-json.ts
File metadata and controls
170 lines (151 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { XMLParser } from 'fast-xml-parser';
import type { CdxBom } from './index.ts';
const COLLECTION_KEYS = [
'tools',
'components',
'externalReferences',
'licenses',
'properties',
'dependencies',
];
const ARRAY_ELEMENTS = [
'tool',
'dependency',
'reference',
'license',
'property',
'vulnerability',
'hash',
];
function getChildKey(key: string): string {
if (key === 'tools') return 'tool';
if (key === 'components') return 'component';
if (key === 'externalReferences') return 'reference';
if (key === 'licenses') return 'license';
if (key === 'dependencies') return 'dependency';
return 'property';
}
function ensureArray<T>(value: T | T[]): T[] {
return Array.isArray(value) ? value : [value];
}
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: '',
textNodeName: '#text',
isArray: (name: string, jpath) =>
ARRAY_ELEMENTS.includes(name) ||
(name === 'component' && jpath.includes('components')),
});
function processObject(obj: any): any {
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
return obj;
}
if ('#text' in obj) {
const { '#text': textValue, ...otherProps } = obj;
if (Object.keys(otherProps).length === 0) {
return textValue;
}
if (textValue) {
otherProps.value = textValue;
}
return otherProps;
}
return obj;
}
function transform(obj: any): any {
if (Array.isArray(obj)) {
return obj.map(transform);
}
if (typeof obj !== 'object' || obj === null) {
return obj;
}
const processed = processObject(obj);
if (Array.isArray(processed)) {
return processed.map(transform);
}
if (typeof processed !== 'object' || processed === null) {
return processed;
}
const result: any = {};
for (const [key, value] of Object.entries<any>(processed)) {
if (key === '?xml') continue;
if (key === 'bom') {
const bomData = transform(value);
const xmlns = bomData.xmlns;
const specVersion = xmlns?.match(/\/(\d+\.\d+)$/)?.[1] || '1.4';
Object.assign(result, {
$schema: `http://cyclonedx.org/schema/bom-${specVersion}.schema.json`,
bomFormat: 'CycloneDX',
specVersion,
version: Number(bomData.version) || 1,
...Object.fromEntries(
Object.entries(bomData).filter(
([k]) => k !== 'xmlns' && k !== 'version',
),
),
});
} else if (
COLLECTION_KEYS.includes(key) &&
value &&
typeof value === 'object'
) {
const childKey = getChildKey(key);
const childValue = value[childKey];
if (key === 'licenses' && childValue) {
const licenses = ensureArray(childValue);
result[key] = licenses.map((lic) => ({ license: transform(lic) }));
} else if (key === 'licenses' && value.expression) {
// Handle license expressions
const expressions = ensureArray(value.expression);
result[key] = expressions.map((expression) => ({ expression }));
} else if (key === 'properties' && childValue) {
const properties = ensureArray(childValue);
result[key] = properties.map((prop) => {
const transformed = transform(prop);
// Ensure properties always have a value field
if (!('value' in transformed)) {
transformed.value = '';
} else if (typeof transformed.value !== 'string') {
transformed.value = String(transformed.value);
}
return transformed;
});
} else if (key === 'dependencies' && childValue) {
const dependencies = ensureArray(childValue);
result[key] = dependencies.map((dep) => {
const transformed = transform(dep);
// Handle nested dependencies - rename 'dependency' to 'dependsOn' and flatten refs
if (transformed.dependency) {
const nestedDeps = ensureArray(transformed.dependency);
transformed.dependsOn = nestedDeps.map(
(nestedDep) => nestedDep.ref || nestedDep,
);
delete transformed.dependency;
}
return transformed;
});
} else {
result[key] = childValue ? transform(childValue) : transform(value);
}
} else if (key === 'hashes' && value?.hash) {
const hashes = ensureArray(value.hash);
result[key] = hashes.map((h) => ({
alg: h.alg,
content: h['#text'] || h.content || h,
}));
} else {
result[key] = transform(value);
}
}
return result;
}
/**
* Converts a CycloneDX XML string to a JSON object.
* The CycloneDX spec does not change between formats, so conversion from XML to JSON is lossless.
* @param xml - The XML string to parse
* @returns The parsed CycloneDX BOM object
*/
export function xmlStringToJSON(xml: string): CdxBom {
const parsed = parser.parse(xml);
return transform(parsed);
}