sanitizeElement sanitizes a single SVG element. It returns true if the element should be kept, false if it should be removed.
(el *xmlparser.Node)
| 90 | // sanitizeElement sanitizes a single SVG element. |
| 91 | // It returns true if the element should be kept, false if it should be removed. |
| 92 | func (p *Processor) sanitizeElement(el *xmlparser.Node) bool { |
| 93 | if el == nil { |
| 94 | return false |
| 95 | } |
| 96 | |
| 97 | tagName := el.Name.Local() |
| 98 | |
| 99 | // Strip <script>, <iframe>, and <form> tags |
| 100 | if tagName == "script" || tagName == "iframe" || tagName == "form" { |
| 101 | return false |
| 102 | } |
| 103 | |
| 104 | // Filter out unsafe attributes (such as on* events) |
| 105 | el.Attrs.Filter(func(attr *xmlparser.Attribute) bool { |
| 106 | _, unsafe := unsafeAttrs[attr.Name.Local()] |
| 107 | return !unsafe |
| 108 | }) |
| 109 | |
| 110 | // Special handling for <use> tags. |
| 111 | if tagName == "use" { |
| 112 | el.Attrs.Filter(func(attr *xmlparser.Attribute) bool { |
| 113 | // Keep non-href attributes |
| 114 | if attr.Name.Local() != "href" { |
| 115 | return true |
| 116 | } |
| 117 | // Strip hrefs that are not internal references |
| 118 | return len(attr.Value) == 0 || attr.Value[0] == '#' |
| 119 | }) |
| 120 | } |
| 121 | |
| 122 | // Recurse into children |
| 123 | el.FilterChildNodes(p.sanitizeElement) |
| 124 | |
| 125 | // Keep this element |
| 126 | return true |
| 127 | } |
nothing calls this directly
no test coverage detected