|
| 1 | +from django.core.files.base import File |
| 2 | + |
| 3 | +import bleach |
| 4 | + |
| 5 | +ALLOWED_SVG_TAGS = ( |
| 6 | + "circle", |
| 7 | + "clipPath", |
| 8 | + "defs", |
| 9 | + "desc", |
| 10 | + "ellipse", |
| 11 | + "feBlend", |
| 12 | + "feColorMatrix", |
| 13 | + "feComponentTransfer", |
| 14 | + "feComposite", |
| 15 | + "feConvolveMatrix", |
| 16 | + "feDiffuseLighting", |
| 17 | + "feDisplacementMap", |
| 18 | + "feDistantLight", |
| 19 | + "feDropShadow", |
| 20 | + "feFlood", |
| 21 | + "feFuncA", |
| 22 | + "feFuncB", |
| 23 | + "feFuncG", |
| 24 | + "feFuncR", |
| 25 | + "feGaussianBlur", |
| 26 | + "feImage", |
| 27 | + "feMerge", |
| 28 | + "feMergeNode", |
| 29 | + "feMorphology", |
| 30 | + "feOffset", |
| 31 | + "fePointLight", |
| 32 | + "feSpecularLighting", |
| 33 | + "feSpotLight", |
| 34 | + "feTile", |
| 35 | + "feTurbulence", |
| 36 | + "filter", |
| 37 | + "foreignObject", |
| 38 | + "g", |
| 39 | + "image", |
| 40 | + "line", |
| 41 | + "linearGradient", |
| 42 | + "marker", |
| 43 | + "mask", |
| 44 | + "metadata", |
| 45 | + "mpath", |
| 46 | + "path", |
| 47 | + "pattern", |
| 48 | + "polygon", |
| 49 | + "polyline", |
| 50 | + "radialGradient", |
| 51 | + "rect", |
| 52 | + "set", |
| 53 | + "stop", |
| 54 | + "style", |
| 55 | + "svg", |
| 56 | + "symbol", |
| 57 | + "text", |
| 58 | + "textPath", |
| 59 | + "title", |
| 60 | + "tspan", |
| 61 | + "use", |
| 62 | + "view", |
| 63 | + # --- Not allowing 'a', 'animate*' and 'script' tags |
| 64 | +) |
| 65 | + |
| 66 | +ALLOWED_SVG_ATTRIBUTES = { |
| 67 | + "*": [ |
| 68 | + # --- Basic presentation attributes |
| 69 | + "alignment-baseline", |
| 70 | + "baseline-shift", |
| 71 | + "clip", |
| 72 | + "clip-path", |
| 73 | + "clip-rule", |
| 74 | + "color", |
| 75 | + "color-interpolation", |
| 76 | + "color-interpolation-filters", |
| 77 | + "cursor", |
| 78 | + "cx", |
| 79 | + "cy", |
| 80 | + "d", |
| 81 | + "direction", |
| 82 | + "display", |
| 83 | + "dominant-baseline", |
| 84 | + "fill", |
| 85 | + "fill-opacity", |
| 86 | + "fill-rule", |
| 87 | + "filter", |
| 88 | + "flood-color", |
| 89 | + "flood-opacity", |
| 90 | + "font-family", |
| 91 | + "font-size", |
| 92 | + "font-size-adjust", |
| 93 | + "font-stretch", |
| 94 | + "font-style", |
| 95 | + "font-variant", |
| 96 | + "font-weight", |
| 97 | + "glyph-orientation-horizontal", |
| 98 | + "glyph-orientation-vertical", |
| 99 | + "height", |
| 100 | + "image-rendering", |
| 101 | + "letter-spacing", |
| 102 | + "lighting-color", |
| 103 | + "marker-end", |
| 104 | + "marker-mid", |
| 105 | + "marker-start", |
| 106 | + "mask", |
| 107 | + "mask-type", |
| 108 | + "opacity", |
| 109 | + "overflow", |
| 110 | + "pointer-events", |
| 111 | + "r", |
| 112 | + "rx", |
| 113 | + "ry", |
| 114 | + "shape-rendering", |
| 115 | + "stop-color", |
| 116 | + "stop-opacity", |
| 117 | + "stroke", |
| 118 | + "stroke-dasharray", |
| 119 | + "stroke-dashoffset", |
| 120 | + "stroke-linecap", |
| 121 | + "stroke-linejoin", |
| 122 | + "stroke-miterlimit", |
| 123 | + "stroke-opacity", |
| 124 | + "stroke-width", |
| 125 | + "text-anchor", |
| 126 | + "text-decoration", |
| 127 | + "text-overflow", |
| 128 | + "text-rendering", |
| 129 | + "transform", |
| 130 | + "transform-origin", |
| 131 | + "unicode-bidi", |
| 132 | + "vector-effect", |
| 133 | + "visibility", |
| 134 | + "white-space", |
| 135 | + "width", |
| 136 | + "word-spacing", |
| 137 | + "writing-mode", |
| 138 | + "x", |
| 139 | + "y", |
| 140 | + # --- Filter attributes |
| 141 | + "amplitude", |
| 142 | + "exponent", |
| 143 | + "intercept", |
| 144 | + "offset", |
| 145 | + "slope", |
| 146 | + "tableValues", |
| 147 | + "type", |
| 148 | + # --- Not allowing 'href', 'data-*', Animation and some other attributes |
| 149 | + ], |
| 150 | + "svg": ["xmlns", "viewBox"], |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +def sanitize_svg_file(data: File) -> File: |
| 155 | + """ |
| 156 | + Function for sanitizing SVG file content. |
| 157 | + The entire file content will be replaced with a sanitized version. All tags and |
| 158 | + attributes that aren't explicitly allowed, are removed from the SVG content. |
| 159 | + """ |
| 160 | + # Making sure that the file is reset properly |
| 161 | + data.seek(0) |
| 162 | + |
| 163 | + file_content = data.read().decode("utf-8") |
| 164 | + sanitized_content = sanitize_svg_content(file_content) |
| 165 | + |
| 166 | + # Replace svg file content with the bleached variant. |
| 167 | + # `truncate(0)` doesn't reset the point, so start with a seek(0) to make sure the |
| 168 | + # content is as expected. |
| 169 | + data.seek(0) |
| 170 | + data.truncate(0) |
| 171 | + data.write(sanitized_content.encode("utf-8")) |
| 172 | + |
| 173 | + # Reset pointer |
| 174 | + data.seek(0) |
| 175 | + return data |
| 176 | + |
| 177 | + |
| 178 | +def sanitize_svg_content(svg_content: str) -> str: |
| 179 | + """ |
| 180 | + Function for sanitizing SVG content. |
| 181 | + All tags and attributes that aren't explicitly allowed, are removed from the SVG |
| 182 | + content. |
| 183 | + """ |
| 184 | + |
| 185 | + return bleach.clean( |
| 186 | + svg_content, |
| 187 | + tags=ALLOWED_SVG_TAGS, |
| 188 | + attributes=ALLOWED_SVG_ATTRIBUTES, |
| 189 | + strip=True, |
| 190 | + ) |
0 commit comments