security: remove style-src unsafe-inline#1144
Open
BenjaminMichaelis wants to merge 2 commits into
Open
Conversation
Replaces inline style attributes in Razor views with CSS classes and updates CSP style-src to drop unsafe-inline while preserving required external origins.
Contributor
There was a problem hiding this comment.
Pull request overview
Hardens the site’s Content Security Policy by removing unsafe-inline from style-src and migrating several inline style="..." usages in Razor views into shared CSS classes to preserve UI layout under stricter CSP.
Changes:
- Removed
unsafe-inlinefrom the CSPstyle-srcdirective inProgram.cs. - Replaced a handful of inline style attributes in Razor views with Bootstrap/util classes and new custom CSS classes.
- Added centralized CSS rules to
wwwroot/css/styles.cssfor MCP setup layout, password strength meter, guideline icon styling, and common image sizing.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| EssentialCSharp.Web/Program.cs | Removes unsafe-inline from style-src in the generated CSP header. |
| EssentialCSharp.Web/wwwroot/css/styles.css | Adds new CSS classes intended to replace inline styles (MCP setup, password meter, guideline icons, image sizing). |
| EssentialCSharp.Web/Views/Shared/_Layout.cshtml | Replaces an inline display:none with a Bootstrap d-none class for the DocSearch container. |
| EssentialCSharp.Web/Views/McpSetup/Index.cshtml | Replaces inline max-width and summary styling with new CSS classes. |
| EssentialCSharp.Web/Views/Home/Home.cshtml | Replaces inline book image height with a shared CSS class. |
| EssentialCSharp.Web/Views/Home/About.cshtml | Replaces inline image heights with shared CSS classes (logo + book cover). |
| EssentialCSharp.Web/Views/Home/Guidelines.cshtml | Changes guideline icon coloring from inline style to type→CSS-class mapping. |
| EssentialCSharp.Web/Areas/Identity/Pages/Account/Manage/TwoFactorAuthentication.cshtml | Replaces inline form display style with Bootstrap utility class. |
| EssentialCSharp.Web/Areas/Identity/Pages/_PasswordStrengthMeter.cshtml | Replaces inline progress height/width styles with CSS classes. |
Comments suppressed due to low confidence (1)
EssentialCSharp.Web/Program.cs:516
- Removing 'unsafe-inline' from
style-srcwill cause CSP violations for existing inline styles and script-driven style mutations. For example:Views/McpSetup/Index.cshtmlstill contains an inline<style>block (see around line ~383), and several first-party scripts set styles viaelement.style(e.g.,wwwroot/js/password-strength.jssetsbar.style.width,wwwroot/js/consent-manager.jstogglesdetails.style.display,wwwroot/js/trydotnet-module.jssetsheading.style.*). With the new CSP, these will be blocked and can break UI behaviors. Consider either (a) eliminating these inline styles/style mutations (move rules to CSS + switch to class toggles), or (b) explicitly allowing style attributes viastyle-src-attr 'unsafe-inline'(and/orstyle-src-elemas appropriate) if you want to keep JS-driven styling while still tighteningstyle-src.
string csp = string.Join("; ",
$"default-src 'self'",
$"script-src 'self' 'unsafe-inline' cdn.jsdelivr.net www.clarity.ms www.googletagmanager.com js.monitor.azure.com https://hcaptcha.com https://*.hcaptcha.com{tryDotNetSources}",
$"style-src 'self' cdnjs.cloudflare.com fonts.googleapis.com https://hcaptcha.com https://*.hcaptcha.com",
$"font-src 'self' fonts.gstatic.com cdnjs.cloudflare.com",
$"img-src 'self' data: https:",
$"connect-src 'self' https://hcaptcha.com https://*.hcaptcha.com https://api.pwnedpasswords.com https://*.algolia.net https://*.algolianet.com https://*.google-analytics.com https://*.clarity.ms https://*.in.applicationinsights.azure.com{GetApplicationInsightsCspSources(app.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"], app.Logger)}{tryDotNetSources}",
$"frame-src https://hcaptcha.com https://*.hcaptcha.com https://newassets.hcaptcha.com{tryDotNetSources}",
$"worker-src blob:",
Move MCP setup chevron styles out of inline script section, remove duplicate guideline icon CSS override, and switch password meter width updates to CSP-safe CSS classes.
| $"default-src 'self'", | ||
| $"script-src 'self' 'unsafe-inline' cdn.jsdelivr.net www.clarity.ms www.googletagmanager.com js.monitor.azure.com https://hcaptcha.com https://*.hcaptcha.com{tryDotNetSources}", | ||
| $"style-src 'self' 'unsafe-inline' cdnjs.cloudflare.com fonts.googleapis.com https://hcaptcha.com https://*.hcaptcha.com", | ||
| $"style-src 'self' cdnjs.cloudflare.com fonts.googleapis.com https://hcaptcha.com https://*.hcaptcha.com", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Why
The CSP
style-srcpolicy still allowedunsafe-inline, which weakens XSS protections by permitting arbitrary inline styling. This change hardens CSP while keeping existing UI behavior intact.What changed
unsafe-inlinefromstyle-srcinEssentialCSharp.Web/Program.cs.style="..."attributes in affected Razor views with CSS classes.wwwroot/css/styles.cssfor:Notes
script-srcstill includesunsafe-inlineand was intentionally left unchanged in this PR to keep scope focused onstyle-src.npm ci; backend build succeeds with-p:SkipFrontendBuild=true.