zhufuyi/sponge

middleware.Log的默认defaultMaxLength太短会导致问题

wxygeek opened this issue · 1 comments

Describe the bug
middleware.Log的默认defaultMaxLength太短,如果request body过长,在后续的ShouldBindJSON中会出现解析错误

More description
传入超过一定长度的复杂request body,并且使用middleware.Log
在后续的ShouldBindJSON过程中会出现错误ShouldBindJSON error: {"error": "invalid character '.' looking for beginning of object key string", "request_id": "u2Nrwro8wr"}
如果把defaultMaxLength修改变大,则不会报错。

问题的原因是:

// If there is sensitive information in the body, you can use WithIgnoreRoutes set the route to ignore logging
func getBodyData(buf *bytes.Buffer, maxLen int) []byte {
	l := buf.Len()
	if l == 0 {
		return []byte("")
	} else if l <= maxLen {
		return buf.Bytes()[:l-1]
	}
	return append(buf.Bytes()[:maxLen], contentMark...)
}

如果l <= maxLen的情况,getBodyData中的append对原buf进行了修改,
在后续c.Request.Body = io.NopCloser(&buf)会传入修改后的错误的request body。

只有 sponge v1.9.2 版本有这个问题,在 sponge v1.9.3 已修复。