Skip to content

Commit

Permalink
Restrict calls from localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
jongio committed Aug 22, 2024
1 parent fca10ab commit 4c6977b
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion cli/azd/cmd/auth_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,32 @@ type TokenResponse struct {

// tokenHandler handles token requests.
func (serve *serveAction) tokenHandler(w http.ResponseWriter, r *http.Request) {
// Extract the IP address from the request's RemoteAddr field
clientIP := r.RemoteAddr

// Only allow requests from 127.0.0.1 or host.docker.internal
allowedIPs := []string{"127.0.0.1", "host.docker.internal"}

// Check if the request comes from an allowed IP address
ipAllowed := false
for _, allowedIP := range allowedIPs {
if clientIP == allowedIP {
ipAllowed = true
break
}
}

if !ipAllowed {
http.Error(w, "Forbidden: Requests are only allowed from 127.0.0.1 or host.docker.internal", http.StatusForbidden)
return
}

resource := r.URL.Query().Get("resource")
if resource == "" {
resource = "https://management.azure.com/"
}

fmt.Printf("Received request for resource: %s\n", resource)
fmt.Printf("Received request for resource: %s from IP: %s\n", resource, clientIP)

ctx := context.Background()
var cred azcore.TokenCredential
Expand All @@ -45,6 +65,7 @@ func (serve *serveAction) tokenHandler(w http.ResponseWriter, r *http.Request) {
})
if err != nil {
fmt.Printf("credentialProvider: %v", err)
http.Error(w, "Failed to get credentials: "+err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -53,6 +74,7 @@ func (serve *serveAction) tokenHandler(w http.ResponseWriter, r *http.Request) {
})
if err != nil {
fmt.Printf("fetching token: %v", err)
http.Error(w, "Failed to fetch token: "+err.Error(), http.StatusInternalServerError)
return
}

Expand Down

0 comments on commit 4c6977b

Please sign in to comment.