Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use nonce in IIS for content security policy?

I have MVC application developed in ASP.NET 4.5.1. The application is using javascripts in several pages. Some of the javascripts are referenced as

@Scripts.Render("~/Scripts/bootstrap")
@Scripts.Render("~/Scripts/js")

and also there are some inline scripts like

<script type="javascript">

   // javascript code
</script>

I want to implement Content Security Policy for this site. The site is hosted in IIS. So in IIS i add content-security-policy header in HTTP Response Header as

 object-src 'none';
 script-src 'nonce-{random}' 'unsafe-inline' 'unsafe-eval' 'strict-dynamic' https: http:;
 base-uri 'self';
 report-uri https://csp.withgoogle.com/csp/<unique_id>/<application_version>

So every response will include this header.

Questions
1. Should nonce attribute only recommended for inline scripting?
2. How do i add randomly generatednonce into script tags and importantly how IIS would know randomly generated nonce value so it can include in response header? (i am assuming every response will have unique nonce value)

like image 747
LP13 Avatar asked Mar 13 '17 18:03

LP13


People also ask

What is nonce in Content Security Policy?

The nonce attribute is useful to allow-list specific elements, such as a particular inline script or style elements. It can help you to avoid using the CSP unsafe-inline directive, which would allow-list all inline scripts or styles.

What is nonce in script tag?

It allows the list of specific elements such as some specific inline script or style elements. It helps to avoid the use of the CSP unsafe-inline directive that would allow-list all inline styles. Usage of nonce attribute.

What is a nonce example?

Once created, nonce words often make their way into common language. For example, Lewis Carroll coined the word "chortle" for the poem Jabberwocky, and James Joyce created the word "quark" for his novel Finnegans Wake.


1 Answers

  1. nonce attribute only used for inline scripts. If you want to take secure your sources from other origins, you can use hash
  2. IIS does not provide nonce generation as default. You need to handle it on the backend.

    i. Define a helper to generate a random nonce string, named CreateNonce(). The string length is not so important but you need to be sure it's a unique value. So, if you define a 10-20 length string with alphanumeric characters, it will be okay. You will call this method on your Views, where are your inline scripts. eg: <script <%CreateNonce()%> When you call this helper, it will return an output that generated nonce value, eg.<script nonce-123abc> and call another helper which is explained on the following step.

    ii. Define another helper to create CSP rules to Response Header for inline scripts, named CSPGenerator(). When you call this method, it should create a Content-Security-Policy response header if it does not exist. If the header is exist, it should append the new nonce value to that.

Pseudocode (sorry, I am not an asp.net developer);

Views.cshtml 

<script <%CreateNonce()%> >

Helpers.cs

Public CreateNonce(){
generateRandomAlphaNumericChars
CSPGenerator('apply-to-csp-rules:generateRandomAlphaNumericChars')
and response.write generateRandomAlphaNumericChars
}

Public CSPGenerator(str nonce_){
GetHttpResponse[csp],
if null, add new response header,
response.header.append="script-src "+ nonce_
}
like image 71
myalcin81 Avatar answered Oct 25 '22 02:10

myalcin81