mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-07 06:40:54 +03:00
EventManager: improve automatic detection of JSON body
Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
@@ -55,6 +55,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
ipBlockedEventName = "IP Blocked"
|
ipBlockedEventName = "IP Blocked"
|
||||||
maxAttachmentsSize = int64(10 * 1024 * 1024)
|
maxAttachmentsSize = int64(10 * 1024 * 1024)
|
||||||
|
objDataPlaceholder = "{{ObjectData}}"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Supported IDP login events
|
// Supported IDP login events
|
||||||
@@ -776,11 +777,11 @@ func (p *EventParams) getStringReplacements(addObjectData, jsonEscaped bool) []s
|
|||||||
} else {
|
} else {
|
||||||
replacements = append(replacements, "{{ErrorString}}", "")
|
replacements = append(replacements, "{{ErrorString}}", "")
|
||||||
}
|
}
|
||||||
replacements = append(replacements, "{{ObjectData}}", "")
|
replacements = append(replacements, objDataPlaceholder, "")
|
||||||
if addObjectData {
|
if addObjectData {
|
||||||
data, err := p.Object.RenderAsJSON(p.Event != operationDelete)
|
data, err := p.Object.RenderAsJSON(p.Event != operationDelete)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
replacements[len(replacements)-1] = string(data)
|
replacements[len(replacements)-1] = p.getStringReplacement(string(data), jsonEscaped)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if p.IDPCustomFields != nil {
|
if p.IDPCustomFields != nil {
|
||||||
@@ -1202,7 +1203,7 @@ func writeHTTPPart(m *multipart.Writer, part dataprovider.HTTPPart, h textproto.
|
|||||||
}
|
}
|
||||||
if part.Body != "" {
|
if part.Body != "" {
|
||||||
cType := h.Get("Content-Type")
|
cType := h.Get("Content-Type")
|
||||||
if strings.Contains(strings.ToLower(cType), "application/json") {
|
if part.Body != objDataPlaceholder && strings.Contains(strings.ToLower(cType), "application/json") {
|
||||||
replacements := params.getStringReplacements(addObjectData, true)
|
replacements := params.getStringReplacements(addObjectData, true)
|
||||||
jsonReplacer := strings.NewReplacer(replacements...)
|
jsonReplacer := strings.NewReplacer(replacements...)
|
||||||
_, err = partWriter.Write([]byte(replaceWithReplacer(part.Body, jsonReplacer)))
|
_, err = partWriter.Write([]byte(replaceWithReplacer(part.Body, jsonReplacer)))
|
||||||
@@ -1235,6 +1236,10 @@ func writeHTTPPart(m *multipart.Writer, part dataprovider.HTTPPart, h textproto.
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func jsonEscapeRuleActionBody(c *dataprovider.EventActionHTTPConfig) bool {
|
||||||
|
return c.Body != objDataPlaceholder && c.HasJSONBody()
|
||||||
|
}
|
||||||
|
|
||||||
func getHTTPRuleActionBody(c *dataprovider.EventActionHTTPConfig, replacer *strings.Replacer,
|
func getHTTPRuleActionBody(c *dataprovider.EventActionHTTPConfig, replacer *strings.Replacer,
|
||||||
cancel context.CancelFunc, user dataprovider.User, params *EventParams, addObjectData bool,
|
cancel context.CancelFunc, user dataprovider.User, params *EventParams, addObjectData bool,
|
||||||
) (io.Reader, string, error) {
|
) (io.Reader, string, error) {
|
||||||
@@ -1250,7 +1255,7 @@ func getHTTPRuleActionBody(c *dataprovider.EventActionHTTPConfig, replacer *stri
|
|||||||
}
|
}
|
||||||
return bytes.NewBuffer(data), "", nil
|
return bytes.NewBuffer(data), "", nil
|
||||||
}
|
}
|
||||||
if c.HasJSONBody() {
|
if jsonEscapeRuleActionBody(c) {
|
||||||
replacements := params.getStringReplacements(addObjectData, true)
|
replacements := params.getStringReplacements(addObjectData, true)
|
||||||
jsonReplacer := strings.NewReplacer(replacements...)
|
jsonReplacer := strings.NewReplacer(replacements...)
|
||||||
return bytes.NewBufferString(replaceWithReplacer(c.Body, jsonReplacer)), "", nil
|
return bytes.NewBufferString(replaceWithReplacer(c.Body, jsonReplacer)), "", nil
|
||||||
@@ -1396,7 +1401,7 @@ func executeCommandRuleAction(c dataprovider.EventActionCommandConfig, params *E
|
|||||||
addObjectData := false
|
addObjectData := false
|
||||||
if params.Object != nil {
|
if params.Object != nil {
|
||||||
for _, k := range c.EnvVars {
|
for _, k := range c.EnvVars {
|
||||||
if strings.Contains(k.Value, "{{ObjectData}}") {
|
if strings.Contains(k.Value, objDataPlaceholder) {
|
||||||
addObjectData = true
|
addObjectData = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -1431,7 +1436,7 @@ func executeCommandRuleAction(c dataprovider.EventActionCommandConfig, params *E
|
|||||||
func executeEmailRuleAction(c dataprovider.EventActionEmailConfig, params *EventParams) error {
|
func executeEmailRuleAction(c dataprovider.EventActionEmailConfig, params *EventParams) error {
|
||||||
addObjectData := false
|
addObjectData := false
|
||||||
if params.Object != nil {
|
if params.Object != nil {
|
||||||
if strings.Contains(c.Body, "{{ObjectData}}") {
|
if strings.Contains(c.Body, objDataPlaceholder) {
|
||||||
addObjectData = true
|
addObjectData = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -708,7 +708,7 @@ func TestEventRuleActions(t *testing.T) {
|
|||||||
HTTPConfig: dataprovider.EventActionHTTPConfig{
|
HTTPConfig: dataprovider.EventActionHTTPConfig{
|
||||||
Endpoint: "http://foo\x7f.com/", // invalid URL
|
Endpoint: "http://foo\x7f.com/", // invalid URL
|
||||||
SkipTLSVerify: true,
|
SkipTLSVerify: true,
|
||||||
Body: "{{ObjectData}}",
|
Body: `"data": "{{ObjectData}}"`,
|
||||||
Method: http.MethodPost,
|
Method: http.MethodPost,
|
||||||
QueryParameters: []dataprovider.KeyValue{
|
QueryParameters: []dataprovider.KeyValue{
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user