Skip to content

Commit 818b1e5

Browse files
committed
Revert file deletion, removed decision
Signed-off-by: Leon Grave <[email protected]>
1 parent a4c45c1 commit 818b1e5

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

docs/dev/decisions/XmlValidator.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ as it was more popular/used and had a more active community.
3939

4040
Decided to replace `libxmljs2`, as it is end of life.
4141

42-
#### 2024-11-26
43-
44-
Decided to replace `libxmljs2` with `libxml2-wasm`, since it's maintained and a functioning XML validator.
45-
4642
## WebBrowsers
4743

4844
there seams to exist no solution for validating XML according to XSD
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*!
2+
This file is part of CycloneDX JavaScript Library.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
SPDX-License-Identifier: Apache-2.0
13+
Copyright (c) OWASP Foundation. All Rights Reserved.
14+
*/
15+
16+
import { readFile } from 'fs/promises'
17+
import { type ParserOptions, parseXml } from 'libxmljs2'
18+
import { pathToFileURL } from 'url'
19+
20+
import type { ValidationError } from '../../validation/types'
21+
import type { Functionality, Validator } from '../xmlValidator'
22+
23+
const xmlParseOptions: Readonly<ParserOptions> = Object.freeze({
24+
nonet: true,
25+
compact: true,
26+
// explicitly prevent XXE
27+
// see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
28+
// see https://github.com/CycloneDX/cyclonedx-javascript-library/issues/1061
29+
noent: false,
30+
dtdload: false
31+
})
32+
33+
/** @internal */
34+
export default (async function (schemaPath: string): Promise<Validator> {
35+
const schema = parseXml(await readFile(schemaPath, 'utf-8'),
36+
{ ...xmlParseOptions, baseUrl: pathToFileURL(schemaPath).toString() })
37+
38+
return function (data: string): null | ValidationError {
39+
const doc = parseXml(data, xmlParseOptions)
40+
return doc.validate(schema)
41+
? null
42+
: doc.validationErrors
43+
}
44+
}) satisfies Functionality

0 commit comments

Comments
 (0)