Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation. Co-Authored-By: Craft Agent <agents-noreply@craft.do>
51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestTLSOriginCookieBoundary(t *testing.T) {
|
|
r := NewOperationalRouter(NewOperations(slog.New(slog.NewTextHandler(io.Discard, nil))))
|
|
r.POST("/write", func(c *gin.Context) {
|
|
c.SetSameSite(http.SameSiteStrictMode)
|
|
c.SetCookie("test_session", "not-a-real-session", 10, "/", "", secureSessionCookie(c), true)
|
|
c.JSON(200, gin.H{"ip": c.ClientIP()})
|
|
})
|
|
s := httptest.NewTLSServer(r)
|
|
defer s.Close()
|
|
client := s.Client()
|
|
for _, tc := range []struct {
|
|
origin string
|
|
status int
|
|
}{{s.URL, 200}, {"https://foreign.invalid", 403}, {strings.Replace(s.URL, "https:", "http:", 1), 403}} {
|
|
q, _ := http.NewRequest("POST", s.URL+"/write", strings.NewReader("{}"))
|
|
q.Header.Set("Origin", tc.origin)
|
|
q.Header.Set("X-Forwarded-For", "198.51.100.1")
|
|
q.Header.Set("X-Forwarded-Proto", "https")
|
|
resp, err := client.Do(q)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body, _ := io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
if resp.StatusCode != tc.status {
|
|
t.Fatal(resp.StatusCode)
|
|
}
|
|
if tc.status == 200 {
|
|
cookies := resp.Cookies()
|
|
if len(cookies) != 1 || !cookies[0].Secure || !cookies[0].HttpOnly || cookies[0].SameSite != http.SameSiteStrictMode {
|
|
t.Fatal("unsafe cookie")
|
|
}
|
|
if strings.Contains(string(body), "198.51.100.1") {
|
|
t.Fatal("untrusted forwarded IP accepted")
|
|
}
|
|
}
|
|
}
|
|
}
|