Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak and json output #90

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion clamav-http/server/v0/scan_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ func (sh *ScanHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

c := clamd.NewClamd(sh.Address)
response, err := c.ScanStream(f, make(chan bool))
abort := make(chan bool)
defer close(abort)

response, err := c.ScanStream(f, abort)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("not okay"))
return
}
defer r.MultipartForm.RemoveAll()

result := <-response
w.WriteHeader(http.StatusOK)
Expand Down
5 changes: 4 additions & 1 deletion clamav-http/server/v0/scan_reply_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ func (srh *ScanReplyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

c := clamd.NewClamd(srh.Address)
response, err := c.ScanStream(f, make(chan bool))
abort := make(chan bool)
defer close(abort)

response, err := c.ScanStream(f, abort)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("not okay"))
return
}
defer r.MultipartForm.RemoveAll()

result := <-response
w.WriteHeader(http.StatusOK)
Expand Down
81 changes: 75 additions & 6 deletions clamav-http/server/v1alpha/docs/scan.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,96 @@
# POST /v1alpha/scan
# POST /v1alpha/scan

## Example Request
## Example Requests

### Plain text response
```
curl -s -F "name=eicar" -F "file=@test/eicar.txt" http://clamav-http/v1alpha/scan
```

## Response
### JSON response
```
curl -s -H "Accept: application/json" -F "name=eicar" -F "file=@test/eicar.txt" http://clamav-http/v1alpha/scan
```

## Responses

### File is infected

Plain text:
```
HTTP/1.1 403 Forbidden
AV Response : FOUND
```

JSON:
```
HTTP/1.1 403 Forbidden
Content-Type: application/json

{
"status": "FOUND",
"description": "Eicar-Test-Signature",
"raw": "stream: Eicar-Test-Signature FOUND",
"message": "FOUND"
}
```

### File is encrypted

Plain text:
```
HTTP/1.1 403 Forbidden
AV Response : FOUND
```

JSON:
```
HTTP/1.1 403 Forbidden
Content-Type: application/json

Everything ok : false
{
"status": "FOUND",
"description": "Heuristics.Encrypted.Zip",
"raw": "stream: Heuristics.Encrypted.Zip FOUND",
"message": "FOUND"
}
```

### File is clean

Plain text:
```
HTTP/1.1 200 OK
AV Response : OK
```

JSON:
```
HTTP/1.1 200 OK
Content-Type: application/json

{
"status": "OK",
"description": "",
"raw": "stream: OK",
"message": "OK"
}
```

### Error responses

Plain text:
```
HTTP/1.1 400 Bad Request
Invalid request: Error message here
```

Everything ok : true
```
JSON:
```
HTTP/1.1 500 Internal Server Error
Content-Type: application/json

{
"error": "Error message here"
}
```
4 changes: 3 additions & 1 deletion clamav-http/server/v1alpha/health_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ type HealthHandler struct {

func (hh *HealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c := clamd.NewClamd(hh.Address)
response, err := c.ScanStream(strings.NewReader(eicar), make(chan bool))
abort := make(chan bool)
defer close(abort)

response, err := c.ScanStream(strings.NewReader(eicar), abort)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
Expand Down
91 changes: 66 additions & 25 deletions clamav-http/server/v1alpha/scan_handler.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
package v1alpha

import (
"encoding/json"
"net/http"
"strings"

"github.com/dutchcoders/go-clamd"
"github.com/sirupsen/logrus"
)

type ScanResponse struct {
Status string `json:"status"`
Description string `json:"description"`
Raw string `json:"raw"`
Message string `json:"message"`
}

type ErrorResponse struct {
Error string `json:"error"`
}

func writeError(w http.ResponseWriter, wantJSON bool, status int, message string) {
w.WriteHeader(status)
if wantJSON {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(ErrorResponse{Error: message})
} else {
w.Write([]byte(message))
}
}

type ScanHandler struct {
Address string
Max_file_mem int64
Expand All @@ -20,63 +43,81 @@ const (
)

func (sh *ScanHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(sh.Max_file_mem * 1024 * 1024)
wantJSON := strings.Contains(r.Header.Get("Accept"), "application/json")

err := r.ParseMultipartForm(sh.Max_file_mem * 1024 * 1024)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
sh.Logger.Errorf("scan error %d: %s", scan_error_0, err.Error())
writeError(w, wantJSON, http.StatusBadRequest, "Invalid request: "+err.Error())
return
}

files := r.MultipartForm.File["file"]

if len(files) == 0 {
w.WriteHeader(http.StatusNoContent)
w.Write([]byte("empty file\n"))
writeError(w, wantJSON, http.StatusNoContent, "empty file")
return
}

f, err := files[0].Open()
defer f.Close()

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
sh.Logger.Errorf("scan error %d: %s", scan_error_1, err.Error())
writeError(w, wantJSON, http.StatusInternalServerError, err.Error())
return
}

c := clamd.NewClamd(sh.Address)
response, err := c.ScanStream(f, make(chan bool))
abort := make(chan bool)
defer close(abort)

response, err := c.ScanStream(f, abort)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
sh.Logger.Errorf("scan error %d: %s", scan_error_2, err.Error())
writeError(w, wantJSON, http.StatusInternalServerError, err.Error())
return
}
defer r.MultipartForm.RemoveAll()

result := <-response

if result.Status == "OK" {
w.WriteHeader(http.StatusOK)
var status int
var message string

switch result.Status {
case "OK":
status = http.StatusOK
message = "OK"
sh.Logger.Infof("Scanning %v: RES_OK", files[0].Filename)
w.Write([]byte("AV Response : OK\n"))
} else if result.Status == "FOUND" {
w.WriteHeader(http.StatusForbidden)
case "FOUND":
status = http.StatusForbidden
message = "FOUND"
sh.Logger.Infof("Scanning %v: RES_FOUND", files[0].Filename)
w.Write([]byte("AV Response : FOUND\n"))
} else if result.Status == "ERROR" {
w.WriteHeader(http.StatusBadRequest)
case "ERROR":
status = http.StatusBadRequest
message = "ERROR"
sh.Logger.Infof("Scanning %v: RES_ERROR", files[0].Filename)
w.Write([]byte("AV Response : ERROR\n"))
} else if result.Status == "PARSE_ERROR" {
w.WriteHeader(http.StatusPreconditionFailed)
case "PARSE_ERROR":
status = http.StatusPreconditionFailed
message = "PARSE_ERROR"
sh.Logger.Infof("Scanning %v: RES_PARSE_ERROR", files[0].Filename)
w.Write([]byte("AV Response : PARSE_ERROR\n"))
default:
status = http.StatusNotImplemented
message = "NOT_IMPLEMENTED"
}

w.WriteHeader(status)

if wantJSON {
w.Header().Set("Content-Type", "application/json")
response := ScanResponse{
Status: result.Status,
Description: result.Description,
Raw: result.Raw,
Message: message,
}
json.NewEncoder(w).Encode(response)
} else {
w.WriteHeader(http.StatusNotImplemented)
w.Write([]byte("AV Response : " + message + "\n"))
}
return
}