1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 18:15:42 +01:00

Enforce mime type 'application/json' when uploading advisories to the provider.

This commit is contained in:
Sascha L. Teichmann 2023-02-01 00:32:30 +01:00
parent e998133429
commit 7cc37bd9fc
2 changed files with 23 additions and 1 deletions

View file

@ -42,6 +42,11 @@ func (c *controller) loadCSAF(r *http.Request) (string, []byte, error) {
} }
defer file.Close() defer file.Close()
// We reject everything which is not announced as JSON.
if handler.Header.Get("Content-Type") != "application/json" {
return "", nil, errors.New("expected content type 'application/json'")
}
if !util.ConfirmingFileName(handler.Filename) { if !util.ConfirmingFileName(handler.Filename) {
return "", nil, errors.New("given csaf filename is not confirming") return "", nil, errors.New("given csaf filename is not confirming")
} }

View file

@ -19,6 +19,7 @@ import (
"log" "log"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"net/textproto"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -206,6 +207,19 @@ func (p *processor) create() error {
return nil return nil
} }
var escapeQuotes = strings.NewReplacer("\\", "\\\\", `"`, "\\\"").Replace
// createFromFile creates an [io.Writer] like [mime/multipart.Writer.CreateFromFile].
// This version allows to set the mime type, too.
func createFromFile(w *multipart.Writer, fieldname, filename, mimeType string) (io.Writer, error) {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
escapeQuotes(fieldname), escapeQuotes(filename)))
h.Set("Content-Type", mimeType)
return w.CreatePart(h)
}
// uploadRequest creates the request for uploading a csaf document by passing the filename. // uploadRequest creates the request for uploading a csaf document by passing the filename.
// According to the flags values the multipart sections of the request are established. // According to the flags values the multipart sections of the request are established.
// It returns the created http request. // It returns the created http request.
@ -233,7 +247,10 @@ func (p *processor) uploadRequest(filename string) (*http.Request, error) {
body := new(bytes.Buffer) body := new(bytes.Buffer)
writer := multipart.NewWriter(body) writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("csaf", filepath.Base(filename)) // As the csaf_provider only accepts uploads with mime type
// "application/json" we have to set this.
part, err := createFromFile(
writer, "csaf", filepath.Base(filename), "application/json")
if err != nil { if err != nil {
return nil, err return nil, err
} }