Spaces:
Running
Running
File size: 9,736 Bytes
15369ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
from dataclasses import dataclass, field
from defusedxml import ElementTree as etree
@dataclass(frozen=True)
class SVGConstraints:
"""Defines constraints for validating SVG documents.
Attributes
----------
max_svg_size : int, default=10000
Maximum allowed size of an SVG file in bytes.
allowed_elements : dict[str, set[str]]
Mapping of the allowed elements to the allowed attributes of each element.
"""
max_svg_size: int = 10000
allowed_elements: dict[str, set[str]] = field(
default_factory=lambda: {
'common': {
'id',
'clip-path',
'clip-rule',
'color',
'color-interpolation',
'color-interpolation-filters',
'color-rendering',
'display',
'fill',
'fill-opacity',
'fill-rule',
'filter',
'flood-color',
'flood-opacity',
'lighting-color',
'marker-end',
'marker-mid',
'marker-start',
'mask',
'opacity',
'paint-order',
'stop-color',
'stop-opacity',
'stroke',
'stroke-dasharray',
'stroke-dashoffset',
'stroke-linecap',
'stroke-linejoin',
'stroke-miterlimit',
'stroke-opacity',
'stroke-width',
'transform',
},
'svg': {
'width',
'height',
'viewBox',
'preserveAspectRatio',
},
'g': {'viewBox'},
'defs': set(),
'symbol': {'viewBox', 'x', 'y', 'width', 'height'},
'use': {'x', 'y', 'width', 'height', 'href'},
'marker': {
'viewBox',
'preserveAspectRatio',
'refX',
'refY',
'markerUnits',
'markerWidth',
'markerHeight',
'orient',
},
'pattern': {
'viewBox',
'preserveAspectRatio',
'x',
'y',
'width',
'height',
'patternUnits',
'patternContentUnits',
'patternTransform',
'href',
},
'linearGradient': {
'x1',
'x2',
'y1',
'y2',
'gradientUnits',
'gradientTransform',
'spreadMethod',
'href',
},
'radialGradient': {
'cx',
'cy',
'r',
'fx',
'fy',
'fr',
'gradientUnits',
'gradientTransform',
'spreadMethod',
'href',
},
'stop': {'offset'},
'filter': {
'x',
'y',
'width',
'height',
'filterUnits',
'primitiveUnits',
},
'feBlend': {'result', 'in', 'in2', 'mode'},
'feColorMatrix': {'result', 'in', 'type', 'values'},
'feComposite': {
'result',
'style',
'in',
'in2',
'operator',
'k1',
'k2',
'k3',
'k4',
},
'feFlood': {'result'},
'feGaussianBlur': {
'result',
'in',
'stdDeviation',
'edgeMode',
},
'feMerge': {
'result',
'x',
'y',
'width',
'height',
'result',
},
'feMergeNode': {'result', 'in'},
'feOffset': {'result', 'in', 'dx', 'dy'},
'feTurbulence': {
'result',
'baseFrequency',
'numOctaves',
'seed',
'stitchTiles',
'type',
},
'path': {'d'},
'rect': {'x', 'y', 'width', 'height', 'rx', 'ry'},
'circle': {'cx', 'cy', 'r'},
'ellipse': {'cx', 'cy', 'rx', 'ry'},
'line': {'x1', 'y1', 'x2', 'y2'},
'polyline': {'points'},
'polygon': {'points'},
}
)
def validate_svg(self, svg_code: str) -> None:
"""Validates an SVG string against a set of predefined constraints.
Parameters
----------
svg_code : str
The SVG string to validate.
Raises
------
ValueError
If the SVG violates any of the defined constraints.
"""
# Check file size
if len(svg_code.encode('utf-8')) > self.max_svg_size:
raise ValueError('SVG exceeds allowed size')
# Parse XML
tree = etree.fromstring(
svg_code.encode('utf-8'),
forbid_dtd=True,
forbid_entities=True,
forbid_external=True,
)
elements = set(self.allowed_elements.keys())
# Check elements and attributes
for element in tree.iter():
# Check for disallowed elements
tag_name = element.tag.split('}')[-1]
if tag_name not in elements:
raise ValueError(f'Disallowed element: {tag_name}')
# Check attributes
for attr, attr_value in element.attrib.items():
# Check for disallowed attributes
attr_name = attr.split('}')[-1]
if (
attr_name not in self.allowed_elements[tag_name]
and attr_name not in self.allowed_elements['common']
):
raise ValueError(f'Disallowed attribute: {attr_name}')
# Check for embedded data
if 'data:' in attr_value.lower():
raise ValueError('Embedded data not allowed')
if ';base64' in attr_value:
raise ValueError('Base64 encoded content not allowed')
# Check that href attributes are internal references
if attr_name == 'href':
if not attr_value.startswith('#'):
raise ValueError(
f'Invalid href attribute in <{tag_name}>. Only internal references (starting with "#") are allowed. Found: "{attr_value}"'
)
if __name__ == '__main__':
svg_validator = SVGConstraints()
valid_svg = """
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
"""
invalid_size_svg = '<svg>' + ' ' * 6000 + '</svg>' # Exceeds default 5000 bytes
invalid_element_svg = """
<svg>
<script>alert('bad');</script>
</svg>
"""
invalid_attribute_svg = """
<svg>
<rect width="100" height="100" onclick="alert('bad')"/>
</svg>
"""
invalid_href_svg = """
<svg>
<use href="http://example.com/image.svg" />
</svg>
"""
invalid_embedded_image_element_svg = """
<svg width="100" height="100">
<image href="data:image/png;base64,iVBOAAAANSUhEUgAAAAUAAAAFCAYAAACN==" width="50" height="50"/>
</svg>
"""
invalid_data_uri_attribute_svg = """
<svg width="100" height="100">
<rect width="50" height="50" fill="url(data:image/png;base64,iVBOAAAANSUhEUgAAAAUAAAAFCAYAAACN==)" />
</svg>
"""
print('Running SVG validation examples:')
print('\nValid SVG example:')
svg_validator.validate_svg(valid_svg)
print(' Validation successful!')
print('\nSVG exceeding size limit:')
try:
svg_validator.validate_svg(invalid_size_svg)
print(' Validation successful! (This should not happen)')
except ValueError as e:
print(f' Validation failed as expected with error: {e}')
print('\nSVG with disallowed element:')
try:
svg_validator.validate_svg(invalid_element_svg)
print(' Validation successful! (This should not happen)')
except ValueError as e:
print(f' Validation failed as expected with error: {e}')
print('\nSVG with disallowed attribute:')
try:
svg_validator.validate_svg(invalid_attribute_svg)
print(' Validation successful! (This should not happen)')
except ValueError as e:
print(f' Validation failed as expected with error: {e}')
print('\nSVG with invalid external href:')
try:
svg_validator.validate_svg(invalid_href_svg)
print(' Validation successful! (This should not happen)')
except ValueError as e:
print(f' Validation failed as expected with error: {e}')
print('\nSVG with disallowed <image> element:')
try:
svg_validator.validate_svg(invalid_embedded_image_element_svg)
print(' Validation successful! (This should not happen)')
except ValueError as e:
print(f' Validation failed as expected with error: {e}')
print('\nSVG with invalid data URI in attribute (fill):')
try:
svg_validator.validate_svg(invalid_data_uri_attribute_svg)
print(' Validation successful! (This should not happen)')
except ValueError as e:
print(f' Validation failed as expected with error: {e}')
|