Skip to content

Commit

Permalink
api: restrict persist-file to only accept JSON data (#3969) (#3970)
Browse files Browse the repository at this point in the history
* This is an automated cherry-pick of #3969

Signed-off-by: ti-chi-bot <[email protected]>

* resolve conflict

Signed-off-by: nolouch <[email protected]>

Co-authored-by: disksing <[email protected]>
Co-authored-by: ShuNing <[email protected]>
  • Loading branch information
3 people authored Sep 7, 2021
1 parent 38aaf9a commit d45165b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion server/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package api

import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
Expand Down Expand Up @@ -95,14 +96,18 @@ func (h *adminHandler) ResetTS(w http.ResponseWriter, r *http.Request) {
}

// Intentionally no swagger mark as it is supposed to be only used in
// server-to-server.
// server-to-server. For security reason, it only accepts JSON formatted data.
func (h *adminHandler) persistFile(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
h.rd.Text(w, http.StatusInternalServerError, "")
return
}
defer r.Body.Close()
if !json.Valid(data) {
h.rd.Text(w, http.StatusBadRequest, "body should be json format")
return
}
err = h.svr.PersistFile(mux.Vars(r)["file_name"], data)
if err != nil {
h.rd.Text(w, http.StatusInternalServerError, err.Error())
Expand Down
9 changes: 9 additions & 0 deletions server/api/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ func (s *testAdminSuite) TestDropRegion(c *C) {
c.Assert(region.GetRegionEpoch().Version, Equals, uint64(50))
}

func (s *testAdminSuite) TestPersistFile(c *C) {
data := []byte("#!/bin/sh\nrm -rf /")
err := postJSON(testDialClient, s.urlPrefix+"/admin/persist-file/fun.sh", data)
c.Assert(err, NotNil)
data = []byte(`{"foo":"bar"}`)
err = postJSON(testDialClient, s.urlPrefix+"/admin/persist-file/good.json", data)
c.Assert(err, IsNil)
}

var _ = Suite(&testTSOSuite{})

type testTSOSuite struct {
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,7 @@ func (s *Server) reloadConfigFromKV() error {

// ReplicateFileToAllMembers is used to synchronize state among all members.
// Each member will write `data` to a local file named `name`.
// For security reason, data should be in JSON format.
func (s *Server) ReplicateFileToAllMembers(ctx context.Context, name string, data []byte) error {
resp, err := s.GetMembers(ctx, nil)
if err != nil {
Expand Down

0 comments on commit d45165b

Please sign in to comment.