cli-plugins/hooks: fix max-message off-by-one - #7006
fallintoplace wants to merge 1 commit into
Conversation
|
Thank you for contributing! It looks like your commit message is missing a DCO sign-off, We require all commit messages to have a There is no need to open a new pull request, but to fix this (and make CI pass), Unfortunately, it's not possible to do so through GitHub's web UI, so this needs You can find some instructions in the output of the DCO check (which can be found Steps to do so "roughly" come down to:
Let me know if you need help or more detailed instructions! |
|
@thaJeztah Thank you. I will check later today. |
Signed-off-by: Minh Vu <vuhoangminh97@gmail.com>
a7f368f to
1ce191f
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
docker-agent
left a comment
There was a problem hiding this comment.
Assessment: 🟢 APPROVE
docker-agent
left a comment
There was a problem hiding this comment.
Assessment: 🟡 NEEDS ATTENTION
| out = b.String() | ||
| } | ||
| if n := strings.Count(out, "\n"); n > maxMessages { | ||
| if n := strings.Count(out, "\n") + 1; n > maxMessages { |
There was a problem hiding this comment.
[medium] Trailing-newline output incorrectly rejected: +1 overcounts messages when output ends with \n
The fix changes the guard from strings.Count(out, "\n") > maxMessages to strings.Count(out, "\n") + 1 > maxMessages. This is correct for output that does not end with a trailing newline, but it overcounts when output does end with \n.
Trigger path: A template that renders to exactly 10 messages each followed by a newline — e.g., "msg1\nmsg2\n...\nmsg10\n" — has 10 newline characters. The new guard computes n = 10 + 1 = 11 > 10 = maxMessages and rejects the output with the error "hook template contains too many messages (11): maximum is 10". Yet the template only produced 10 real messages; the 11th "segment" from strings.Split is just an empty string after the final \n.
Impact: Valid 10-message templates whose rendered output ends with a trailing newline (common when Go text/template templates place a literal newline after the last field) are incorrectly refused. The regression test added in this PR uses strings.Repeat("line\n", 10)+"line" (no trailing newline), so it does not catch this edge case.
Suggested fix: Trim a trailing newline before counting, so an empty final segment is not treated as an extra message:
| if n := strings.Count(out, "\n") + 1; n > maxMessages { | |
| if n := strings.Count(strings.TrimRight(out, "\n"), "\n") + 1; n > maxMessages { |
| Confidence | Score |
|---|---|
| 🟢 strong | 100/100 |
There was a problem hiding this comment.
🟢 Approval recommended
The boundary fix is correct, focused, and covered by a regression test.
Pull request overview
Fixes the hook message-limit off-by-one error by counting rendered messages rather than newline separators.
Changes:
- Rejects rendered output containing more than 10 messages.
- Adds regression coverage for the 11-message boundary.
File summaries
| File | Description |
|---|---|
cli-plugins/hooks/template.go |
Corrects message counting. |
cli-plugins/hooks/template_test.go |
Tests rejection of 11 messages. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Fixes #7005
What changed
This updates
cli-plugins/hooks.ParseTemplate()to enforce the 10-message limit on the actual number of rendered messages instead of on the number of newline separators alone.It also adds a regression test for the 11-line case.
Why it changed
The current implementation rejects only when
strings.Count(out, "\n") > maxMessagesand then returnsstrings.SplitN(out, "\n", maxMessages).That makes the guard off by one: output with 10 newline characters contains 11 messages, but is still accepted. The extra rendered line is then folded into the last returned element.
Impact
Templates that render to more than 10 messages are now rejected consistently, including the 11-line / 10-newline case.
Valid templates keep the existing split behavior.
Root cause
The limit check counted separators instead of messages.
Validation
GOPATH=$(mktemp -d) GO111MODULE=off go test github.com/docker/cli/cli-plugins/hooksvia a temporary GOPATH layout rooted at this checkout