Unified · Split
| @@ -27,12 +27,17 @@ type Option func(*AmpModule) · 26 unmodified lines | |||
| 27 | type AmpModule struct { | 27 | type AmpModule struct { |
| 28 | secretSource SecretSource | 28 | secretSource SecretSource |
| 29 | proxy *httputil.ReverseProxy | 29 | proxy *httputil.ReverseProxy |
| 30 | proxyMu sync.RWMutex // protects proxy for hot-reload | ||
| 30 | accessManager *sdkaccess.Manager | 31 | accessManager *sdkaccess.Manager |
| 31 | authMiddleware_ gin.HandlerFunc | 32 | authMiddleware_ gin.HandlerFunc |
| 32 | modelMapper *DefaultModelMapper | 33 | modelMapper *DefaultModelMapper |
| 33 | enabled bool | 34 | enabled bool |
| 34 | registerOnce sync.Once | 35 | registerOnce sync.Once |
| 35 | 36 | ||
| 37 | // restrictToLocalhost controls localhost-only access for management routes (hot-reloadable) | ||
| 38 | restrictToLocalhost bool | ||
| 39 | restrictMu sync.RWMutex | ||
| 40 | |||
| 36 | // configMu protects lastConfig for partial reload comparison | 41 | // configMu protects lastConfig for partial reload comparison |
| 37 | configMu sync.RWMutex | 42 | configMu sync.RWMutex |
| 38 | lastConfig *config.AmpCode | 43 | lastConfig *config.AmpCode |
| @@ -115,6 +120,9 @@ func (m *AmpModule) Register(ctx modules.Context) error { · 76 unmodified lines | |||
| 115 | settingsCopy := settings | 120 | settingsCopy := settings |
| 116 | m.lastConfig = &settingsCopy | 121 | m.lastConfig = &settingsCopy |
| 117 | 122 | ||
| 123 | // Initialize localhost restriction setting (hot-reloadable) | ||
| 124 | m.setRestrictToLocalhost(settings.RestrictManagementToLocalhost) | ||
| 125 | |||
| 118 | // Always register provider aliases - these work without an upstream | 126 | // Always register provider aliases - these work without an upstream |
| 119 | m.registerProviderAliases(ctx.Engine, ctx.BaseHandler, auth) | 127 | m.registerProviderAliases(ctx.Engine, ctx.BaseHandler, auth) |
| 120 | 128 | ||
| @@ -139,13 +147,12 @@ func (m *AmpModule) Register(ctx modules.Context) error { · 18 unmodified lines | |||
| 139 | return | 147 | return |
| 140 | } | 148 | } |
| 141 | 149 | ||
| 142 | m.proxy = proxy | 150 | m.setProxy(proxy) |
| 143 | m.enabled = true | 151 | m.enabled = true |
| 144 | 152 | ||
| 145 | // Register management proxy routes (requires upstream) | 153 | // Register management proxy routes (requires upstream) |
| 146 | // Restrict to localhost by default for security (prevents drive-by browser attacks) | 154 | // Uses dynamic middleware that checks m.IsRestrictedToLocalhost() for hot-reload support |
| 147 | handler := proxyHandler(proxy) | 155 | m.registerManagementRoutes(ctx.Engine, ctx.BaseHandler) |
| 148 | m.registerManagementRoutes(ctx.Engine, ctx.BaseHandler, handler, settings.RestrictManagementToLocalhost) | ||
| 149 | 156 | ||
| 150 | log.Infof("amp upstream proxy enabled for: %s", upstreamURL) | 157 | log.Infof("amp upstream proxy enabled for: %s", upstreamURL) |
| 151 | log.Debug("amp provider alias routes registered") | 158 | log.Debug("amp provider alias routes registered") |
| @@ -172,7 +179,7 @@ func (m *AmpModule) getAuthMiddleware(ctx modules.Context) gin.HandlerFunc { · 20 unmodified lines | |||
| 172 | 179 | ||
| 173 | // OnConfigUpdated handles configuration updates with partial reload support. | 180 | // OnConfigUpdated handles configuration updates with partial reload support. |
| 174 | // Only updates components that have actually changed to avoid unnecessary work. | 181 | // Only updates components that have actually changed to avoid unnecessary work. |
| 175 | // URL changes still require restart (logged as warning). | 182 | // Supports hot-reload for: model-mappings, upstream-api-key, upstream-url, restrict-management-to-localhost. |
| 176 | func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error { | 183 | func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error { |
| 177 | newSettings := cfg.AmpCode | 184 | newSettings := cfg.AmpCode |
| 178 | 185 | ||
| @@ -199,7 +206,7 @@ func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error { · 20 unmodified lines | |||
| 199 | } | 206 | } |
| 200 | 207 | ||
| 201 | if m.enabled { | 208 | if m.enabled { |
| 202 | // Check upstream URL change (requires restart) | 209 | // Check upstream URL change - now supports hot-reload |
| 203 | newUpstreamURL := strings.TrimSpace(newSettings.UpstreamURL) | 210 | newUpstreamURL := strings.TrimSpace(newSettings.UpstreamURL) |
| 204 | oldUpstreamURL := "" | 211 | oldUpstreamURL := "" |
| 205 | if oldSettings != nil { | 212 | if oldSettings != nil { |
| @@ -207,10 +214,19 @@ func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error { · 1 unmodified line | |||
| 207 | } | 214 | } |
| 208 | 215 | ||
| 209 | if newUpstreamURL == "" && oldUpstreamURL != "" { | 216 | if newUpstreamURL == "" && oldUpstreamURL != "" { |
| 210 | log.Warn("amp upstream URL removed from config, restart required to disable") | 217 | log.Warn("amp upstream URL removed from config, proxy disabled until restart") |
| 211 | } else if newUpstreamURL != oldUpstreamURL { | 218 | m.setProxy(nil) |
| 212 | changes = append(changes, "upstream-url(restart required)") | 219 | changes = append(changes, "upstream-url(disabled)") |
| 213 | log.Warnf("amp config: upstream-url changed (%s -> %s), restart required", oldUpstreamURL, newUpstreamURL) | 220 | } else if newUpstreamURL != oldUpstreamURL && newUpstreamURL != "" { |
| 221 | // Recreate proxy with new URL | ||
| 222 | proxy, err := createReverseProxy(newUpstreamURL, m.secretSource) | ||
| 223 | if err != nil { | ||
| 224 | log.Errorf("amp config: failed to create proxy for new upstream URL %s: %v", newUpstreamURL, err) | ||
| 225 | } else { | ||
| 226 | m.setProxy(proxy) | ||
| 227 | changes = append(changes, "upstream-url") | ||
| 228 | log.Infof("amp config partial reload: upstream URL updated (%s -> %s)", oldUpstreamURL, newUpstreamURL) | ||
| 229 | } | ||
| 214 | } | 230 | } |
| 215 | 231 | ||
| 216 | // Check API key change | 232 | // Check API key change |
| @@ -226,11 +242,15 @@ func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error { · 9 unmodified lines | |||
| 226 | } | 242 | } |
| 227 | } | 243 | } |
| 228 | 244 | ||
| 229 | // Check restrict-management-to-localhost change (requires restart) | 245 | // Check restrict-management-to-localhost change - now supports hot-reload |
| 230 | if oldSettings != nil && oldSettings.RestrictManagementToLocalhost != newSettings.RestrictManagementToLocalhost { | 246 | if oldSettings != nil && oldSettings.RestrictManagementToLocalhost != newSettings.RestrictManagementToLocalhost { |
| 231 | changes = append(changes, "restrict-management-to-localhost(restart required)") | 247 | m.setRestrictToLocalhost(newSettings.RestrictManagementToLocalhost) |
| 232 | log.Warnf("amp config: restrict-management-to-localhost changed (%t -> %t), restart required", | 248 | changes = append(changes, "restrict-management-to-localhost") |
| 233 | oldSettings.RestrictManagementToLocalhost, newSettings.RestrictManagementToLocalhost) | 249 | if newSettings.RestrictManagementToLocalhost { |
| 250 | log.Infof("amp config partial reload: management routes now restricted to localhost") | ||
| 251 | } else { | ||
| 252 | log.Warnf("amp config partial reload: management routes now accessible from any IP - this is insecure!") | ||
| 253 | } | ||
| 234 | } | 254 | } |
| 235 | } | 255 | } |
| 236 | 256 | ||
| @@ -291,3 +311,31 @@ func (m *AmpModule) hasAPIKeyChanged(old *config.AmpCode, new *config.AmpCode) b · 54 unmodified lines | |||
| 291 | func (m *AmpModule) GetModelMapper() *DefaultModelMapper { | 311 | func (m *AmpModule) GetModelMapper() *DefaultModelMapper { |
| 292 | return m.modelMapper | 312 | return m.modelMapper |
| 293 | } | 313 | } |
| 314 | |||
| 315 | // getProxy returns the current proxy instance (thread-safe for hot-reload). | ||
| 316 | func (m *AmpModule) getProxy() *httputil.ReverseProxy { | ||
| 317 | m.proxyMu.RLock() | ||
| 318 | defer m.proxyMu.RUnlock() | ||
| 319 | return m.proxy | ||
| 320 | } | ||
| 321 | |||
| 322 | // setProxy updates the proxy instance (thread-safe for hot-reload). | ||
| 323 | func (m *AmpModule) setProxy(proxy *httputil.ReverseProxy) { | ||
| 324 | m.proxyMu.Lock() | ||
| 325 | defer m.proxyMu.Unlock() | ||
| 326 | m.proxy = proxy | ||
| 327 | } | ||
| 328 | |||
| 329 | // IsRestrictedToLocalhost returns whether management routes are restricted to localhost. | ||
| 330 | func (m *AmpModule) IsRestrictedToLocalhost() bool { | ||
| 331 | m.restrictMu.RLock() | ||
| 332 | defer m.restrictMu.RUnlock() | ||
| 333 | return m.restrictToLocalhost | ||
| 334 | } | ||
| 335 | |||
| 336 | // setRestrictToLocalhost updates the localhost restriction setting. | ||
| 337 | func (m *AmpModule) setRestrictToLocalhost(restrict bool) { | ||
| 338 | m.restrictMu.Lock() | ||
| 339 | defer m.restrictMu.Unlock() | ||
| 340 | m.restrictToLocalhost = restrict | ||
| 341 | } | ||
| @@ -152,9 +152,9 @@ func TestModelMapper_UpdateMappings_SkipsInvalid(t *testing.T) { · 151 unmodified lines | |||
| 152 | mapper := NewModelMapper(nil) | 152 | mapper := NewModelMapper(nil) |
| 153 | 153 | ||
| 154 | mapper.UpdateMappings([]config.AmpModelMapping{ | 154 | mapper.UpdateMappings([]config.AmpModelMapping{ |
| 155 | {From: "", To: "model-b"}, // Invalid: empty from | 155 | {From: "", To: "model-b"}, // Invalid: empty from |
| 156 | {From: "model-a", To: ""}, // Invalid: empty to | 156 | {From: "model-a", To: ""}, // Invalid: empty to |
| 157 | {From: " ", To: "model-b"}, // Invalid: whitespace from | 157 | {From: " ", To: "model-b"}, // Invalid: whitespace from |
| 158 | {From: "model-c", To: "model-d"}, // Valid | 158 | {From: "model-c", To: "model-d"}, // Valid |
| 159 | }) | 159 | }) |
| 160 | 160 | ||
| @@ -14,15 +14,16 @@ import ( · 13 unmodified lines | |||
| 14 | log "github.com/sirupsen/logrus" | 14 | log "github.com/sirupsen/logrus" |
| 15 | ) | 15 | ) |
| 16 | 16 | ||
| 17 | // localhostOnlyMiddleware restricts access to localhost (127.0.0.1, ::1) only. | 17 | // localhostOnlyMiddleware returns a middleware that dynamically checks the module's |
| 18 | // Returns 403 Forbidden for non-localhost clients. | 18 | // localhost restriction setting. This allows hot-reload of the restriction without restarting. |
| 19 | // | 19 | func (m *AmpModule) localhostOnlyMiddleware() gin.HandlerFunc { |
| 20 | // Security: Uses RemoteAddr (actual TCP connection) instead of ClientIP() to prevent | ||
| 21 | // header spoofing attacks via X-Forwarded-For or similar headers. This means the | ||
| 22 | // middleware will not work correctly behind reverse proxies - users deploying behind | ||
| 23 | // nginx/Cloudflare should disable this feature and use firewall rules instead. | ||
| 24 | func localhostOnlyMiddleware() gin.HandlerFunc { | ||
| 25 | return func(c *gin.Context) { | 20 | return func(c *gin.Context) { |
| 21 | // Check current setting (hot-reloadable) | ||
| 22 | if !m.IsRestrictedToLocalhost() { | ||
| 23 | c.Next() | ||
| 24 | return | ||
| 25 | } | ||
| 26 | |||
| 26 | // Use actual TCP connection address (RemoteAddr) to prevent header spoofing | 27 | // Use actual TCP connection address (RemoteAddr) to prevent header spoofing |
| 27 | // This cannot be forged by X-Forwarded-For or other client-controlled headers | 28 | // This cannot be forged by X-Forwarded-For or other client-controlled headers |
| 28 | remoteAddr := c.Request.RemoteAddr | 29 | remoteAddr := c.Request.RemoteAddr |
| @@ -79,21 +80,32 @@ func noCORSMiddleware() gin.HandlerFunc { · 50 unmodified lines | |||
| 79 | 80 | ||
| 80 | // registerManagementRoutes registers Amp management proxy routes | 81 | // registerManagementRoutes registers Amp management proxy routes |
| 81 | // These routes proxy through to the Amp control plane for OAuth, user management, etc. | 82 | // These routes proxy through to the Amp control plane for OAuth, user management, etc. |
| 82 | // If restrictToLocalhost is true, routes will only accept connections from 127.0.0.1/::1. | 83 | // Uses dynamic middleware and proxy getter for hot-reload support. |
| 83 | func (m *AmpModule) registerManagementRoutes(engine *gin.Engine, baseHandler *handlers.BaseAPIHandler, proxyHandler gin.HandlerFunc, restrictToLocalhost bool) { | 84 | func (m *AmpModule) registerManagementRoutes(engine *gin.Engine, baseHandler *handlers.BaseAPIHandler) { |
| 84 | ampAPI := engine.Group("/api") | 85 | ampAPI := engine.Group("/api") |
| 85 | 86 | ||
| 86 | // Always disable CORS for management routes to prevent browser-based attacks | 87 | // Always disable CORS for management routes to prevent browser-based attacks |
| 87 | ampAPI.Use(noCORSMiddleware()) | 88 | ampAPI.Use(noCORSMiddleware()) |
| 88 | 89 | ||
| 89 | // Apply localhost-only restriction if configured | 90 | // Apply dynamic localhost-only restriction (hot-reloadable via m.IsRestrictedToLocalhost()) |
| 90 | if restrictToLocalhost { | 91 | ampAPI.Use(m.localhostOnlyMiddleware()) |
| 91 | ampAPI.Use(localhostOnlyMiddleware()) | 92 | |
| 93 | if m.IsRestrictedToLocalhost() { | ||
| 92 | log.Info("amp management routes restricted to localhost only (CORS disabled)") | 94 | log.Info("amp management routes restricted to localhost only (CORS disabled)") |
| 93 | } else { | 95 | } else { |
| 94 | log.Warn("amp management routes are NOT restricted to localhost - this is insecure!") | 96 | log.Warn("amp management routes are NOT restricted to localhost - this is insecure!") |
| 95 | } | 97 | } |
| 96 | 98 | ||
| 99 | // Dynamic proxy handler that uses m.getProxy() for hot-reload support | ||
| 100 | proxyHandler := func(c *gin.Context) { | ||
| 101 | proxy := m.getProxy() | ||
| 102 | if proxy == nil { | ||
| 103 | c.JSON(503, gin.H{"error": "amp upstream proxy not available"}) | ||
| 104 | return | ||
| 105 | } | ||
| 106 | proxy.ServeHTTP(c.Writer, c.Request) | ||
| 107 | } | ||
| 108 | |||
| 97 | // Management routes - these are proxied directly to Amp upstream | 109 | // Management routes - these are proxied directly to Amp upstream |
| 98 | ampAPI.Any("/internal", proxyHandler) | 110 | ampAPI.Any("/internal", proxyHandler) |
| 99 | ampAPI.Any("/internal/*path", proxyHandler) | 111 | ampAPI.Any("/internal/*path", proxyHandler) |
| @@ -114,11 +126,8 @@ func (m *AmpModule) registerManagementRoutes(engine *gin.Engine, baseHandler *ha · 14 unmodified lines | |||
| 114 | ampAPI.Any("/tab/*path", proxyHandler) | 126 | ampAPI.Any("/tab/*path", proxyHandler) |
| 115 | 127 | ||
| 116 | // Root-level routes that AMP CLI expects without /api prefix | 128 | // Root-level routes that AMP CLI expects without /api prefix |
| 117 | // These need the same security middleware as the /api/* routes | 129 | // These need the same security middleware as the /api/* routes (dynamic for hot-reload) |
| 118 | rootMiddleware := []gin.HandlerFunc{noCORSMiddleware()} | 130 | rootMiddleware := []gin.HandlerFunc{noCORSMiddleware(), m.localhostOnlyMiddleware()} |
| 119 | if restrictToLocalhost { | ||
| 120 | rootMiddleware = append(rootMiddleware, localhostOnlyMiddleware()) | ||
| 121 | } | ||
| 122 | engine.GET("/threads.rss", append(rootMiddleware, proxyHandler)...) | 131 | engine.GET("/threads.rss", append(rootMiddleware, proxyHandler)...) |
| 123 | 132 | ||
| 124 | // Root-level auth routes for CLI login flow | 133 | // Root-level auth routes for CLI login flow |
| @@ -134,7 +143,7 @@ func (m *AmpModule) registerManagementRoutes(engine *gin.Engine, baseHandler *ha · 9 unmodified lines | |||
| 134 | geminiHandlers := gemini.NewGeminiAPIHandler(baseHandler) | 143 | geminiHandlers := gemini.NewGeminiAPIHandler(baseHandler) |
| 135 | geminiBridge := createGeminiBridgeHandler(geminiHandlers) | 144 | geminiBridge := createGeminiBridgeHandler(geminiHandlers) |
| 136 | geminiV1Beta1Fallback := NewFallbackHandler(func() *httputil.ReverseProxy { | 145 | geminiV1Beta1Fallback := NewFallbackHandler(func() *httputil.ReverseProxy { |
| 137 | return m.proxy | 146 | return m.getProxy() |
| 138 | }) | 147 | }) |
| 139 | geminiV1Beta1Handler := geminiV1Beta1Fallback.WrapHandler(geminiBridge) | 148 | geminiV1Beta1Handler := geminiV1Beta1Fallback.WrapHandler(geminiBridge) |
| 140 | 149 | ||
| @@ -177,10 +186,10 @@ func (m *AmpModule) registerProviderAliases(engine *gin.Engine, baseHandler *han · 36 unmodified lines | |||
| 177 | openaiResponsesHandlers := openai.NewOpenAIResponsesAPIHandler(baseHandler) | 186 | openaiResponsesHandlers := openai.NewOpenAIResponsesAPIHandler(baseHandler) |
| 178 | 187 | ||
| 179 | // Create fallback handler wrapper that forwards to ampcode.com when provider not found | 188 | // Create fallback handler wrapper that forwards to ampcode.com when provider not found |
| 180 | // Uses lazy evaluation to access proxy (which is created after routes are registered) | 189 | // Uses m.getProxy() for hot-reload support (proxy can be updated at runtime) |
| 181 | // Also includes model mapping support for routing unavailable models to alternatives | 190 | // Also includes model mapping support for routing unavailable models to alternatives |
| 182 | fallbackHandler := NewFallbackHandlerWithMapper(func() *httputil.ReverseProxy { | 191 | fallbackHandler := NewFallbackHandlerWithMapper(func() *httputil.ReverseProxy { |
| 183 | return m.proxy | 192 | return m.getProxy() |
| 184 | }, m.modelMapper) | 193 | }, m.modelMapper) |
| 185 | 194 | ||
| 186 | // Provider-specific routes under /api/provider/:provider | 195 | // Provider-specific routes under /api/provider/:provider |
| @@ -13,16 +13,26 @@ func TestRegisterManagementRoutes(t *testing.T) { · 12 unmodified lines | |||
| 13 | gin.SetMode(gin.TestMode) | 13 | gin.SetMode(gin.TestMode) |
| 14 | r := gin.New() | 14 | r := gin.New() |
| 15 | 15 | ||
| 16 | // Spy to track if proxy handler was called | 16 | // Create module with proxy for testing |
| 17 | m := &AmpModule{ | ||
| 18 | restrictToLocalhost: false, // disable localhost restriction for tests | ||
| 19 | } | ||
| 20 | |||
| 21 | // Create a mock proxy that tracks calls | ||
| 17 | proxyCalled := false | 22 | proxyCalled := false |
| 18 | proxyHandler := func(c *gin.Context) { | 23 | mockProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 19 | proxyCalled = true | 24 | proxyCalled = true |
| 20 | c.String(200, "proxied") | 25 | w.WriteHeader(200) |
| 21 | } | 26 | w.Write([]byte("proxied")) |
| 27 | })) | ||
| 28 | defer mockProxy.Close() | ||
| 29 | |||
| 30 | // Create real proxy to mock server | ||
| 31 | proxy, _ := createReverseProxy(mockProxy.URL, NewStaticSecretSource("")) | ||
| 32 | m.setProxy(proxy) | ||
| 22 | 33 | ||
| 23 | m := &AmpModule{} | ||
| 24 | base := &handlers.BaseAPIHandler{} | 34 | base := &handlers.BaseAPIHandler{} |
| 25 | m.registerManagementRoutes(r, base, proxyHandler, false) // false = don't restrict to localhost in tests | 35 | m.registerManagementRoutes(r, base) |
| 26 | 36 | ||
| 27 | managementPaths := []struct { | 37 | managementPaths := []struct { |
| 28 | path string | 38 | path string |
| @@ -41,9 +51,9 @@ func TestRegisterManagementRoutes(t *testing.T) { · 12 unmodified lines | |||
| 41 | {"/api/otel", http.MethodGet}, | 51 | {"/api/otel", http.MethodGet}, |
| 42 | {"/api/tab", http.MethodGet}, | 52 | {"/api/tab", http.MethodGet}, |
| 43 | {"/api/tab/some/path", http.MethodGet}, | 53 | {"/api/tab/some/path", http.MethodGet}, |
| 44 | {"/auth", http.MethodGet}, // Root-level auth route | 54 | {"/auth", http.MethodGet}, // Root-level auth route |
| 45 | {"/auth/cli-login", http.MethodGet}, // CLI login flow | 55 | {"/auth/cli-login", http.MethodGet}, // CLI login flow |
| 46 | {"/auth/callback", http.MethodGet}, // OAuth callback | 56 | {"/auth/callback", http.MethodGet}, // OAuth callback |
| 47 | // Google v1beta1 bridge should still proxy non-model requests (GET) and allow POST | 57 | // Google v1beta1 bridge should still proxy non-model requests (GET) and allow POST |
| 48 | {"/api/provider/google/v1beta1/models", http.MethodGet}, | 58 | {"/api/provider/google/v1beta1/models", http.MethodGet}, |
| 49 | {"/api/provider/google/v1beta1/models", http.MethodPost}, | 59 | {"/api/provider/google/v1beta1/models", http.MethodPost}, |
| @@ -231,8 +241,13 @@ func TestLocalhostOnlyMiddleware_PreventsSpoofing(t *testing.T) { · 181 unmodified lines | |||
| 231 | gin.SetMode(gin.TestMode) | 241 | gin.SetMode(gin.TestMode) |
| 232 | r := gin.New() | 242 | r := gin.New() |
| 233 | 243 | ||
| 234 | // Apply localhost-only middleware | 244 | // Create module with localhost restriction enabled |
| 235 | r.Use(localhostOnlyMiddleware()) | 245 | m := &AmpModule{ |
| 246 | restrictToLocalhost: true, | ||
| 247 | } | ||
| 248 | |||
| 249 | // Apply dynamic localhost-only middleware | ||
| 250 | r.Use(m.localhostOnlyMiddleware()) | ||
| 236 | r.GET("/test", func(c *gin.Context) { | 251 | r.GET("/test", func(c *gin.Context) { |
| 237 | c.String(http.StatusOK, "ok") | 252 | c.String(http.StatusOK, "ok") |
| 238 | }) | 253 | }) |
| @@ -305,3 +320,53 @@ func TestLocalhostOnlyMiddleware_PreventsSpoofing(t *testing.T) { · 66 unmodified lines | |||
| 305 | }) | 320 | }) |
| 306 | } | 321 | } |
| 307 | } | 322 | } |
| 323 | |||
| 324 | func TestLocalhostOnlyMiddleware_HotReload(t *testing.T) { | ||
| 325 | gin.SetMode(gin.TestMode) | ||
| 326 | r := gin.New() | ||
| 327 | |||
| 328 | // Create module with localhost restriction initially enabled | ||
| 329 | m := &AmpModule{ | ||
| 330 | restrictToLocalhost: true, | ||
| 331 | } | ||
| 332 | |||
| 333 | // Apply dynamic localhost-only middleware | ||
| 334 | r.Use(m.localhostOnlyMiddleware()) | ||
| 335 | r.GET("/test", func(c *gin.Context) { | ||
| 336 | c.String(http.StatusOK, "ok") | ||
| 337 | }) | ||
| 338 | |||
| 339 | // Test 1: Remote IP should be blocked when restriction is enabled | ||
| 340 | req := httptest.NewRequest(http.MethodGet, "/test", nil) | ||
| 341 | req.RemoteAddr = "192.168.1.100:12345" | ||
| 342 | w := httptest.NewRecorder() | ||
| 343 | r.ServeHTTP(w, req) | ||
| 344 | |||
| 345 | if w.Code != http.StatusForbidden { | ||
| 346 | t.Errorf("Expected 403 when restriction enabled, got %d", w.Code) | ||
| 347 | } | ||
| 348 | |||
| 349 | // Test 2: Hot-reload - disable restriction | ||
| 350 | m.setRestrictToLocalhost(false) | ||
| 351 | |||
| 352 | req = httptest.NewRequest(http.MethodGet, "/test", nil) | ||
| 353 | req.RemoteAddr = "192.168.1.100:12345" | ||
| 354 | w = httptest.NewRecorder() | ||
| 355 | r.ServeHTTP(w, req) | ||
| 356 | |||
| 357 | if w.Code != http.StatusOK { | ||
| 358 | t.Errorf("Expected 200 after disabling restriction, got %d", w.Code) | ||
| 359 | } | ||
| 360 | |||
| 361 | // Test 3: Hot-reload - re-enable restriction | ||
| 362 | m.setRestrictToLocalhost(true) | ||
| 363 | |||
| 364 | req = httptest.NewRequest(http.MethodGet, "/test", nil) | ||
| 365 | req.RemoteAddr = "192.168.1.100:12345" | ||
| 366 | w = httptest.NewRecorder() | ||
| 367 | r.ServeHTTP(w, req) | ||
| 368 | |||
| 369 | if w.Code != http.StatusForbidden { | ||
| 370 | t.Errorf("Expected 403 after re-enabling restriction, got %d", w.Code) | ||
| 371 | } | ||
| 372 | } | ||