fix(agentic): normalise remote host inputs

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 09:12:49 +00:00
parent 6bc24d5213
commit 2a95ba6ff5
4 changed files with 25 additions and 0 deletions

View file

@ -124,6 +124,7 @@ func (s *PrepSubsystem) dispatchRemote(ctx context.Context, _ *mcp.CallToolReque
// addr := resolveHost("charon") // "10.69.69.165:9101" // addr := resolveHost("charon") // "10.69.69.165:9101"
func resolveHost(host string) string { func resolveHost(host string) string {
host = core.Trim(host)
aliases := map[string]string{ aliases := map[string]string{
"charon": "10.69.69.165:9101", "charon": "10.69.69.165:9101",
"cladius": "127.0.0.1:9101", "cladius": "127.0.0.1:9101",
@ -143,6 +144,7 @@ func resolveHost(host string) string {
// token := remoteToken("charon") // token := remoteToken("charon")
func remoteToken(host string) string { func remoteToken(host string) string {
host = core.Trim(host)
envKey := core.Sprintf("AGENT_TOKEN_%s", core.Upper(host)) envKey := core.Sprintf("AGENT_TOKEN_%s", core.Upper(host))
if token := core.Env(envKey); token != "" { if token := core.Env(envKey); token != "" {
return token return token

View file

@ -19,6 +19,7 @@ type RemoteClient struct {
// client := agentic.NewRemoteClient("charon") // client := agentic.NewRemoteClient("charon")
func NewRemoteClient(host string) RemoteClient { func NewRemoteClient(host string) RemoteClient {
host = core.Trim(host)
address := resolveHost(host) address := resolveHost(host)
return RemoteClient{ return RemoteClient{
Host: host, Host: host,

View file

@ -297,6 +297,17 @@ func TestRemoteClient_NewRemoteClient_Good(t *testing.T) {
assert.Equal(t, "http://10.69.69.165:9101/mcp", client.URL) assert.Equal(t, "http://10.69.69.165:9101/mcp", client.URL)
} }
func TestRemoteClient_NewRemoteClient_Good_TrimmedInput(t *testing.T) {
t.Setenv("AGENT_TOKEN_CHARON", "token-123")
client := NewRemoteClient(" charon ")
assert.Equal(t, "charon", client.Host)
assert.Equal(t, "10.69.69.165:9101", client.Address)
assert.Equal(t, "token-123", client.Token)
assert.Equal(t, "http://10.69.69.165:9101/mcp", client.URL)
}
func TestRemoteClient_ToolCallBody_Good(t *testing.T) { func TestRemoteClient_ToolCallBody_Good(t *testing.T) {
client := NewRemoteClient("local") client := NewRemoteClient("local")

View file

@ -22,6 +22,11 @@ func TestRemote_ResolveHost_Good_CustomHost(t *testing.T) {
assert.Equal(t, "192.168.1.100:8080", resolveHost("192.168.1.100:8080")) assert.Equal(t, "192.168.1.100:8080", resolveHost("192.168.1.100:8080"))
} }
func TestRemote_ResolveHost_Good_TrimmedInput(t *testing.T) {
assert.Equal(t, "10.69.69.165:9101", resolveHost(" charon "))
assert.Equal(t, "my-server:9101", resolveHost(" my-server "))
}
// --- remoteToken --- // --- remoteToken ---
func TestRemote_RemoteToken_Good_FromEnv(t *testing.T) { func TestRemote_RemoteToken_Good_FromEnv(t *testing.T) {
@ -44,6 +49,12 @@ func TestRemote_RemoteToken_Good_EnvPrecedence(t *testing.T) {
assert.Equal(t, "specific-token", token, "host-specific env should take precedence") assert.Equal(t, "specific-token", token, "host-specific env should take precedence")
} }
func TestRemote_RemoteToken_Good_TrimmedInput(t *testing.T) {
t.Setenv("AGENT_TOKEN_CHARON", "trimmed-token")
token := remoteToken(" charon ")
assert.Equal(t, "trimmed-token", token)
}
// --- resolveHost Bad/Ugly --- // --- resolveHost Bad/Ugly ---
func TestRemote_ResolveHost_Bad(t *testing.T) { func TestRemote_ResolveHost_Bad(t *testing.T) {