Skip to content

Commit 73945f1

Browse files
committed
fix: resolve compile issues, type safety bugs, security vulnerabilities, and logic flaws
1 parent 3d35228 commit 73945f1

6 files changed

Lines changed: 31 additions & 17 deletions

File tree

cmd/git-remote-gitant/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ func flushFetches(daemonURL, repoID string, wants []string, writer *bufio.Writer
145145
// Build pkt-line want request
146146
var pktBuf bytes.Buffer
147147
for _, want := range wants {
148-
fmt.Fprintf(&pktBuf, "%04xwant %s\n", len("want ")+len(want)+1, want)
148+
fmt.Fprintf(&pktBuf, "%04xwant %s\n", 4+len("want ")+len(want)+1, want)
149149
}
150150
pktBuf.WriteString("0000") // flush packet
151-
fmt.Fprintf(&pktBuf, "%04xdone\n", len("done\n")+4)
151+
pktBuf.WriteString("0009done\n")
152152

153153
endpoint := fmt.Sprintf("%s/api/v1/repos/%s/git-upload-pack", daemonURL, url.PathEscape(repoID))
154154
resp, err := http.Post(endpoint, "application/x-git-upload-pack-request", &pktBuf)

cmd/gitant/agents.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ var agentDelegateCmd = &cobra.Command{
100100
}
101101

102102
var result map[string]interface{}
103-
if err := client.Post("/api/v1/agents/"+did+"/delegate", req, &result); err != nil {
103+
if err := client.Post(apiPath("/api/v1/agents", did, "delegate"), req, &result); err != nil {
104104
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
105105
os.Exit(1)
106106
}

cmd/gitant/ci.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"os/exec"
7+
"strings"
68

79
"github.com/GrayCodeAI/gitant-cli/internal/cli"
810
"github.com/spf13/cobra"
@@ -179,16 +181,16 @@ func shortSHA(sha string) string {
179181
}
180182

181183
func getCurrentBranch() string {
182-
// Try to get current branch from git
183-
out, err := os.ReadFile(".git/HEAD")
184+
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
185+
out, err := cmd.Output()
184186
if err != nil {
185187
return "main"
186188
}
187-
head := string(out)
188-
if len(head) > 16 && head[:16] == "ref: refs/heads/" {
189-
return head[16 : len(head)-1]
189+
branch := strings.TrimSpace(string(out))
190+
if branch == "" || branch == "HEAD" {
191+
return "main"
190192
}
191-
return "main"
193+
return branch
192194
}
193195

194196
func init() {

cmd/gitant/gitlawb_commands.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var certListCmd = &cobra.Command{
4242
}
4343

4444
for _, c := range result.Certificates {
45-
fmt.Printf("%s\t%s\t%s→%s\t%s\t%s\n", c.ID, c.Ref, c.OldOID[:8], c.NewOID[:8], c.Signer, c.Timestamp)
45+
fmt.Printf("%s\t%s\t%s→%s\t%s\t%s\n", c.ID, c.Ref, shortSHA(c.OldOID), shortSHA(c.NewOID), c.Signer, c.Timestamp)
4646
}
4747
fmt.Fprintf(os.Stderr, "%d certificate(s)\n", result.Total)
4848
},

internal/cli/clone.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func Clone(daemonURL, repoID, localPath string) error {
3636
// For each ref, fetch the commit and all reachable objects
3737
seen := make(map[string]bool)
3838
for _, ref := range repoInfo.Refs {
39-
if err := fetchObjectRecursive(client, repoID, repo, ref.Hash, seen); err != nil {
39+
if err := fetchObjectRecursive(client, repoID, repo, ref.Hash, seen, 0); err != nil {
4040
fmt.Fprintf(Stderr(), "warning: failed to fetch %s: %v\n", ref.Hash, err)
4141
}
4242
}
@@ -55,7 +55,10 @@ func Clone(daemonURL, repoID, localPath string) error {
5555
}
5656

5757
// fetchObjectRecursive fetches an object and all its children
58-
func fetchObjectRecursive(client *Client, repoID string, repo *git.Repository, hashStr string, seen map[string]bool) error {
58+
func fetchObjectRecursive(client *Client, repoID string, repo *git.Repository, hashStr string, seen map[string]bool, depth int) error {
59+
if depth > 1000 {
60+
return fmt.Errorf("recursion depth limit exceeded")
61+
}
5962
if seen[hashStr] {
6063
return nil
6164
}
@@ -105,12 +108,12 @@ func fetchObjectRecursive(client *Client, repoID string, repo *git.Repository, h
105108
for _, line := range strings.Split(contentStr, "\n") {
106109
if strings.HasPrefix(line, "tree ") {
107110
treeHash := strings.TrimPrefix(line, "tree ")
108-
if err := fetchObjectRecursive(client, repoID, repo, treeHash, seen); err != nil {
111+
if err := fetchObjectRecursive(client, repoID, repo, treeHash, seen, depth+1); err != nil {
109112
fmt.Fprintf(Stderr(), "warning: failed to fetch tree %s: %v\n", treeHash, err)
110113
}
111114
} else if strings.HasPrefix(line, "parent ") {
112115
parentHash := strings.TrimPrefix(line, "parent ")
113-
if err := fetchObjectRecursive(client, repoID, repo, parentHash, seen); err != nil {
116+
if err := fetchObjectRecursive(client, repoID, repo, parentHash, seen, depth+1); err != nil {
114117
fmt.Fprintf(Stderr(), "warning: failed to fetch parent %s: %v\n", parentHash, err)
115118
}
116119
}
@@ -131,7 +134,7 @@ func fetchObjectRecursive(client *Client, repoID string, repo *git.Repository, h
131134
break
132135
}
133136
entryHash := fmt.Sprintf("%x", content[nullIdx+1:nullIdx+21])
134-
if err := fetchObjectRecursive(client, repoID, repo, entryHash, seen); err != nil {
137+
if err := fetchObjectRecursive(client, repoID, repo, entryHash, seen, depth+1); err != nil {
135138
fmt.Fprintf(Stderr(), "warning: failed to fetch tree entry %s: %v\n", entryHash, err)
136139
}
137140
i = nullIdx + 21
@@ -163,7 +166,7 @@ func Pull(repoPath, daemonURL, repoID string) error {
163166
// Fetch objects for new refs
164167
seen := make(map[string]bool)
165168
for _, ref := range repoInfo.Refs {
166-
if err := fetchObjectRecursive(client, repoID, repo, ref.Hash, seen); err != nil {
169+
if err := fetchObjectRecursive(client, repoID, repo, ref.Hash, seen, 0); err != nil {
167170
fmt.Fprintf(Stderr(), "warning: failed to fetch %s: %v\n", ref.Hash, err)
168171
}
169172
}

internal/cli/push.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,12 @@ func collectObjects(repo *git.Repository, hashes []plumbing.Hash) ([]GitObject,
161161
var objects []GitObject
162162

163163
for _, hash := range hashes {
164+
commit, err := repo.CommitObject(hash)
165+
if err != nil {
166+
return nil, fmt.Errorf("loading commit %s: %w", hash.String(), err)
167+
}
164168
// Walk commits
165-
commitIter := object.NewCommitPreorderIter(&object.Commit{Hash: hash}, nil, nil)
169+
commitIter := object.NewCommitPreorderIter(commit, nil, nil)
166170
commitIter.ForEach(func(c *object.Commit) error {
167171
if seen[c.Hash.String()] {
168172
return nil
@@ -243,6 +247,11 @@ func encodeObject(hash plumbing.Hash, objType string, repo *git.Repository) (Git
243247
}
244248
defer reader.Close()
245249

250+
const maxObjectSize = 50 * 1024 * 1024 // 50MB
251+
if obj.Size() > maxObjectSize {
252+
return GitObject{}, fmt.Errorf("object size %d exceeds limit of %d bytes", obj.Size(), maxObjectSize)
253+
}
254+
246255
buf := make([]byte, obj.Size())
247256
if _, err := io.ReadFull(reader, buf); err != nil {
248257
return GitObject{}, err

0 commit comments

Comments
 (0)