Add time and size limit to remote requests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent fd1f766 · Browse files · 1 file·+29 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+29 added−17 removed
- other:+29 added−17 removed
Provenance
- Commit
1a97617- Tree
047e9e4- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
fd1f766
1 file changed+29 added−17 removed
modified:src/server.go+29 added-17 removed
6 unmodified lines7 "fmt"8 "net"9 "strconv"10 "strings"11+ "time"12 )13 14 const (14- crlf = "\r\n"15- httpOk = "HTTP/1.1 200 OK" + crlf16- httpBadRequest = "HTTP/1.1 400 Bad Request" + crlf15+ crlf = "\r\n"16+ httpOk = "HTTP/1.1 200 OK" + crlf17+ httpBadRequest = "HTTP/1.1 400 Bad Request" + crlf18+ httpReadTimeout = 10 * time.Second19+ maxContentLength = 1024 * 102420 )21 22 func startHttpServer(port int, channel chan []*action) error {23 if port == 0 {30 unmodified lines54 // * No --listen: 2.8MB55 // * --listen with net/http: 5.7MB56 // * --listen w/o net/http: 3.3MB57 func handleHttpRequest(conn net.Conn, channel chan []*action) string {55- line := 056- headerRead := false58 contentLength := 059 body := ""60 bad := func(message string) string {61 message += "\n"62 return httpBadRequest + fmt.Sprintf("Content-Length: %d%s", len(message), crlf+crlf+message)63 }64+ conn.SetReadDeadline(time.Now().Add(httpReadTimeout))65 scanner := bufio.NewScanner(conn)66 scanner.Split(func(data []byte, atEOF bool) (int, []byte, error) {67 found := bytes.Index(data, []byte(crlf))68 if found >= 0 {5 unmodified lines74 }75 return 0, nil, nil76 })77 78+ section := 079 for scanner.Scan() {80 text := scanner.Text()78- if line == 0 && !strings.HasPrefix(text, "POST / HTTP") {79- return bad("invalid request method")80- }81- if text == crlf {82- headerRead = true83- }84- if !headerRead {81+ switch section {82+ case 0:83+ if !strings.HasPrefix(text, "POST / HTTP") {84+ return bad("invalid request method")85+ }86+ section++87+ case 1:88+ if text == crlf {89+ if contentLength == 0 {90+ return bad("content-length header missing")91+ }92+ section++93+ continue94+ }95 pair := strings.SplitN(text, ":", 2)96 if len(pair) == 2 && strings.ToLower(pair[0]) == "content-length" {97 length, err := strconv.Atoi(strings.TrimSpace(pair[1]))88- if err != nil {98+ if err != nil || length <= 0 || length > maxContentLength {99 return bad("invalid content length")100 }101 contentLength = length102 }93- } else if contentLength <= 0 {94- break95- } else {103+ case 2:104 body += text105 }98- line++106 }107 108+ if len(body) < contentLength {109+ return bad("incomplete request")110+ }111+ body = body[:contentLength]112+113 errorMessage := ""114 actions := parseSingleActionList(strings.Trim(string(body), "\r\n"), func(message string) {115 errorMessage = message116 })10 unmodified lines