Skip to content

Commit

Permalink
api: restrict persist-file to only accept JSON data (#3969)
Browse files Browse the repository at this point in the history
Signed-off-by: disksing <i@disksing.com>
  • Loading branch information
disksing authored Aug 12, 2021
1 parent 87f41e4 commit bf39734
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"
"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 := io.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 @@ -88,6 +88,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 @@ -1328,6 +1328,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 bf39734

Please sign in to comment.