fix: harden runtime against panics/races, close path-traversal and fail-open gaps

- Runtime.Run rejects concurrent invocations instead of racing on shared state; panics in tools, hooks, and permission checkers are recovered and surfaced as errors/denials instead of crashing the loop
- AgentSpec.Loop now actually overrides RuntimeDeps.AgentLoop per-agent, as documented
- session.Store validates agent ID / session key as single path components, preventing traversal outside the session directory
- ValidatePathInWorkDir resolves symlinks on the target itself, not just its parent, closing a workspace-escape gap
- bash tool now fails closed on an unrecognized ExecPolicy.Level instead of allowing everything
- browser tool rejects unknown actions instead of silently no-op'ing

Bump README to v0.3.4 and document the AgentSpec.Loop override semantics.
This commit is contained in:
sausheong committed 2026-08-02 10:14:20 +08:00
1 parent 445614757b
commit eb4fa67976
15 files changed
+421 -37

No files matched your search

+49
View File
@@ -53,8 +53,29 @@ func (s *Store) sessionPath(agentID, key string) string {
return filepath.Join(s.sessionDir(agentID), key+".jsonl")
}
// validateStoreComponent keeps caller-provided identifiers as one filesystem
// path component. Empty values remain supported for backward compatibility,
// but separators, volume names, and dot traversal are rejected.
func validateStoreComponent(kind, value string) error {
if value == "." || value == ".." || filepath.VolumeName(value) != "" ||
strings.ContainsAny(value, `/\`) {
return fmt.Errorf("invalid %s %q: must be a single path component", kind, value)
}
return nil
}
func validateStoreIDs(agentID, key string) error {
if err := validateStoreComponent("agent ID", agentID); err != nil {
return err
}
return validateStoreComponent("session key", key)
}
// Load reads a session from its JSONL file.
func (s *Store) Load(agentID, key string) (*Session, error) {
if err := validateStoreIDs(agentID, key); err != nil {
return nil, err
}
path := s.sessionPath(agentID, key)
f, err := os.Open(path)
@@ -103,6 +124,11 @@ func (s *Store) Load(agentID, key string) (*Session, error) {
func (s *Store) AppendEntry(sess *Session, entry SessionEntry) {
s.mu.Lock()
defer s.mu.Unlock()
if err := validateStoreIDs(sess.AgentID, sess.Key); err != nil {
s.markDegraded("invalid session path", err)
slog.Error("refusing to persist session outside store", "error", err)
return
}
dir := s.sessionDir(sess.AgentID)
if err := os.MkdirAll(dir, 0o755); err != nil {
@@ -135,6 +161,9 @@ func (s *Store) AppendEntry(sess *Session, entry SessionEntry) {
// Create creates an empty session file on disk so it shows up in List.
func (s *Store) Create(agentID, key string) error {
if err := validateStoreIDs(agentID, key); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
@@ -153,6 +182,9 @@ func (s *Store) Create(agentID, key string) error {
// List returns metadata for all sessions belonging to the given agent.
func (s *Store) List(agentID string) ([]SessionInfo, error) {
if err := validateStoreComponent("agent ID", agentID); err != nil {
return nil, err
}
dir := s.sessionDir(agentID)
entries, err := os.ReadDir(dir)
if err != nil {
@@ -224,6 +256,9 @@ func (s *Store) List(agentID string) ([]SessionInfo, error) {
// Exists checks whether a session file exists for the given agent and key.
func (s *Store) Exists(agentID, key string) bool {
if validateStoreIDs(agentID, key) != nil {
return false
}
path := s.sessionPath(agentID, key)
_, err := os.Stat(path)
return err == nil
@@ -231,6 +266,12 @@ func (s *Store) Exists(agentID, key string) bool {
// Rename renames a session file from oldKey to newKey.
func (s *Store) Rename(agentID, oldKey, newKey string) error {
if err := validateStoreIDs(agentID, oldKey); err != nil {
return err
}
if err := validateStoreComponent("session key", newKey); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
@@ -249,6 +290,9 @@ func (s *Store) Rename(agentID, oldKey, newKey string) error {
// Delete removes a session's JSONL file.
func (s *Store) Delete(agentID, key string) error {
if err := validateStoreIDs(agentID, key); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
@@ -264,6 +308,11 @@ func (s *Store) Delete(agentID, key string) error {
func (s *Store) Rewrite(sess *Session) {
s.mu.Lock()
defer s.mu.Unlock()
if err := validateStoreIDs(sess.AgentID, sess.Key); err != nil {
s.markDegraded("invalid session path", err)
slog.Error("refusing to rewrite session outside store", "error", err)
return
}
dir := s.sessionDir(sess.AgentID)
if err := os.MkdirAll(dir, 0o755); err != nil {