Validation errors are hard to present safely to the user (missing abstraction)
评估
调研方向
从 cyclonedx/validation/init.py 以及 issue 中引用的 validation/json.py 和 validation/xml.py 入口点开始,然后使用列出的 schemaTestData 文件复现示例。当 JSON 和 XML 验证通过一个通用抽象提供稳定的路径和安全的消息,同时在 data 中保留底层原始错误时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
https://cyclonedx-python-library.readthedocs.io/en/v10.2.0/autoapi/cyclonedx/validation/
We are using both JSON and XML inputs, and when something is wrong with the input, it is not easy to get the location of the problem or even what is wrong can be hidden in a multi-MB message.
One of the problem is, that the underlying libraries make it hard:
jsonschemaincludes all the input (instance) in the error message, which in the SBOM case can be quite big, producing the above mentioned multi-MB message (thisuniqueItemscheck can fail on e.g. thedependencies):yield ValidationError(f"{instance!r} has non-unique elements")- in the xml case, somehow the easiest solution was to get the error from the logs: https://github.com/CycloneDX/cyclonedx-python-lib/blob/1a932a2ab00efb029c7b685cba7d9e5af3b7ea19/cyclonedx/validation/xml.py#L71
The other problem is, that CycloneDX makes no attempt at transforming these different object types into something sensible and type-safe for users, the raw objects are simply leaked through the interface as is in https://github.com/CycloneDX/cyclonedx-python-lib/blob/1a932a2ab00efb029c7b685cba7d9e5af3b7ea19/cyclonedx/validation/__init__.py#L36
Code samples triggering long messages:
from cyclonedx.validation.json import JsonStrictValidator
from cyclonedx.schema import SchemaVersion
test_data_file = "tests/_data/schemaTestData/1.2/invalid-license-id-1.2.json"
schema_version = SchemaVersion.V1_2
validator = JsonStrictValidator(schema_version)
with open(test_data_file) as tdfh:
test_data = tdfh.read()
validation_error = validator.validate_str(test_data)
print(str(validation_error))
This message is 35508 characters long - 767 lines!
from cyclonedx.validation.xml import XmlValidator
from cyclonedx.schema import SchemaVersion
test_data_file = "tests/_data/schemaTestData/1.1/invalid-license-id-1.1.xml"
schema_version = SchemaVersion.V1_1
validator = XmlValidator(schema_version)
with open(test_data_file) as tdfh:
test_data = tdfh.read()
validation_error = validator.validate_str(test_data)
print(str(validation_error))
This message is 12423 characters long - 1 line.
I would expect the errors returned/raised by CycloneDX something like below:
class ValidationError:
# abstract class
data: Any
"raw problem, for debugging"
path: str
message: str
class XmlValidationError(ValidationError):
# this subclass knows what data is
@property
def path(self):
return self.data.path
@property
def message(self):
return self.data.message
class JsonValidationError(ValidationError):
# this subclass knows what data is
@property
def path(self):
return self.data.json_path
@property
def message(self):
# ensures the error is transformed to something sensible
# resolving a problem caused by using jsonscheme for CycloneDX users
instance = repr(self.data.instance)
return self.data.message.replace(instance, shortened(instance))
# where shortened(long_text) ~ 'first n ... last n', that is the middle of the string replaced
# this would still add some context, but it will be safe to display
These would provide a stable abstraction over generally useful validation error properties, and also hide implementation details from users, like third party objects lxml.etree._LogEntry and jsonschema.exceptions.ValidationError. The above proposal is also backward compatible, keeping data intact, if someone depends on it.
- 主要语言
- Python
- 星标
- 117
- 派生
- 67
- 平均合并
- 21 小时 9 分钟
- 30 天内合并 PR
- 3
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
CycloneDX/cyclonedx-python-lib 的其他 Issue
-
[PERF] Quadratic (O(N^2)) serialization time for large BOMs — `Bom.validate()` → `register_dependency()` linear scan可能已有人在做 @inspired-geek 于 110 天前认领。 未关闭performance
难度 3/5 1-2 天 新手友好度 36/100
CycloneDX/cyclonedx-python-lib#1006 · 2 条评论 ·
-
tests: test all model enums可能重新可做 @jkowalleck 于 126 天前认领,目前没有进行中的 PR。 未关闭QA
CycloneDX/cyclonedx-python-lib#991 · 已指派 1 人 ·
-
feat(deps)!: make all de/serialization libraries optional可能重新可做 @Simoh23999 于 73 天前认领,目前没有进行中的 PR。 未关闭breaking change dependencies
难度 5/5 一周以上 新手友好度 35/100
CycloneDX/cyclonedx-python-lib#979 · 2 条评论 ·
-
feat: Add support for Component signature可能已有人在做 @wiebe-vandendriessche 于 125 天前认领。 未关闭enhancement help wanted schema 1.4
难度 3/5 1-2 天 新手友好度 58/100
CycloneDX/cyclonedx-python-lib#978 · 4 条评论 ·
-
chore: have coverage uploaded consitently可能重新可做 @jkowalleck 于 172 天前认领,目前没有进行中的 PR。 未关闭chore
CycloneDX/cyclonedx-python-lib#966 · 已指派 1 人 ·
查看 CycloneDX/cyclonedx-python-lib 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 75/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 2 天内回复
-
Status: Triage Type: Bug
难度 2/5 1-3 小时 新手友好度 86/100
-
Objects.List treats versions=false (and similar flags) as true可能已有人在做 @andaaron 今天认领。 未关闭priority: p2 type: bug
难度 2/5 1-3 小时 新手友好度 78/100
googleapis/storage-testbench#829 ·
维护者通常 2 天内回复
-
[Bug]: memory provider treats 1:1 DMs as shared surfaces, so DM turns never receive reviewed records未关闭
难度 2/5 1-3 小时 新手友好度 79/100
rlaope/oh-my-hermes#2088 ·
维护者通常 1 天内回复