From 0b4fefd0bab8a99b0d20c2eee0abb90b537f6e03 Mon Sep 17 00:00:00 2001 From: Solderpunk Date: Sun, 24 Nov 2019 21:05:32 +0200 Subject: [PATCH] Terminate CGI processes after 10 seconds. --- handler.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/handler.go b/handler.go index 1bd5012..7df49dc 100644 --- a/handler.go +++ b/handler.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "context" "fmt" "io" "io/ioutil" @@ -124,7 +125,9 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry) return // If this file is executable, get dynamic content } else if info.Mode().Perm() & 0111 == 0111 { - cmd := exec.Command(path) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, path) stdin, err := cmd.StdinPipe() if err != nil { conn.Write([]byte("42 CGI error!\r\n")) @@ -134,7 +137,12 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry) defer stdin.Close() io.WriteString(stdin, URL.String()) io.WriteString(stdin, "\r\n") - out, err := cmd.CombinedOutput() + out, err := cmd.Output() + if ctx.Err() == context.DeadlineExceeded { + conn.Write([]byte("42 CGI process timed out!\r\n")) + log.Status = 42 + return + } if err != nil { conn.Write([]byte("42 CGI error!\r\n")) log.Status = 42