我试图在将提交的数据编组到指定的结构体之前,尽可能净化输入。
这是我使用的模型。
type Post struct {
Id int `json:"Id"`
CreatedAt time.Time `json:"CreatedAt"`
UpdatedAt time.Time `json:"UpdatedAt"`
CreatorId int `json:"CreatorId"`
Creator *User
Editors []int `json:"Editors"`
Status Status `json:"Status"`
Title string `json:"Title"`
ShortDescription string `json:"ShortDescription"`
Description string `json:"Description"`
Content string `json:"Content"`
Url string `json:"Url"`
Media *Media
Categories []Category `json:"Categories"`
Tags []Tag `json:"Tags"`
MediaId int `json:"MediaId"`
Keywords string `json:"Keywords"`
Data []string `json:"Data"`
}
这是一个可能提交的JSON数据示例。
{"Id":1,"CreatedAt":"2016-10-11T21:29:46.134+02:00","UpdatedAt":"0001-01-01T00:00:00Z","CreatorId":1,"Editors":null,"Status":1,"Title":"This is the title of the first post, to be changed.<script>alert()</script>","ShortDescription":"this is the short description of this post","Description":"","Content":"Contrary to popular belief Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC making it over 2000 years old. Richard McClintock","Url":"lorem-ipsum-first"}
在执行ReadJSON
请求并在此过程中插入数据之前,如何最有效地对上述JSON表单数据进行消毒,从而消除任何恶意代码,就像使用<script>alert()</script>
一样。
如果有任何其他可以使用的信息,请告诉我,我很乐意添加它。
谢谢