Repository navigation
fix: cut captured bodies and skill file text on a rune boundary - #5347
shunxinyi-ljs wants to merge 3 commits into
Conversation
The access-log capture sliced request and response bodies at a byte offset, and the admin skill browser sliced text files at 1 MiB. Chinese content is three bytes per character, so the last character was cut in half and reached clients and logs as U+FFFD. Route all three cuts through common.TruncateUTF8Bytes, which keeps the same byte budget and backs off to the previous rune start.
lyingbug
left a comment
There was a problem hiding this comment.
The Go Lint rerun passed. I reproduced two issues with this change. Please preserve the response log prefix across multiple writes and keep the skill preview allocation within its 1 MiB budget. The inline comments include the reproductions and suggested fixes.
| for end > 0 && !utf8.RuneStart(b[end]) { | ||
| end-- | ||
| } | ||
| r.body.Write(b[:end]) |
There was a problem hiding this comment.
Keep the captured bytes contiguous across writes. Write 10,239 x bytes followed by 界 in one Write, then write Z in a second call. The first call drops 界 at the 10 KiB limit, but the second fills the remaining byte with Z. The logged body therefore skips a character. If the first write instead ends after the first byte of 界 and the second contains the rest and Z, the capture ends in invalid UTF-8. Both layouts send exactly the same response and should log just the 10,239 x bytes followed by the truncation marker.
Please capture up to maxBodySize+1 consecutive bytes across writes and apply TruncateUTF8Bytes once when formatting the log, as the request path does. Please add a regression test for both write layouts.
| } | ||
| if len(body) > skillFileTextLimit { | ||
| out.Content = string(body[:skillFileTextLimit]) | ||
| out.Content = common.TruncateUTF8Bytes(string(body), skillFileTextLimit) |
There was a problem hiding this comment.
Bound the allocation to the preview size. string(body) copies the entire file before TruncateUTF8Bytes takes a substring. For a supported 32 MiB text file, the preview still returns only 1 MiB, but I measured 33,554,624 B/op with this change versus 1,048,656 B/op on main for the same input.
Please find the UTF-8 boundary at or before skillFileTextLimit in the original byte slice, then convert only body[:end] to a string. That keeps the existing multibyte test passing and the allocation proportional to the preview.
The response writer cut each Write call to a rune boundary, so bytes dropped in one call could not be restored by the next: the same response logged differently depending on how the handler split it, and a rune split between calls left invalid UTF-8. Capture maxBodySize+1 contiguous bytes and truncate once when the log line is built, matching the request path. Also cut the skill file preview in the original byte slice. Converting the whole file to a string first copied all 32 MiB of a large text file for a 1 MiB preview. Tested two write layouts that send the same bytes: the preview now keeps the same 10,239 bytes before the marker in both. The preview allocation on a 32 MiB text file stays around the 1 MiB cap.
|
Both points are fixed in c2f2276. Contiguous response capture.
Preview allocation. The preview now finds the UTF-8 boundary at or before
Verification: |
e1acb40 to
ea19785
Compare
Description
Three text previews were still cut at a byte offset:
request_body/response_bodyare capped at 10 KiB ininternal/middleware/logger.go. The request preview slicedbodyBytes[:maxBodySize], and the response writer sliced the chunk it kept.projectSkillFileContentsliced text files atskillFileTextLimit(1 MiB).Chinese text is three bytes per character, so each cut landed inside a character. The skill file content is returned to the settings drawer inside a JSON string and the access log goes through
SanitizeForLog, so the half character reached users and logs as U+FFFD.All three cuts now go through the existing
common.TruncateUTF8Bytes, which keeps the byte budget and backs off to the previous rune start. The response writer also records whether it dropped bytes, because after a rune-safe cutlen(body) == maxBodySizeno longer means "truncated".Type of Change
Related Issue
None.
Testing
Branches are rebased onto
cbdcebab; the two production files are unchanged since26723dca.go test ./internal/middleware ./internal/application/service -count=1— pass (service package takes about 135 s).mainand rerunning the new tests fails:TestReadRequestBodyKeepsMultiByteCharactersAtTheLimit:request body preview is not valid UTF-8 (len=10270)TestLoggerKeepsMultiByteCharactersInResponseBody:access log contains a replacement characterTestProjectSkillFileContentKeepsMultiByteCharactersAtTheTextLimit:truncated content must stay valid UTF-8gofmt -landgo veton both packages: clean.Checklist
git diff --check origin/main...HEADpasses(full
go build ./...needs a cgo vector-store build this machine cannot complete; package-scoped checks were used instead)