fix: protect local HTTP boundary and saved setup credentials (#2)

This commit is contained in:
Mikhail Chusavitin committed 2026-10-26 10:35:37 +03:00
1 parent 134685d482
commit c0c7804494
7 files changed
+184 -34

No files matched your search

+27 -1
View File
@@ -1,6 +1,7 @@
package middleware
import (
"mime"
"net"
"net/http"
"net/url"
@@ -11,6 +12,19 @@ import (
func CORS() gin.HandlerFunc {
return func(c *gin.Context) {
hostURL, hostErr := url.Parse("http://" + c.Request.Host)
if hostErr != nil || hostURL.Host != c.Request.Host || hostURL.Path != "" || hostURL.RawQuery != "" || !isLoopbackOrigin("http://"+c.Request.Host) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "Invalid request host"})
return
}
if c.GetHeader("Sec-Fetch-Site") == "cross-site" {
c.AbortWithStatus(http.StatusForbidden)
return
}
if referer := c.GetHeader("Referer"); referer != "" && !isLoopbackOrigin(referer) {
c.AbortWithStatus(http.StatusForbidden)
return
}
origin := strings.TrimSpace(c.GetHeader("Origin"))
if origin != "" {
if isLoopbackOrigin(origin) {
@@ -20,7 +34,7 @@ func CORS() gin.HandlerFunc {
c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Authorization")
c.Header("Access-Control-Expose-Headers", "Content-Length, Content-Disposition")
c.Header("Access-Control-Max-Age", "86400")
} else if c.Request.Method == http.MethodOptions {
} else {
c.AbortWithStatus(http.StatusForbidden)
return
}
@@ -30,6 +44,15 @@ func CORS() gin.HandlerFunc {
c.AbortWithStatus(http.StatusNoContent)
return
}
if c.Request.Method != http.MethodGet && c.Request.Method != http.MethodHead &&
strings.HasPrefix(c.Request.URL.Path, "/api/") && c.Request.ContentLength != 0 {
mediaType, _, err := mime.ParseMediaType(c.GetHeader("Content-Type"))
isUpload := strings.HasPrefix(c.FullPath(), "/api/projects/") && strings.HasSuffix(c.FullPath(), "/vendor-import")
if err != nil || (mediaType != "application/json" && !(isUpload && mediaType == "multipart/form-data")) {
c.AbortWithStatusJSON(http.StatusUnsupportedMediaType, gin.H{"error": "Unsupported content type"})
return
}
}
c.Next()
}
@@ -43,6 +66,9 @@ func isLoopbackOrigin(origin string) bool {
if u.Scheme != "http" && u.Scheme != "https" {
return false
}
if u.User != nil || u.Opaque != "" {
return false
}
host := strings.TrimSpace(u.Hostname())
if host == "" {
return false
+66
View File
@@ -0,0 +1,66 @@
package middleware
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
)
func TestLocalRequestBoundary(t *testing.T) {
gin.SetMode(gin.TestMode)
tests := []struct {
name, method, path, host, origin, referer, fetchSite, contentType, body string
want int
}{
{name: "CLI JSON", method: "POST", contentType: "application/json", body: "{}", want: 200},
{name: "local browser", method: "POST", origin: "http://localhost:8080", contentType: "application/json; charset=utf-8", body: "{}", want: 200},
{name: "foreign form", method: "POST", path: "/setup", origin: "https://attacker.example", contentType: "application/x-www-form-urlencoded", body: "host=example", want: 403},
{name: "foreign JSON", method: "POST", origin: "https://attacker.example", contentType: "text/plain", body: "{}", want: 403},
{name: "rebind read", method: "GET", host: "attacker.example:8080", want: 403},
{name: "null origin", method: "POST", origin: "null", want: 403},
{name: "foreign referer", method: "POST", referer: "https://attacker.example/page", want: 403},
{name: "cross site", method: "POST", fetchSite: "cross-site", want: 403},
{name: "plain JSON", method: "POST", contentType: "text/plain", body: "{}", want: 415},
{name: "API form", method: "POST", contentType: "application/x-www-form-urlencoded", body: "x=y", want: 415},
{name: "setup form", method: "POST", path: "/setup", origin: "http://127.0.0.1:8080", contentType: "application/x-www-form-urlencoded", body: "host=example", want: 200},
{name: "upload", method: "POST", path: "/api/projects/id/vendor-import", contentType: "multipart/form-data; boundary=example", body: "example", want: 200},
{name: "multipart JSON route", method: "POST", contentType: "multipart/form-data; boundary=example", body: "example", want: 415},
{name: "bodyless action", method: "POST", want: 200},
{name: "local preflight", method: "OPTIONS", origin: "http://localhost:8080", want: 204},
{name: "foreign preflight", method: "OPTIONS", origin: "https://attacker.example", want: 403},
{name: "IPv6", method: "GET", host: "[::1]:8080", want: 200},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
router := gin.New()
router.Use(CORS())
called := false
handler := func(c *gin.Context) { called = true; c.Status(200) }
router.Any("/api/configs", handler)
router.Any("/setup", handler)
router.POST("/api/projects/:uuid/vendor-import", handler)
path := tt.path
if path == "" {
path = "/api/configs"
}
req := httptest.NewRequest(tt.method, path, strings.NewReader(tt.body))
req.Host = tt.host
if req.Host == "" {
req.Host = "127.0.0.1:8080"
}
for key, value := range map[string]string{"Origin": tt.origin, "Referer": tt.referer, "Sec-Fetch-Site": tt.fetchSite, "Content-Type": tt.contentType} {
if value != "" {
req.Header.Set(key, value)
}
}
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != tt.want || called != (tt.want == http.StatusOK) {
t.Fatalf("status=%d called=%v, want %d", w.Code, called, tt.want)
}
})
}
}