-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
892 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
PKG := inbucket | ||
SHELL := /bin/sh | ||
|
||
SRC := $(shell find . -type f -name '*.go' -not -path "./vendor/*") | ||
PKGS := $$(go list ./... | grep -v /vendor/) | ||
|
||
.PHONY: all build clean fmt install lint simplify test | ||
|
||
all: test lint build | ||
|
||
clean: | ||
go clean | ||
|
||
deps: | ||
go get -t ./... | ||
|
||
build: clean deps | ||
go build | ||
|
||
install: build | ||
go install | ||
|
||
test: clean deps | ||
go test -race ./... | ||
|
||
fmt: | ||
@gofmt -l -w $(SRC) | ||
|
||
simplify: | ||
@gofmt -s -l -w $(SRC) | ||
|
||
lint: | ||
@test -z "$(shell gofmt -l . | tee /dev/stderr)" || echo "[WARN] Fix formatting issues with 'make fmt'" | ||
@golint -set_exit_status $${PKGS} | ||
@go vet $${PKGS} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package sanitize | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
|
||
"github.com/gorilla/css/scanner" | ||
) | ||
|
||
// propertyRule may someday allow control of what values are valid for a particular property. | ||
type propertyRule struct{} | ||
|
||
var allowedProperties = map[string]propertyRule{ | ||
"align": {}, | ||
"background-color": {}, | ||
"border": {}, | ||
"border-bottom": {}, | ||
"border-left": {}, | ||
"border-radius": {}, | ||
"border-right": {}, | ||
"border-top": {}, | ||
"box-sizing": {}, | ||
"clear": {}, | ||
"color": {}, | ||
"content": {}, | ||
"display": {}, | ||
"font-family": {}, | ||
"font-size": {}, | ||
"font-weight": {}, | ||
"height": {}, | ||
"line-height": {}, | ||
"margin": {}, | ||
"margin-bottom": {}, | ||
"margin-left": {}, | ||
"margin-right": {}, | ||
"margin-top": {}, | ||
"max-height": {}, | ||
"max-width": {}, | ||
"overflow": {}, | ||
"padding": {}, | ||
"padding-bottom": {}, | ||
"padding-left": {}, | ||
"padding-right": {}, | ||
"padding-top": {}, | ||
"table-layout": {}, | ||
"text-align": {}, | ||
"text-decoration": {}, | ||
"text-shadow": {}, | ||
"vertical-align": {}, | ||
"width": {}, | ||
"word-break": {}, | ||
} | ||
|
||
// Handler Token, return next state. | ||
type stateHandler func(b *bytes.Buffer, t *scanner.Token) stateHandler | ||
|
||
func sanitizeStyle(input string) string { | ||
b := &bytes.Buffer{} | ||
scan := scanner.New(input) | ||
state := stateStart | ||
for { | ||
t := scan.Next() | ||
if t.Type == scanner.TokenEOF { | ||
return b.String() | ||
} | ||
if t.Type == scanner.TokenError { | ||
return "" | ||
} | ||
state = state(b, t) | ||
if state == nil { | ||
return "" | ||
} | ||
} | ||
} | ||
|
||
func stateStart(b *bytes.Buffer, t *scanner.Token) stateHandler { | ||
switch t.Type { | ||
case scanner.TokenIdent: | ||
_, ok := allowedProperties[strings.ToLower(t.Value)] | ||
if !ok { | ||
return stateEat | ||
} | ||
b.WriteString(t.Value) | ||
return stateValid | ||
case scanner.TokenS: | ||
return stateStart | ||
} | ||
// Unexpected type. | ||
b.WriteString("/*" + t.Type.String() + "*/") | ||
return stateEat | ||
} | ||
|
||
func stateEat(b *bytes.Buffer, t *scanner.Token) stateHandler { | ||
if t.Type == scanner.TokenChar && t.Value == ";" { | ||
// Done eating. | ||
return stateStart | ||
} | ||
// Throw away this token. | ||
return stateEat | ||
} | ||
|
||
func stateValid(b *bytes.Buffer, t *scanner.Token) stateHandler { | ||
state := stateValid | ||
if t.Type == scanner.TokenChar && t.Value == ";" { | ||
// End of property. | ||
state = stateStart | ||
} | ||
b.WriteString(t.Value) | ||
return state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package sanitize | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSanitizeStyle(t *testing.T) { | ||
testCases := []struct { | ||
input, want string | ||
}{ | ||
{"", ""}, | ||
{ | ||
"color: red;", | ||
"color: red;", | ||
}, | ||
{ | ||
"background-color: black; color: white", | ||
"background-color: black;color: white", | ||
}, | ||
{ | ||
"background-color: black; invalid: true; color: white", | ||
"background-color: black;color: white", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.input, func(t *testing.T) { | ||
got := sanitizeStyle(tc.input) | ||
if got != tc.want { | ||
t.Errorf("got: %q, want: %q, input: %q", got, tc.want, tc.input) | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package sanitize | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"io" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/microcosm-cc/bluemonday" | ||
"golang.org/x/net/html" | ||
) | ||
|
||
var ( | ||
cssSafe = regexp.MustCompile(".*") | ||
policy = bluemonday.UGCPolicy(). | ||
AllowElements("center"). | ||
AllowAttrs("style").Matching(cssSafe).Globally() | ||
) | ||
|
||
func HTML(html string) (output string, err error) { | ||
output, err = sanitizeStyleTags(html) | ||
if err != nil { | ||
return "", err | ||
} | ||
output = policy.Sanitize(output) | ||
return | ||
} | ||
|
||
func sanitizeStyleTags(input string) (string, error) { | ||
r := strings.NewReader(input) | ||
b := &bytes.Buffer{} | ||
if err := styleTagFilter(b, r); err != nil { | ||
return "", err | ||
} | ||
return b.String(), nil | ||
} | ||
|
||
func styleTagFilter(w io.Writer, r io.Reader) error { | ||
bw := bufio.NewWriter(w) | ||
b := make([]byte, 256) | ||
z := html.NewTokenizer(r) | ||
for { | ||
b = b[:0] | ||
tt := z.Next() | ||
switch tt { | ||
case html.ErrorToken: | ||
err := z.Err() | ||
if err == io.EOF { | ||
return bw.Flush() | ||
} | ||
return err | ||
case html.StartTagToken, html.SelfClosingTagToken: | ||
name, hasAttr := z.TagName() | ||
if !hasAttr { | ||
bw.Write(z.Raw()) | ||
continue | ||
} | ||
b = append(b, '<') | ||
b = append(b, name...) | ||
for { | ||
key, val, more := z.TagAttr() | ||
strval := string(val) | ||
style := false | ||
if strings.ToLower(string(key)) == "style" { | ||
style = true | ||
strval = sanitizeStyle(strval) | ||
} | ||
if !style || strval != "" { | ||
b = append(b, ' ') | ||
b = append(b, key...) | ||
b = append(b, '=', '"') | ||
b = append(b, []byte(html.EscapeString(strval))...) | ||
b = append(b, '"') | ||
} | ||
if !more { | ||
break | ||
} | ||
} | ||
if tt == html.SelfClosingTagToken { | ||
b = append(b, '/') | ||
} | ||
bw.Write(append(b, '>')) | ||
default: | ||
bw.Write(z.Raw()) | ||
} | ||
} | ||
} |
Oops, something went wrong.