🐛 bug: escape HTML output in Ctx.Format - #4232
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
WalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.11.4)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" 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.
Code Review
This pull request enhances security by implementing HTML escaping in the Format method to prevent XSS vulnerabilities. Corresponding test cases were added for HTML, JSON, and XML formats to ensure correct handling of script tags. A review comment suggests refactoring a JSON test case to use a direct string assertion for better clarity and consistency with other tests.
There was a problem hiding this comment.
Pull request overview
This PR hardens Ctx.Format against HTML injection by escaping HTML output before embedding it in the <p>...</p> wrapper, while keeping JSON/TXT/XML behavior unchanged.
Changes:
- Escape HTML output in
Ctx.Formatusinghtml.EscapeStringbefore wrapping in<p>...</p>. - Extend
Test_Ctx_Formatto assert HTML escaping for a<script>payload and to ensure JSON/XML formatting output matches the directc.JSON/c.XMLbehavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| ctx.go | Escapes HTML branch output in Ctx.Format before wrapping in <p>...</p>. |
| ctx_test.go | Adds assertions verifying HTML escaping and unchanged JSON/XML behavior. |
Motivation
Ctx.Formatrenders HTML by escaping the formatted string before embedding it in the<p>wrapper while keeping JSON/TXT/XML branches RFC-consistent and unchanged.Description
htmland applyhtml.EscapeString(b)in thehtmlbranch offunc (c *Ctx) Format(body interface{}) errorso content is escaped before wrapping with<p>...</p>inctx.go.Test_Ctx_Formatto verify that HTML mode escapes a<script>payload and that JSON/XML behavior remains unchanged by comparingFormatoutput to directc.JSON/c.XMLoutput where appropriate inctx_test.go.DefaultRes.AutoFormat/res.go) in this checkout and did not find a matching file or symbol, so no v3-side change was applied here.