Skip to content

A Possible Problem with BOM Validation  #54

Closed

Description

@CleanHit

I've used this part of your code to validate user provided BOM files

import xmlschema  # same as your used version
bom_schema = xmlschema.XMLSchema("http://cyclonedx.org/schema/bom/1.1")

I've changed the version to 1.1 here and in my test code since I get this error when using 1.0:

  File "/home/glozmantal/PycharmProjects/Android-iOS-Code-OSS-Report/venv/lib64/python3.8/site-packages/xmlschema/validators/schema.py", line 885, in get_schema
    raise XMLSchemaKeyError('the namespace {!r} is not loaded'.format(namespace)) from None
xmlschema.exceptions.XMLSchemaKeyError: "the namespace 'http://cyclonedx.org/schema/bom/1.1' is not loaded"

When I use

bom_schema.is_valid(bom.name):  # I use pathlib so 'bom.name' is equivalent to 'bom' in your code.

And when I try to validate this broken test bom.xml file that was created using the ORT tool which uses BOM schema 1.1 against "http://cyclonedx.org/schema/bom/1.1", I get a positive validation. I've changed the license text value, apart from distorting the XML format integrity for the validation test.

<?xml version="1.0" encoding="UTF-8"?>
<bom xmlns="http://cyclonedx.org/schema/bom/1.1" serialNumber="urn:uuid:1817d96f-20c1-41a6-b8fa-fdb16bdc97de" version="1">
    <components>
        <component type="library">
        <group>androidx.annotation</group>
        <name>annotation
        <version>1.1.0</version>
        <description>The Support Library is a static library that you can add to your Android application in order to use APIs that are either not available for older platform versions or utility APIs that aren't a part of the framework APIs.</description>
        <scope>required</scope>
        <hashes>
            <hash alg="SHA-1">e3a6fb2f40e3a3842e6b7472628ba4ce416ea4c8</hash>
        </hashes>
        <licenses>
            <license>
                <id>Apache-2.0</id>
                <text content-type="plain/text"><![CDATA[ TEST
LICENSE TEXT.
]]></text>

            </license>
        </licenses>
        <purl>pkg:maven/androidx.annotation/[email protected]</purl>

    </externalReferences>

Activity

  1. coderpatros commented on Mar 11, 2020

    @coderpatros
    Member

    @CleanHit I wasn't able to reproduce this. I couldn't even parse that xml. Have you confirmed that bom.name is what you expect?

    And I suggest you do something like this...

    import xmlschema
    from xml.etree import ElementTree
    
    xt = ElementTree.parse('bom.xml')
    
    schema_tag = xt.getroot().tag
    schema_url = schema_tag[1:].split('}')[0]
    
    print('Loading schema', schema_url)
    bom_schema = xmlschema.XMLSchema(schema_url)
    
    print('Validating bom.xml')
    print(bom_schema.is_valid('bom.xml'))
    

    That will read the schema url from the xml file. Saves you hard coding the schema version and should support validating BOMs of different versions without code changes.

  2. CleanHit commented on Mar 12, 2020

    @CleanHit
    Author

    Thanks for pointing out the dynamic load of the schema URL. As it turns out I've made a little mistake.The bom.name doesn't do the trick if the file is not in the root folder of the project, so I had to use bom which is in my case the whole path with the file name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions