Skip to content

Commit

Permalink
Sanitize use tag in SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthSim committed Feb 25, 2023
1 parent 947d65c commit 62f8d08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@

### Change
- Make the `expires` processing option set `Expires` and `Cache-Control` headers.
- Sanitize `use` tags in SVGs.

## [3.13.2] - 2023-02-15
### Change
Expand Down
19 changes: 17 additions & 2 deletions svg/svg.go
Expand Up @@ -35,6 +35,8 @@ func Satitize(data *imagedata.ImageData) (*imagedata.ImageData, error) {

ignoreTag := 0

var curTagName string

for {
tt, tdata := l.Next()

Expand Down Expand Up @@ -67,15 +69,28 @@ func Satitize(data *imagedata.ImageData) (*imagedata.ImageData, error) {

return &newData, nil
case xml.StartTagToken:
if strings.ToLower(string(l.Text())) == "script" {
curTagName = strings.ToLower(string(l.Text()))

if curTagName == "script" {
ignoreTag++
continue
}

buf.Write(tdata)
case xml.AttributeToken:
if _, unsafe := unsafeAttrs[strings.ToLower(string(l.Text()))]; unsafe {
attrName := strings.ToLower(string(l.Text()))

if _, unsafe := unsafeAttrs[attrName]; unsafe {
continue
}

if curTagName == "use" && (attrName == "href" || attrName == "xlink:href") {
val := strings.TrimSpace(strings.Trim(string(l.AttrVal()), `"'`))
if len(val) > 0 && val[0] != '#' {
continue
}
}

buf.Write(tdata)
default:
buf.Write(tdata)
Expand Down

0 comments on commit 62f8d08

Please sign in to comment.