doclang 0.7.2__tar.gz → 0.7.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {doclang-0.7.2 → doclang-0.7.3}/CHANGELOG.md +5 -0
- {doclang-0.7.2/doclang.egg-info → doclang-0.7.3}/PKG-INFO +1 -1
- {doclang-0.7.2 → doclang-0.7.3}/doclang/_packaging.py +22 -18
- {doclang-0.7.2 → doclang-0.7.3}/doclang/backends/saxonche.py +4 -3
- {doclang-0.7.2 → doclang-0.7.3}/doclang/doclang.xsd +1 -1
- {doclang-0.7.2 → doclang-0.7.3}/doclang/packaging.py +4 -1
- doclang-0.7.3/doclang/utils.py +127 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang/xsd_validation.py +4 -3
- {doclang-0.7.2 → doclang-0.7.3/doclang.egg-info}/PKG-INFO +1 -1
- {doclang-0.7.2 → doclang-0.7.3}/pyproject.toml +1 -1
- doclang-0.7.2/doclang/utils.py +0 -66
- {doclang-0.7.2 → doclang-0.7.3}/LICENSE +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/MANIFEST.in +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/README.md +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang/README.md +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang/__init__.py +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang/_schemas.py +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang/backends/__init__.py +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang/backends/_svrl.py +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang/cli.py +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang/doclang.sch +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang/schematron.py +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang/validation.py +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang/version.py +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang.egg-info/SOURCES.txt +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang.egg-info/dependency_links.txt +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang.egg-info/entry_points.txt +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang.egg-info/requires.txt +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/doclang.egg-info/top_level.txt +0 -0
- {doclang-0.7.2 → doclang-0.7.3}/setup.cfg +0 -0
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
## [v0.7.3](https://github.com/doclang-project/doclang/releases/tag/v0.7.3)
|
|
2
|
+
|
|
3
|
+
* refuse symbolic links when packing pages and assets (#171)
|
|
4
|
+
* reject DTD and entity references in DocLang validation (#170)
|
|
5
|
+
|
|
1
6
|
## [v0.7.2](https://github.com/doclang-project/doclang/releases/tag/v0.7.2)
|
|
2
7
|
|
|
3
8
|
* expand lxml version support (#158)
|
|
@@ -58,6 +58,11 @@ def _require_directory(path: Path, *, label: str) -> None:
|
|
|
58
58
|
raise PackagingError(f"{label} not found or not a directory: {path}")
|
|
59
59
|
|
|
60
60
|
|
|
61
|
+
def _reject_symlink(path: Path, *, label: str) -> None:
|
|
62
|
+
if path.is_symlink():
|
|
63
|
+
raise PackagingError(f"{label} must not be a symbolic link: {path}")
|
|
64
|
+
|
|
65
|
+
|
|
61
66
|
def _validate_archive_relative_path(path: str, *, label: str) -> None:
|
|
62
67
|
if not path or path.startswith("/") or "\\" in path:
|
|
63
68
|
raise PackagingError(f"Invalid {label} path: {path!r}")
|
|
@@ -66,12 +71,22 @@ def _validate_archive_relative_path(path: str, *, label: str) -> None:
|
|
|
66
71
|
raise PackagingError(f"Invalid {label} path: {path!r}")
|
|
67
72
|
|
|
68
73
|
|
|
69
|
-
def
|
|
74
|
+
def _copy_file(source: Path, destination: Path, *, label: str) -> None:
|
|
75
|
+
_reject_symlink(source, label=label)
|
|
76
|
+
_require_file(source, label=label)
|
|
77
|
+
destination.parent.mkdir(parents=True, exist_ok=True)
|
|
78
|
+
shutil.copy2(source, destination)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _copy_tree_into(source: Path, destination: Path, *, label: str) -> None:
|
|
82
|
+
_reject_symlink(source, label=label)
|
|
83
|
+
_require_directory(source, label=label)
|
|
70
84
|
destination.mkdir(parents=True, exist_ok=True)
|
|
71
85
|
for item in source.iterdir():
|
|
72
86
|
target = destination / item.name
|
|
87
|
+
_reject_symlink(item, label=label)
|
|
73
88
|
if item.is_dir():
|
|
74
|
-
|
|
89
|
+
_copy_tree_into(item, target, label=label)
|
|
75
90
|
else:
|
|
76
91
|
shutil.copy2(item, target)
|
|
77
92
|
|
|
@@ -88,24 +103,18 @@ def _place_pages(stage: Path, pages: PagesInput) -> None:
|
|
|
88
103
|
if not isinstance(page_number, int) or page_number < 1:
|
|
89
104
|
raise PackagingError(f"Page numbers must be positive integers, got {page_number!r}")
|
|
90
105
|
source_path = Path(source)
|
|
91
|
-
_require_file(source_path, label="Page file")
|
|
92
106
|
destination = pages_dir / f"{page_number}{source_path.suffix}"
|
|
93
|
-
|
|
94
|
-
shutil.copy2(source_path, destination)
|
|
107
|
+
_copy_file(source_path, destination, label="Page file")
|
|
95
108
|
return
|
|
96
109
|
|
|
97
110
|
if isinstance(pages, str | Path):
|
|
98
|
-
|
|
99
|
-
_require_directory(source_dir, label="Pages directory")
|
|
100
|
-
_copy_tree_into(source_dir, pages_dir)
|
|
111
|
+
_copy_tree_into(Path(pages), pages_dir, label="Pages directory")
|
|
101
112
|
return
|
|
102
113
|
|
|
103
114
|
for index, source in enumerate(pages, start=1):
|
|
104
115
|
source_path = Path(source)
|
|
105
|
-
_require_file(source_path, label="Page file")
|
|
106
116
|
destination = pages_dir / f"{index}{source_path.suffix}"
|
|
107
|
-
|
|
108
|
-
shutil.copy2(source_path, destination)
|
|
117
|
+
_copy_file(source_path, destination, label="Page file")
|
|
109
118
|
|
|
110
119
|
|
|
111
120
|
def _place_assets(stage: Path, assets: AssetsInput) -> None:
|
|
@@ -113,16 +122,11 @@ def _place_assets(stage: Path, assets: AssetsInput) -> None:
|
|
|
113
122
|
if isinstance(assets, Mapping):
|
|
114
123
|
for archive_path, source in assets.items():
|
|
115
124
|
_validate_archive_relative_path(archive_path, label="asset")
|
|
116
|
-
source_path = Path(source)
|
|
117
|
-
_require_file(source_path, label="Asset file")
|
|
118
125
|
destination = assets_dir / archive_path
|
|
119
|
-
|
|
120
|
-
shutil.copy2(source_path, destination)
|
|
126
|
+
_copy_file(Path(source), destination, label="Asset file")
|
|
121
127
|
return
|
|
122
128
|
|
|
123
|
-
|
|
124
|
-
_require_directory(source_dir, label="Assets directory")
|
|
125
|
-
_copy_tree_into(source_dir, assets_dir)
|
|
129
|
+
_copy_tree_into(Path(assets), assets_dir, label="Assets directory")
|
|
126
130
|
|
|
127
131
|
|
|
128
132
|
def _write_opc_metadata(stage: Path) -> None:
|
|
@@ -13,7 +13,7 @@ from lxml import etree
|
|
|
13
13
|
|
|
14
14
|
from doclang.backends._svrl import _svrl_failed_asserts_to_violations
|
|
15
15
|
from doclang.schematron import SchematronViolation, _require_saxonche_backend
|
|
16
|
-
from doclang.utils import _ensure_namespace
|
|
16
|
+
from doclang.utils import _ensure_namespace, _parse_doclang_document, _write_xml_without_dtd
|
|
17
17
|
|
|
18
18
|
# ISO Schematron transpiler - converts .sch to XSLT 3.0
|
|
19
19
|
_ISO_SCHEMATRON_TRANSPILER = """<?xml version="1.0" encoding="UTF-8"?>
|
|
@@ -139,13 +139,14 @@ class SaxoncheValidator:
|
|
|
139
139
|
print(f"Using Schematron file: {schema_path}")
|
|
140
140
|
|
|
141
141
|
with open(xml_path, "rb") as f:
|
|
142
|
-
xml_doc =
|
|
142
|
+
xml_doc = _parse_doclang_document(f)
|
|
143
143
|
|
|
144
144
|
if allow_empty_namespace:
|
|
145
145
|
xml_doc = _ensure_namespace(xml_doc)
|
|
146
146
|
|
|
147
147
|
with tempfile.NamedTemporaryFile(mode="wb", suffix=".xml", delete=True) as tmp:
|
|
148
|
-
|
|
148
|
+
# Never re-emit a DOCTYPE: Saxon would otherwise expand entities.
|
|
149
|
+
_write_xml_without_dtd(xml_doc, tmp)
|
|
149
150
|
tmp.flush()
|
|
150
151
|
tmp_xml_path = tmp.name
|
|
151
152
|
|
|
@@ -34,9 +34,12 @@ def pack(
|
|
|
34
34
|
``assets`` may be a directory (copied into ``assets/``) or a mapping of
|
|
35
35
|
archive-relative asset path to source file.
|
|
36
36
|
|
|
37
|
+
Symbolic links are refused for ``pages`` and ``assets`` (including entries
|
|
38
|
+
inside directories) so pack does not follow link targets into the archive.
|
|
39
|
+
|
|
37
40
|
Returns the resolved path to the created archive.
|
|
38
41
|
|
|
39
|
-
Raises :class:`PackagingError` on packaging failure.
|
|
42
|
+
Raises :class:`PackagingError` on packaging failure (including symbolic links).
|
|
40
43
|
Raises :class:`~doclang.ValidationError` when ``validate=True`` and the
|
|
41
44
|
document fails validation.
|
|
42
45
|
"""
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Shared utility functions for DocLang validation.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import BinaryIO, Protocol, Union
|
|
7
|
+
|
|
8
|
+
from lxml import etree
|
|
9
|
+
|
|
10
|
+
from doclang.version import _resolve_version
|
|
11
|
+
|
|
12
|
+
_DOCLANG_NAMESPACE = "https://www.doclang.ai/ns/v0"
|
|
13
|
+
_VERSION = _resolve_version()
|
|
14
|
+
|
|
15
|
+
_DTD_REJECTED_MESSAGE = "DTD declarations and entity references are not allowed in DocLang documents"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class _BinaryWriter(Protocol):
|
|
19
|
+
def write(self, data: bytes, /) -> int: ...
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _safe_xml_parser() -> etree.XMLParser:
|
|
23
|
+
"""Return an lxml parser that does not expand entities or load external DTDs."""
|
|
24
|
+
return etree.XMLParser(
|
|
25
|
+
resolve_entities=False,
|
|
26
|
+
no_network=True,
|
|
27
|
+
load_dtd=False,
|
|
28
|
+
dtd_validation=False,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _contains_entity_nodes(node: etree._Element) -> bool:
|
|
33
|
+
for child in node:
|
|
34
|
+
if not isinstance(child.tag, str):
|
|
35
|
+
# Comments/PIs are skipped elsewhere; treat non-element nodes as unsafe
|
|
36
|
+
# when they are entity references (lxml exposes them as _Entity).
|
|
37
|
+
if type(child).__name__ == "_Entity":
|
|
38
|
+
return True
|
|
39
|
+
continue
|
|
40
|
+
if _contains_entity_nodes(child):
|
|
41
|
+
return True
|
|
42
|
+
return False
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _reject_dtd_or_entities(xml_doc: etree._ElementTree) -> None:
|
|
46
|
+
"""Fail closed if the document declares a DTD or contains entity nodes."""
|
|
47
|
+
if (xml_doc.docinfo.doctype or "").strip() or _contains_entity_nodes(xml_doc.getroot()):
|
|
48
|
+
raise ValueError(_DTD_REJECTED_MESSAGE)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _parse_xml(source: Union[str, Path, BinaryIO]) -> etree._ElementTree:
|
|
52
|
+
"""Parse XML with :func:`_safe_xml_parser` (no DTD/entity policy checks)."""
|
|
53
|
+
return etree.parse(source, parser=_safe_xml_parser())
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _parse_doclang_document(source: Union[str, Path, BinaryIO]) -> etree._ElementTree:
|
|
57
|
+
"""Parse a DocLang document safely and reject DTD / entity payloads."""
|
|
58
|
+
xml_doc = _parse_xml(source)
|
|
59
|
+
_reject_dtd_or_entities(xml_doc)
|
|
60
|
+
return xml_doc
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _write_xml_without_dtd(xml_doc: etree._ElementTree, out: _BinaryWriter) -> None:
|
|
64
|
+
"""Serialize ``xml_doc`` without preserving any DOCTYPE / internal subset."""
|
|
65
|
+
out.write(
|
|
66
|
+
etree.tostring(
|
|
67
|
+
xml_doc.getroot(),
|
|
68
|
+
encoding="utf-8",
|
|
69
|
+
xml_declaration=True,
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _ensure_namespace(xml_doc: etree._ElementTree) -> etree._ElementTree:
|
|
75
|
+
"""
|
|
76
|
+
Ensure the document has the DocLang namespace.
|
|
77
|
+
If the root element has no namespace, add the default DocLang namespace.
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
xml_doc: The XML document tree
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
The XML document tree with namespace added if it was missing
|
|
84
|
+
"""
|
|
85
|
+
root = xml_doc.getroot()
|
|
86
|
+
|
|
87
|
+
# Check if root element has a namespace
|
|
88
|
+
root_tag = str(root.tag)
|
|
89
|
+
if root_tag.startswith("{"):
|
|
90
|
+
# Already has a namespace
|
|
91
|
+
return xml_doc
|
|
92
|
+
|
|
93
|
+
# No namespace - add DocLang namespace
|
|
94
|
+
# Create a new root with namespace
|
|
95
|
+
new_root = etree.Element(f"{{{_DOCLANG_NAMESPACE}}}{root.tag}", nsmap={None: _DOCLANG_NAMESPACE})
|
|
96
|
+
|
|
97
|
+
# Copy attributes
|
|
98
|
+
for key, value in root.attrib.items():
|
|
99
|
+
new_root.set(key, value)
|
|
100
|
+
|
|
101
|
+
# Copy children recursively
|
|
102
|
+
def copy_element(source, target):
|
|
103
|
+
target.text = source.text
|
|
104
|
+
target.tail = source.tail
|
|
105
|
+
for child in source:
|
|
106
|
+
# Skip comments and processing instructions
|
|
107
|
+
if not isinstance(child.tag, str):
|
|
108
|
+
continue
|
|
109
|
+
|
|
110
|
+
child_tag = str(child.tag)
|
|
111
|
+
if child_tag.startswith("{"):
|
|
112
|
+
# Child already has namespace
|
|
113
|
+
new_child = etree.SubElement(target, child_tag)
|
|
114
|
+
else:
|
|
115
|
+
# Add namespace to child
|
|
116
|
+
new_child = etree.SubElement(target, f"{{{_DOCLANG_NAMESPACE}}}{child_tag}")
|
|
117
|
+
|
|
118
|
+
for key, value in child.attrib.items():
|
|
119
|
+
new_child.set(key, value)
|
|
120
|
+
|
|
121
|
+
copy_element(child, new_child)
|
|
122
|
+
|
|
123
|
+
copy_element(root, new_root)
|
|
124
|
+
|
|
125
|
+
# Create new document with namespaced root
|
|
126
|
+
new_doc = etree.ElementTree(new_root)
|
|
127
|
+
return new_doc
|
|
@@ -10,7 +10,7 @@ from typing import Any, Union
|
|
|
10
10
|
from lxml import etree
|
|
11
11
|
|
|
12
12
|
from doclang._schemas import _bundled_xsd_path
|
|
13
|
-
from doclang.utils import _ensure_namespace
|
|
13
|
+
from doclang.utils import _ensure_namespace, _parse_doclang_document, _parse_xml
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
def _validate_xsd_at(
|
|
@@ -29,11 +29,12 @@ def _validate_xsd_at(
|
|
|
29
29
|
"""
|
|
30
30
|
try:
|
|
31
31
|
with open(xsd_file, "rb") as f:
|
|
32
|
-
|
|
32
|
+
# Bundled/trusted schema: hardened parse only (no DocLang DTD policy).
|
|
33
|
+
schema_doc = _parse_xml(f)
|
|
33
34
|
schema = etree.XMLSchema(schema_doc)
|
|
34
35
|
|
|
35
36
|
with open(xml_file, "rb") as f:
|
|
36
|
-
xml_doc =
|
|
37
|
+
xml_doc = _parse_doclang_document(f)
|
|
37
38
|
|
|
38
39
|
if allow_empty_namespace:
|
|
39
40
|
xml_doc = _ensure_namespace(xml_doc)
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "doclang"
|
|
7
|
-
version = "0.7.
|
|
7
|
+
version = "0.7.3" # DO NOT EDIT MANUALLY, updated automatically
|
|
8
8
|
description = "DocLang reference toolkit"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
doclang-0.7.2/doclang/utils.py
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Shared utility functions for DocLang validation.
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
from lxml import etree
|
|
6
|
-
|
|
7
|
-
from doclang.version import _resolve_version
|
|
8
|
-
|
|
9
|
-
_DOCLANG_NAMESPACE = "https://www.doclang.ai/ns/v0"
|
|
10
|
-
_VERSION = _resolve_version()
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def _ensure_namespace(xml_doc: etree._ElementTree) -> etree._ElementTree:
|
|
14
|
-
"""
|
|
15
|
-
Ensure the document has the DocLang namespace.
|
|
16
|
-
If the root element has no namespace, add the default DocLang namespace.
|
|
17
|
-
|
|
18
|
-
Args:
|
|
19
|
-
xml_doc: The XML document tree
|
|
20
|
-
|
|
21
|
-
Returns:
|
|
22
|
-
The XML document tree with namespace added if it was missing
|
|
23
|
-
"""
|
|
24
|
-
root = xml_doc.getroot()
|
|
25
|
-
|
|
26
|
-
# Check if root element has a namespace
|
|
27
|
-
root_tag = str(root.tag)
|
|
28
|
-
if root_tag.startswith("{"):
|
|
29
|
-
# Already has a namespace
|
|
30
|
-
return xml_doc
|
|
31
|
-
|
|
32
|
-
# No namespace - add DocLang namespace
|
|
33
|
-
# Create a new root with namespace
|
|
34
|
-
new_root = etree.Element(f"{{{_DOCLANG_NAMESPACE}}}{root.tag}", nsmap={None: _DOCLANG_NAMESPACE})
|
|
35
|
-
|
|
36
|
-
# Copy attributes
|
|
37
|
-
for key, value in root.attrib.items():
|
|
38
|
-
new_root.set(key, value)
|
|
39
|
-
|
|
40
|
-
# Copy children recursively
|
|
41
|
-
def copy_element(source, target):
|
|
42
|
-
target.text = source.text
|
|
43
|
-
target.tail = source.tail
|
|
44
|
-
for child in source:
|
|
45
|
-
# Skip comments and processing instructions
|
|
46
|
-
if not isinstance(child.tag, str):
|
|
47
|
-
continue
|
|
48
|
-
|
|
49
|
-
child_tag = str(child.tag)
|
|
50
|
-
if child_tag.startswith("{"):
|
|
51
|
-
# Child already has namespace
|
|
52
|
-
new_child = etree.SubElement(target, child_tag)
|
|
53
|
-
else:
|
|
54
|
-
# Add namespace to child
|
|
55
|
-
new_child = etree.SubElement(target, f"{{{_DOCLANG_NAMESPACE}}}{child_tag}")
|
|
56
|
-
|
|
57
|
-
for key, value in child.attrib.items():
|
|
58
|
-
new_child.set(key, value)
|
|
59
|
-
|
|
60
|
-
copy_element(child, new_child)
|
|
61
|
-
|
|
62
|
-
copy_element(root, new_root)
|
|
63
|
-
|
|
64
|
-
# Create new document with namespaced root
|
|
65
|
-
new_doc = etree.ElementTree(new_root)
|
|
66
|
-
return new_doc
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|