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

bugfix: fix multipart panic + support for filename + content-type #5702

Merged
merged 2 commits into from
Oct 10, 2024
Merged
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
44 changes: 43 additions & 1 deletion pkg/fuzz/dataformat/multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ import (
"io"
"mime"
"mime/multipart"
"net/textproto"

mapsutil "github.com/projectdiscovery/utils/maps"
)

type MultiPartForm struct {
boundary string
boundary string
filesMetadata map[string]FileMetadata
}

type FileMetadata struct {
ContentType string
Filename string
}

var (
Expand Down Expand Up @@ -41,11 +48,40 @@ func (m *MultiPartForm) Encode(data KV) (string, error) {
data.Iterate(func(key string, value any) bool {
var fw io.Writer
var err error

if filesArray, ok := value.([]interface{}); ok {
fileMetadata, ok := m.filesMetadata[key]
if !ok {
Itererr = fmt.Errorf("file metadata not found for key %s", key)
return false
}

for _, file := range filesArray {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name=%q; filename=%q`,
key, fileMetadata.Filename))
h.Set("Content-Type", fileMetadata.ContentType)

if fw, err = w.CreatePart(h); err != nil {
Itererr = err
return false
}

if _, err = fw.Write([]byte(file.(string))); err != nil {
Itererr = err
return false
}
}
return true
}

// Add field
if fw, err = w.CreateFormField(key); err != nil {
Itererr = err
return false
}

if _, err = fw.Write([]byte(value.(string))); err != nil {
Itererr = err
return false
Expand Down Expand Up @@ -98,6 +134,7 @@ func (m *MultiPartForm) Decode(data string) (KV, error) {
result.Set(key, values[0])
}
}
m.filesMetadata = make(map[string]FileMetadata)
for key, files := range form.File {
fileContents := []interface{}{}
for _, fileHeader := range files {
Expand All @@ -112,6 +149,11 @@ func (m *MultiPartForm) Decode(data string) (KV, error) {
return KV{}, err
}
fileContents = append(fileContents, buffer.String())

m.filesMetadata[key] = FileMetadata{
ContentType: fileHeader.Header.Get("Content-Type"),
Filename: fileHeader.Filename,
}
}
result.Set(key, fileContents)
}
Expand Down
Loading