mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
reScriptTag = regexp.MustCompile(`(?is)<script\b[^>]*>.*?</script>`)
|
|
reIFrameTag = regexp.MustCompile(`(?is)<iframe\b[^>]*>.*?</iframe>`)
|
|
reObjectTag = regexp.MustCompile(`(?is)<object\b[^>]*>.*?</object>`)
|
|
reEmbedTag = regexp.MustCompile(`(?is)<embed\b[^>]*>.*?</embed>`)
|
|
reEventAttr = regexp.MustCompile(`(?i)\s+on[a-z]+\s*=\s*("[^"]*"|'[^']*'|[^\s>]+)`)
|
|
reJSURL = regexp.MustCompile(`(?i)(href|src)\s*=\s*("|')\s*javascript:[^"']*("|')`)
|
|
reDataJSURL = regexp.MustCompile(`(?i)(href|src)\s*=\s*("|')\s*data:text/html[^"']*("|')`)
|
|
)
|
|
|
|
const maxDetailHTMLBytes = 512 * 1024
|
|
|
|
func sanitizeDetailHTML(raw string) (string, error) {
|
|
if raw == "" {
|
|
return "", nil
|
|
}
|
|
if len(raw) > maxDetailHTMLBytes {
|
|
return "", errDetailHTMLTooLarge
|
|
}
|
|
out := raw
|
|
out = reScriptTag.ReplaceAllString(out, "")
|
|
out = reIFrameTag.ReplaceAllString(out, "")
|
|
out = reObjectTag.ReplaceAllString(out, "")
|
|
out = reEmbedTag.ReplaceAllString(out, "")
|
|
out = reEventAttr.ReplaceAllString(out, "")
|
|
out = reJSURL.ReplaceAllString(out, `$1=""`)
|
|
out = reDataJSURL.ReplaceAllString(out, `$1=""`)
|
|
return strings.TrimSpace(out), nil
|
|
}
|
|
|
|
type detailHTMLError string
|
|
|
|
func (e detailHTMLError) Error() string { return string(e) }
|
|
|
|
const errDetailHTMLTooLarge = detailHTMLError("detail_html exceeds maximum size")
|