Browse Source

Extract HTML sanitizer to its own file

master
Simon Ser 4 years ago
parent
commit
8d248bc32f
No known key found for this signature in database GPG Key ID: FDE7BE0E88F5E48
2 changed files with 19 additions and 7 deletions
  1. +1
    -7
      plugins/base/routes.go
  2. +18
    -0
      plugins/base/sanitize_html.go

+ 1
- 7
plugins/base/routes.go View File

@@ -16,7 +16,6 @@ import (
"github.com/emersion/go-message"
"github.com/emersion/go-smtp"
"github.com/labstack/echo/v4"
"github.com/microcosm-cc/bluemonday"
)

func registerRoutes(p *koushin.GoPlugin) {
@@ -246,12 +245,7 @@ func handleGetPart(ctx *koushin.Context, raw bool) error {

isHTML := false
if strings.EqualFold(mimeType, "text/html") {
p := bluemonday.UGCPolicy()
// TODO: be more strict
p.AllowElements("style")
p.AllowAttrs("style")
p.AddTargetBlankToFullyQualifiedLinks(true)
body = p.Sanitize(body)
body = sanitizeHTML(body)
isHTML = true
}



+ 18
- 0
plugins/base/sanitize_html.go View File

@@ -0,0 +1,18 @@
package koushinbase

import (
"github.com/microcosm-cc/bluemonday"
)

func sanitizeHTML(b string) string {
p := bluemonday.UGCPolicy()

// TODO: be more strict
p.AllowElements("style")
p.AllowAttrs("style")

p.AddTargetBlankToFullyQualifiedLinks(true)
p.RequireNoFollowOnLinks(true)

return p.Sanitize(b)
}

Loading…
Cancel
Save