aboutsummaryrefslogtreecommitdiff
path: root/git_hooks_handle.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-02-18 23:02:53 +0800
committerRunxi Yu <me@runxiyu.org>2025-02-18 23:02:53 +0800
commitca4ac52de24371172d129db48e780ac3d87b05a4 (patch)
tree5b2ff812a10889c1354bf92967d9621925927cba /git_hooks_handle.go
parenthttp: Refactor more handler code duplication into the router (diff)
downloadforge-ca4ac52de24371172d129db48e780ac3d87b05a4.tar.gz
forge-ca4ac52de24371172d129db48e780ac3d87b05a4.tar.zst
forge-ca4ac52de24371172d129db48e780ac3d87b05a4.zip
hooks: Check error on conn.Write
Diffstat (limited to 'git_hooks_handle.go')
-rw-r--r--git_hooks_handle.go48
1 files changed, 36 insertions, 12 deletions
diff --git a/git_hooks_handle.go b/git_hooks_handle.go
index 841c8d7..00e2627 100644
--- a/git_hooks_handle.go
+++ b/git_hooks_handle.go
@@ -27,12 +27,16 @@ func hooks_handle_connection(conn net.Conn) {
// another user.
ucred, err := get_ucred(conn)
if err != nil {
- conn.Write([]byte{1})
+ if _, err := conn.Write([]byte{1}); err != nil {
+ return
+ }
fmt.Fprintln(conn, "Unable to get peer credentials:", err.Error())
return
}
if ucred.Uid != uint32(os.Getuid()) {
- conn.Write([]byte{1})
+ if _, err := conn.Write([]byte{1}); err != nil {
+ return
+ }
fmt.Fprintln(conn, "UID mismatch")
return
}
@@ -40,14 +44,18 @@ func hooks_handle_connection(conn net.Conn) {
cookie := make([]byte, 64)
_, err = conn.Read(cookie)
if err != nil {
- conn.Write([]byte{1})
+ if _, err := conn.Write([]byte{1}); err != nil {
+ return
+ }
fmt.Fprintln(conn, "Failed to read cookie:", err.Error())
return
}
pack_to_hook, ok := pack_to_hook_by_cookie.Load(string(cookie))
if !ok {
- conn.Write([]byte{1})
+ if _, err := conn.Write([]byte{1}); err != nil {
+ return
+ }
fmt.Fprintln(conn, "Invalid handler cookie")
return
}
@@ -55,7 +63,9 @@ func hooks_handle_connection(conn net.Conn) {
var argc64 uint64
err = binary.Read(conn, binary.NativeEndian, &argc64)
if err != nil {
- conn.Write([]byte{1})
+ if _, err := conn.Write([]byte{1}); err != nil {
+ return
+ }
fmt.Fprintln(conn, "Failed to read argc:", err.Error())
return
}
@@ -66,7 +76,9 @@ func hooks_handle_connection(conn net.Conn) {
b := make([]byte, 1)
n, err := conn.Read(b)
if err != nil || n != 1 {
- conn.Write([]byte{1})
+ if _, err := conn.Write([]byte{1}); err != nil {
+ return
+ }
fmt.Fprintln(conn, "Failed to read arg:", err.Error())
return
}
@@ -87,7 +99,9 @@ func hooks_handle_connection(conn net.Conn) {
switch filepath.Base(args[0]) {
case "pre-receive":
if pack_to_hook.direct_access {
- conn.Write([]byte{0})
+ if _, err := conn.Write([]byte{0}); err != nil {
+ return
+ }
} else {
ref_ok := make(map[string]uint8)
// 0 for ok
@@ -103,14 +117,18 @@ func hooks_handle_connection(conn net.Conn) {
old_oid, rest, found := strings.Cut(line, " ")
if !found {
- conn.Write([]byte{1})
+ if _, err := conn.Write([]byte{1}); err != nil {
+ return
+ }
fmt.Fprintln(conn, "Invalid pre-receive line:", line)
break
}
new_oid, ref_name, found := strings.Cut(rest, " ")
if !found {
- conn.Write([]byte{1})
+ if _, err := conn.Write([]byte{1}); err != nil {
+ return
+ }
fmt.Fprintln(conn, "Invalid pre-receive line:", line)
break
}
@@ -129,10 +147,14 @@ func hooks_handle_connection(conn net.Conn) {
}
if or_all_in_map(ref_ok) == 0 {
- conn.Write([]byte{0})
+ if _, err := conn.Write([]byte{0}); err != nil {
+ return
+ }
fmt.Fprintln(conn, "Stuff")
} else {
- conn.Write([]byte{1})
+ if _, err := conn.Write([]byte{1}); err != nil {
+ return
+ }
for ref, status := range ref_ok {
switch status {
case 0:
@@ -148,7 +170,9 @@ func hooks_handle_connection(conn net.Conn) {
}
}
default:
- conn.Write([]byte{1})
+ if _, err := conn.Write([]byte{1}); err != nil {
+ return
+ }
fmt.Fprintln(conn, "Invalid hook:", args[0])
}
}