-
Notifications
You must be signed in to change notification settings - Fork 0
fix(csp): drop unsafe-eval from the built Vue SPA responses #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+149
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.StaticFiles; | ||
| using Microsoft.Extensions.FileProviders; | ||
| using Microsoft.Net.Http.Headers; | ||
| using Viper.Classes; | ||
|
|
||
| namespace Viper.test.Classes; | ||
|
|
||
| /// <summary>Built Vue SPA responses must not permit dynamic code evaluation. See CspPolicy.</summary> | ||
| public class CspPolicyTests | ||
| { | ||
| // Shape of the emitted header. Joonasw's CspOptions joins directives with ';' and no space. | ||
| private const string ApplicationPolicy = | ||
| "script-src 'self' 'nonce-abc123' 'unsafe-eval';style-src 'self' fonts.googleapis.com 'unsafe-inline';img-src 'self' data:;frame-src 'none'"; | ||
|
|
||
| private const string TightenedPolicy = | ||
| "script-src 'self' 'nonce-abc123';style-src 'self' fonts.googleapis.com 'unsafe-inline';img-src 'self' data:;frame-src 'none'"; | ||
|
|
||
| [Fact] | ||
| public void WithoutUnsafeEval_ApplicationPolicy_DropsOnlyTheAllowance() | ||
| { | ||
| Assert.Equal(TightenedPolicy, CspPolicy.WithoutUnsafeEval(ApplicationPolicy)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("script-src 'self' 'unsafe-eval'", "script-src 'self'")] | ||
| [InlineData("script-src 'unsafe-eval' 'self'", "script-src 'self'")] | ||
| [InlineData("script-src 'self' 'unsafe-eval' 'nonce-x'", "script-src 'self' 'nonce-x'")] | ||
| public void WithoutUnsafeEval_HandlesEveryPositionInADirective(string policy, string expected) | ||
| { | ||
| Assert.Equal(expected, CspPolicy.WithoutUnsafeEval(policy)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("script-src 'unsafe-eval'", "script-src 'none'")] | ||
| [InlineData("script-src 'unsafe-eval';img-src 'self'", "script-src 'none';img-src 'self'")] | ||
| public void WithoutUnsafeEval_SoleSourceExpression_FallsBackToNone(string policy, string expected) | ||
| { | ||
| Assert.Equal(expected, CspPolicy.WithoutUnsafeEval(policy)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WithoutUnsafeEval_ValuelessDirective_IsNotGivenASource() | ||
| { | ||
| Assert.Equal( | ||
| "script-src 'self';upgrade-insecure-requests", | ||
| CspPolicy.WithoutUnsafeEval("script-src 'self' 'unsafe-eval';upgrade-insecure-requests")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WithoutUnsafeEval_PolicyWithoutTheAllowance_IsUnchanged() | ||
| { | ||
| const string policy = "script-src 'self' 'nonce-abc123';img-src 'self'"; | ||
|
|
||
| Assert.Equal(policy, CspPolicy.WithoutUnsafeEval(policy)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| public void WithoutUnsafeEval_MissingHeader_ReturnsEmpty(string? policy) | ||
| { | ||
| Assert.Equal(string.Empty, CspPolicy.WithoutUnsafeEval(policy)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TightenForBuiltSpa_RewritesThePolicyOnTheResponse() | ||
| { | ||
| var http = new DefaultHttpContext(); | ||
| http.Response.Headers[HeaderNames.ContentSecurityPolicy] = ApplicationPolicy; | ||
|
|
||
| CspPolicy.TightenForBuiltSpa(ResponseContextFor(http)); | ||
|
|
||
| Assert.Equal(TightenedPolicy, http.Response.Headers[HeaderNames.ContentSecurityPolicy].ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TightenForBuiltSpa_NoPolicyOnTheResponse_AddsNoHeader() | ||
| { | ||
| // The CSP middleware is skipped for HealthChecks UI paths, so the header can be absent. | ||
| var http = new DefaultHttpContext(); | ||
|
|
||
| CspPolicy.TightenForBuiltSpa(ResponseContextFor(http)); | ||
|
|
||
| Assert.False(http.Response.Headers.ContainsKey(HeaderNames.ContentSecurityPolicy)); | ||
| } | ||
|
|
||
| private static StaticFileResponseContext ResponseContextFor(HttpContext http) | ||
| { | ||
| return new StaticFileResponseContext(http, new NotFoundFileInfo("index.html")); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| using Microsoft.AspNetCore.StaticFiles; | ||
| using Microsoft.Net.Http.Headers; | ||
|
|
||
| namespace Viper.Classes | ||
| { | ||
| /// <summary> | ||
| /// The app-wide policy keeps 'unsafe-eval': _VIPERLayout Razor pages mount Vue's full build on | ||
| /// the document body, compiling that in-DOM template via Function(code)(). Dropping it blanks them. | ||
| /// </summary> | ||
| public static class CspPolicy | ||
| { | ||
| private const string UnsafeEval = "'unsafe-eval'"; | ||
|
|
||
| /// <summary>Drops the allowance from a built SPA response with precompiled templates.</summary> | ||
| public static void TightenForBuiltSpa(StaticFileResponseContext ctx) | ||
| { | ||
| var headers = ctx.Context.Response.Headers; | ||
| if (headers.TryGetValue(HeaderNames.ContentSecurityPolicy, out var policy)) | ||
| { | ||
| headers[HeaderNames.ContentSecurityPolicy] = WithoutUnsafeEval(policy.ToString()); | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Returns the policy with every 'unsafe-eval' source expression removed.</summary> | ||
| public static string WithoutUnsafeEval(string? headerValue) | ||
| { | ||
| return string.IsNullOrEmpty(headerValue) | ||
| ? string.Empty | ||
| : string.Join(';', headerValue.Split(';').Select(StripUnsafeEval)); | ||
| } | ||
|
|
||
| private static string StripUnsafeEval(string directive) | ||
| { | ||
| // Without this, a valueless directive like upgrade-insecure-requests would gain 'none'. | ||
| if (!directive.Contains(UnsafeEval, StringComparison.Ordinal)) | ||
| { | ||
| return directive; | ||
| } | ||
|
|
||
| string[] kept = directive | ||
| .Split(' ', StringSplitOptions.RemoveEmptyEntries) | ||
| .Where(token => !string.Equals(token, UnsafeEval, StringComparison.Ordinal)) | ||
| .ToArray(); | ||
|
|
||
| // A bare directive name already matches nothing; 'none' states that explicitly. | ||
| return kept.Length == 1 | ||
| ? kept[0] + " 'none'" | ||
| : string.Join(' ', kept); | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.