Skip to content

armgit / ax

Public Imported from GitHub
0

Agent Executor (AX) is Google's open agentic orchestration runtime

Go

Ensure agent IDs are validated before registration (#20)

Jaana Dogan954af654f7ff
diff --git a/internal/controller/registry.go b/internal/controller/registry.go
index f58908b..0d0b8f7 100644
--- a/internal/controller/registry.go
+++ b/internal/controller/registry.go
@@ -75,6 +75,10 @@ func (r *Registry) RegisterLocal(cfg config.LocalAgentConfig) error {
r.mu.Lock()
defer r.mu.Unlock()
+ if err := validateID(cfg.ID); err != nil {
+ return err
+ }
+
if _, ok := r.agents[cfg.ID]; ok {
return fmt.Errorf("agent %s already registered", cfg.ID)
}
@@ -98,8 +102,11 @@ func (r *Registry) RegisterRemote(cfg config.RemoteAgentConfig) error {
r.mu.Lock()
defer r.mu.Unlock()
- // TODO(lhuan): Consider enforcing health check during registration. Only allow registration if the agent is reachable and healthy.
+ if err := validateID(cfg.ID); err != nil {
+ return err
+ }
+ // TODO(lhuan): Consider enforcing health check during registration. Only allow registration if the agent is reachable and healthy.
if _, ok := r.agents[cfg.ID]; ok {
return fmt.Errorf("agent %s already registered", cfg.ID)
}
diff --git a/internal/controller/session.go b/internal/controller/session.go
index 9491cfc..be848d6 100644
--- a/internal/controller/session.go
+++ b/internal/controller/session.go
@@ -17,7 +17,6 @@ package controller
import (
"context"
"fmt"
- "regexp"
"sync"
"github.com/google/gar/internal/eventlog"
@@ -55,7 +54,7 @@ func NewSessionManager(factory eventlog.EventLogFactory) *SessionManager {
// NewSession creates a new session with the given ID.
func (sm *SessionManager) NewSession(sessionID string) (*Session, error) {
- if err := validateSessionID(sessionID); err != nil {
+ if err := validateID(sessionID); err != nil {
return nil, err
}
sm.mu.Lock()
@@ -94,7 +93,7 @@ func (sm *SessionManager) LoadSession(ctx context.Context, sessionID string) (*S
// If checkpointID is empty, loads to the latest state.
// If checkpointID is provided, loads up to and including that checkpoint UUID.
func (sm *SessionManager) LoadSessionFromCheckpoint(ctx context.Context, sessionID string, checkpointID string) (*Session, error) {
- if err := validateSessionID(sessionID); err != nil {
+ if err := validateID(sessionID); err != nil {
return nil, err
}
sm.mu.Lock()
@@ -316,17 +315,3 @@ func (s *Session) CheckpointIDs() []string {
}
return checkpointIDs
}
-
-var sessionIDRegex = regexp.MustCompile(`^[A-Za-z0-9\-_]+$`)
-
-// validateSessionID checks if the session ID contains allowed characters.
-func validateSessionID(sessionID string) error {
- if sessionID == "" {
- return fmt.Errorf("session_id is required")
- }
-
- if !sessionIDRegex.MatchString(sessionID) {
- return fmt.Errorf("invalid session ID %q: must only contain A-Z, a-z, 0-9, -, and _", sessionID)
- }
- return nil
-}
diff --git a/internal/controller/validation.go b/internal/controller/validation.go
new file mode 100644
index 0000000..6f866c0
--- /dev/null
+++ b/internal/controller/validation.go
@@ -0,0 +1,35 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package controller
+
+import (
+ "errors"
+ "fmt"
+ "regexp"
+)
+
+var validIDRegex = regexp.MustCompile(`^[A-Za-z0-9\-_]+$`)
+
+// validateID checks if a session ID or agent ID contains allowed characters.
+func validateID(id string) error {
+ if id == "" {
+ return errors.New("empty ID")
+ }
+
+ if !validIDRegex.MatchString(id) {
+ return fmt.Errorf("invalid ID %q: must only contain A-Z, a-z, 0-9, -, and _", id)
+ }
+ return nil
+}
diff --git a/internal/controller/session_test.go b/internal/controller/validation_test.go
similarity index 97%
rename from internal/controller/session_test.go
rename to internal/controller/validation_test.go
index 9e0236b..0714399 100644
--- a/internal/controller/session_test.go
+++ b/internal/controller/validation_test.go
@@ -63,7 +63,7 @@ func TestValidateSessionID(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- err := validateSessionID(tt.sessionID)
+ err := validateID(tt.sessionID)
if (err != nil) != tt.wantErr {
t.Errorf("validateSessionID(%q) error = %v, wantErr %v", tt.sessionID, err, tt.wantErr)
}