我有一个结构,其中注入了 XSS。为了删除它,我 json.Marshal 它,然后运行 json.HTMLEscape。然后我 json.Unmarshal 将其转换为一个新结构。
问题是新结构仍然注入了 XSS。
我根本不知道如何从结构中删除 XSS。我可以编写一个函数在现场执行此操作,但考虑到有 json.HTMLEscape 并且我们可以将其解组,它应该可以正常工作,但事实并非如此。
type Person struct {
Name string `json:"name"`
}
func main() {
var p, p2 Person
// p.Name has XSS
p.Name = "<script>alert(1)</script>"
var tBytes bytes.Buffer
// I marshal it so I can use json.HTMLEscape
marshalledJson, _ := json.Marshal(p)
json.HTMLEscape(&tBytes, marshalledJson)
// here I insert it into a new struct, sadly the p2 struct has the XSS still
err := json.Unmarshal(tBytes.Bytes(), &p2)
if err != nil {
fmt.Printf(err.Error())
}
fmt.Print(p2)
}
预期结果是 p2.Name 要像<script>alert(1)</script>