From 538b7fa167549dbe8ea6a50e585b92abe502a9ff Mon Sep 17 00:00:00 2001 From: Chris Lenk Date: Wed, 3 Apr 2024 13:26:34 -0400 Subject: [PATCH] Fix unicode errors when validating files Previously undetected because testing performed on systems where utf-8 is default. --- stix2validator/test/v21/misc_tests.py | 7 +++++++ .../test/v21/test_examples/identity_unicode.json | 9 +++++++++ stix2validator/validator.py | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 stix2validator/test/v21/test_examples/identity_unicode.json diff --git a/stix2validator/test/v21/misc_tests.py b/stix2validator/test/v21/misc_tests.py index 5451675c..33cc5d14 100644 --- a/stix2validator/test/v21/misc_tests.py +++ b/stix2validator/test/v21/misc_tests.py @@ -20,6 +20,8 @@ 'test_examples', 'identity.json') IDENTITY_CUSTOM = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_examples', 'identity_custom.json') +IDENTITY_UNICODE = os.path.join(os.path.dirname(os.path.realpath(__file__)), + 'test_examples', 'identity_unicode.json') INVALID_BRACES = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_examples', 'invalid_braces.json') INVALID_COMMA = os.path.join(os.path.dirname(os.path.realpath(__file__)), @@ -71,6 +73,11 @@ def test_validate_file_warning(caplog): assert re.search("Custom property .+ should ", caplog.text) +def test_validate_file_unicode(caplog): + results = validate_file(IDENTITY_UNICODE) + assert results.is_valid + + def test_validate_file_invalid_brace(caplog): results = validate_file(INVALID_BRACES) assert not results.is_valid diff --git a/stix2validator/test/v21/test_examples/identity_unicode.json b/stix2validator/test/v21/test_examples/identity_unicode.json new file mode 100644 index 00000000..ae3c9a29 --- /dev/null +++ b/stix2validator/test/v21/test_examples/identity_unicode.json @@ -0,0 +1,9 @@ +{ + "type": "identity", + "spec_version": "2.1", + "id": "identity--8c6af861-7b20-41ef-9b59-6344fd872a8f", + "created": "2016-08-08T15:50:10.983Z", + "modified": "2016-08-08T15:50:10.983Z", + "name": "Heizölrückstoßabdämpfung", + "identity_class": "organization" +} diff --git a/stix2validator/validator.py b/stix2validator/validator.py index f4983087..982f3562 100644 --- a/stix2validator/validator.py +++ b/stix2validator/validator.py @@ -453,7 +453,7 @@ def validate_file(fn, options=None): options = ValidationOptions(files=fn) try: - with open(fn) as instance_file: + with open(fn, encoding="utf-8") as instance_file: file_results.object_results = validate(instance_file, options) except Exception as ex: