Skip to content

Commit

Permalink
CSP round 3(?) (#2003)
Browse files Browse the repository at this point in the history
* Make `Content-Security-Policy` not so hardcoded

* Add more `Content-Security-Policy` directives
  • Loading branch information
YoshiRulz authored Oct 14, 2024
1 parent a094d91 commit 5742d41
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 24 additions & 2 deletions TASVideos/Extensions/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,30 @@ public static IApplicationBuilder UseStaticFilesWithExtensionMapping(this IAppli
});
}

public static IApplicationBuilder UseMvcWithOptions(this IApplicationBuilder app, IHostEnvironment env)
public static IApplicationBuilder UseMvcWithOptions(this IApplicationBuilder app, IHostEnvironment env, AppSettings settings)
{
var userAgentReportURL = $"{settings.BaseUrl}/Diagnostics/UserAgentInterventionReports";
string[] trustedJSHosts = [
"https://cdn.jsdelivr.net",
"https://cdnjs.cloudflare.com",
"https://code.jquery.com",
"https://www.google.com/recaptcha/",
"https://www.gstatic.com/recaptcha/",
"https://www.youtube.com",
];
string[] cspDirectives = [
"base-uri 'none'", // neutralises the `<base/>` footgun
"default-src 'self'", // fallback for other `*-src` directives
"font-src 'self' https://cdnjs.cloudflare.com/ajax/libs/font-awesome/", // CSS `font: url();` and `@font-face { src: url(); }` will be blocked unless they're from one of these domains (this also blocks nonstandard fonts installed on the system maybe)
"form-action 'self'", // domains allowed for `<form action/>` (POST target page)
"frame-src 'self' https://www.youtube.com/embed/", // allow these domains in <iframe/>
"img-src *", // allow hotlinking images from any domain in UGC (not great)
"require-trusted-types-for 'script'", // experimental, but Google seems to be pushing it: should block `HTMLScriptElement.innerHTML = "user.pwn();";`, and similarly block adding in-line scripts as attrs
$"script-src 'self' {string.Join(' ', trustedJSHosts)}", // `<script/>`s will be blocked unless they're from one of these domains
"style-src 'unsafe-inline' 'self' https://cdnjs.cloudflare.com/ajax/libs/font-awesome/", // allow `<style/>`, and `<link rel="stylesheet"/>` if it's from our domain or trusted CDN
"upgrade-insecure-requests", // browser should automagically replace links to any `http://tasvideos.org/...` URL (in UGC, for example) with HTTPS
];
var contentSecurityPolicyValue = string.Join("; ", cspDirectives);
var permissionsPolicyValue = string.Join(", ", [
"camera=()", // defaults to `self`
"display-capture=()", // defaults to `self`
Expand All @@ -71,7 +93,7 @@ public static IApplicationBuilder UseMvcWithOptions(this IApplicationBuilder app
context.Response.Headers.XContentTypeOptions = "nosniff";
context.Response.Headers["Referrer-Policy"] = "strict-origin-when-cross-origin";
context.Response.Headers.XPoweredBy = "";
context.Response.Headers.ContentSecurityPolicy = "upgrade-insecure-requests; script-src 'self' https://code.jquery.com https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://www.youtube.com https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/";
context.Response.Headers.ContentSecurityPolicy = contentSecurityPolicyValue;
await next();
});

Expand Down
2 changes: 1 addition & 1 deletion TASVideos/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
.UseAuthentication()
.UseMiddleware<CustomLocalizationMiddleware>()
.UseSerilogRequestLogging()
.UseMvcWithOptions(app.Environment);
.UseMvcWithOptions(app.Environment, settings);

if (app.Environment.IsDevelopment())
{
Expand Down

0 comments on commit 5742d41

Please sign in to comment.