fix(doc): reject unsafe str_replace code spans#1818
Conversation
📝 WalkthroughWalkthroughAdds validation to the v2 docs update path that rejects Changesstr_replace punctuation-only inline code validation
Estimated code review effort: 2 (Simple) | ~15 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@shortcuts/doc/docs_update_v2.go`:
- Around line 133-142: The validation in validateStrReplaceInlineCodeContent is
mixing the failure message with recovery guidance; keep the error text focused
on the punctuation-only <code> content issue and move the “fetch block ids and
use --command block_replace” guidance into .WithHint(...), while leaving
.WithParam("--content") unchanged. Update the errs.NewValidationError call in
validateStrReplaceInlineCodeContent so the main message stays concise and the
hint carries the remediation steps.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: fc454a54-c1c2-46ca-9c7f-3ecc0244d18e
📒 Files selected for processing (4)
shortcuts/doc/docs_update_test.goshortcuts/doc/docs_update_v2.goskills/lark-doc/references/lark-doc-update.mdtests/cli_e2e/docs/docs_update_dryrun_test.go
| func validateStrReplaceInlineCodeContent(format, content string) error { | ||
| if strings.TrimSpace(format) != "xml" || content == "" { | ||
| return nil | ||
| } | ||
| code, ok := firstPunctuationOnlyInlineCode(content) | ||
| if !ok { | ||
| return nil | ||
| } | ||
| return errs.NewValidationError(errs.SubtypeInvalidArgument, "docs_ai str_replace can strip punctuation-only <code> content %q; fetch block ids and use --command block_replace for this rich-text edit", code).WithParam("--content") | ||
| } |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Search for WithHint method definition in the errs package
rg -n 'func.*WithHint' --type go internal/errs/ || rg -n 'WithHint' --type go internal/errs/Repository: larksuite/cli
Length of output: 261
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Locate the relevant file and inspect the surrounding code.
git ls-files 'shortcuts/doc/docs_update_v2.go' '*/errs/*.go' '*/errs/**/*.go' | sed -n '1,200p'
echo '--- docs_update_v2.go (context) ---'
nl -ba shortcuts/doc/docs_update_v2.go | sed -n '120,155p'
echo '--- search for WithHint / error builder methods ---'
rg -n 'WithHint|type .*ValidationError|NewValidationError|WithParam\(' . --type go
echo '--- outline candidate error package files ---'
fd -a 'errs' . || trueRepository: larksuite/cli
Length of output: 259
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo '--- target file context ---'
cat -n shortcuts/doc/docs_update_v2.go | sed -n '125,150p'
echo '--- locate error builder package files ---'
fd -a 'errs' . || true
fd -a 'error' . | sed -n '1,120p' || true
echo '--- search for WithHint / NewValidationError / WithParam ---'
rg -n 'WithHint|NewValidationError|WithParam\(' . --type go
echo '--- inspect matching files with line numbers ---'
for f in $(rg -l 'WithHint|NewValidationError|WithParam\(' . --type go); do
echo "### $f"
cat -n "$f" | sed -n '1,220p'
doneRepository: larksuite/cli
Length of output: 50370
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo '--- docs_update_v2.go around the target lines ---'
sed -n '130,145p' shortcuts/doc/docs_update_v2.go | cat -n
echo '--- direct WithHint usages in the repo ---'
rg -n 'WithHint\(' shortcuts errs internal --type go | head -n 80
echo '--- validation error builder definition ---'
rg -n 'type ValidationError|func .*WithHint|func NewValidationError' errs internal --type go | head -n 120Repository: larksuite/cli
Length of output: 12839
Move the recovery steps into .WithHint(...)
Keep the validation message focused on the failure; move the “fetch block ids and use --command block_replace” guidance into .WithHint(...) and leave WithParam("--content") as-is.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@shortcuts/doc/docs_update_v2.go` around lines 133 - 142, The validation in
validateStrReplaceInlineCodeContent is mixing the failure message with recovery
guidance; keep the error text focused on the punctuation-only <code> content
issue and move the “fetch block ids and use --command block_replace” guidance
into .WithHint(...), while leaving .WithParam("--content") unchanged. Update the
errs.NewValidationError call in validateStrReplaceInlineCodeContent so the main
message stays concise and the hint carries the remediation steps.
Source: Coding guidelines
Summary
Reject unsafe
docs +update --command str_replacecontent that contains punctuation-only inline<code>spans, because the upstream str_replace path strips those tags whileblock_replacepreserves them.Changes
<code>inline spans forstr_replacereplacement content.--contentwith guidance to useblock_replace.str_replaceedge case in the lark-doc update reference.Test Plan
GOCACHE=$PWD/.cache/go-build GOPROXY=https://goproxy.cn,direct go test ./shortcuts/doc -count=1GOCACHE=$PWD/.cache/go-build GOPROXY=https://goproxy.cn,direct go build -o ./lark-cli .LARK_CLI_BIN=$PWD/lark-cli GOCACHE=$PWD/.cache/go-build GOPROXY=https://goproxy.cn,direct go test ./tests/cli_e2e/docs -run 'TestDocs_DryRunDefaultsToV2OpenAPI|TestDocs_UpdateRejectsStrReplacePunctuationOnlyInlineCode' -count=1git diff --checkRelated Issues
Summary by CodeRabbit