fix(17): T-17-15 apply the frontend_url rewrite only for a safe http(s) URL
- models.AllowedFrontendURL: empty, or an absolute http(s) URL with a host and without whitespace, control characters, a double quote, <, > or & - classes.Rewrite leaves the document unchanged for a refused value, inserts through EscapeText/EscapeAttr and keeps a loc or href that would not stay an http(s) URL - TestRewriteUnsafeFrontendURL, TestAllowedFrontendURL and a stored-row check in TestSitemapImportSmoke
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// The settings keys of the five priority tiers, in the order PHP checked
|
||||
// them. A tier rule names one of them.
|
||||
const (
|
||||
@@ -54,6 +61,36 @@ func (Settings) Fillable() []string {
|
||||
// Rules is empty: the PHP settings form validated nothing.
|
||||
func (Settings) Rules() map[string]string { return map[string]string{} }
|
||||
|
||||
// AllowedFrontendURL reports whether u may be the frontend_url setting: the
|
||||
// empty string, or an absolute http or https URL with a host and without
|
||||
// whitespace, control characters, a double quote, <, > or &. The PHP plugin
|
||||
// stored any text. The value is written into every loc, every hreflang link
|
||||
// and the stylesheet instruction of the sitemap, and sitemap.xsl renders
|
||||
// each loc as a link on the public origin, so a value outside this form
|
||||
// (a javascript: URL, or text that closes the attribute it is written into)
|
||||
// would reach the readers of /sitemap.xml (T-17-15).
|
||||
func AllowedFrontendURL(u string) bool {
|
||||
if u == "" {
|
||||
return true
|
||||
}
|
||||
if !utf8.ValidString(u) {
|
||||
return false
|
||||
}
|
||||
if strings.ContainsFunc(u, func(r rune) bool {
|
||||
return unicode.IsControl(r) || unicode.IsSpace(r) || r == '"' || r == '<' || r == '>' || r == '&'
|
||||
}) {
|
||||
return false
|
||||
}
|
||||
parsed, err := url.Parse(u)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if parsed.Scheme != "http" && parsed.Scheme != "https" {
|
||||
return false
|
||||
}
|
||||
return parsed.Opaque == "" && parsed.Hostname() != ""
|
||||
}
|
||||
|
||||
// DefaultSettings is the settings PHP read when nothing was saved: tiers on
|
||||
// with the default priorities.
|
||||
func DefaultSettings() Settings {
|
||||
|
||||
61
models/settings_test.go
Normal file
61
models/settings_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package models_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/sm-sitemap-plugin/models"
|
||||
)
|
||||
|
||||
// TestAllowedFrontendURL pins the one definition of a frontend_url that may
|
||||
// be saved, imported and applied (T-17-15): empty, or an absolute http(s)
|
||||
// URL with a host and without whitespace, control characters, a double
|
||||
// quote, <, > or &.
|
||||
func TestAllowedFrontendURL(t *testing.T) {
|
||||
for _, u := range []string{
|
||||
"",
|
||||
"https://www.ex.pl",
|
||||
"https://www.ex.pl/",
|
||||
"https://www.ex.pl///",
|
||||
"http://localhost:3000",
|
||||
"HTTPS://WWW.EX.PL",
|
||||
"https://www.ex.pl/app",
|
||||
"https://www.ex.pl:8443/base/",
|
||||
"https://[::1]:3000",
|
||||
// The frontend URLs of the golden inputs.
|
||||
"https://api.example.test",
|
||||
"https://www.example.test/",
|
||||
} {
|
||||
if !models.AllowedFrontendURL(u) {
|
||||
t.Errorf("AllowedFrontendURL(%q) = false, want true", u)
|
||||
}
|
||||
}
|
||||
for _, u := range []string{
|
||||
"0",
|
||||
"javascript:alert(document.domain)//",
|
||||
"JavaScript://www.ex.pl/%0aalert(1)",
|
||||
`https://x"/><y a="`,
|
||||
"https://www.ex.pl/?a=1&b=2",
|
||||
"https://www.ex.pl <x>",
|
||||
" https://www.ex.pl",
|
||||
"https://www.ex.pl\n",
|
||||
"https://www.ex.pl\t",
|
||||
"https://www.ex.pl ",
|
||||
"https://www.ex.pl\x7f",
|
||||
"https://www.ex.pl\xff",
|
||||
"//www.ex.pl",
|
||||
"www.ex.pl",
|
||||
"/path",
|
||||
"https:///x",
|
||||
"https://:8080",
|
||||
"https:www.ex.pl",
|
||||
"http://",
|
||||
"https://",
|
||||
"https://%zz",
|
||||
"data:text/html,x",
|
||||
"ftp://www.ex.pl",
|
||||
} {
|
||||
if models.AllowedFrontendURL(u) {
|
||||
t.Errorf("AllowedFrontendURL(%q) = true, want false", u)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user