<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Samarth Anavatti on Medium]]></title>
        <description><![CDATA[Stories by Samarth Anavatti on Medium]]></description>
        <link>https://medium.com/@samarthanavatti?source=rss-5159ede9820e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*LJeItPU-jJtco1g2.</url>
            <title>Stories by Samarth Anavatti on Medium</title>
            <link>https://medium.com/@samarthanavatti?source=rss-5159ede9820e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 18:16:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@samarthanavatti/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Handling Content Security Policy (CSP) Nonce for Dynamic Scripts in Angular]]></title>
            <link>https://medium.com/@samarthanavatti/handling-content-security-policy-csp-nonce-for-dynamic-scripts-in-angular-3794cde59127?source=rss-5159ede9820e------2</link>
            <guid isPermaLink="false">https://medium.com/p/3794cde59127</guid>
            <dc:creator><![CDATA[Samarth Anavatti]]></dc:creator>
            <pubDate>Sat, 01 Feb 2025 05:45:51 GMT</pubDate>
            <atom:updated>2025-02-01T05:45:51.703Z</atom:updated>
            <content:encoded><![CDATA[<p>When working with Content Security Policy (CSP), executing external scripts dynamically in an Angular application can lead to console errors related to unsafe-inline. This happens because CSP blocks inline script execution unless a valid nonce is provided.</p><p>The recommended approach is to generate a dynamic nonce for better security. This blog explores how to dynamically inject a nonce using Nginx.</p><h3>Dynamic Nonce Approach</h3><p>To improve security, we need to generate a dynamic nonce for every request. We can do this by configuring Nginx to replace a placeholder nonce with a dynamically generated one.</p><h3>Step 1: Update Angular JS Code to Use Placeholder Nonce</h3><pre>function loadExternalScript() {<br>    const script = document.createElement(&#39;script&#39;);<br>    script.src = &#39;https://example.com/external.js&#39;;<br>    script.setAttribute(&#39;nonce&#39;, &#39;NGINX_CSP_NONCE&#39;); // Placeholder nonce<br>    document.body.appendChild(script);<br>}<br><br>loadExternalScript();</pre><h3>Step 2: Update Nginx Configuration to Inject Dynamic Nonce</h3><pre>set $nonce $request_id;<br>add_header Content-Security-Policy &quot;script-src &#39;self&#39; &#39;nonce-$nonce&#39;&quot;;<br><br>sub_filter &#39;NGINX_CSP_NONCE&#39; &quot;$nonce&quot;;<br>sub_filter_once off;<br>sub_filter_types text/html text/javascript application/javascript;</pre><h3>Explanation:</h3><ul><li>$nonce is dynamically generated using $request_id.</li><li>The CSP header is updated to use this nonce dynamically.</li><li>sub_filter replaces the placeholder NGINX_CSP_NONCE in the JavaScript response with the actual nonce.</li><li>sub_filter_types text/html text/javascript application/javascript; ensures that replacement happens in JavaScript and HTML files.</li></ul><h3>Additional Notes</h3><ul><li>If your application is containerized and using Nginx inside a Docker container, ensure you install nginx-full as sub_filter is not included in the minimal version:</li></ul><pre>RUN apt -y update &amp;&amp; apt -y install nginx-full  </pre><p>Verify that sub_filter is working by checking the response:</p><pre>curl -I https://your-app.com/main.js</pre><h3>Conclusion</h3><p>Using a dynamic nonce in CSP is a robust way to enhance security while allowing controlled script execution in an Angular application. By leveraging Nginx’s sub_filter, we can replace placeholder nonces dynamically without modifying the frontend code for each request. This approach ensures compliance with CSP policies while maintaining application flexibility and security.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3794cde59127" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>