zhufuyi / sponge

Sponge is a powerful Go development framework, it's easy to develop web, gRPC and microservice projects.
https://go-sponge.com
MIT License
1.47k stars 146 forks source link

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

Closed wxygeek closed 1 month ago

wxygeek commented 2 months ago

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。

zhufuyi commented 2 months ago

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