Skip to content

fix(tokens): reject path traversal in readAntigravity conversation id - #405

Open
prascli wants to merge 1 commit into
solongate:mainfrom
prascli:fix/107-antigravity-path-traversal
Open

prascli wants to merge 1 commit into
solongate:mainfrom
prascli:fix/107-antigravity-path-traversal

Conversation

@prascli

@prascli prascli commented Nov 19, 2026

Copy link
Copy Markdown

Summary

readAntigravity built the SQLite database path by directly joining an attacker-influenceable conversationID into the conversations directory. A crafted ID containing ../ (or path separators) could escape that directory and open an arbitrary SQLite file readable by the process.

This change rejects any conversationID containing path separators or .. segments, and adds a containment check (filepath.Clean + filepath.Rel) so the resolved path must remain inside the conversations directory before the file is opened.

Changes

  • packages/guard/tokens/parse.go: sanitize and contain the conversation ID path.
  • packages/guard/tokens/parse_test.go: add TestReadAntigravityRejectsTraversal.

Verification

  • cd packages/guard/tokens && go test ./... — PASS
  • go vet ./... — clean
  • gofmt -l . — no files

Related issue

Fixes #107.

Signed-off-by: Indra Prasetya <11029197+prascli@users.noreply.github.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 5 complexity · 0 duplication

Metric Results
Complexity 5
Duplication 0

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

⚠️ Resolve issues before merging

Two security issues should be fixed before merging: encoded traversal may be decoded by SQLite after validation, and symlinks can redirect an apparently contained path outside the conversations directory. The current tests also do not demonstrate rejection when an outside database actually exists.

The required independent containment-failure test scenario is missing.

Test suggestions

  • Reject IDs containing slash, backslash, or dot-dot traversal patterns.
  • Successfully read a database for a valid conversation ID.
  • Return nil when the conversation database does not exist.
  • Exercise the containment-check failure path independently.
  • Verify percent-encoded traversal cannot open a database outside the conversations directory.
  • Verify symlinked conversation database paths cannot escape the conversations directory.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Exercise the containment-check failure path independently.
2. Verify percent-encoded traversal cannot open a database outside the conversations directory.
3. Verify symlinked conversation database paths cannot escape the conversations directory.

TIP Add review instructions in .codacy/instructions/review.md, or an AGENTS.md, so the reviewer knows your conventions.
TIP How was this review? Give us feedback

}

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.

}

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tokens: readAntigravity builds a SQLite path from unsanitized conversationID, enabling path traversal

1 participant