Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to use libxml2-wasm for XML validation #1184

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file.
* Serializers and `Bom`-Normalizers will take changed `Models.Bom.tools` into account ([#1152] via [#1163])
* Dependencies
* Support `libxmljs2@^0.35` (via [#1173])
* Support `libxml2-wasm@^0.41` as an alternative for `libxmljs2` (via [#1184])
* Use `packageurl-js@^2.0.1`, was `@>=0.0.6 <0.0.8 || ^1` (via [#1142])

[#1142]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/1142
Expand Down
3 changes: 2 additions & 1 deletion docs/dev/decisions/XmlValidator.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ There are several implementations for this:
* [`libxmljs3`](https://www.npmjs.com/package/libxmljs3)
* unmaintained copy of `libxmljs2`
* ! DO NOT USE !
* Any alternative? Please open a pull-request to add them.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please bring this line back

* [`libxml2-wasm`](https://www.npmjs.com/package/libxml2-wasm)
* maintained WASM implementation of a libxml2 wrapper

At the moment of writing (2023-04-21),
`libxmljs` and `libxmljs2` are both working on several test environments. Both had the needed capabilities.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"ajv-formats": "^3.0.1",
"ajv-formats-draft2019": "^1.6.1",
"libxmljs2": "^0.31 || ^0.32 || ^0.33 || ^0.35",
jkowalleck marked this conversation as resolved.
Show resolved Hide resolved
"libxml2-wasm": "^0.4.1",
"xmlbuilder2": "^3.0.2"
},
"devDependencies": {
Expand Down
53 changes: 53 additions & 0 deletions src/_optPlug.node/__xmlValidators/libxml2-wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*!
This file is part of CycloneDX JavaScript Library.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache-2.0
Copyright (c) OWASP Foundation. All Rights Reserved.
*/

import { readFile } from 'fs/promises';
import { ParseOption, XmlDocument, XsdValidator } from 'libxml2-wasm';
import { pathToFileURL } from 'url';

import type { ValidationError } from '../../validation/types';
import type { Functionality, Validator } from '../xmlValidator';

/** @internal */
export default (async function (schemaPath: string): Promise<Validator> {
const schema = XmlDocument.fromString(
await readFile(schemaPath, 'utf-8'),
{
option: ParseOption.XML_PARSE_NONET | ParseOption.XML_PARSE_COMPACT,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interface for this wrapper is somewhat different, building the parse options is combining the flags you want on. In the other implementation it's an object where they could be turned on and off explicitely. So this should result in the same options.

I also added this implementation to the xmlValidator tests, and that includes an XXE test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets see how the tests turn out.

url: pathToFileURL(schemaPath).toString()
});
const validator = XsdValidator.fromDoc(schema);

return function (data: string): null | ValidationError {
const doc = XmlDocument.fromString(data, { option: ParseOption.XML_PARSE_NONET | ParseOption.XML_PARSE_COMPACT });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the options should be a constant, that is created outside the function.

let errors = null;
try {
validator.validate(doc);
}
catch (validationErrors) {
errors = validationErrors;
}

doc.dispose();
validator.dispose();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really free/dispose the validator and schema here?

Copy link
Author

@SierraNL SierraNL Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, this will go wrong the second call. I could just not dispose the validator and the schema. But the library emphasises proper disposing (https://jameslan.github.io/libxml2-wasm/v0.4/documents/Memory_Management.html). Here I'm really lacking in Typescript knowledge on how to solve this, could I use a using here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regarding using, read here: https://www.totaltypescript.com/typescript-5-2-new-keyword-using

regarding manually disposing/freeing: maybe just try it out. in the end, it all is javascript - just see what you can do.

schema.dispose();

return errors;
}
}) satisfies Functionality
3 changes: 2 additions & 1 deletion src/_optPlug.node/xmlValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export default opWrapper<Functionality>('XmlValidator', [
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-require-imports
-- needed */

['libxmljs2', () => require('./__xmlValidators/libxmljs2').default]
['libxmljs2', () => require('./__xmlValidators/libxmljs2').default],
['libxml2-wasm', () => require('./__xmlValidators/libxml2-wasm').default]
jkowalleck marked this conversation as resolved.
Show resolved Hide resolved
// ... add others here, pull-requests welcome!

/* eslint-enable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-require-imports */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const { realpathSync } = require('fs')
const { join } = require('path')

suite('functional: internals: OpPlug.node.xmlValidator implementation', () => {
for (const impl of ['libxmljs2']) {
for (const impl of ['libxmljs2','libxml2-wasm']) {
suite(impl, () => {
let makeValidator
try {
Expand Down
Loading