Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wxygeek opened this issue Sep 6, 2024 · 1 comment
Open

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

wxygeek opened this issue Sep 6, 2024 · 1 comment

Comments

@wxygeek
Copy link
Contributor

wxygeek commented Sep 6, 2024

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
Copy link
Owner

zhufuyi commented Sep 6, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants