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

Less restrictive checks in MTOMDecoder #230

Merged
merged 5 commits into from
Aug 13, 2023
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
50 changes: 33 additions & 17 deletions soap/MTOMEncoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,33 @@ func (e *mtomEncoder) Flush() error {
func getBinaryFields(data interface{}, fields *[]reflect.Value) {
v := reflect.Indirect(reflect.ValueOf(data))

if v.Kind() != reflect.Struct {
return
}
for i := 0; i < v.NumField(); i++ {
if !v.Field(i).CanInterface() {
continue
switch v.Kind() {
case reflect.Ptr:
getBinaryFields(v.Elem(), fields)
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
if !v.Field(i).CanInterface() {
continue
}
f := v.Field(i)
if _, ok := f.Interface().(*Binary); ok {
*fields = append(*fields, f)
} else {
getBinaryFields(f.Interface(), fields)
}
}
f := v.Field(i)
if _, ok := f.Interface().(*Binary); ok {
*fields = append(*fields, f)
} else {
getBinaryFields(f.Interface(), fields)
break
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
e := v.Index(i)
if !e.CanInterface() {
continue
}
if _, ok := e.Interface().(*Binary); ok {
*fields = append(*fields, v)
} else {
getBinaryFields(e.Interface(), fields)
}
}
}
}
Expand Down Expand Up @@ -202,8 +217,8 @@ func getMtomHeader(contentType string) (string, error) {
}

startInfo, ok := params["start-info"]
if !ok || startInfo != "application/soap+xml" {
return "", fmt.Errorf(`Expected param start-info="application/soap+xml", got %s`, startInfo)
if !ok || (!strings.Contains(startInfo, "application/soap+xml") && !strings.Contains(startInfo, "text/xml")) {
return "", fmt.Errorf(`Expected param start-info to contain "application/soap+xml" or "text/xml", got %s`, startInfo)
}
return boundary, nil
}
Expand All @@ -218,9 +233,6 @@ func newMtomDecoder(r io.Reader, boundary string) *mtomDecoder {
}

func (d *mtomDecoder) Decode(v interface{}) error {
fields := make([]reflect.Value, 0)
getBinaryFields(v, &fields)

packages := make(map[string]*Binary, 0)
for {
p, err := d.reader.NextPart()
Expand All @@ -231,7 +243,7 @@ func (d *mtomDecoder) Decode(v interface{}) error {
return err
}
contentType := p.Header.Get("Content-Type")
if contentType == "application/xop+xml" {
if strings.HasPrefix(contentType, "application/xop+xml") {
err := xml.NewDecoder(p).Decode(v)
if err != nil {
return err
Expand All @@ -254,6 +266,10 @@ func (d *mtomDecoder) Decode(v interface{}) error {
}
}

// Get binary fields after reading the structure, so I have all fields mapped
fields := make([]reflect.Value, 0)
getBinaryFields(v, &fields)

// Set binary fields with correct content
for _, f := range fields {
b := f.Interface().(*Binary)
Expand Down