Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/guard/tokens/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"

_ "modernc.org/sqlite"
)
Expand Down Expand Up @@ -245,16 +246,28 @@ func readAntigravity(conversationID string) []tokenTurn {
if conversationID == "" {
return nil
}
// Reject IDs that could escape the conversations directory: no path
// separators, no traversal segments, no absolute-path tricks.
if strings.ContainsAny(conversationID, `/\`) || strings.Contains(conversationID, "..") {
return nil
}
home, err := os.UserHomeDir()
if err != nil {
return nil
}
dbPath := filepath.Join(home, ".gemini", "antigravity-cli", "conversations", conversationID+".db")
if _, err := os.Stat(dbPath); err != nil {
baseDir := filepath.Join(home, ".gemini", "antigravity-cli", "conversations")
dbPath := filepath.Join(baseDir, conversationID+".db")
// Containment check: the resolved path must stay inside baseDir.
cleanPath := filepath.Clean(dbPath)
rel, err := filepath.Rel(baseDir, cleanPath)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

A symlink inside the conversations directory can point to a database outside that directory while still satisfying the string-based containment check. Enforce containment after resolving filesystem links, or prevent symlink traversal when opening the database, and add a symlink regression test.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return nil
}
if _, err := os.Stat(cleanPath); err != nil {
return nil
}

db, err := sql.Open("sqlite", "file:"+dbPath+"?mode=ro")
db, err := sql.Open("sqlite", "file:"+cleanPath+"?mode=ro")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 HIGH RISK

Percent-encoded traversal such as %2e%2e%2f can pass the current validation and then be decoded by SQLite when opening the URI. Construct the SQLite URI with safe path escaping or otherwise validate the decoded path, and add a regression test with an accessible database outside the conversations directory.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

if err != nil {
return nil
}
Expand Down
8 changes: 8 additions & 0 deletions packages/guard/tokens/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ func TestReadAntigravity(t *testing.T) {
}
}

func TestReadAntigravityRejectsTraversal(t *testing.T) {
for _, id := range []string{"../etc/passwd", "../../secret", "a/b", `a\b`, "a..b"} {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Strengthen these tests by creating a readable database outside the conversations directory at the traversal target and asserting that the traversal ID returns nil. This ensures the test detects an actual path escape rather than only a missing-file result.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

if got := readAntigravity(id); got != nil {
t.Errorf("readAntigravity(%q) = %v, want nil", id, got)
}
}
}

func TestReadAntigravityNotFound(t *testing.T) {
if got := readAntigravity(""); got != nil {
t.Errorf("readAntigravity(\"\") = %v, want nil", got)
Expand Down