Skip to content

armgit / ax

Public Imported from GitHub
0

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

Go

Introduce session ID validation (#12)

Jaana Dogan201af967ff15
diff --git a/internal/controller/session.go b/internal/controller/session.go
index 5432bc3..5703ddf 100644
--- a/internal/controller/session.go
+++ b/internal/controller/session.go
@@ -17,6 +17,7 @@ package controller
import (
"context"
"fmt"
+ "regexp"
"sync"
"github.com/google/gar/internal/eventlog"
@@ -54,6 +55,9 @@ 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 {
+ return nil, err
+ }
sm.mu.Lock()
defer sm.mu.Unlock()
@@ -90,6 +94,9 @@ 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 {
+ return nil, err
+ }
sm.mu.Lock()
defer sm.mu.Unlock()
@@ -314,3 +321,17 @@ 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/session_test.go b/internal/controller/session_test.go
new file mode 100644
index 0000000..9e0236b
--- /dev/null
+++ b/internal/controller/session_test.go
@@ -0,0 +1,72 @@
+// 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 (
+ "testing"
+)
+
+func TestValidateSessionID(t *testing.T) {
+ tests := []struct {
+ name string
+ sessionID string
+ wantErr bool
+ }{
+ {
+ name: "valid lowercase",
+ sessionID: "session123",
+ wantErr: false,
+ },
+ {
+ name: "valid mixed",
+ sessionID: "Session-ID_123",
+ wantErr: false,
+ },
+ {
+ name: "valid simple",
+ sessionID: "Session-ID",
+ wantErr: false,
+ },
+ {
+ name: "valid underscore",
+ sessionID: "session_id",
+ wantErr: false,
+ },
+ {
+ name: "invalid space",
+ sessionID: "session id",
+ wantErr: true,
+ },
+ {
+ name: "invalid char",
+ sessionID: "session!",
+ wantErr: true,
+ },
+ {
+ name: "empty",
+ sessionID: "",
+ wantErr: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ err := validateSessionID(tt.sessionID)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("validateSessionID(%q) error = %v, wantErr %v", tt.sessionID, err, tt.wantErr)
+ }
+ })
+ }
+}
diff --git a/internal/server/server.go b/internal/server/server.go
index 09427b6..d83db07 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -49,10 +49,6 @@ func (s *Server) TriggerSession(req *proto.TriggerSessionRequest, stream grpc.Se
inputs := req.Inputs
checkpointID := req.CheckpointId
- if sessionID == "" {
- return fmt.Errorf("session_id is required")
- }
-
// TODO(jbd): This state update should be sent directly from the controller.
if err := stream.Send(&proto.TriggerSessionResponse{
State: proto.State_STATE_RUNNING,