From 0096e473515bdb47220611d722d3a2f59e1feb80 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Tue, 10 Oct 2023 18:39:25 +0800 Subject: [PATCH] feat: fix 403 error in CorsFilter --- routers/base.go | 8 ++++++++ routers/cors_filter.go | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/routers/base.go b/routers/base.go index 9cbaad3f..997d1d99 100644 --- a/routers/base.go +++ b/routers/base.go @@ -171,6 +171,14 @@ func getHostname(s string) string { return res } +func removePort(s string) string { + ipStr, _, err := net.SplitHostPort(s) + if err != nil { + ipStr = s + } + return ipStr +} + func isHostIntranet(s string) bool { ipStr, _, err := net.SplitHostPort(s) if err != nil { diff --git a/routers/cors_filter.go b/routers/cors_filter.go index 78c57685..a109108a 100644 --- a/routers/cors_filter.go +++ b/routers/cors_filter.go @@ -44,7 +44,7 @@ func CorsFilter(ctx *context.Context) { origin := ctx.Input.Header(headerOrigin) originConf := conf.GetConfigString("origin") originHostname := getHostname(origin) - host := ctx.Request.Host + host := removePort(ctx.Request.Host) if strings.HasPrefix(origin, "http://localhost") || strings.HasPrefix(origin, "https://localhost") || strings.HasPrefix(origin, "http://127.0.0.1") || strings.HasPrefix(origin, "http://casdoor-app") { setCorsHeaders(ctx, origin)