Skip to content

fix(doc): reject unsafe str_replace code spans#1818

Open
xu91102 wants to merge 1 commit into
larksuite:mainfrom
xu91102:fix/docs-str-replace-code-punctuation
Open

fix(doc): reject unsafe str_replace code spans#1818
xu91102 wants to merge 1 commit into
larksuite:mainfrom
xu91102:fix/docs-str-replace-code-punctuation

Conversation

@xu91102

@xu91102 xu91102 commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Reject unsafe docs +update --command str_replace content that contains punctuation-only inline <code> spans, because the upstream str_replace path strips those tags while block_replace preserves them.

Changes

  • Detect punctuation-only XML <code> inline spans for str_replace replacement content.
  • Return a typed validation error bound to --content with guidance to use block_replace.
  • Add shortcut and CLI dry-run regression coverage.
  • Document the str_replace edge case in the lark-doc update reference.

Test Plan

  • GOCACHE=$PWD/.cache/go-build GOPROXY=https://goproxy.cn,direct go test ./shortcuts/doc -count=1
  • GOCACHE=$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=1
  • git diff --check
  • conflict marker scan

Related Issues

Summary by CodeRabbit

  • Bug Fixes
    • Improved validation for document updates so punctuation-only inline code is rejected in cases where it could produce unreliable replacements.
    • Clarified error feedback to guide users toward a safer block-based replacement workflow.
  • Documentation
    • Expanded best-practice guidance for rich-text replacements, including when to use block replacements instead of inline code updates.
  • Tests
    • Added coverage for both rejected and accepted inline-code replacement scenarios, plus an end-to-end CLI check.

@github-actions github-actions Bot added domain/ccm PR touches the ccm domain size/M Single-domain feat or fix with limited business impact labels Jul 9, 2026
@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

Adds validation to the v2 docs update path that rejects str_replace XML content containing punctuation-only inline <code> tokens (e.g., ::, .), directing users to block_replace instead. Includes new XML parsing helpers, unit tests, an e2e dry-run test, and documentation updates.

Changes

str_replace punctuation-only inline code validation

Layer / File(s) Summary
Punctuation-only inline code validation logic
shortcuts/doc/docs_update_v2.go
Adds validateStrReplaceInlineCodeContent, firstPunctuationOnlyInlineCode, and isPunctuationOnlyCodeText helpers using XML decoding to detect punctuation-only <code> text in str_replace content when doc-format is xml, wired into validateUpdateV2 to return a --content validation error.
Unit and e2e tests, docs update
shortcuts/doc/docs_update_test.go, tests/cli_e2e/docs/docs_update_dryrun_test.go, skills/lark-doc/references/lark-doc-update.md
Adds unit tests confirming rejection of punctuation-only <code> and acceptance of alphanumeric <code>, an e2e dry-run test asserting exit code 2 and error substrings, and documentation guidance recommending block_replace as a workaround.

Estimated code review effort: 2 (Simple) | ~15 minutes

Possibly related PRs

  • larksuite/cli#1346: Both PRs modify validateUpdateV2 in shortcuts/doc/docs_update_v2.go, one adding typed validation errors and the other adding the punctuation-only <code> rejection check.

Suggested reviewers: SunPeiYang996, fangshuyu-768

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely matches the main change: rejecting unsafe str_replace code spans in docs updates.
Description check ✅ Passed The description follows the required template with Summary, Changes, Test Plan, and Related Issues, and it is sufficiently detailed.
Linked Issues check ✅ Passed The PR addresses #1680 by rejecting punctuation-only <code> spans in str_replace, adding tests and docs guidance to use block_replace.
Out of Scope Changes check ✅ Passed All changes stay on-topic, covering validation, tests, and docs for the str_replace edge case.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 74d8458 and 0b4a4e4.

📒 Files selected for processing (4)
  • shortcuts/doc/docs_update_test.go
  • shortcuts/doc/docs_update_v2.go
  • skills/lark-doc/references/lark-doc-update.md
  • tests/cli_e2e/docs/docs_update_dryrun_test.go

Comment on lines +133 to +142
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")
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 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' . || true

Repository: 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'
done

Repository: 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 120

Repository: 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

domain/ccm PR touches the ccm domain size/M Single-domain feat or fix with limited business impact

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs +update str_replace strips <code> tags around pure punctuation

1 participant