🐛 bug: fix HTML escaping in AutoFormat#4228
Conversation
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/42d9cb22-074d-4c33-a38e-c250234957a0 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4228 +/- ##
==========================================
- Coverage 91.24% 91.12% -0.12%
==========================================
Files 123 123
Lines 11910 12076 +166
==========================================
+ Hits 10867 11004 +137
- Misses 655 673 +18
- Partials 388 399 +11
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR hardens Fiber’s AutoFormat HTML response path against XSS by escaping user-provided content before embedding it in an HTML <p> wrapper.
Changes:
- Escape HTML special characters in
DefaultRes.AutoFormatwhen the negotiated type ishtml. - Add a dedicated regression test covering multiple common XSS payload patterns for
AutoFormatHTML output.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
res.go |
Escapes HTML output in AutoFormat to prevent user input from being interpreted as markup/script. |
ctx_test.go |
Adds coverage ensuring the HTML AutoFormat branch properly escapes potentially malicious input across several vectors. |
|
@copilot apply changes based on the comments in this thread |
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/e13b1223-74cf-4f5a-83f9-4c2cc4e2e9eb Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Addressed in cc5d809: switched |
There was a problem hiding this comment.
Pull request overview
Hardens Fiber’s AutoFormat HTML response path against XSS by ensuring user-provided content is HTML-escaped before being embedded into an HTML wrapper.
Changes:
- Escape HTML content in
DefaultRes.AutoFormatwhenAcceptselectstext/html(wraps escaped text in a<p>). - Add a dedicated XSS-prevention test covering multiple common injection vectors.
- Update generated interface docs to reflect the new
AutoFormatHTML escaping behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
res.go |
Escapes AutoFormat HTML output via template.HTMLEscapeString before wrapping in <p>...</p>. |
ctx_test.go |
Adds Test_Ctx_AutoFormat_XSS_Prevention validating escaping across multiple payload patterns. |
ctx_interface_gen.go |
Updates the generated Ctx interface comment to document HTML escaping behavior. |
res_interface_gen.go |
Updates the generated Res interface comment to document HTML escaping behavior. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Hardens Fiber’s AutoFormat HTML rendering path to prevent XSS by escaping the rendered body when text/html is negotiated, and adds regression tests to validate escaping behavior.
Changes:
- Escape HTML output in
DefaultRes.AutoFormatwhen the negotiated type ishtml. - Add a dedicated XSS regression test covering multiple common injection vectors.
- Update
AutoFormatdocumentation comments (including generated interfaces) to reflect the escaping behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| res.go | Escapes the HTML response body in AutoFormat before wrapping it in <p>...</p>. |
| ctx_test.go | Adds Test_Ctx_AutoFormat_XSS_Prevention to assert HTML-escaping and prevent regressions. |
| res_interface_gen.go | Updates generated Res interface doc comment for AutoFormat to mention escaping. |
| ctx_interface_gen.go | Updates generated Ctx interface doc comment for AutoFormat to mention escaping. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
@claude[agent]+claude-sonnet-4.6 Should we remove the " " wrapping for html in AutoFormat() ? |
Yes, I recommend removing the Should I proceed with removing the |
|
@ReneWerner87 Thoughts? |
|
@claude[agent] We've adopted this feature from Express.js—how does it work there? Do we only use plain text as input? Is there a wrapper? What about XSS injection? |
Express.js Express.js approach:
Fiber's AutoFormat:
Recommendation: Since Express doesn't wrap content, we should remove the |
|
okay |
|
Let's talk about this again on Discord; I'm not sure about the restriction that says you can only inject plain text now. |
|
We are fine with the adjustments |
|
Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
Description
Fixes a potential XSS vulnerability in
AutoFormatwhere user input was directly concatenated into HTML without escaping.Changes introduced
Security Fix:
AutoFormatnow usestemplate.HTMLEscapeString()to escape HTML special characters before renderingres.go:460- Changed from"<p>" + b + "</p>"to"<p>" + template.HTMLEscapeString(b) + "</p>"htmlpackage tores.goTest Coverage: Added comprehensive
Test_Ctx_AutoFormat_XSS_Preventioncovering:<,>,&)Benchmarks: No performance impact -
html.EscapeString()is optimized for common casesDocumentation Update: No documentation changes needed - security fix maintains existing API
Changelog/What's New: Security fix - prevents XSS attacks in AutoFormat HTML rendering
Migration Guide: No breaking changes - output format identical for non-malicious content
API Alignment with Express: N/A - security hardening
API Longevity: Maintains backward compatibility while fixing security issue
Examples: Test suite demonstrates protection against common XSS vectors
Type of change
Checklist
/docs/directory for Fiber's documentation.