mirror of
https://github.com/Finesssee/ProxyPilot.git
synced 2026-11-18 18:04:20 +00:00
Harden management and OAuth surfaces
This commit is contained in:
+4
-4
@@ -17,10 +17,10 @@ services:
|
||||
ports:
|
||||
- "8317:8317"
|
||||
- "8085:8085"
|
||||
- "1455:1455"
|
||||
- "54545:54545"
|
||||
- "51121:51121"
|
||||
- "11451:11451"
|
||||
- "127.0.0.1:1455:1455"
|
||||
- "127.0.0.1:54545:54545"
|
||||
- "127.0.0.1:51121:51121"
|
||||
- "127.0.0.1:11451:11451"
|
||||
volumes:
|
||||
- ${CLI_PROXY_CONFIG_PATH:-./config.yaml}:/ProxyPilot/config.yaml
|
||||
- ${CLI_PROXY_AUTH_PATH:-./auths}:/root/.cli-proxy-api
|
||||
|
||||
@@ -142,7 +142,7 @@ func startCallbackForwarder(port int, provider, targetBase string) (*callbackFor
|
||||
stopForwarderInstance(port, prev)
|
||||
}
|
||||
|
||||
addr := fmt.Sprintf("0.0.0.0:%d", port)
|
||||
addr := fmt.Sprintf("127.0.0.1:%d", port)
|
||||
ln, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to listen on %s: %w", addr, err)
|
||||
|
||||
@@ -158,7 +158,7 @@ func (m *AmpModule) registerManagementRoutes(engine *gin.Engine, baseHandler *ha
|
||||
var authWithBypass gin.HandlerFunc
|
||||
if auth != nil {
|
||||
ampAPI.Use(auth)
|
||||
authWithBypass = wrapManagementAuth(auth, "/threads", "/auth", "/docs", "/settings")
|
||||
authWithBypass = wrapManagementAuth(auth, "/auth")
|
||||
}
|
||||
|
||||
// Inject client API key into request context for per-client upstream routing
|
||||
|
||||
@@ -86,6 +86,80 @@ func TestRegisterManagementRoutes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRootManagementRoutesRequireAuthExceptAuthFlow(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
|
||||
m := &AmpModule{
|
||||
restrictToLocalhost: false,
|
||||
}
|
||||
|
||||
mockProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("proxied"))
|
||||
}))
|
||||
defer mockProxy.Close()
|
||||
|
||||
proxy, err := createReverseProxy(mockProxy.URL, NewStaticSecretSource(""))
|
||||
if err != nil {
|
||||
t.Fatalf("create proxy: %v", err)
|
||||
}
|
||||
m.setProxy(proxy)
|
||||
|
||||
authMiddleware := func(c *gin.Context) {
|
||||
if c.GetHeader("Authorization") != "Bearer good-key" {
|
||||
c.AbortWithStatus(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
c.Set("userApiKey", "good-key")
|
||||
c.Next()
|
||||
}
|
||||
|
||||
m.registerManagementRoutes(r, &handlers.BaseAPIHandler{}, authMiddleware)
|
||||
srv := httptest.NewServer(r)
|
||||
defer srv.Close()
|
||||
|
||||
for _, path := range []string{"/threads", "/threads/abc", "/settings", "/settings/profile", "/docs"} {
|
||||
t.Run(path+" without key", func(t *testing.T) {
|
||||
resp, err := http.Get(srv.URL + path)
|
||||
if err != nil {
|
||||
t.Fatalf("request failed: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusUnauthorized)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("auth flow bypasses key", func(t *testing.T) {
|
||||
resp, err := http.Get(srv.URL + "/auth/callback")
|
||||
if err != nil {
|
||||
t.Fatalf("request failed: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("threads accepts valid key", func(t *testing.T) {
|
||||
req, err := http.NewRequest(http.MethodGet, srv.URL+"/threads", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("build request: %v", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer good-key")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("request failed: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRegisterProviderAliases_AllProvidersRegistered(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
|
||||
@@ -1184,9 +1184,17 @@ func (s *Server) Stop(ctx context.Context) error {
|
||||
// - gin.HandlerFunc: The CORS middleware handler
|
||||
func corsMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if strings.HasPrefix(c.Request.URL.Path, "/v0/management") {
|
||||
if c.Request.Method == http.MethodOptions {
|
||||
c.AbortWithStatus(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
|
||||
c.Header("Access-Control-Allow-Headers", "*")
|
||||
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Type, X-API-Key, X-Management-Key")
|
||||
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(http.StatusNoContent)
|
||||
|
||||
@@ -147,6 +147,34 @@ func TestManagementUsageRequiresManagementAuthAndPopsArray(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagementRoutesDoNotEmitWildcardCORS(t *testing.T) {
|
||||
t.Setenv("MANAGEMENT_PASSWORD", "test-management-key")
|
||||
|
||||
server := newTestServer(t)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/v0/management/config", nil)
|
||||
req.Header.Set("Origin", "https://evil.example")
|
||||
req.Header.Set("Authorization", "Bearer test-management-key")
|
||||
rr := httptest.NewRecorder()
|
||||
server.engine.ServeHTTP(rr, req)
|
||||
|
||||
if got := rr.Header().Get("Access-Control-Allow-Origin"); got != "" {
|
||||
t.Fatalf("Access-Control-Allow-Origin = %q, want empty", got)
|
||||
}
|
||||
|
||||
preflight := httptest.NewRequest(http.MethodOptions, "/v0/management/config", nil)
|
||||
preflight.Header.Set("Origin", "https://evil.example")
|
||||
preflight.Header.Set("Access-Control-Request-Method", http.MethodPut)
|
||||
preflightRR := httptest.NewRecorder()
|
||||
server.engine.ServeHTTP(preflightRR, preflight)
|
||||
if preflightRR.Code != http.StatusForbidden {
|
||||
t.Fatalf("preflight status = %d, want %d", preflightRR.Code, http.StatusForbidden)
|
||||
}
|
||||
if got := preflightRR.Header().Get("Access-Control-Allow-Origin"); got != "" {
|
||||
t.Fatalf("preflight Access-Control-Allow-Origin = %q, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHomeEnabledHidesManagementEndpointsAndControlPanel(t *testing.T) {
|
||||
t.Setenv("MANAGEMENT_PASSWORD", "test-management-key")
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ func (s *OAuthServer) Start() error {
|
||||
mux.HandleFunc("/success", s.handleSuccess)
|
||||
|
||||
s.server = &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", s.port),
|
||||
Addr: fmt.Sprintf("127.0.0.1:%d", s.port),
|
||||
Handler: mux,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
@@ -309,7 +309,7 @@ func (s *OAuthServer) sendResult(result *OAuthResult) {
|
||||
// Returns:
|
||||
// - bool: True if the port is available, false otherwise
|
||||
func (s *OAuthServer) isPortAvailable() bool {
|
||||
addr := fmt.Sprintf(":%d", s.port)
|
||||
addr := fmt.Sprintf("127.0.0.1:%d", s.port)
|
||||
listener, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return false
|
||||
|
||||
@@ -84,7 +84,7 @@ func (s *OAuthServer) Start() error {
|
||||
mux.HandleFunc("/success", s.handleSuccess)
|
||||
|
||||
s.server = &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", s.port),
|
||||
Addr: fmt.Sprintf("127.0.0.1:%d", s.port),
|
||||
Handler: mux,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
@@ -306,7 +306,7 @@ func (s *OAuthServer) sendResult(result *OAuthResult) {
|
||||
// Returns:
|
||||
// - bool: True if the port is available, false otherwise
|
||||
func (s *OAuthServer) isPortAvailable() bool {
|
||||
addr := fmt.Sprintf(":%d", s.port)
|
||||
addr := fmt.Sprintf("127.0.0.1:%d", s.port)
|
||||
listener, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return false
|
||||
|
||||
@@ -55,7 +55,7 @@ func (s *OAuthServer) Start() error {
|
||||
mux.HandleFunc("/oauth2callback", s.handleCallback)
|
||||
|
||||
s.server = &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", s.port),
|
||||
Addr: fmt.Sprintf("127.0.0.1:%d", s.port),
|
||||
Handler: mux,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
@@ -133,7 +133,7 @@ func (s *OAuthServer) sendResult(res *OAuthResult) {
|
||||
}
|
||||
|
||||
func (s *OAuthServer) isPortAvailable() bool {
|
||||
addr := fmt.Sprintf(":%d", s.port)
|
||||
addr := fmt.Sprintf("127.0.0.1:%d", s.port)
|
||||
listener, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return false
|
||||
|
||||
@@ -192,6 +192,16 @@ func cloneHeaders(headers map[string][]string) map[string][]string {
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneMaskedHeaders(headers map[string][]string) map[string][]string {
|
||||
out := cloneHeaders(headers)
|
||||
for key, values := range out {
|
||||
for i, value := range values {
|
||||
values[i] = util.MaskSensitiveHeaderValue(key, value)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (l *FileRequestLogger) forwardRequestLogToHome(ctx context.Context, headers map[string][]string, logText string) error {
|
||||
if l == nil || !l.homeEnabled {
|
||||
return nil
|
||||
@@ -201,7 +211,7 @@ func (l *FileRequestLogger) forwardRequestLogToHome(ctx context.Context, headers
|
||||
return nil
|
||||
}
|
||||
payload := homeRequestLogPayload{
|
||||
Headers: cloneHeaders(headers),
|
||||
Headers: cloneMaskedHeaders(headers),
|
||||
RequestLog: logText,
|
||||
}
|
||||
raw, errMarshal := json.Marshal(&payload)
|
||||
|
||||
@@ -85,8 +85,11 @@ func TestFileRequestLogger_HomeEnabled_ForwardsWhenRequestLogEnabled(t *testing.
|
||||
if got.Headers == nil || got.Headers["Content-Type"][0] != "application/json" {
|
||||
t.Fatalf("headers.content-type = %+v, want application/json", got.Headers["Content-Type"])
|
||||
}
|
||||
if got.Headers == nil || got.Headers["Authorization"][0] != "Bearer secret" {
|
||||
t.Fatalf("headers.authorization = %+v, want Bearer secret", got.Headers["Authorization"])
|
||||
if got.Headers == nil || got.Headers["Authorization"][0] == "Bearer secret" {
|
||||
t.Fatalf("headers.authorization = %+v, want redacted value", got.Headers["Authorization"])
|
||||
}
|
||||
if got.Headers["Authorization"][0] == "" || got.Headers["Authorization"][0] == "Bearer " {
|
||||
t.Fatalf("headers.authorization = %+v, want non-empty redacted value", got.Headers["Authorization"])
|
||||
}
|
||||
if got.RequestLog == "" {
|
||||
t.Fatalf("request_log empty, want non-empty")
|
||||
|
||||
Reference in New Issue
Block a user