Summary
A flaw in ViewComponent's around_render and collection rendering APIs allows HTML-unsafe strings to bypass the usual output escaping in Ruby on Rails components.
When around_render or collection rendering is used with user-controlled data, the framework can incorrectly mark unsafe content as html_safe, creating a high-risk XSS condition in affected applications.
Affected context
This vulnerability appears in Ruby on Rails applications that use the ViewComponent framework (versions 4.0.0 through 4.12.0) and rely on ViewComponent::Base#around_render to wrap, replace, or instrument component output.
It is most relevant where components process or include user-controlled data, and where ViewComponent::Collection#render_in is used to join multiple per-item results.
In such contexts, the framework's normal HTML escaping guarantees can be unintentionally bypassed, especially when developers expect around_render to be as safe as regular component rendering.
Exploit preconditions
- Attack surface: Web application views rendered through ViewComponent in Ruby on Rails.
- Required access: Ability to supply user-controlled data that is rendered via components using around_render or collection rendering.
- Trigger point: Invocation of
ViewComponent::Base#around_renderorViewComponent::Collection#render_inthat returns or joins HTML-unsafe strings. - Why exploitation is possible: The around_render hook can return strings that are not subjected to the usual HTML escaping, and the collection rendering logic can mark the combined output as html_safe, allowing attacker-controlled markup or scripts to be delivered to the browser.
Why this happens
The underlying issue is an output encoding and trust boundary error within the ViewComponent rendering pipeline.
The framework's normal rendering path applies escaping to component output, but the around_render hook operates outside that safety net, allowing raw strings to bypass encoding.
When combined with collection rendering that joins per-item results and marks them as html_safe, the system effectively upgrades untrusted content into a trusted SafeBuffer.
This behavior maps to CWE-79, where user-controlled data is incorporated into HTML output without proper neutralization of script-related characters.
Potential impact
- Attacker-controlled JavaScript execution in users' browsers.
- Injection of malicious HTML or DOM modifications on affected pages.
- Session theft, account hijacking, or CSRF token exfiltration via XSS.
- Defacement or manipulation of UI content and phishing within the application.
- Use of XSS to pivot into further attacks, such as API abuse from a trusted user context.
General mitigation guidance
Mitigation should focus on re-establishing strict output encoding guarantees around component rendering and hooks.
Common defensive patterns include:
- Ensuring all content returned from around_render is properly escaped or explicitly treated as untrusted, especially when it incorporates user input.
- Avoiding html_safe or SafeBuffer markings on aggregated output unless every contributing fragment is verified safe.
- Centralizing output encoding responsibility in the rendering framework rather than in ad-hoc hooks or wrappers.
- Designing components so that instrumentation, wrapping, or conditional rendering never alters the escaping semantics of user-controlled data.
- Applying least-privilege principles to helper APIs that can mark content as safe, and limiting their usage to narrowly reviewed cases.
How teams detect issues like this
High-level detection approaches may include:
- Static analysis rules that flag usages of
around_render,render_in,html_safe, andActiveSupport::SafeBufferwhere user-controlled data is involved. - Code review checklists that require examination of component hooks and collection rendering for bypasses of the normal escaping pipeline.
- Framework-level policies discouraging or restricting manual safe-marking of strings, and requiring justification when content is marked html_safe.
- Automated security tests that inject XSS payloads into component inputs and verify that rendered output is correctly encoded.

