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

Conversation

SierraNL
Copy link

@SierraNL SierraNL commented Nov 26, 2024

Due to libxmljs2 not being maintained and contains a vulnerability, a replacement needed to be found. This commit replaces it with libxml2-wasm, which is a new, but maintained library, which serves the purpose of validating XML.

The implementation is as close the the previous library in regards to flags passed to libxml2, but only adapted to a different interface and the recommendation to dispose all objects.

This is my first contribution to this project, and typescript isn't my usual language, so comments are welcome.

related to: #1079

Due to libxmljs2 not being maintained and contains a vulnerability, a replacement needed to be found.
This commit replaces it with libxml2-wasm, which is a new, but maintained library, which serves the purpose of validating XML.

The implementation is as close the the previous library in regards to flags passed to libxml2, but only adapted to a different interface and the recommendation to dispose all objects.

This is my first contribution to this project, and typescript isn't my usual language, so comments are welcome.

Resolves: CycloneDX#1079
Signed-off-by: Leon Grave <[email protected]>
@SierraNL SierraNL requested a review from a team as a code owner November 26, 2024 10:45
@jkowalleck
Copy link
Member

thanks for donating this feature, @SierraNL .

let me clarify some things:

Due to libxmljs2 [...] contains a vulnerability [...]

this is not the case. The current libxml2 library contains a feature, that, if used wrong downstream, could lead to a vulnerability downstream. The downstream usage in the CycloneDX-JS-lib does not use it wrong, so no vulnerability exists.

[...] a replacement needed to be found

This is true in the long term, but we do not intend to replace libxmljs2 right away. Instead, we want to allow alternatives.
Therefore, some of your changes need to be reverted.

src/_optPlug.node/xmlValidator.ts Show resolved Hide resolved
src/_optPlug.node/__xmlValidators/libxmljs2.ts Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
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.

Signed-off-by: Leon Grave <[email protected]>
@@ -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

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.

}

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.

@jkowalleck jkowalleck changed the title Switch to libxml2-wasm for XML validation allow to use libxml2-wasm for XML validation Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants