From 39d70527aefa488de650187a31e797dcd6257080 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Sun, 28 May 2017 03:36:22 +1000 Subject: [PATCH] protect < > & for json message, prevent it from being encoded. --- kfsend.go | 13 ++++++++++++- kfsend_test.go | 6 +++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/kfsend.go b/kfsend.go index fa06059..82d86a7 100644 --- a/kfsend.go +++ b/kfsend.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "encoding/json" "fmt" "log" @@ -28,10 +29,20 @@ func kfSendTxt(user, txt string) { s.Text.Content = txt s.ToUser = user - j, _ := json.Marshal(s) + //j, _ := json.Marshal(s) // it will escape html < > and & to \u0006 + j, _ := myJSONMarshal(s) // this reserves < > and & + log.Println(string(j)) postJSON(j, u) } +func myJSONMarshal(t interface{}) ([]byte, error) { + buffer := &bytes.Buffer{} + encoder := json.NewEncoder(buffer) + encoder.SetEscapeHTML(false) + err := encoder.Encode(t) + return buffer.Bytes(), err +} + type sendPicMsg struct { ToUser string `json:"touser"` MsgType string `json:"msgtype"` diff --git a/kfsend_test.go b/kfsend_test.go index bdbbd9e..c1809bd 100644 --- a/kfsend_test.go +++ b/kfsend_test.go @@ -10,10 +10,10 @@ var toUser = "oUN420bxqFqlx0ZQHciUOesZO3PE" func TestSendTxt(t *testing.T) { SetupConfig() - msg := fmt.Sprintf("测试消息, %s ", time.Now().String()) + msg := fmt.Sprintf("测试消息 & < >, %s ", time.Now().String()) - randinit() - msg = RandStringRunes(2048) + // randinit() + // msg = RandStringRunes(2048) kfSendTxt(toUser, msg) }