|
|
@ -122,22 +122,79 @@ func escapeUrlComponent(val string) string { |
|
|
|
return w.String() |
|
|
|
return w.String() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func sanitizedUrl(val string) (string, error) { |
|
|
|
// Query represents a query
|
|
|
|
|
|
|
|
type Query struct { |
|
|
|
|
|
|
|
Key string |
|
|
|
|
|
|
|
Value string |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func parseQuery(query string) (values []Query, err error) { |
|
|
|
|
|
|
|
for query != "" { |
|
|
|
|
|
|
|
key := query |
|
|
|
|
|
|
|
if i := strings.IndexAny(key, "&;"); i >= 0 { |
|
|
|
|
|
|
|
key, query = key[:i], key[i+1:] |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
query = "" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if key == "" { |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
value := "" |
|
|
|
|
|
|
|
if i := strings.Index(key, "="); i >= 0 { |
|
|
|
|
|
|
|
key, value = key[:i], key[i+1:] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
key, err1 := url.QueryUnescape(key) |
|
|
|
|
|
|
|
if err1 != nil { |
|
|
|
|
|
|
|
if err == nil { |
|
|
|
|
|
|
|
err = err1 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
value, err1 = url.QueryUnescape(value) |
|
|
|
|
|
|
|
if err1 != nil { |
|
|
|
|
|
|
|
if err == nil { |
|
|
|
|
|
|
|
err = err1 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
values = append(values, Query{ |
|
|
|
|
|
|
|
Key: key, |
|
|
|
|
|
|
|
Value: value, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return values, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func encodeQueries(queries []Query) string { |
|
|
|
|
|
|
|
var b strings.Builder |
|
|
|
|
|
|
|
for i, query := range queries { |
|
|
|
|
|
|
|
b.WriteString(url.QueryEscape(query.Key)) |
|
|
|
|
|
|
|
b.WriteString("=") |
|
|
|
|
|
|
|
b.WriteString(url.QueryEscape(query.Value)) |
|
|
|
|
|
|
|
if i < len(queries)-1 { |
|
|
|
|
|
|
|
b.WriteString("&") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return b.String() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func sanitizedURL(val string) (string, error) { |
|
|
|
u, err := url.Parse(val) |
|
|
|
u, err := url.Parse(val) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return "", err |
|
|
|
return "", err |
|
|
|
} |
|
|
|
} |
|
|
|
// sanitize the url query params
|
|
|
|
|
|
|
|
sanitizedQueryValues := make(url.Values, 0) |
|
|
|
// we use parseQuery but not u.Query to keep the order not change because
|
|
|
|
queryValues := u.Query() |
|
|
|
// url.Values is a map which has a random order.
|
|
|
|
for k, vals := range queryValues { |
|
|
|
queryValues, err := parseQuery(u.RawQuery) |
|
|
|
sk := html.EscapeString(k) |
|
|
|
if err != nil { |
|
|
|
for _, v := range vals { |
|
|
|
return "", err |
|
|
|
sv := v |
|
|
|
|
|
|
|
sanitizedQueryValues.Add(sk, sv) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// sanitize the url query params
|
|
|
|
|
|
|
|
for i, query := range queryValues { |
|
|
|
|
|
|
|
queryValues[i].Key = html.EscapeString(query.Key) |
|
|
|
} |
|
|
|
} |
|
|
|
u.RawQuery = sanitizedQueryValues.Encode() |
|
|
|
u.RawQuery = encodeQueries(queryValues) |
|
|
|
// u.String() will also sanitize host/scheme/user/pass
|
|
|
|
// u.String() will also sanitize host/scheme/user/pass
|
|
|
|
return u.String(), nil |
|
|
|
return u.String(), nil |
|
|
|
} |
|
|
|
} |
|
|
@ -158,7 +215,7 @@ func (p *Policy) writeLinkableBuf(buff *bytes.Buffer, token *html.Token) { |
|
|
|
tokenBuff.WriteString(html.EscapeString(attr.Val)) |
|
|
|
tokenBuff.WriteString(html.EscapeString(attr.Val)) |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
u, err := sanitizedUrl(u) |
|
|
|
u, err := sanitizedURL(u) |
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
|
tokenBuff.WriteString(u) |
|
|
|
tokenBuff.WriteString(u) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|