Fix CSP violations by replacing inline styles with CSS classes #370
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The
renderInput()andrenderToggleAll()methods inSelect2.phpwere adding inline styles usingHtml::addCssStyle()and directstyleattributes, which violates Content Security Policy (CSP) directives that restrict'unsafe-inline'styles.This prevents applications from implementing strict CSP policies for security hardening.
Solution
select2-addl.cssand minified versionChanges Made
src/Select2.php:Html::addCssStyle($this->options, $opts)withHtml::addCssClass()calls'style' => '...'attributes with'class' => '...'src/assets/css/select2-addl.css&select2-addl.min.css:.s2-plugin-loading- for hidden plugin loading state.s2-plugin-loading-bs3- Bootstrap 3 specific width override.s2-accesskey-btn- for invisible accesskey button styling.s2-togall-hidden- for hidden toggle-all parent elementBenefits
✅ CSP Compliance: Eliminates need for
'unsafe-inline'in style-src directive✅ Security: Enables stricter Content Security Policies
✅ Maintainability: Styling centralized in CSS files vs scattered inline
✅ Performance: CSS classes are more efficient than inline styles
✅ Consistency: Follows existing
s2-prefix naming convention✅ Compatibility: Zero breaking changes, fully backward compatible
Testing
pluginLoadingenabledstyle-src 'self'policyBrowser Support
No impact on browser support - uses standard CSS classes that work in all supported browsers.