chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,115 @@
|
||||
// Package embedpolicy separates browser framing policy from API transport origins.
|
||||
package embedpolicy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NormalizeOrigin accepts an HTTP(S) origin, with an optional trailing slash.
|
||||
// Reject URL components that cannot appear in an Origin or CSP host source.
|
||||
func NormalizeOrigin(raw string) (string, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil || u == nil || (u.Scheme != "http" && u.Scheme != "https") || u.Hostname() == "" ||
|
||||
u.User != nil || (u.Path != "" && u.Path != "/") || u.RawQuery != "" || u.ForceQuery ||
|
||||
strings.Contains(raw, "#") || strings.ContainsAny(u.Host, " \t\r\n'\";,*\\") {
|
||||
return "", fmt.Errorf("invalid allowed origin: %q", raw)
|
||||
}
|
||||
host := strings.ToLower(u.Host)
|
||||
if (u.Scheme == "https" && u.Port() == "443") || (u.Scheme == "http" && u.Port() == "80") {
|
||||
host = strings.ToLower(u.Hostname())
|
||||
if strings.Contains(host, ":") {
|
||||
host = "[" + host + "]"
|
||||
}
|
||||
}
|
||||
return u.Scheme + "://" + host, nil
|
||||
}
|
||||
|
||||
// NormalizePattern accepts an exact origin, a subdomain wildcard, or the development wildcard.
|
||||
func NormalizePattern(raw string) (string, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "*" {
|
||||
return raw, nil
|
||||
}
|
||||
if strings.HasPrefix(raw, "*.") {
|
||||
origin, err := NormalizeOrigin("https://" + strings.TrimPrefix(raw, "*."))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid allowed origin: %q", raw)
|
||||
}
|
||||
u, _ := url.Parse(origin)
|
||||
if net.ParseIP(u.Hostname()) != nil {
|
||||
return "", fmt.Errorf("invalid allowed origin: %q", raw)
|
||||
}
|
||||
// Preserve an explicit wildcard port, including the HTTPS default port.
|
||||
original, _ := url.Parse("https://" + strings.TrimPrefix(raw, "*."))
|
||||
host := strings.ToLower(original.Host)
|
||||
return "*." + host, nil
|
||||
}
|
||||
return NormalizeOrigin(raw)
|
||||
}
|
||||
|
||||
// Allows matches an API origin against normalized host patterns.
|
||||
func Allows(origin string, patterns []string) bool {
|
||||
normalized, err := NormalizeOrigin(origin)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
u, _ := url.Parse(normalized)
|
||||
for _, raw := range patterns {
|
||||
pattern, err := NormalizePattern(raw)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if pattern == "*" || pattern == normalized {
|
||||
return true
|
||||
}
|
||||
if strings.HasPrefix(pattern, "*.") {
|
||||
wildcard, _ := url.Parse("https://" + strings.TrimPrefix(pattern, "*."))
|
||||
port := u.Port()
|
||||
if port == "" {
|
||||
if u.Scheme == "https" {
|
||||
port = "443"
|
||||
} else {
|
||||
port = "80"
|
||||
}
|
||||
}
|
||||
if strings.HasSuffix(u.Hostname(), "."+wildcard.Hostname()) &&
|
||||
(wildcard.Port() == "" || wildcard.Port() == port) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// FrameAncestors also permits same-origin management previews. Empty or wholly
|
||||
// invalid legacy lists fail closed; invalid strings never reach the CSP header.
|
||||
func FrameAncestors(patterns []string) string {
|
||||
sources := []string{}
|
||||
for _, raw := range patterns {
|
||||
pattern, err := NormalizePattern(raw)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if pattern == "*" {
|
||||
return "frame-ancestors *"
|
||||
}
|
||||
if strings.HasPrefix(pattern, "*.") {
|
||||
u, _ := url.Parse("https://" + strings.TrimPrefix(pattern, "*."))
|
||||
host := pattern
|
||||
if u.Port() == "" {
|
||||
host += ":*"
|
||||
}
|
||||
sources = append(sources, "http://"+host, "https://"+host)
|
||||
} else {
|
||||
sources = append(sources, pattern)
|
||||
}
|
||||
}
|
||||
if len(sources) == 0 {
|
||||
return "frame-ancestors 'none'"
|
||||
}
|
||||
return "frame-ancestors 'self' " + strings.Join(sources, " ")
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package embedpolicy
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestOriginPatterns(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
origin, pattern string
|
||||
want bool
|
||||
}{
|
||||
{"https://B.example", "https://b.example/", true},
|
||||
{"https://a.example", "https://a.example:443", true},
|
||||
{"http://a.example:8080", "http://a.example:8080", true},
|
||||
{"http://a.example", "https://a.example", false},
|
||||
{"https://a.example:8443", "https://a.example", false},
|
||||
{"https://shop.example.com:8443", "*.example.com", true},
|
||||
{"https://SHOP.EXAMPLE.COM", "*.EXAMPLE.COM", true},
|
||||
{"https://shop.example.com", "*.example.com:443", true},
|
||||
{"http://shop.example.com", "*.example.com:443", false},
|
||||
{"https://example.com", "*.example.com", false},
|
||||
{"https://evil-example.com", "*.example.com", false},
|
||||
{"https://example.com.evil.test", "*.example.com", false},
|
||||
{"https://evil.test/path.example.com", "*.example.com", false},
|
||||
{"null", "*", false},
|
||||
{"", "*", false},
|
||||
} {
|
||||
t.Run(tc.origin+"/"+tc.pattern, func(t *testing.T) {
|
||||
if got := Allows(tc.origin, []string{tc.pattern}); got != tc.want {
|
||||
t.Fatalf("got %v want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRejectInvalidPatterns(t *testing.T) {
|
||||
for _, raw := range []string{
|
||||
"https://a.example/path", "https://a.example?x=1", "https://a.example?", "https://a.example#",
|
||||
"https://user@a.example", "https://a.example;", "https://a.example; frame-ancestors *",
|
||||
"https://*", "javascript:alert(1)", "*.127.0.0.1", "https://a.example/../",
|
||||
} {
|
||||
if _, err := NormalizePattern(raw); err == nil {
|
||||
t.Errorf("accepted %q", raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrameAncestors(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
patterns []string
|
||||
want string
|
||||
}{
|
||||
{nil, "frame-ancestors 'none'"},
|
||||
{[]string{" ", "https://a.example;"}, "frame-ancestors 'none'"},
|
||||
{[]string{"https://A.example:443/"}, "frame-ancestors 'self' https://a.example"},
|
||||
{[]string{"*.example.com"}, "frame-ancestors 'self' http://*.example.com:* https://*.example.com:*"},
|
||||
{[]string{"*.example.com:8443"}, "frame-ancestors 'self' http://*.example.com:8443 https://*.example.com:8443"},
|
||||
{[]string{"*"}, "frame-ancestors *"},
|
||||
} {
|
||||
if got := FrameAncestors(tc.patterns); got != tc.want {
|
||||
t.Errorf("%v: got %q want %q", tc.patterns, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user