Repository navigation
fix(tokens): reject path traversal in readAntigravity conversation id #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import ( | |
| "os" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| _ "modernc.org/sqlite" | ||
| ) | ||
|
|
@@ -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) | ||
| 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") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 HIGH RISK Percent-encoded traversal such as
|
||
| if err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"} { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
|
||
| 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) | ||
|
|
||
There was a problem hiding this comment.
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.
TIPReact 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.