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

[MM-53425]: Added additional checks for POST type APIs #209

Merged
merged 7 commits into from
Sep 14, 2023
54 changes: 54 additions & 0 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ func (p *Plugin) handleTelemetry(w http.ResponseWriter, r *http.Request) {
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
r.Body.Close()

if telemetryRequest == nil {
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
p.API.LogError("Invalid request body")
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", errors.New("invalid request body"))
return
}

if telemetryRequest.Event != "" {
p.trackFrontend(userID, telemetryRequest.Event, telemetryRequest.Properties)
Expand Down Expand Up @@ -180,9 +187,16 @@ func (p *Plugin) handleAdd(w http.ResponseWriter, r *http.Request) {
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
r.Body.Close()

senderName := p.listManager.GetUserName(userID)

if addRequest == nil {
p.API.LogError("Invalid request body")
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", errors.New("invalid request body"))
return
}

if addRequest.SendTo == "" {
_, err = p.listManager.AddIssue(userID, addRequest.Message, addRequest.Description, addRequest.PostID)
if err != nil {
Expand Down Expand Up @@ -349,6 +363,12 @@ func (p *Plugin) handleEdit(w http.ResponseWriter, r *http.Request) {
}
r.Body.Close()

if editRequest == nil {
p.API.LogError("Invalid request body")
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", errors.New("invalid request body"))
return
}

foreignUserID, list, oldMessage, err := p.listManager.EditIssue(userID, editRequest.ID, editRequest.Message, editRequest.Description)
if err != nil {
p.API.LogError("Unable to edit message: err=" + err.Error())
Expand Down Expand Up @@ -395,6 +415,12 @@ func (p *Plugin) handleChangeAssignment(w http.ResponseWriter, r *http.Request)
}
r.Body.Close()
hanzei marked this conversation as resolved.
Show resolved Hide resolved

if changeRequest == nil {
p.API.LogError("Invalid request body")
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", errors.New("invalid request body"))
return
}

if changeRequest.SendTo == "" {
http.Error(w, "No user specified", http.StatusBadRequest)
return
Expand Down Expand Up @@ -449,6 +475,13 @@ func (p *Plugin) handleAccept(w http.ResponseWriter, r *http.Request) {
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
r.Body.Close()

if acceptRequest == nil {
p.API.LogError("Invalid request body")
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", errors.New("invalid request body"))
return
}

todoMessage, sender, err := p.listManager.AcceptIssue(userID, acceptRequest.ID)
if err != nil {
Expand Down Expand Up @@ -485,6 +518,13 @@ func (p *Plugin) handleComplete(w http.ResponseWriter, r *http.Request) {
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
r.Body.Close()

if completeRequest == nil {
p.API.LogError("Invalid request body")
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", errors.New("invalid request body"))
return
}

issue, foreignID, listToUpdate, err := p.listManager.CompleteIssue(userID, completeRequest.ID)
if err != nil {
Expand Down Expand Up @@ -530,6 +570,13 @@ func (p *Plugin) handleRemove(w http.ResponseWriter, r *http.Request) {
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
r.Body.Close()

if removeRequest == nil {
p.API.LogError("Invalid request body")
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", errors.New("invalid request body"))
return
}

issue, foreignID, isSender, listToUpdate, err := p.listManager.RemoveIssue(userID, removeRequest.ID)
if err != nil {
Expand Down Expand Up @@ -581,6 +628,13 @@ func (p *Plugin) handleBump(w http.ResponseWriter, r *http.Request) {
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
r.Body.Close()

if bumpRequest == nil {
p.API.LogError("Invalid request body")
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", errors.New("invalid request body"))
return
}

todoMessage, foreignUser, foreignIssueID, err := p.listManager.BumpIssue(userID, bumpRequest.ID)
if err != nil {
Expand Down