mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-13 06:40:59 +00:00
fix(settings): reject spaces, '\' and control chars in URI path settings
webBasePath, subPath, subJsonPath and subClashPath are URL paths, so '/' stays allowed, but spaces, backslashes and control characters break routing. Strip them as you type (shared sanitizePath helper, now also applied to the panel base path) and reject them on save in AllSetting.CheckValid so direct API callers are covered too.
This commit is contained in:
@@ -128,6 +128,15 @@ type AllSettingView struct {
|
||||
}
|
||||
|
||||
// CheckValid validates all settings in the AllSetting struct, checking IP addresses, ports, SSL certificates, and other configuration values.
|
||||
func pathHasForbiddenChar(s string) bool {
|
||||
for _, r := range s {
|
||||
if r == '\\' || r == ' ' || r < 0x20 || r == 0x7f {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *AllSetting) CheckValid() error {
|
||||
if s.WebListen != "" {
|
||||
ip := net.ParseIP(s.WebListen)
|
||||
@@ -169,6 +178,20 @@ func (s *AllSetting) CheckValid() error {
|
||||
}
|
||||
}
|
||||
|
||||
for _, p := range []struct {
|
||||
name string
|
||||
value string
|
||||
}{
|
||||
{"web base path", s.WebBasePath},
|
||||
{"subscription path", s.SubPath},
|
||||
{"subscription JSON path", s.SubJsonPath},
|
||||
{"subscription Clash path", s.SubClashPath},
|
||||
} {
|
||||
if pathHasForbiddenChar(p.value) {
|
||||
return common.NewError("URI path contains an invalid character:", p.name)
|
||||
}
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(s.WebBasePath, "/") {
|
||||
s.WebBasePath = "/" + s.WebBasePath
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package entity
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPathHasForbiddenChar(t *testing.T) {
|
||||
valid := []string{
|
||||
"",
|
||||
"/",
|
||||
"/sub/",
|
||||
"/json/",
|
||||
"/a/b/c/",
|
||||
"/My-Path_123/",
|
||||
}
|
||||
for _, p := range valid {
|
||||
if pathHasForbiddenChar(p) {
|
||||
t.Errorf("pathHasForbiddenChar(%q) = true, want false", p)
|
||||
}
|
||||
}
|
||||
|
||||
invalid := []string{
|
||||
"/sub path/",
|
||||
"/back\\slash/",
|
||||
"/tab\there/",
|
||||
"/new\nline/",
|
||||
"/\x7f/",
|
||||
}
|
||||
for _, p := range invalid {
|
||||
if !pathHasForbiddenChar(p) {
|
||||
t.Errorf("pathHasForbiddenChar(%q) = false, want true", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user