Skip to content

Commit

Permalink
sanitize mime types
Browse files Browse the repository at this point in the history
  • Loading branch information
i5heu committed Nov 21, 2022
1 parent 58bddfd commit 0a05234
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions cmd/simple-S3-cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/i5heu/simple-S3-cache/internal/config"
"github.com/i5heu/simple-S3-cache/internal/helper"
"github.com/i5heu/simple-S3-cache/internal/log"
"github.com/i5heu/simple-S3-cache/internal/ramCache"
"github.com/i5heu/simple-S3-cache/internal/storageCache"
Expand Down Expand Up @@ -94,8 +95,9 @@ func (h *Handler) handler(ctx *fasthttp.RequestCtx) {
}

size = uint(len(bytes))
h.dataStoreRAM.CacheData(url, bytes, res.Header.Get("Content-Type"))
h.dataStoreStorage.CacheData(url, bytes, res.Header.Get("Content-Type"))
ctx.Response.Header.Set("Content-Type", res.Header.Get("Content-Type"))
sanitizedMime := helper.SanitizeMimeType(res.Header.Get("Content-Type"))
h.dataStoreRAM.CacheData(url, bytes, sanitizedMime)
h.dataStoreStorage.CacheData(url, bytes, sanitizedMime)
ctx.Response.Header.Set("Content-Type", sanitizedMime)
ctx.Response.SetBody(bytes)
}
21 changes: 21 additions & 0 deletions internal/helper/sanitize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package helper

import "unicode"

func SanitizeMimeType(mime string) string {
if mime == "" {
return "application/octet-stream"
}
if len([]byte(mime)) > 256 {
return "application/octet-stream"
}

// check if mime is only ascii
for _, rune := range mime {
if unicode.IsPrint(rune) == false {
return "application/octet-stream"
}
}

return mime
}

0 comments on commit 0a05234

Please sign in to comment.