{"componentChunkName":"component---src-templates-blog-list-template-js","path":"/123","result":{"data":{"allMarkdownRemark":{"edges":[{"node":{"excerpt":"In this blog, we learn how to implement the AntiXssMiddleware in .NET Core. First, we will understand about the cross-site scripting. Cross…","fields":{"slug":"/engineering/anti-xss-middleware-asp-core/"},"html":"<p>In this blog, we learn how to implement the <strong>AntiXssMiddleware</strong> in .NET Core. First, we will understand about the cross-site scripting.</p>\n<h2 id=\"cross-site-scriptingxss\" style=\"position:relative;\"><a href=\"#cross-site-scriptingxss\" aria-label=\"cross site scriptingxss permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Cross-Site Scripting(XSS)</h2>\n<p>Cross-site scripting is a security vulnerability and a client-side code injection attack. In this attack, the malicious script is injected into legitimate websites.\nCross-site scripting allows an attacker to act like a victim user and to carry out the actions that the user can perform. The attacker can access the user's data as well.</p>\n<h2 id=\"implement-antixssmiddleware-in-net-core\" style=\"position:relative;\"><a href=\"#implement-antixssmiddleware-in-net-core\" aria-label=\"implement antixssmiddleware in net core permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Implement AntiXssMiddleware in .NET Core</h2>\n<p><strong>Step 1:</strong> Create Asp.NET Core Web Application project in Visual Studio.</p>\n<p><strong>Step 2:</strong> Select type as API in the next step and create the project. You will find a default controller which is created in the controller folder named as <strong>WeatherForecastController.cs</strong></p>\n<p><strong>Step 3:</strong> Now create a new folder named Middleware in the root directory.</p>\n<p><strong>Step 4 :</strong> Create a new file <strong>AntiXssMiddleware.cs</strong> in that Middleware folder.</p>\n<p><strong>Step 5:</strong> Now add the Newtonsoft.json package into your solution</p>\n<p>By doing the above steps you will have below structure in your solution. </p>\n<p><span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 545px; \">\n      <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 99.26605504587155%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAABJ0AAASdAHeZh94AAAC5klEQVQ4y42T2U7bQBSG/aSt1AC9QirLS/QCLtpCKfAelUpTKqAQCAkhwdm879uM7Sy243V6nFBUBaRW+jSyZ/TP+c8/M1Tr6y57ujuo7vS+vdevPuDGPrrdA5z6J6f+Ed3uu80D+MbNz7hRYt3s2bWd9snB2tYhdXqt1ju4eslVz7u1lvmraZ7VtZ816fsFc1qTftTEi4Z5cs6c1ZXb7qjRGwNtNqxe6Subh1SPUTUDyZqt6I6k2opqw69mYG8U+uOZN4oWuH7ojSMAJg0LN9vC6vYx9UDLHKeKsqFoNscrDCeyvCKImm5h0/YME4mSjv0giosgyoFJmCUpYQS0unlEKYrD6ui+0221WreNZu36pnXfNgwrzYo4yeI4jWZxkmRZVixIkpwQwkvozbsjaiAHmktoxrzvG/WOekdrzQe9w2DFyTVMNFQ8jn9QndzySavvrW4dUfVeeDeMOn25ejmo1pTqlVjv+n2V0GJBS4SWijnkiQexgNWLO28FbGM/TNPM9Sc8S6uqxvECeE7SHGy/SJyWtiXVLXs2URmGg8d0j+dFbcCIFhqHMQGCWRE+YxrmWU4GnFPZOKYsFCQZcf1AlPUHejDkJEW1VM02LFc3HAgctv6bcFaKBcVbg55B7I9DKCorRn8oqAaC5SDK4FTgA1gSByAuIG13deuYsnE4mkQetnXLZnkZThVcLPCAcQQbLYnzojznChwViKdh6rmubOgML3GCopvYtFxzbhvMw+qSbYhMVP232/PKcBNHHi7FnDRgBBAjd4K9YJaUDv/RM+wkSIasm2Cbnet7fbY/4OGSIjyZJeTFnisbXx7TZlhpyIpDVoLisIU/b3U0nS15fhKzEq5sQGVcpq27jqjo8CR0A0dxHqeLasueF8+jIKTLWK/Wy8DKVA0PMZxAdxl4W64/hQcI9+G5ciGGK9Zjrdfrh6XtIEr9IDQdDxK20QgSdtDoReULYugZZsFqnJFynPM/4t/yJDQqMYy44AAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Solution Structure\" title=\"Solution Structure\" src=\"/static/187a28fdf9137d82de1c19e817bd8e99/3ddad/SolutionArch.png\" srcset=\"/static/187a28fdf9137d82de1c19e817bd8e99/3ddad/SolutionArch.png 545w\" sizes=\"(max-width: 545px) 100vw, 545px\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\" loading=\"lazy\">\n    </span></p>\n<p><strong>Step 6:</strong> Now edit the AntiXssMiddlewars.cs file and paste below code.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">System</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">System</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Collections</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Generic</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">System</span><span class=\"mtk1\">.</span><span class=\"mtk10\">IO</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">System</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Linq</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">System</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Net</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">System</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Text</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">System</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Text</span><span class=\"mtk1\">.</span><span class=\"mtk10\">RegularExpressions</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">System</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Threading</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Tasks</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Microsoft</span><span class=\"mtk1\">.</span><span class=\"mtk10\">AspNetCore</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Builder</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Microsoft</span><span class=\"mtk1\">.</span><span class=\"mtk10\">AspNetCore</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Http</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Newtonsoft</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Json</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">namespace</span><span class=\"mtk1\"> </span><span class=\"mtk10\">AntiXssMiddleware</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Middleware</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">{</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">class</span><span class=\"mtk1\"> </span><span class=\"mtk10\">AntiXssMiddleware</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">private</span><span class=\"mtk1\"> </span><span class=\"mtk4\">readonly</span><span class=\"mtk1\"> </span><span class=\"mtk10\">RequestDelegate</span><span class=\"mtk1\"> </span><span class=\"mtk12\">_next</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">private</span><span class=\"mtk1\"> </span><span class=\"mtk10\">ErrorResponse</span><span class=\"mtk1\"> </span><span class=\"mtk12\">_error</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">private</span><span class=\"mtk1\"> </span><span class=\"mtk4\">readonly</span><span class=\"mtk1\"> </span><span class=\"mtk4\">int</span><span class=\"mtk1\"> </span><span class=\"mtk12\">_statusCode</span><span class=\"mtk1\"> = (</span><span class=\"mtk4\">int</span><span class=\"mtk1\">)</span><span class=\"mtk12\">HttpStatusCode</span><span class=\"mtk1\">.</span><span class=\"mtk12\">BadRequest</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk11\">AntiXssMiddleware</span><span class=\"mtk1\">(</span><span class=\"mtk10\">RequestDelegate</span><span class=\"mtk1\"> </span><span class=\"mtk12\">next</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">_next</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">next</span><span class=\"mtk1\"> ?? </span><span class=\"mtk15\">throw</span><span class=\"mtk1\"> </span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">ArgumentNullException</span><span class=\"mtk1\">(</span><span class=\"mtk4\">nameof</span><span class=\"mtk1\">(</span><span class=\"mtk12\">next</span><span class=\"mtk1\">));</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">async</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Task</span><span class=\"mtk1\"> </span><span class=\"mtk11\">Invoke</span><span class=\"mtk1\">(</span><span class=\"mtk10\">HttpContext</span><span class=\"mtk1\"> </span><span class=\"mtk12\">context</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk3\">// Check XSS in URL</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (!</span><span class=\"mtk12\">string</span><span class=\"mtk1\">.</span><span class=\"mtk11\">IsNullOrWhiteSpace</span><span class=\"mtk1\">(</span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Request</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Path</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Value</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">url</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Request</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Path</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Value</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">CrossSiteScriptingValidation</span><span class=\"mtk1\">.</span><span class=\"mtk11\">IsDangerousString</span><span class=\"mtk1\">(</span><span class=\"mtk12\">url</span><span class=\"mtk1\">, </span><span class=\"mtk4\">out</span><span class=\"mtk1\"> </span><span class=\"mtk7\">_</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                        </span><span class=\"mtk4\">await</span><span class=\"mtk1\"> </span><span class=\"mtk11\">RespondWithAnError</span><span class=\"mtk1\">(</span><span class=\"mtk12\">context</span><span class=\"mtk1\">).</span><span class=\"mtk11\">ConfigureAwait</span><span class=\"mtk1\">(</span><span class=\"mtk4\">false</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                        </span><span class=\"mtk15\">return</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk3\">// Check XSS in query string</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (!</span><span class=\"mtk12\">string</span><span class=\"mtk1\">.</span><span class=\"mtk11\">IsNullOrWhiteSpace</span><span class=\"mtk1\">(</span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Request</span><span class=\"mtk1\">.</span><span class=\"mtk12\">QueryString</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Value</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">queryString</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">WebUtility</span><span class=\"mtk1\">.</span><span class=\"mtk11\">UrlDecode</span><span class=\"mtk1\">(</span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Request</span><span class=\"mtk1\">.</span><span class=\"mtk12\">QueryString</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Value</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">CrossSiteScriptingValidation</span><span class=\"mtk1\">.</span><span class=\"mtk11\">IsDangerousString</span><span class=\"mtk1\">(</span><span class=\"mtk12\">queryString</span><span class=\"mtk1\">, </span><span class=\"mtk4\">out</span><span class=\"mtk1\"> </span><span class=\"mtk7\">_</span><span class=\"mtk1\">))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                        </span><span class=\"mtk4\">await</span><span class=\"mtk1\"> </span><span class=\"mtk11\">RespondWithAnError</span><span class=\"mtk1\">(</span><span class=\"mtk12\">context</span><span class=\"mtk1\">).</span><span class=\"mtk11\">ConfigureAwait</span><span class=\"mtk1\">(</span><span class=\"mtk4\">false</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                        </span><span class=\"mtk15\">return</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk3\">// Check XSS in request content</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">originalBody</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Request</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Body</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk15\">try</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">content</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">await</span><span class=\"mtk1\"> </span><span class=\"mtk11\">ReadRequestBody</span><span class=\"mtk1\">(</span><span class=\"mtk12\">context</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">CrossSiteScriptingValidation</span><span class=\"mtk1\">.</span><span class=\"mtk11\">IsDangerousString</span><span class=\"mtk1\">(</span><span class=\"mtk12\">content</span><span class=\"mtk1\">, </span><span class=\"mtk4\">out</span><span class=\"mtk1\"> </span><span class=\"mtk7\">_</span><span class=\"mtk1\">)) </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                            </span><span class=\"mtk4\">await</span><span class=\"mtk1\"> </span><span class=\"mtk11\">RespondWithAnError</span><span class=\"mtk1\">(</span><span class=\"mtk12\">context</span><span class=\"mtk1\">).</span><span class=\"mtk11\">ConfigureAwait</span><span class=\"mtk1\">(</span><span class=\"mtk4\">false</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                            </span><span class=\"mtk15\">return</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk4\">await</span><span class=\"mtk1\"> </span><span class=\"mtk11\">_next</span><span class=\"mtk1\">(</span><span class=\"mtk12\">context</span><span class=\"mtk1\">).</span><span class=\"mtk11\">ConfigureAwait</span><span class=\"mtk1\">(</span><span class=\"mtk4\">false</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk15\">finally</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Request</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Body</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">originalBody</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">private</span><span class=\"mtk1\"> </span><span class=\"mtk4\">static</span><span class=\"mtk1\"> </span><span class=\"mtk4\">async</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Task</span><span class=\"mtk1\">&lt;</span><span class=\"mtk4\">string</span><span class=\"mtk1\">&gt; </span><span class=\"mtk11\">ReadRequestBody</span><span class=\"mtk1\">(</span><span class=\"mtk10\">HttpContext</span><span class=\"mtk1\"> </span><span class=\"mtk12\">context</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">buffer</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">MemoryStream</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk4\">await</span><span class=\"mtk1\"> </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Request</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Body</span><span class=\"mtk1\">.</span><span class=\"mtk11\">CopyToAsync</span><span class=\"mtk1\">(</span><span class=\"mtk12\">buffer</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Request</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Body</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">buffer</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">buffer</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Position</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">0</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">encoding</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">Encoding</span><span class=\"mtk1\">.</span><span class=\"mtk12\">UTF8</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">requestContent</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">await</span><span class=\"mtk1\"> </span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">StreamReader</span><span class=\"mtk1\">(</span><span class=\"mtk12\">buffer</span><span class=\"mtk1\">, </span><span class=\"mtk12\">encoding</span><span class=\"mtk1\">).</span><span class=\"mtk11\">ReadToEndAsync</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Request</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Body</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Position</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">0</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk12\">requestContent</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">private</span><span class=\"mtk1\"> </span><span class=\"mtk4\">async</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Task</span><span class=\"mtk1\"> </span><span class=\"mtk11\">RespondWithAnError</span><span class=\"mtk1\">(</span><span class=\"mtk10\">HttpContext</span><span class=\"mtk1\"> </span><span class=\"mtk12\">context</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Response</span><span class=\"mtk1\">.</span><span class=\"mtk11\">Clear</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Response</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Headers</span><span class=\"mtk1\">.</span><span class=\"mtk11\">AddHeaders</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Response</span><span class=\"mtk1\">.</span><span class=\"mtk12\">ContentType</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&quot;application/json; charset=utf-8&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Response</span><span class=\"mtk1\">.</span><span class=\"mtk12\">StatusCode</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">_statusCode</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">_error</span><span class=\"mtk1\"> == </span><span class=\"mtk4\">null</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk12\">_error</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">ErrorResponse</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk12\">Description</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&quot;Error from AntiXssMiddleware&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk12\">ErrorCode</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">500</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                };</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk4\">await</span><span class=\"mtk1\"> </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Response</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteAsync</span><span class=\"mtk1\">(</span><span class=\"mtk12\">_error</span><span class=\"mtk1\">.</span><span class=\"mtk11\">ToJSON</span><span class=\"mtk1\">());</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">static</span><span class=\"mtk1\"> </span><span class=\"mtk4\">class</span><span class=\"mtk1\"> </span><span class=\"mtk10\">AntiXssMiddlewareExtension</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">static</span><span class=\"mtk1\"> </span><span class=\"mtk10\">IApplicationBuilder</span><span class=\"mtk1\"> </span><span class=\"mtk11\">UseAntiXssMiddleware</span><span class=\"mtk1\">(</span><span class=\"mtk4\">this</span><span class=\"mtk1\"> </span><span class=\"mtk10\">IApplicationBuilder</span><span class=\"mtk1\"> </span><span class=\"mtk12\">builder</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk12\">builder</span><span class=\"mtk1\">.</span><span class=\"mtk11\">UseMiddleware</span><span class=\"mtk1\">&lt;</span><span class=\"mtk10\">AntiXssMiddleware</span><span class=\"mtk1\">&gt;();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk3\">/// </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">summary</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk3\">/// Imported from System.Web.CrossSiteScriptingValidation Class</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk3\">/// </span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">summary</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">static</span><span class=\"mtk1\"> </span><span class=\"mtk4\">class</span><span class=\"mtk1\"> </span><span class=\"mtk10\">CrossSiteScriptingValidation</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">private</span><span class=\"mtk1\"> </span><span class=\"mtk4\">static</span><span class=\"mtk1\"> </span><span class=\"mtk4\">readonly</span><span class=\"mtk1\"> </span><span class=\"mtk4\">char</span><span class=\"mtk1\">[] </span><span class=\"mtk12\">StartingChars</span><span class=\"mtk1\"> = { </span><span class=\"mtk8\">&#39;&lt;&#39;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&#39;&&#39;</span><span class=\"mtk1\"> };</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">        #region </span><span class=\"mtk8\">Public methods</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">static</span><span class=\"mtk1\"> </span><span class=\"mtk4\">bool</span><span class=\"mtk1\"> </span><span class=\"mtk11\">IsDangerousString</span><span class=\"mtk1\">(</span><span class=\"mtk4\">string</span><span class=\"mtk1\"> </span><span class=\"mtk12\">s</span><span class=\"mtk1\">, </span><span class=\"mtk4\">out</span><span class=\"mtk1\"> </span><span class=\"mtk4\">int</span><span class=\"mtk1\"> </span><span class=\"mtk12\">matchIndex</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk3\">//bool inComment = false;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">matchIndex</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">0</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk15\">for</span><span class=\"mtk1\"> (</span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">i</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">0</span><span class=\"mtk1\">; ;)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            {</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk3\">// Look for the start of one of our patterns </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">n</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">s</span><span class=\"mtk1\">.</span><span class=\"mtk11\">IndexOfAny</span><span class=\"mtk1\">(</span><span class=\"mtk12\">StartingChars</span><span class=\"mtk1\">, </span><span class=\"mtk12\">i</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk3\">// If not found, the string is safe</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">n</span><span class=\"mtk1\"> &lt; </span><span class=\"mtk7\">0</span><span class=\"mtk1\">) </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk4\">false</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk3\">// If it&#39;s the last char, it&#39;s safe </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">n</span><span class=\"mtk1\"> == </span><span class=\"mtk12\">s</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Length</span><span class=\"mtk1\"> - </span><span class=\"mtk7\">1</span><span class=\"mtk1\">) </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk4\">false</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk12\">matchIndex</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">n</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk15\">switch</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">s</span><span class=\"mtk1\">[</span><span class=\"mtk12\">n</span><span class=\"mtk1\">])</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk15\">case</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&#39;&lt;&#39;</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                        </span><span class=\"mtk3\">// If the &lt; is followed by a letter or &#39;!&#39;, it&#39;s unsafe (looks like a tag or HTML comment)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                        </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk11\">IsAtoZ</span><span class=\"mtk1\">(</span><span class=\"mtk12\">s</span><span class=\"mtk1\">[</span><span class=\"mtk12\">n</span><span class=\"mtk1\"> + </span><span class=\"mtk7\">1</span><span class=\"mtk1\">]) || </span><span class=\"mtk12\">s</span><span class=\"mtk1\">[</span><span class=\"mtk12\">n</span><span class=\"mtk1\"> + </span><span class=\"mtk7\">1</span><span class=\"mtk1\">] == </span><span class=\"mtk8\">&#39;!&#39;</span><span class=\"mtk1\"> || </span><span class=\"mtk12\">s</span><span class=\"mtk1\">[</span><span class=\"mtk12\">n</span><span class=\"mtk1\"> + </span><span class=\"mtk7\">1</span><span class=\"mtk1\">] == </span><span class=\"mtk8\">&#39;/&#39;</span><span class=\"mtk1\"> || </span><span class=\"mtk12\">s</span><span class=\"mtk1\">[</span><span class=\"mtk12\">n</span><span class=\"mtk1\"> + </span><span class=\"mtk7\">1</span><span class=\"mtk1\">] == </span><span class=\"mtk8\">&#39;?&#39;</span><span class=\"mtk1\">) </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk4\">true</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                        </span><span class=\"mtk15\">break</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                    </span><span class=\"mtk15\">case</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&#39;&&#39;</span><span class=\"mtk1\">:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                        </span><span class=\"mtk3\">// If the & is followed by a #, it&#39;s unsafe (e.g. S) </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                        </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">s</span><span class=\"mtk1\">[</span><span class=\"mtk12\">n</span><span class=\"mtk1\"> + </span><span class=\"mtk7\">1</span><span class=\"mtk1\">] == </span><span class=\"mtk8\">&#39;#&#39;</span><span class=\"mtk1\">) </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk4\">true</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                        </span><span class=\"mtk15\">break</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk3\">// Continue searching</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk12\">i</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">n</span><span class=\"mtk1\"> + </span><span class=\"mtk7\">1</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">        #endregion</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">        #region </span><span class=\"mtk8\">Private methods</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">private</span><span class=\"mtk1\"> </span><span class=\"mtk4\">static</span><span class=\"mtk1\"> </span><span class=\"mtk4\">bool</span><span class=\"mtk1\"> </span><span class=\"mtk11\">IsAtoZ</span><span class=\"mtk1\">(</span><span class=\"mtk4\">char</span><span class=\"mtk1\"> </span><span class=\"mtk12\">c</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">c</span><span class=\"mtk1\"> &gt;= </span><span class=\"mtk8\">&#39;a&#39;</span><span class=\"mtk1\"> && </span><span class=\"mtk12\">c</span><span class=\"mtk1\"> &lt;= </span><span class=\"mtk8\">&#39;z&#39;</span><span class=\"mtk1\">) || (</span><span class=\"mtk12\">c</span><span class=\"mtk1\"> &gt;= </span><span class=\"mtk8\">&#39;A&#39;</span><span class=\"mtk1\"> && </span><span class=\"mtk12\">c</span><span class=\"mtk1\"> &lt;= </span><span class=\"mtk8\">&#39;Z&#39;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">        #endregion</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">static</span><span class=\"mtk1\"> </span><span class=\"mtk4\">void</span><span class=\"mtk1\"> </span><span class=\"mtk11\">AddHeaders</span><span class=\"mtk1\">(</span><span class=\"mtk4\">this</span><span class=\"mtk1\"> </span><span class=\"mtk10\">IHeaderDictionary</span><span class=\"mtk1\"> </span><span class=\"mtk12\">headers</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">headers</span><span class=\"mtk1\">[</span><span class=\"mtk8\">&quot;P3P&quot;</span><span class=\"mtk1\">].</span><span class=\"mtk11\">IsNullOrEmpty</span><span class=\"mtk1\">())</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk12\">headers</span><span class=\"mtk1\">.</span><span class=\"mtk11\">Add</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;P3P&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&quot;CP=</span><span class=\"mtk6\">\\&quot;</span><span class=\"mtk8\">IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT</span><span class=\"mtk6\">\\&quot;</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">static</span><span class=\"mtk1\"> </span><span class=\"mtk4\">bool</span><span class=\"mtk1\"> </span><span class=\"mtk11\">IsNullOrEmpty</span><span class=\"mtk1\">&lt;</span><span class=\"mtk10\">T</span><span class=\"mtk1\">&gt;(</span><span class=\"mtk4\">this</span><span class=\"mtk1\"> </span><span class=\"mtk10\">IEnumerable</span><span class=\"mtk1\">&lt;</span><span class=\"mtk10\">T</span><span class=\"mtk1\">&gt; </span><span class=\"mtk12\">source</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk12\">source</span><span class=\"mtk1\"> == </span><span class=\"mtk4\">null</span><span class=\"mtk1\"> || !</span><span class=\"mtk12\">source</span><span class=\"mtk1\">.</span><span class=\"mtk11\">Any</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">static</span><span class=\"mtk1\"> </span><span class=\"mtk4\">string</span><span class=\"mtk1\"> </span><span class=\"mtk11\">ToJSON</span><span class=\"mtk1\">(</span><span class=\"mtk4\">this</span><span class=\"mtk1\"> </span><span class=\"mtk4\">object</span><span class=\"mtk1\"> </span><span class=\"mtk12\">value</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk12\">JsonConvert</span><span class=\"mtk1\">.</span><span class=\"mtk11\">SerializeObject</span><span class=\"mtk1\">(</span><span class=\"mtk12\">value</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">class</span><span class=\"mtk1\"> </span><span class=\"mtk10\">ErrorResponse</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">int</span><span class=\"mtk1\"> </span><span class=\"mtk12\">ErrorCode</span><span class=\"mtk1\"> { </span><span class=\"mtk4\">get</span><span class=\"mtk1\">; </span><span class=\"mtk4\">set</span><span class=\"mtk1\">; }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">string</span><span class=\"mtk1\"> </span><span class=\"mtk12\">Description</span><span class=\"mtk1\"> { </span><span class=\"mtk4\">get</span><span class=\"mtk1\">; </span><span class=\"mtk4\">set</span><span class=\"mtk1\">; }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p>In the above file we have created the method for checking the Xss in QueryParam, RequestUri and RequestBody. </p>\n<p>Here we have different methods which are as follows:-</p>\n<p><strong>ReadRequestBody</strong> which is used for reading the RequestBody.</p>\n<p><strong>RespondWithAnError</strong> which is used for returning the error.</p>\n<p><strong>IsDangerousString</strong> which is checking if there is any dangerous string like any script in the given string.</p>\n<p><strong>Step 7:</strong> Edit the Startup.cs file and add below line in <strong>Configure</strong> method.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">app</span><span class=\"mtk1\">.</span><span class=\"mtk11\">UseAntiXssMiddleware</span><span class=\"mtk1\">();</span></span></code></pre>\n<p><strong>Step 8 :</strong> After editing the Startup.cs file will look like below</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">System</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">System</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Collections</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Generic</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">System</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Linq</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">System</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Threading</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Tasks</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">AntiXssMiddleware</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Middleware</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Microsoft</span><span class=\"mtk1\">.</span><span class=\"mtk10\">AspNetCore</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Builder</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Microsoft</span><span class=\"mtk1\">.</span><span class=\"mtk10\">AspNetCore</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Hosting</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Microsoft</span><span class=\"mtk1\">.</span><span class=\"mtk10\">AspNetCore</span><span class=\"mtk1\">.</span><span class=\"mtk10\">HttpsPolicy</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Microsoft</span><span class=\"mtk1\">.</span><span class=\"mtk10\">AspNetCore</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Mvc</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Microsoft</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Extensions</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Configuration</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Microsoft</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Extensions</span><span class=\"mtk1\">.</span><span class=\"mtk10\">DependencyInjection</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Microsoft</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Extensions</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Hosting</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">using</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Microsoft</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Extensions</span><span class=\"mtk1\">.</span><span class=\"mtk10\">Logging</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">namespace</span><span class=\"mtk1\"> </span><span class=\"mtk10\">AntiXssMiddleware</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">{</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">class</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Startup</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk11\">Startup</span><span class=\"mtk1\">(</span><span class=\"mtk10\">IConfiguration</span><span class=\"mtk1\"> </span><span class=\"mtk12\">configuration</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">Configuration</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">configuration</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk10\">IConfiguration</span><span class=\"mtk1\"> </span><span class=\"mtk12\">Configuration</span><span class=\"mtk1\"> { </span><span class=\"mtk4\">get</span><span class=\"mtk1\">; }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk3\">// This method gets called by the runtime. Use this method to add services to the container.</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">void</span><span class=\"mtk1\"> </span><span class=\"mtk11\">ConfigureServices</span><span class=\"mtk1\">(</span><span class=\"mtk10\">IServiceCollection</span><span class=\"mtk1\"> </span><span class=\"mtk12\">services</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">services</span><span class=\"mtk1\">.</span><span class=\"mtk11\">AddControllers</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk3\">// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">void</span><span class=\"mtk1\"> </span><span class=\"mtk11\">Configure</span><span class=\"mtk1\">(</span><span class=\"mtk10\">IApplicationBuilder</span><span class=\"mtk1\"> </span><span class=\"mtk12\">app</span><span class=\"mtk1\">, </span><span class=\"mtk10\">IWebHostEnvironment</span><span class=\"mtk1\"> </span><span class=\"mtk12\">env</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">env</span><span class=\"mtk1\">.</span><span class=\"mtk11\">IsDevelopment</span><span class=\"mtk1\">())</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk12\">app</span><span class=\"mtk1\">.</span><span class=\"mtk11\">UseDeveloperExceptionPage</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            }</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">app</span><span class=\"mtk1\">.</span><span class=\"mtk11\">UseHttpsRedirection</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">app</span><span class=\"mtk1\">.</span><span class=\"mtk11\">UseAntiXssMiddleware</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">app</span><span class=\"mtk1\">.</span><span class=\"mtk11\">UseRouting</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">app</span><span class=\"mtk1\">.</span><span class=\"mtk11\">UseAuthorization</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">app</span><span class=\"mtk1\">.</span><span class=\"mtk11\">UseEndpoints</span><span class=\"mtk1\">(</span><span class=\"mtk12\">endpoints</span><span class=\"mtk1\"> =&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                </span><span class=\"mtk12\">endpoints</span><span class=\"mtk1\">.</span><span class=\"mtk11\">MapControllers</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            });</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p><strong>Step 9:</strong> Now build and run the solution.</p>\n<p>As we run the default API which is <code>https://localhost:44369/weatherforecast</code> we will get the below response.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"json\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">[</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;date&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;2020-08-21T11:58:40.0289718+05:30&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;temperatureC&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk7\">27</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;temperatureF&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk7\">80</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;summary&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;Sweltering&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    },</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;date&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;2020-08-22T11:58:40.0289896+05:30&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;temperatureC&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk7\">21</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;temperatureF&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk7\">69</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;summary&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;Cool&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    },</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;date&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;2020-08-23T11:58:40.0289899+05:30&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;temperatureC&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk7\">-20</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;temperatureF&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk7\">-3</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;summary&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;Hot&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    },</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;date&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;2020-08-24T11:58:40.0289901+05:30&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;temperatureC&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk7\">21</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;temperatureF&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk7\">69</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;summary&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;Sweltering&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    },</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;date&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;2020-08-25T11:58:40.0289902+05:30&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;temperatureC&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk7\">2</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;temperatureF&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk7\">35</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">&quot;summary&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;Balmy&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">]</span></span></code></pre>\n<p>Now if we inject any script in the above url like <code>https://localhost:44369/weatherforecast&#x3C;script>&#x3C;/script></code> we will get the response as </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"json\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">{</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">&quot;ErrorCode&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk7\">500</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">&quot;Description&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;Error from AntiXssMiddleware&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p><strong>Note:</strong> </p>\n<ol>\n<li>The default port may be different when you run the project. So change the port accordingly.</li>\n<li>You can customize the error message according to your need.</li>\n</ol>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>In this blog, we learnt about how to implement AntiXssMiddlware in ASP.NET Core Web Application Project. We have implemented the AntiXssMiddleware in API's QueryParam, ReuqestUri and RequestBody. So if any script is injected in QueryParam, RequestUri or RequestBody then it will give the error.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk17 { color: #808080; }\n  .dark-default-dark .mtk6 { color: #D7BA7D; }\n</style>","frontmatter":{"date":"August 26, 2020","updated_date":null,"description":null,"title":"Implement AntiXssMiddleware in .NET Core Web","tags":["C#","ASP.NET"],"pinned":null,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/813c7a5a008113481ac2c05836830d14/ee604/antixss.png","srcSet":"/static/813c7a5a008113481ac2c05836830d14/69585/antixss.png 200w,\n/static/813c7a5a008113481ac2c05836830d14/497c6/antixss.png 400w,\n/static/813c7a5a008113481ac2c05836830d14/ee604/antixss.png 800w,\n/static/813c7a5a008113481ac2c05836830d14/f3583/antixss.png 1200w,\n/static/813c7a5a008113481ac2c05836830d14/5707d/antixss.png 1600w,\n/static/813c7a5a008113481ac2c05836830d14/eeb1b/antixss.png 1920w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Hemant Manwani","github":"hemant404","avatar":null}}}},{"node":{"excerpt":"In this post, we will look at the step-by-step process for Kafka Installation on Windows. Kafka is an open-source stream-processing software…","fields":{"slug":"/engineering/quick-kafka-installation/"},"html":"<p>In this post, we will look at the step-by-step process for Kafka Installation on Windows. Kafka is an open-source stream-processing software platform and comes under the Apache software foundation.</p>\n<h2 id=\"what-is-kafka\" style=\"position:relative;\"><a href=\"#what-is-kafka\" aria-label=\"what is kafka permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>What is Kafka?</strong></h2>\n<p>Kafka is used for real-time streams of data, to collect big data, or to do real-time analysis (or both). Kafka is used with in-memory microservices to provide durability and it can be used to feed events to complex event streaming systems and IoT/IFTTT-style automation systems. </p>\n<h2 id=\"installation-\" style=\"position:relative;\"><a href=\"#installation-\" aria-label=\"installation  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Installation :</strong></h2>\n<h3 id=\"1-java-setup\" style=\"position:relative;\"><a href=\"#1-java-setup\" aria-label=\"1 java setup permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Java Setup:</h3>\n<p>Kafka requires Java 8 for running. And hence, this is the first step that we should do to install Kafka. To install Java, there are a couple of options. We can go for the Oracle JDK version 8 from the <a href=\"https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html\">Official Oracle Website</a>.</p>\n<h3 id=\"2-kafka--zookeeper-configuration\" style=\"position:relative;\"><a href=\"#2-kafka--zookeeper-configuration\" aria-label=\"2 kafka  zookeeper configuration permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. Kafka &#x26; Zookeeper Configuration:</h3>\n<p><strong>Step 1:</strong> Download Apache Kafka from its <a href=\"https://kafka.apache.org/downloads\">Official Site</a>.</p>\n<p><strong>Step 2:</strong> Extract tgz via cmd or from the available tool  to a location of your choice:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">tar -xvzf kafka_2.12-2.4.1.tgz</span></code></pre>\n<p><strong>Step 3:</strong> Copy the path of the Kafka folder. Now go to <em>config</em> inside Kafka folder and open <em>zookeeper.properties</em> file. Copy the path against the field <em>dataDir</em> and add <em>/zookeeper-data</em> to the path.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 636px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 41.35220125786164%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABZklEQVQoz21Ria6cMAzk/3+xYoHcB0sSjsCy6k7tvPekqmqkkZ0hNmNP93g8MOkJfndQWSFsAVMaMSwj3OoQ9wi/OYgsoYvCfMywq6VcwxHP4DvXiSzQ9X2PUY3wh2+ELJIeWUjKzWrajxxhTAMM8df7wnqtWI4F27Wh3hXnfeIknvNuYIVygo0WPmg8nxZmtlCOFEUNaQXs7CA9RfpW32crLGfG/jrw7+l4ZCFJTTQQVDxTjNTcJY+0Z8QcKSaEErDWguM+SFHFUpem6r8NRzGSQoPRTXCkUpsJMzWq79qw3zvu33dTxmPyHs/32ZBqQia1zG+v/bvhNLRxRBBwiyO1GiEqpCujXIV2xtiaqnIWKl7bHpnnhgc1Yp7ffpkiBuinhkmGooIi2ESu+wG9/QWTTXM77oEcdaSQnfeNY7WB+J979+jJFBpZ06is0lAUfmqGBC+hgsTrvr8W9PkOn0/Dz/k7/wPAdV3BuDwGNAAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"zookeeper\"\n        title=\"zookeeper\"\n        src=\"/static/726644262b05de677792b4af683311e0/9be90/zookeeper.png\"\n        srcset=\"/static/726644262b05de677792b4af683311e0/9be90/zookeeper.png 636w\"\n        sizes=\"(max-width: 636px) 100vw, 636px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span>\n<strong>Step 4:</strong> we have to modify the config/server.properties file. Below is the change:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">fileslog.dirs=C:\\kafka\\kafka-logs</span></code></pre>\n<p>Basically, we are pointing the log.dirs to the new folder /data/kafka.</p>\n<h2 id=\"run-kafka-server\" style=\"position:relative;\"><a href=\"#run-kafka-server\" aria-label=\"run kafka server permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Run Kafka Server:</strong></h2>\n<p><strong>Step 1:</strong> Kafka requires Zookeeper to run. Basically, Kafka uses Zookeeper to manage the entire cluster and various brokers. Therefore, a running instance of Zookeeper is a prerequisite to Kafka.</p>\n<p>To start Zookeeper, we can open a PowerShell prompt and execute the below command:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">.\\bin\\windows\\zookeeper-server-start.bat .\\config\\zookeeper.properties</span></code></pre>\n<p>If the command is successful, Zookeeper will start on port 2181.</p>\n<p><strong>Step 2:</strong> Now open another command prompt and change the directory to the kafka folder. Run kafka server using the command: </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">.\\bin\\windows\\kafka-server-start.bat .\\config\\server.properties</span></code></pre>\n<p><strong>Now your Kafka Server is up and running</strong>, you can create topics to store messages. Also, we can produce or consume data directly from the command prompt.</p>\n<h2 id=\"create-a-kafka-topic\" style=\"position:relative;\"><a href=\"#create-a-kafka-topic\" aria-label=\"create a kafka topic permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Create a Kafka Topic:</strong></h2>\n<ol>\n<li>Open a new command prompt in the location C:\\kafka\\bin\\windows.</li>\n<li>Run the following command:</li>\n</ol>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test</span></code></pre>\n<h2 id=\"creating-kafka-producer\" style=\"position:relative;\"><a href=\"#creating-kafka-producer\" aria-label=\"creating kafka producer permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Creating Kafka Producer:</strong></h2>\n<ol>\n<li>Open a new command prompt in the location C:\\kafka\\bin\\windows</li>\n<li>Run the following command:</li>\n</ol>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">kafka-console-producer.bat --broker-list localhost:9092 --topic test</span></code></pre>\n<h2 id=\"creating-kafka-consumer\" style=\"position:relative;\"><a href=\"#creating-kafka-consumer\" aria-label=\"creating kafka consumer permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Creating Kafka Consumer:</strong></h2>\n<ol>\n<li>Open a new command prompt in the location C:\\kafka\\bin\\windows.</li>\n<li>Run the following command:</li>\n</ol>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning</span></code></pre>\n<p>If you see these messages on consumer console,<em>Congratulations!!!</em> you all done. Then you can play with producer and consumer terminal bypassing some Kafka messages.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","frontmatter":{"date":"August 25, 2020","updated_date":null,"description":null,"title":"Setting Up and Running Apache Kafka on Windows OS","tags":["Kafka","Windows"],"pinned":null,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/cf0f7f91117e9c259a3ad57b67a89c15/ee604/messagelog.png","srcSet":"/static/cf0f7f91117e9c259a3ad57b67a89c15/69585/messagelog.png 200w,\n/static/cf0f7f91117e9c259a3ad57b67a89c15/497c6/messagelog.png 400w,\n/static/cf0f7f91117e9c259a3ad57b67a89c15/ee604/messagelog.png 800w,\n/static/cf0f7f91117e9c259a3ad57b67a89c15/f3583/messagelog.png 1200w,\n/static/cf0f7f91117e9c259a3ad57b67a89c15/5707d/messagelog.png 1600w,\n/static/cf0f7f91117e9c259a3ad57b67a89c15/7ddcb/messagelog.png 2700w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Ashish Sharma","github":"ashish8947","avatar":null}}}},{"node":{"excerpt":"Getting Started with OAuth 2.0 OAuth has been a jargon for quite some time now and it is difficult for a beginner to learn it, not because…","fields":{"slug":"/engineering/oauth2/"},"html":"<h1 id=\"getting-started-with-oauth-20\" style=\"position:relative;\"><a href=\"#getting-started-with-oauth-20\" aria-label=\"getting started with oauth 20 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Getting Started with OAuth 2.0</h1>\n<p>OAuth has been a jargon for quite some time now and it is difficult for a beginner to learn it, not because OAuth is hard, but because of the confusing facts found about OAuth on the web. So I wrote this article to explain why and how OAuth is used in very simple terms.</p>\n<p>Let’s start with the basics: OAuth stands for Open Authorization. It’s a process through which an application or website can access private data from another website.\nIt provides applications the ability for “secure designated access.” For example, you can tell Google that it’s OK for abc.com to access your google account or contact without having to give abc.com your google password. </p>\n<p>OAuth never share password data but instead uses authorization tokens to prove an identity between consumers and service providers. OAuth is an authentication protocol that allows you to approve one application interacting with another on your behalf without giving away your password.</p>\n<h3 id=\"now-lets-have-a-look-at-oauth-20-terminology\" style=\"position:relative;\"><a href=\"#now-lets-have-a-look-at-oauth-20-terminology\" aria-label=\"now lets have a look at oauth 20 terminology permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Now Let’s have a look at OAuth 2.0 Terminology.</h3>\n<ul>\n<li><strong>Resource Owner:</strong> The resource owner is the user who authorizes an application to access their account. The application's access to the user's account is limited to the “scope” of the authorization granted (e.g., read or write access).</li>\n<li><strong>Client:</strong> The client is the application that’s trying to access the user's account. It needs to get permission from the user before accessing the account. For example, a client application can present the user with the login page to get an access token for access to a particular resource.</li>\n<li><strong>Authorization Server:</strong> The authorization server validates the user credentials and redirects the user back to the client with an authorization code. The client communicates with the authorization server to confirm its identity and exchanges the code for an access token.</li>\n<li><strong>Resource Server:</strong> A resource server is a server for access-protected resources. It handles authenticated requests from an app that has an access token. </li>\n<li><strong>Scope:</strong> It specifies the level of access that the application is requesting from the client.</li>\n<li><strong>Consent:</strong> The consent screen tells your users who is requesting access to their data and what kind of data you're asking to access. </li>\n</ul>\n<p>We have a pretty good understanding of OAuth 2.0 and Terminology, let’s move further and discuss the OAuth grant type that is widely used in this protocol.</p>\n<p>In total, there are five different grant type flows defined and described to perform authorizations tasks. Those are</p>\n<ul>\n<li>Authorization Code Grant</li>\n<li>Implicit Grant</li>\n<li>Resource Owner Credentials Grant</li>\n<li>Client Credentials Grant</li>\n<li>Refresh Token Grant</li>\n</ul>\n<h3 id=\"authorization-code-grant\" style=\"position:relative;\"><a href=\"#authorization-code-grant\" aria-label=\"authorization code grant permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Authorization Code Grant</h3>\n<p>The Authorization Code Grant Type is the most commonly used grant type.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 56.30769230769231%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAABP0lEQVQoz6VSy27DIBD0/39VD1WkSJHaWw7Oo41xEhw/sF1YMI90AokVVZV66EhGZtnZnR3Irv9Ahs+GIEcRtJyjIeJXgo1Ipzeydo73wljjnpJwPEX8YFZVxTkfhuFOpq69LF7k21Keysl5EKSURVGUZTmOI7bGGKzOOaVUvtnuPj77B9kT23XL12G1GI9MINuYtm3zPO/7fhbvvUdbKdWZV0XB6rrRWmdh6NTqPQluOrHdbBhjaHVTRCSEwIr+0IKg8x7lbBwH8ZvsS90cGQvW8vN5vV6jJ1JRuItomgbkWQJpzcoTkX4YRoRhIAxTJTPBRPxwOOz3e/wj8mxkcaznma/JIYolAKT6KA8RjJ2sSkAcO/u4gjtZk7LR1cTEimnBxJVA80wOwZO245ckpYMPWXJyLvznq0qZNRcTTd8fTX5uA9GrbgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Authorization Code Grant\"\n        title=\"Authorization Code Grant\"\n        src=\"/static/c45e678b01cae83852178c3e0fd38eda/e5715/image4.png\"\n        srcset=\"/static/c45e678b01cae83852178c3e0fd38eda/a6d36/image4.png 650w,\n/static/c45e678b01cae83852178c3e0fd38eda/e5715/image4.png 768w,\n/static/c45e678b01cae83852178c3e0fd38eda/d9199/image4.png 960w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p><strong>The Story:</strong> A user tries to log in on  abc.com  but he can’t remember his password and he discovers an option to sign in with google, by clicking on this, the user will easily get logged using google account.</p>\n<p><strong>Flow</strong></p>\n<p>The client redirects the user to the authorization server having the following parameters in the query string.</p>\n<p><strong>Step 1</strong></p>\n<ul>\n<li><strong>response_type</strong> having the value code</li>\n<li><strong>client_id</strong> having the client identifier</li>\n<li><strong>redirect_uri</strong> having the client redirect URI. </li>\n<li><strong>scope</strong> a space-delimited list of scopes</li>\n<li><strong>state</strong> having a random string </li>\n</ul>\n<p>After successful authentication, the user will be redirected to the Consent screen where he needs to provide consent to abc.com to access the account detail.\nAuthorization code is generated by the authorization server and sent back to the client with redirect Uri.</p>\n<p><strong>Step 2</strong>\nThe client will now send a POST request to the authorization server with the following parameters:</p>\n<ul>\n<li><strong>grant_type</strong> having the value of authorization_code</li>\n<li><strong>client_id</strong> having the client identifier</li>\n<li><strong>client_secret</strong> having the client secret</li>\n<li><strong>redirect_uri</strong> having the same redirect URI the user redirected back.</li>\n<li><strong>code</strong> having the authorization code from the query string</li>\n</ul>\n<p>In the entire flow, the access token is never exposed to a web browser.</p>\n<h3 id=\"implicit-grant\" style=\"position:relative;\"><a href=\"#implicit-grant\" aria-label=\"implicit grant permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Implicit Grant</h3>\n<p>The Implicit flow was a simplified OAuth flow previously recommended for client-side applications like JavaScript apps where the access token was returned immediately without an extra authorization code exchange step.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 56.30769230769231%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAABHklEQVQoz6WRW0+EMBCF+f//ShOzGk185EEQDQW2UFwKlBl6wwHEXS8bN/EkTQqZr3PmTDD9QwEd473qGo/qEsAs8t5/wGgtl81oRntS5JyjImtP/030WZYl57xt2w0+vIndlXq8U0WmrRsRiazrOkmSqqrGcdSL6C2l1FMUPb+8yg12wGKxu27vb9oslV03qNl/nucE933/pbNznJdpyoSoETGYuqa9fZB6tlcfDnEcM8ao27mZaVqzGAGA2XYp6iJjnibf78MwjKNohf2ibzAgsqwAwG1mgGEYKIyfpb92TnMhj4EhUnqrE+ppz4uC1NqS7eOeCUMYrDFEUqTrkqSUzSK6rDtbrDlA0/UKBvTOB6uTz4f/tL1WCt5o0O8PqYFDZ9VclAAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Implicit Grant\"\n        title=\"Implicit Grant\"\n        src=\"/static/e3f47393896df6a6eaa11af651d2d857/e5715/image2.png\"\n        srcset=\"/static/e3f47393896df6a6eaa11af651d2d857/a6d36/image2.png 650w,\n/static/e3f47393896df6a6eaa11af651d2d857/e5715/image2.png 768w,\n/static/e3f47393896df6a6eaa11af651d2d857/d9199/image2.png 960w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>The Story: In this flow abc.com directly get access token without an extra authorization code exchange steps and able to access resources on a resource server</p>\n<p><strong>Flow</strong></p>\n<p>The client will redirect the user to the authorization server with the following parameters in the query string:</p>\n<ul>\n<li><strong>response_type</strong> having the value token</li>\n<li><strong>client_id</strong> having the client identifier</li>\n<li><strong>redirect_uri</strong> having the client redirect URI. </li>\n<li><strong>scope</strong> a space-delimited list of scopes</li>\n<li><strong>state</strong> having the random string</li>\n</ul>\n<p>It is not recommended to use the implicit flow (and some servers prohibit this flow entirely) due to the inherent risks of returning access tokens in an HTTP redirect without any confirmation that it has been received by the client.</p>\n<h3 id=\"resource-owner-credentials-grant\" style=\"position:relative;\"><a href=\"#resource-owner-credentials-grant\" aria-label=\"resource owner credentials grant permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Resource Owner Credentials Grant</h3>\n<p>The resource owner password credentials grant type is suitable in cases where the resource owner has a trust relationship with the client, such as a highly privileged application. The authorization server should take special care when enabling this grant type and only allow it when other flows are not viable.</p>\n<p>This grant type is suitable for clients capable of obtaining the resource owner’s credentials (username and password, typically using an interactive form). It is also used to migrate existing clients using direct authentication schemes such as HTTP Basic or Digest authentication to OAuth by converting the stored credentials to an access token.</p>\n<p><strong>Flow</strong></p>\n<p>The client will ask the user for their authorization credentials (usually a username and password).\nThe client then sends a POST request with following body parameters to the authorization server:</p>\n<ul>\n<li><strong>grant_type</strong> having the value password</li>\n<li><strong>client_id</strong> having the client’s ID</li>\n<li><strong>client_secret</strong> having the client’s secret</li>\n<li><strong>scope</strong> having a space-delimited list of requested scope permissions.</li>\n<li><strong>username</strong> having  a user’s username</li>\n<li><strong>password</strong> having a user’s password</li>\n</ul>\n<h3 id=\"client-credentials-grant\" style=\"position:relative;\"><a href=\"#client-credentials-grant\" aria-label=\"client credentials grant permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Client Credentials Grant</h3>\n<p>Using this flow the client can request an access token using only its client credentials (or other supported means of authentication).</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 56.30769230769231%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAh0lEQVQoz8WQTQvCMBBE+///oHgqgocKASGGdLObzDrGr+KxBfsguWzezpDBNzDwGJDn5Cpr5XxzndfIG2u7XE4yTdbgwHOQUgohxBiXr82slKKqIgKAMqp7PB7yeGb/arwetA4WUKZz7XD1K5lCqdVa41a8Qzj+iSVcp51Pcgffwn/8sH3kO1t0jcbKdW8qAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Client Credentials Grant\"\n        title=\"Client Credentials Grant\"\n        src=\"/static/50e12cb65c96fbbf7e1d502e6dacc539/e5715/image3.png\"\n        srcset=\"/static/50e12cb65c96fbbf7e1d502e6dacc539/a6d36/image3.png 650w,\n/static/50e12cb65c96fbbf7e1d502e6dacc539/e5715/image3.png 768w,\n/static/50e12cb65c96fbbf7e1d502e6dacc539/d9199/image3.png 960w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p><strong>The Story:</strong> The client application presents its client credentials (client identifier and client secret) to the authorization server requesting approval to access the protected resource (owned by the client application) on the resource server.\nThe authorization server authenticates the client credential and issues an access token.</p>\n<p><strong>Flow</strong></p>\n<p>The client sends a POST request with following body parameters to the authorization server:</p>\n<ul>\n<li><strong>grant_type</strong> having the value client_credentials</li>\n<li><strong>client_id</strong> having the client’s ID</li>\n<li><strong>client_secret</strong> having the client’s secret</li>\n<li><strong>scope</strong> having a space-delimited list of requested scope permissions.</li>\n</ul>\n<h3 id=\"refresh-token-grant\" style=\"position:relative;\"><a href=\"#refresh-token-grant\" aria-label=\"refresh token grant permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Refresh Token Grant</h3>\n<p>Access tokens eventually expire, however, some grants respond with a refresh token which enables the client to refresh the access token.</p>\n<p><strong>Flow</strong></p>\n<p>The client sends a POST request with following body parameters to the authorization server:</p>\n<ul>\n<li><strong>grant_type</strong> having the value refresh_token</li>\n<li><strong>refresh_token</strong> having the refresh token</li>\n<li><strong>client_id</strong> having the client’s ID</li>\n<li><strong>client_secret</strong> having the client’s secret</li>\n<li><strong>scope</strong> having a space-delimited list of requested scope permissions. </li>\n</ul>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>I hope you got an idea of how OAuth works and why it is needed. Now it’s time for you to go explore, find out more about the OAuth flow and implement it into your application.\nGood Luck and have fun! Thank you for following this article and hope it helped you! Please do buzz me if you want any help: indrasen.kumar@loginradius.com</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n</style>","frontmatter":{"date":"August 24, 2020","updated_date":null,"description":"Using this blog one can easily understand the basic concept of Oauth 2.0","title":"Getting Started with OAuth 2.0","tags":["Engineering","Oauth","Authentication"],"pinned":null,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.7699115044247788,"src":"/static/e6413c80a3d38d63b14543319d3c54dd/ee604/oauth2.png","srcSet":"/static/e6413c80a3d38d63b14543319d3c54dd/69585/oauth2.png 200w,\n/static/e6413c80a3d38d63b14543319d3c54dd/497c6/oauth2.png 400w,\n/static/e6413c80a3d38d63b14543319d3c54dd/ee604/oauth2.png 800w,\n/static/e6413c80a3d38d63b14543319d3c54dd/40ffe/oauth2.png 960w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Indrasen Kumar","github":"indrasen715","avatar":null}}}},{"node":{"excerpt":"What is the Rest API? RESTful programming provides stateless and a uniform interface, Rest API is HTTP-based URLs that hide the back-end…","fields":{"slug":"/engineering/best-practice-guide-for-rest-api-security/"},"html":"<h3 id=\"what-is-the-rest-api\" style=\"position:relative;\"><a href=\"#what-is-the-rest-api\" aria-label=\"what is the rest api permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is the Rest API?</h3>\n<p>RESTful programming provides stateless and a uniform interface, Rest API is HTTP-based URLs that hide the back-end infrastructure from the user. Rest APIs provide the back end for modern web and mobile applications. </p>\n<h3 id=\"why-is-api-security-important\" style=\"position:relative;\"><a href=\"#why-is-api-security-important\" aria-label=\"why is api security important permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why is API security important?</h3>\n<p>Rest APIs are the most important layer in the back-end infrastructure for most modern applications. Cybercriminals are increasingly targeting APIs. Ensuring web API security is the most important and crucial. Let’s see what you can do to ensure REST API security.</p>\n<h3 id=\"common-security-vulnerabilities--mistakes-and-best-practices-to-secure-rest-apis\" style=\"position:relative;\"><a href=\"#common-security-vulnerabilities--mistakes-and-best-practices-to-secure-rest-apis\" aria-label=\"common security vulnerabilities  mistakes and best practices to secure rest apis permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Common Security Vulnerabilities &#x26; Mistakes and Best Practices to Secure REST APIs</h3>\n<h4 id=\"always-use-https\" style=\"position:relative;\"><a href=\"#always-use-https\" aria-label=\"always use https permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Always Use HTTPS</h4>\n<p>API security start with Http Connection. All requests from clients to your API should be encrypted (HTTPS). Unfortunately, many client HTTP do not enable HTTPS/secure connections by default it’s necessary to enforce that from the server. When Clients who attempt to connect via HTTP should forcefully be redirected to secure HTTPS connections. </p>\n<p>You can use HTTP Strict Transport Security security header enforcing Https for web and You can return error for API in case Rest API call on HTTP</p>\n<p>You can get a free certificate with Let's Encrypt. SSL provides security from basic API vulnerabilities with almost minimal effort</p>\n<h3 id=\"distributed-denial-of-service-attacks-ddos\" style=\"position:relative;\"><a href=\"#distributed-denial-of-service-attacks-ddos\" aria-label=\"distributed denial of service attacks ddos permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Distributed Denial of Service Attacks (DDoS)</h3>\n<p>A Distributed Denial of Service (DDoS) is a targeted cyber attack on a web site or device where a malicious attacker flood of traffic is sent from single or multiple sources. the main purpose of DDos is to make a machine or network resource unavailable to its genuine users by temporarily or disrupting services of a host connected to the Internet. if we are not using appropriate security practice or tools then it makes RESTful API into a non-functional situation.</p>\n<h6 id=\"how-to-prevent-or-stop-ddos-attacks\" style=\"position:relative;\"><a href=\"#how-to-prevent-or-stop-ddos-attacks\" aria-label=\"how to prevent or stop ddos attacks permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to Prevent or Stop DDoS Attacks</h6>\n<p>API DoS attacks are more common these days. Rest APIs utilizations also increasing day-by-day. The organization's dependency is increasing day-by-day because of business needed a unified platform. An attacker can use multiple ways for the DDoS attack so as developer or security engineer you need to implement long-term solution not a temporary</p>\n<h5 id=\"rate-limit\" style=\"position:relative;\"><a href=\"#rate-limit\" aria-label=\"rate limit permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Rate Limit</h5>\n<p> Attackers can make so many repeated calls on the APIs. it can make resources unavailable to its genuine users. A rate limit is the number of API calls an app or user can make within a given period. When this limit is exceeded, block API access temporarily and return the 429 (too many requests) HTTP error code.</p>\n<p>I m adding node js examples to implement the rate limit. multiple npm packages are available for node js</p>\n<p> <strong>NodeJs</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> </span><span class=\"mtk12\">rateLimit</span><span class=\"mtk1\"> </span><span class=\"mtk15\">from</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&#39;express-rate-limit&#39;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">export</span><span class=\"mtk1\"> </span><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">apiRatelimit</span><span class=\"mtk1\"> = </span><span class=\"mtk11\">rateLimit</span><span class=\"mtk1\">({</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">windowMs:</span><span class=\"mtk1\"> </span><span class=\"mtk7\">60</span><span class=\"mtk1\"> * </span><span class=\"mtk7\">60</span><span class=\"mtk1\"> * </span><span class=\"mtk7\">1000</span><span class=\"mtk1\">, </span><span class=\"mtk3\">// 1 hrs in milliseconds</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">max:</span><span class=\"mtk1\"> </span><span class=\"mtk7\">100</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">message:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&#39;You have exceeded the 100 requests in 1 hrs limit!&#39;</span><span class=\"mtk1\">, </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">headers:</span><span class=\"mtk1\"> </span><span class=\"mtk4\">true</span><span class=\"mtk1\">, </span><span class=\"mtk3\">// it will add X-RateLimit-Limit , X-RateLimit-Remaining and Retry-After Headers in the request </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">});</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">//  you can add this in the middleware. it will apply rate limit for the all requests </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">app</span><span class=\"mtk1\">.</span><span class=\"mtk11\">use</span><span class=\"mtk1\">(</span><span class=\"mtk12\">apiRatelimit</span><span class=\"mtk1\">);</span></span></code></pre>\n<h5 id=\"passive-cache\" style=\"position:relative;\"><a href=\"#passive-cache\" aria-label=\"passive cache permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Passive cache</h5>\n<p>Active cache means if the service first attempts to read from the cache backend and falls back to reading from the actual source. The service is not dependent or requesting the data from the actual upstream server. a cache backend is a key-value store (e.g. Redis) or In-Memory cache and the actual source of data is an SQL, MongoDB, etc.</p>\n<p>Passive cache architecture ensures high volume traffic never hit to actual server or service.</p>\n<p>I m adding node js examples to implement the passive cache. multiple npm packages are available for node js</p>\n<p> <strong>NodeJs</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> </span><span class=\"mtk12\">nodeCache</span><span class=\"mtk1\"> </span><span class=\"mtk15\">from</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;node-cache&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">myCache</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">nodeCache</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">// set object in the cache </span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">obj</span><span class=\"mtk1\"> = { </span><span class=\"mtk12\">userid:</span><span class=\"mtk1\"> </span><span class=\"mtk7\">909887</span><span class=\"mtk1\">, </span><span class=\"mtk12\">name:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;example&quot;</span><span class=\"mtk1\"> };</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">success</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">myCache</span><span class=\"mtk1\">.</span><span class=\"mtk11\">set</span><span class=\"mtk1\">( </span><span class=\"mtk8\">&quot;userKey&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">obj</span><span class=\"mtk1\">, </span><span class=\"mtk7\">600</span><span class=\"mtk1\"> ); </span><span class=\"mtk3\">// ttl is 600 seconds </span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">//read object from the cache </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">value</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">myCache</span><span class=\"mtk1\">.</span><span class=\"mtk11\">get</span><span class=\"mtk1\">( </span><span class=\"mtk8\">&quot;userKey&quot;</span><span class=\"mtk1\"> );</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">if</span><span class=\"mtk1\"> ( </span><span class=\"mtk12\">value</span><span class=\"mtk1\"> == </span><span class=\"mtk4\">undefined</span><span class=\"mtk1\"> ){</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t</span><span class=\"mtk3\">// handle miss!</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<h4 id=\"sensitive-data-exposure\" style=\"position:relative;\"><a href=\"#sensitive-data-exposure\" aria-label=\"sensitive data exposure permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Sensitive Data Exposure</h4>\n<p>Sensitive data exposure happens when an application, organization, or other entity unable to properly secure sensitive data. It is different from a data breach, it includes personal information, tokens, etc. We can make sure sensitive data security using<br>\nmultiple ways which include encryption at rest or in transit and masking </p>\n<h4 id=\"cross-site-scripting\" style=\"position:relative;\"><a href=\"#cross-site-scripting\" aria-label=\"cross site scripting permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Cross-Site Scripting</h4>\n<p>Cross-Site Scripting (XSS) attacks are a type of injection, in which attacker aims to execute malicious scripts in a web browser of the victim. an attacker can transfer untrusted data into the API as part of a query or command.which can result in an attacker obtaining unauthorized access to information or carry out other damages.</p>\n<h6 id=\"how-to-prevent-or-stop-cross-site-scripting-xss-attack\" style=\"position:relative;\"><a href=\"#how-to-prevent-or-stop-cross-site-scripting-xss-attack\" aria-label=\"how to prevent or stop cross site scripting xss attack permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to Prevent or Stop Cross-Site Scripting (XSS) Attack</h6>\n<h6 id=\"1filter-input-on-arrival\" style=\"position:relative;\"><a href=\"#1filter-input-on-arrival\" aria-label=\"1filter input on arrival permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1.Filter input on arrival:</h6>\n<p>At the point where user input is received, filter as strictly as possible based on what is expected or valid input.</p>\n<h6 id=\"2-use-appropriate-response-headers\" style=\"position:relative;\"><a href=\"#2-use-appropriate-response-headers\" aria-label=\"2 use appropriate response headers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. Use appropriate response headers:</h6>\n<p>To prevent XSS in HTTP responses that aren't intended to contain any HTML or JavaScript, you can use the Content-Type and X-Content-Type-Options headers to ensure that browsers interpret the responses in the way you intend.</p>\n<p>If you want to know more details about the security headers. Please go to <a href=\"/http-security-headers/\">Security Headers</a></p>\n<h6 id=\"3-use-content-security-policy\" style=\"position:relative;\"><a href=\"#3-use-content-security-policy\" aria-label=\"3 use content security policy permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. Use Content Security Policy:</h6>\n<p>As a last line of defense against attackers, you can use Content Security Policy (CSP) to reduce the severity of any XSS vulnerabilities that still occur.</p>\n<p>Node js we can use <code>xss-clean</code> package. This dependency will prevent users from inserting HTML &#x26; Scripts on input.</p>\n<p> <strong>NodeJs</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> </span><span class=\"mtk12\">xssClean</span><span class=\"mtk1\"> </span><span class=\"mtk15\">from</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;xss-clean&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">// Use this as middleare </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">app</span><span class=\"mtk1\">.</span><span class=\"mtk11\">use</span><span class=\"mtk1\">(</span><span class=\"mtk11\">xssClean</span><span class=\"mtk1\">())</span></span></code></pre>\n<h4 id=\"insufficient-logging-and-monitoring\" style=\"position:relative;\"><a href=\"#insufficient-logging-and-monitoring\" aria-label=\"insufficient logging and monitoring permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Insufficient Logging and Monitoring</h4>\n<p>We can discover suspicious activity using proper logging and monitoring. When We have insufficient logging and monitoring in that case sometimes we can miss some system access or user activity logs, a step of the particular activity and security alerts. </p>\n<h5 id=\"logging-and-monitoring\" style=\"position:relative;\"><a href=\"#logging-and-monitoring\" aria-label=\"logging and monitoring permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Logging and Monitoring</h5>\n<p>A lot of logging and monitoring tools are available. We can choose the best tools as per our requirement also we can define some policies like data retention policy that includes how far backlogs will be kept. Instrument your API access actions to record key metrics and events. Keep logs indexable and searchable.</p>\n<h4 id=\"give-limited-access\" style=\"position:relative;\"><a href=\"#give-limited-access\" aria-label=\"give limited access permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Give Limited Access</h4>\n<p>Each API should limit access, API only able to perform what tasks they need to do. We can do this with Role-Based Access, separate read/write API Keys, OAuth Scopes, and permissions systems. This minimizes the chances that you’ll accidentally expose a sensitive field.</p>\n<h3 id=\"security-reports\" style=\"position:relative;\"><a href=\"#security-reports\" aria-label=\"security reports permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Security Reports</h3>\n<p>Sometimes, people find security vulnerabilities, and they would like to report them so the vendor or the developer can fix them. you must have a public contact point where security issues can be reported.</p>\n<p>We can create a <code>security.txt</code> file on the site. <code>security.txt</code> is a proposed standard for websites' security information that will allow security researchers to easily report security vulnerabilities. The \"security.txt\" that is similar to robots.txt. security.txt files have been adopted by Google, GitHub, LinkedIn, and Facebook</p>\n<p>You can easily create <code>secuirty.txt</code> file using the <a href=\"https://securitytxt.org/\">securitytxt.org</a> </p>\n<h3 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h3>\n<p>Now days lot of data breaches are happing. We can save mostly data breaches after following some basic security guidelines.You have to pay attention to security during Rest API development. I have covered most of the general Rest API security issues with resolution. these guidelines will help you for developing more secure and quality REST API service.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n</style>","frontmatter":{"date":"August 20, 2020","updated_date":null,"description":null,"title":"Best Practice Guide For Rest API Security | LoginRadius","tags":["RestAPI","Rest API","Rest API Security","Best Practice","Rest API Developer Guide","Security"],"pinned":null,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.7699115044247788,"src":"/static/0fa4b802f45fe1aadfd22a6700b780f7/14b42/index.jpg","srcSet":"/static/0fa4b802f45fe1aadfd22a6700b780f7/f836f/index.jpg 200w,\n/static/0fa4b802f45fe1aadfd22a6700b780f7/2244e/index.jpg 400w,\n/static/0fa4b802f45fe1aadfd22a6700b780f7/14b42/index.jpg 800w,\n/static/0fa4b802f45fe1aadfd22a6700b780f7/47498/index.jpg 1200w,\n/static/0fa4b802f45fe1aadfd22a6700b780f7/0e329/index.jpg 1600w,\n/static/0fa4b802f45fe1aadfd22a6700b780f7/d8255/index.jpg 1920w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Vijay Singh Shekhawat","github":"code-vj","avatar":null}}}},{"node":{"excerpt":"In this blog we will learn how to create our own webpack configuration to bundle a small JavaScript utility library using webpack and babel…","fields":{"slug":"/engineering/write-a-javascript-library-using-webpack-and-babel/"},"html":"<p>In this blog we will learn how to create our own webpack configuration to bundle a small JavaScript utility library using webpack and babel.</p>\n<h4 id=\"this-blog-will-be-divided-into-two-parts\" style=\"position:relative;\"><a href=\"#this-blog-will-be-divided-into-two-parts\" aria-label=\"this blog will be divided into two parts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>This blog will be divided into two parts:</h4>\n<ul>\n<li><strong>Part I</strong> will be about setting up the source code.</li>\n<li><strong>Part II</strong> will be about setting up the webpack and babel configuration and testing our library.</li>\n</ul>\n<p>Let's create the source code for our library. For that we will be create two utility functions into two separate files in our source folder.</p>\n<h4 id=\"step-1--create-a-directory-demo-and-run-following-command-in-it\" style=\"position:relative;\"><a href=\"#step-1--create-a-directory-demo-and-run-following-command-in-it\" aria-label=\"step 1  create a directory demo and run following command in it permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step 1 : Create a directory <code>demo</code> and run following command in it.</h4>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"bash\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">  $ npm init -y</span></span></code></pre>\n<p>The above command will create a <code>package.json</code> in your project root. I am using a --y to initialize it with default options.</p>\n<p>Directory Structure</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"bash\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">demo</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  |-- src/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  |-- package.json</span></span></code></pre>\n<h4 id=\"step-2-adding-our-source-code\" style=\"position:relative;\"><a href=\"#step-2-adding-our-source-code\" aria-label=\"step 2 adding our source code permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step 2: Adding our source code.</h4>\n<p>Let's add our source code into <code>src</code> directory:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"bash\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">src</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> |--index.js</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> |--capital.js</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> |--addDOMContent.js</span></span></code></pre>\n<p>Our utility library contains two functions <code>capital</code>, to capitalize a string and <code>addDOMContent</code>, to add content to a web page, each in it's own module.</p>\n<p><code>capital.js</code></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">function</span><span class=\"mtk1\"> </span><span class=\"mtk11\">capital</span><span class=\"mtk1\">(</span><span class=\"mtk12\">string</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">capitalizedString</span><span class=\"mtk1\"> =</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">string</span><span class=\"mtk1\">.</span><span class=\"mtk11\">substring</span><span class=\"mtk1\">(</span><span class=\"mtk7\">0</span><span class=\"mtk1\">, </span><span class=\"mtk7\">1</span><span class=\"mtk1\">).</span><span class=\"mtk11\">toUpperCase</span><span class=\"mtk1\">() + </span><span class=\"mtk12\">string</span><span class=\"mtk1\">.</span><span class=\"mtk11\">substring</span><span class=\"mtk1\">(</span><span class=\"mtk7\">1</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk12\">capitalizedString</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">export</span><span class=\"mtk1\"> </span><span class=\"mtk15\">default</span><span class=\"mtk1\"> </span><span class=\"mtk12\">capital</span></span></code></pre>\n<p><code>addDOMContent.js</code></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">function</span><span class=\"mtk1\"> </span><span class=\"mtk11\">addDOMContent</span><span class=\"mtk1\">(</span><span class=\"mtk12\">content</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">node</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">document</span><span class=\"mtk1\">.</span><span class=\"mtk11\">createElement</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;h1&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">node</span><span class=\"mtk1\">.</span><span class=\"mtk12\">innerText</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">content</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">document</span><span class=\"mtk1\">.</span><span class=\"mtk12\">body</span><span class=\"mtk1\">.</span><span class=\"mtk11\">appendChild</span><span class=\"mtk1\">(</span><span class=\"mtk12\">node</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">export</span><span class=\"mtk1\"> </span><span class=\"mtk15\">default</span><span class=\"mtk1\"> </span><span class=\"mtk12\">addDOMContent</span></span></code></pre>\n<p>Inside our <code>index.js</code>, we will import these two functions.</p>\n<p><code>index.js</code></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> </span><span class=\"mtk12\">capital</span><span class=\"mtk1\"> </span><span class=\"mtk15\">from</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;./capital&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> </span><span class=\"mtk12\">addDOMContent</span><span class=\"mtk1\"> </span><span class=\"mtk15\">from</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;./addDOMContent&quot;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">export</span><span class=\"mtk1\"> { </span><span class=\"mtk12\">capital</span><span class=\"mtk1\">, </span><span class=\"mtk12\">addDOMContent</span><span class=\"mtk1\"> }</span></span></code></pre>\n<p>So far we got the source code ready but we still need to bundle it so that the browsers can understand and oh boy!, we need to support some older browsers too 🙄. Anyway, being responsible developers we are going to do that 😎.</p>\n<h4 id=\"step-3-lets-install-some-of-our-project-dev-dependencies-as-they-are-only-needed-during-development\" style=\"position:relative;\"><a href=\"#step-3-lets-install-some-of-our-project-dev-dependencies-as-they-are-only-needed-during-development\" aria-label=\"step 3 lets install some of our project dev dependencies as they are only needed during development permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step 3: Let's install some of our project dev dependencies as they are only needed during development.</h4>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"bash\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\"> $ npm i --save-dev webpack webpack-cli @babel/core @babel/preset-env babel-loader</span></span></code></pre>\n<p>We need <code>webpack</code> to bundle our code and <code>webpack-cli</code> is a command-line tool that uses <code>webpack</code> to do the same. Also webpack requires <code>babel-loader</code> to transpile our ES6 code to ES5 before bundling (Remember, what I said about being responsible developers 😃).</p>\n<h4 id=\"step-4-now-lets-get-our-webpack-and-babel-configuration-in-place-we-are-almost-there\" style=\"position:relative;\"><a href=\"#step-4-now-lets-get-our-webpack-and-babel-configuration-in-place-we-are-almost-there\" aria-label=\"step 4 now lets get our webpack and babel configuration in place we are almost there permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step 4: Now let's get our webpack and babel configuration in place. (We are almost there)</h4>\n<p>4.1. Create a <code>webpack.config.js</code> at the root of the project.</p>\n<p><code>webpack.config.js</code></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">path</span><span class=\"mtk1\"> = </span><span class=\"mtk11\">require</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;path&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk10\">module</span><span class=\"mtk1\">.</span><span class=\"mtk10\">exports</span><span class=\"mtk1\"> = {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">entry:</span><span class=\"mtk1\"> </span><span class=\"mtk12\">path</span><span class=\"mtk1\">.</span><span class=\"mtk11\">resolve</span><span class=\"mtk1\">(</span><span class=\"mtk12\">__dirname</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&quot;src/index.js&quot;</span><span class=\"mtk1\">),</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">output:</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">path:</span><span class=\"mtk1\"> </span><span class=\"mtk12\">path</span><span class=\"mtk1\">.</span><span class=\"mtk11\">resolve</span><span class=\"mtk1\">(</span><span class=\"mtk12\">__dirname</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&quot;dist&quot;</span><span class=\"mtk1\">),</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">filename:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;index_bundle.js&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">library:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;$&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">libraryTarget:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;umd&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  },</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">module:</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">rules:</span><span class=\"mtk1\"> [</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">test:</span><span class=\"mtk5\"> /</span><span class=\"mtk6\">\\.</span><span class=\"mtk8\">(</span><span class=\"mtk5\">js</span><span class=\"mtk8\">)</span><span class=\"mtk11\">$</span><span class=\"mtk5\">/</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">exclude:</span><span class=\"mtk5\"> /node_modules/</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">use:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;babel-loader&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      },</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    ],</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  },</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">mode:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;development&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p>There are a couple of things we need to understand about webpack configuration. Stay with me for a couple more minutes.</p>\n<ul>\n<li><code>entry</code>: In order for webpack to know where to start with, it needs to know the entry point to our app.</li>\n<li><code>module.rules</code>: Each file in <code>Node</code> is treated like a module. Webpack itself understands only Javascript and JSON modules. Since we want to transpile ES6, we need <code>babel-loader</code> and webpack needs to know the rules on how to process the Javascript using the given loader.</li>\n<li><code>output</code>: After creating the bundle, webpack needs to know what name to give it and where to put it.<code>library</code> and <code>libraryTarget</code> are used to expose our library where <code>library</code> being the name,<code>$</code> here and <code>libraryTarget</code> is the property to configure, how the library will be exposed. Here we will be using UMD. UMD is a module system capable of working everywhere, be it in the client, on the server or elsewhere.</li>\n<li>\n<p>mode: Webpack bundles code into either development mode (unminified) or in production(minified) mode. I am using a hard coded value here for the demo. You can set it using environment variables too.</p>\n<p>4.2. Create a <code>.babelrc</code> file at the root of the project.</p>\n</li>\n</ul>\n<p><code>.babelrc</code></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">{</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  presets: [</span><span class=\"mtk8\">&quot;@babel/preset-env&quot;</span><span class=\"mtk1\">]</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p><code>@babel/preset-env</code> let's us use the latest Javascript without any polyfills and syntax transforms.<code>babel-loader</code> uses <code>babel</code> under the hood.</p>\n<p>By Now our Project Structure should look like this:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"bash\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">demo</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">|-- src</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">|   |-- index.js</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">|   |-- capital.js</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">|   |-- addDOMContent.js</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">|-- webpack.config.js</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">|-- .babelrc</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">|-- package.json</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">|-- node_modules</span></span></code></pre>\n<h4 id=\"step-5-one-last-step-i-know-i-keep-saying-that-but-i-promise-this-is-last-\" style=\"position:relative;\"><a href=\"#step-5-one-last-step-i-know-i-keep-saying-that-but-i-promise-this-is-last-\" aria-label=\"step 5 one last step i know i keep saying that but i promise this is last  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step 5: One last step. I know I keep saying that but I promise this is last 😬.</h4>\n<p>We have added our source files, now let's add an <code>npm script</code> to build final code using webpack and modify the <code>main</code> property inside our <code>package.json</code> to point it to our bundled code.</p>\n<p><code>package.json</code></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"json\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">{</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">&quot;name&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;demo&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">&quot;version&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;1.0.0&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">&quot;description&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk14\">+</span><span class=\"mtk1\">  </span><span class=\"mtk12\">&quot;main&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;dist/index_bundle.js&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk14\">+</span><span class=\"mtk1\">  </span><span class=\"mtk12\">&quot;scripts&quot;</span><span class=\"mtk1\">: {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk14\">+</span><span class=\"mtk1\">    </span><span class=\"mtk12\">&quot;build&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;webpack&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk14\">+</span><span class=\"mtk1\">  },</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">&quot;keywords&quot;</span><span class=\"mtk1\">: [],</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">&quot;author&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;Hridayesh Sharma&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">&quot;license&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;ISC&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">&quot;dependencies&quot;</span><span class=\"mtk1\">: {},</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">&quot;devDependencies&quot;</span><span class=\"mtk1\">: {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">&quot;@babel/core&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;^7.10.4&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">&quot;@babel/preset-env&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;^7.11.0&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">&quot;babel-loader&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;^8.1.0&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">&quot;webpack&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;^4.44.1&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">&quot;webpack-cli&quot;</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;^3.3.12&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p>In <code>package.json</code> the <code>main</code> property is a direction to the entry point of the module that the package.json is describing.</p>\n<h4 id=\"hurray-we-have-finally-created-our-utility-library-using-es6\" style=\"position:relative;\"><a href=\"#hurray-we-have-finally-created-our-utility-library-using-es6\" aria-label=\"hurray we have finally created our utility library using es6 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Hurray! We have finally created our utility library using ES6.🥳</h4>\n<p>Run <code>$npm run build</code> to generate the bundled code and use it in the next step.</p>\n<h4 id=\"lets-test-our-library-now\" style=\"position:relative;\"><a href=\"#lets-test-our-library-now\" aria-label=\"lets test our library now permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Let's test our library now.</h4>\n<p><code>index.html</code></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"html\" data-index=\"11\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">&lt;!</span><span class=\"mtk12\">DOCTYPE</span><span class=\"mtk1\"> </span><span class=\"mtk12\">html</span><span class=\"mtk1\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">html</span><span class=\"mtk1\"> </span><span class=\"mtk12\">lang</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;en&quot;</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">head</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">meta</span><span class=\"mtk1\"> </span><span class=\"mtk12\">charset</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;UTF-8&quot;</span><span class=\"mtk1\"> </span><span class=\"mtk17\">/&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">meta</span><span class=\"mtk1\"> </span><span class=\"mtk12\">name</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;viewport&quot;</span><span class=\"mtk1\"> </span><span class=\"mtk12\">content</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;width=device-width, initial-scale=1.0&quot;</span><span class=\"mtk1\"> </span><span class=\"mtk17\">/&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">title</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">Demo</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">title</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">head</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">body</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">script</span><span class=\"mtk1\"> </span><span class=\"mtk12\">src</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;dist/index_bundle.js&quot;</span><span class=\"mtk17\">&gt;&lt;/</span><span class=\"mtk4\">script</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">script</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      console.log($)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      alert($.capital(&quot;hridayesh&quot;))</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      $.addDOMContent(&quot;Well It Works Fine!!!&quot;)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">script</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">body</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">html</span><span class=\"mtk17\">&gt;</span></span></code></pre>\n<p>Save it and run it in your browser. You will see the name capitalized.</p>\n<p>The complete code is available at <a href=\"https://github.com/LoginRadius/engineering-blog-samples/tree/master/JavaScript/WebpackBabelJSLibrary\">LoginRadius Engineering Blog Sample Repo</a></p>\n<p>Thanks for reading the blog. For detailed information and execution example of this blog, please refer to the video below:</p>\n<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/lXlpshtv_IU\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk5 { color: #D16969; }\n  .dark-default-dark .mtk6 { color: #D7BA7D; }\n  .dark-default-dark .mtk14 { color: #F44747; }\n  .dark-default-dark .mtk17 { color: #808080; }\n</style>","frontmatter":{"date":"August 18, 2020","updated_date":null,"description":"Writing your own webpack configuration for a JavaScript library in ES6 and learn webpack along the way.","title":"Let's Write a JavaScript Library in ES6 using Webpack and Babel","tags":["JavaScript","Webpack","NodeJs"],"pinned":null,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/4a7d7b651d2d5406780c786320c2b664/ee604/cover.png","srcSet":"/static/4a7d7b651d2d5406780c786320c2b664/69585/cover.png 200w,\n/static/4a7d7b651d2d5406780c786320c2b664/497c6/cover.png 400w,\n/static/4a7d7b651d2d5406780c786320c2b664/ee604/cover.png 800w,\n/static/4a7d7b651d2d5406780c786320c2b664/a8378/cover.png 1024w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Hridayesh Sharma","github":"vyasriday","avatar":null}}}},{"node":{"excerpt":"Every Web Developer should know about cross-domain security While working in the world of the internet, all of the complex systems are…","fields":{"slug":"/engineering/cross-domain-security/"},"html":"<h2 id=\"every-web-developer-should-know-about-cross-domain-security\" style=\"position:relative;\"><a href=\"#every-web-developer-should-know-about-cross-domain-security\" aria-label=\"every web developer should know about cross domain security permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Every Web Developer should know about cross-domain security</strong></h2>\n<p>While working in the world of the internet, all of the complex systems are interconnected in a shareable environment. But exposing systems in the outer world will invite security vulnerabilities and data breach for the organization. Cross-domain security address this security threat by enabling information sharing in more reliable and secure environments. Cross-domain security is an inclusive approach to defending against all kinds of threats to data connections at the boundaries of sensitive or classified networks.</p>\n<h2 id=\"the-major-concepts-of-security\" style=\"position:relative;\"><a href=\"#the-major-concepts-of-security\" aria-label=\"the major concepts of security permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Major Concepts of Security</h2>\n<h3 id=\"100-security-doesnt-exists\" style=\"position:relative;\"><a href=\"#100-security-doesnt-exists\" aria-label=\"100 security doesnt exists permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>100% Security doesn’t exists.</h3>\n<p>There is no way of being 100% protected from being hacked. If anyone ever tells you that, they are wrong.</p>\n<h3 id=\"single-layer-of-protection-isnt-enough\" style=\"position:relative;\"><a href=\"#single-layer-of-protection-isnt-enough\" aria-label=\"single layer of protection isnt enough permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Single layer of protection isn’t enough.</h3>\n<p>You can’t just say…</p>\n<blockquote>\n<p>\"Oh, because I even have CSP implemented, I am safe. I can cross out cross-site scripting from my vulnerabilities list because that can’t happen now.\"</p>\n</blockquote>\n<p>Maybe that is a given to some, but it is easy to find yourself thinking in this manner. In my opinion one reason that programmers can easily find themselves thinking this way is because so much of coding is black and white, 0 or 1, true or false. Security is not that so simple.</p>\n<h2 id=\"cross-origin-resource-sharing-cors\" style=\"position:relative;\"><a href=\"#cross-origin-resource-sharing-cors\" aria-label=\"cross origin resource sharing cors permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Cross-Origin Resource Sharing (CORS)</h2>\n<p>Have you ever gotten an error that looked something like this?</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">No &#39;Access-Control-Allow-Origin&#39; header is available on the requested resource. Origin &#39;null&#39; is therefore not allowed access.</span></code></pre>\n<p>You are certainly not alone. And then you Google it, and someone tells you to urge this extension which will make all of  your problems go away!</p>\n<blockquote>\n<p> <em>Awesome, right?</em></p>\n</blockquote>\n<p><strong>CORS is there to protect you, not hurt you!</strong></p>\n<p>In order to explain how CORS helps you, let’s starts about cookies, specifically <strong>authentication cookies</strong>. Authentication cookies are wont to tell a server that you are simply logged in, and that they are automatically sent with any request you make to that server.</p>\n<blockquote>\n<p> Let’s think you’re logged in to yahoo, and they use authentication cookies. You click on bit.ly/r43nugi which redirects you to <a href=\"http://www.cryptoearn.co/\">cryptoearn</a>. A script within <a href=\"http://www.cryptoearn.co/\">cryptoearn</a> makes a client-side request to yahoo.com which sends your authentication cookie!</p>\n</blockquote>\n<p>In a no-CORS world, they might make changes to your account without you even knowing. Until, obiviously , they post bit.ly/r43nugi on your timeline, and everyone of your relative orfriends click on thereon, and then the cycle continues in an evil breadth-first scheme that conquers all of yahoo’s users, and the world is consumed by <a href=\"http://www.cryptoearn.co/\">cryptoearn</a>. ?</p>\n<p>In CORS world, however, yahoo would only allow requests with an origin of yahoo.com to edit data on their server. In other words, they might limit cross-origin resource sharing. You might then ask…</p>\n<blockquote>\n<p> <em>Well can <a href=\"http://www.cryptoearn.co/\">cryptoearn</a> just change the origin header on their request, so that it looks like it is coming from *yahoo.com</em>?*</p>\n</blockquote>\n<p>They will try, but it won’t work because the browser will just ignore it and use the actual origin.</p>\n<blockquote>\n<p> <em>Ok, but what if <a href=\"http://www.cryptoearn.co/\">cryptoearn</a> made the request server-side?</em></p>\n</blockquote>\n<p>In this case, they can bypass CORS, but they can't crack this because they won’t be ready to send your authentication cookie along for the ride. The script should be executed on the client side to urge access to your client side cookies.</p>\n<h3 id=\"what-is-a-security-policy\" style=\"position:relative;\"><a href=\"#what-is-a-security-policy\" aria-label=\"what is a security policy permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is a security Policy?</h3>\n<blockquote>\n<p> Servers are generally host web sites, applications, images, fonts, and many more. When you use any browser, you are likely attempting to access a definite website (that is hosted on a server). Websites often request these hosted resources from different locations (servers) on the web. Security policies on servers mitigate the risks associated with requesting assets hosted on distinct server. Let’s take a glance at an example of a security policy: <em>same-origin</em>.\nThe **same-origin *<em>policy is very restrictive. Under this policy, a document (i.e., sort of a web page) hosted on server A can only interact with other documents that also are on server A. In short, the same-origin policy enforces that documents that interact with one another have the same *origin</em>.</p>\n</blockquote>\n<p>The CORS standards manage cross-origin requests by adding a new HTTP headers to the standard list of headers. The following are the new HTTP headers added by the CORS standard:</p>\n<ul>\n<li>Access-Control-Allow-Origin</li>\n<li>Access-Control-Allow-Credentials</li>\n<li>Access-Control-Allow-Headers</li>\n<li>Access-Control-Allow-Methods</li>\n<li>Access-Control-Expose-Headers</li>\n<li>Access-Control-Max-Age</li>\n<li>Access-Control-Request-Headers</li>\n<li>Access-Control-Request-Method</li>\n<li>Origin</li>\n</ul>\n<h2 id=\"content-security-policy-csp\" style=\"position:relative;\"><a href=\"#content-security-policy-csp\" aria-label=\"content security policy csp permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Content Security Policy (CSP)</h2>\n<p>To dig in to  CSP, we first need to talk about one of the most common vulnerabilities on the web: XSS, which means cross-site scripting.</p>\n<p>XSS is when some evil guy injects JavaScript into your client-side code. You might think…</p>\n<blockquote>\n<p> <em>What are they going to do? Change a color from red to blue?</em></p>\n</blockquote>\n<p>Let’s think of someone has successfully injected JavaScript into client-side code of a website you are visiting.</p>\n<p>What could they do that would be malicious?</p>\n<ul>\n<li>They might make HTTP requests to another site pretending to be you.</li>\n<li>They might add an anchor tag that sends you to a website that looks same to the one you are on with some slightly different, malicious characteristics.</li>\n<li>They might add a script tag with inline JavaScript.</li>\n<li>They might add a script tag that fetches a remote JavaScript file somewhere.</li>\n<li>They might add an iframe that covers the page and looks like part of the website prompting you to insert your password.</li>\n</ul>\n<p>The possibilities are endless.</p>\n<p>CSP is something prevent this from happening by limiting:</p>\n<ul>\n<li>what can be opened in an iframe</li>\n<li>what stylesheets can be loaded</li>\n<li>\n<p>where requests can be made, etc.</p>\n<p>So how does it work?</p>\n</li>\n</ul>\n<p>Whenever you click on a link or type a website URL in the address bar of your internet browser, your browser makes a GET request. It eventually makes its way to a server which serves up HTML along with HTTP headers.  for more details about what headers, open up the Network tab in your console, and visit some sites.</p>\n<p>You might see a response header that looks like below:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">content-security-policy: default-src * data: blob:;script-src *.yahoo.com *.fbcdn.net *.yahoo.net *.google-analytics.com *.virtualearth.net *.google.com 127.0.0.1:* *.spotilocal.com:* &#39;unsafe-inline&#39; &#39;unsafe-eval&#39; *.atlassolutions.com blob: data: &#39;self&#39;;style-src data: blob: &#39;unsafe-inline&#39; *;connect-src *.yahoo.com yahoo.com *.fbcdn.net *.yahoo.net *.spotilocal.com:* wss://*.yahoo.com:* https://fb.scanandcleanlocal.com:* *.atlassolutions.com attachment.fbsbx.com ws://localhost:* blob: *.cdninstagram.com &#39;self&#39; chrome-extension://boadgeojelhgndaghljhdicfkmllpafd chrome-extension://dliochdbjfkdbacpmhlcpmleaejidimm;</span></code></pre>\n<p>That is the content security policy of yahoo.com. Let’s reformat it to make it easier to read:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">content-security-policy:</span>\n<span class=\"grvsc-line\">default-src * data: blob:;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">script-src *.yahoo.com *.fbcdn.net *.yahoo.net *.google-analytics.com *.virtualearth.net *.google.com 127.0.0.1:* *.spotilocal.com:* &#39;unsafe-inline&#39; &#39;unsafe-eval&#39; *.atlassolutions.com blob: data: &#39;self&#39;;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">style-src data: blob: &#39;unsafe-inline&#39; *;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">connect-src *.yahoo.com yahoo.com *.fbcdn.net *.yahoo.net *.spotilocal.com:* wss://*.yahoo.com:* https://fb.scanandcleanlocal.com:* *.atlassolutions.com attachment.fbsbx.com ws://localhost:* blob: *.cdninstagram.com &#39;self&#39; chrome-extension://boadgeojelhgndaghljhdicfkmllpafd chrome-extension://dliochdbjfkdbacpmhlcpmleaejidimm;</span></code></pre>\n<p>Now, let’s break down the directives.</p>\n<ul>\n<li><strong>default-src</strong> the CSP directives that are not explicitly listed will be restricted.</li>\n<li><strong>script-src</strong> scripts that can be loaded will be restricted</li>\n<li><strong>style-src</strong> stylesheets that can be loaded will be restricted.</li>\n<li><strong>connect-src</strong> URLs which can be loaded using script interfaces, so fetch, XHR, ajax, etc will be restricted</li>\n</ul>\n<p><em>Note:</em> there are many more CSP directives than just these four shown above. The internet browser will read the CSP header and apply those directives to everything within the HTML file that was served. If the directives are set correctly, they allow only what is required.</p>\n<p>If there is no CSP header is present, then everything goes, and nothing is restricted. Everywhere you see * , that is a wildcard. You can think of replacing * with anything and it will be allowed.</p>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>Security is something that should be important to everyone, not just the people who have it explicitly named in their job title, and always try to have additional layer for better security.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","frontmatter":{"date":"August 16, 2020","updated_date":null,"description":"Cross domain security address security threat by enabling the information sharing in more reliable and secure environments. Cross domain security is an inclusive approach to defending against all kind of threats to data connections at the boundaries of sensitive or classified networks.","title":"Cross Domain Security","tags":["Security","Web Security","Cross-Domain"],"pinned":null,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/790cea1b5ac8ae81febf53547ae0d319/ee604/cross_domain_security.png","srcSet":"/static/790cea1b5ac8ae81febf53547ae0d319/69585/cross_domain_security.png 200w,\n/static/790cea1b5ac8ae81febf53547ae0d319/497c6/cross_domain_security.png 400w,\n/static/790cea1b5ac8ae81febf53547ae0d319/ee604/cross_domain_security.png 800w,\n/static/790cea1b5ac8ae81febf53547ae0d319/f3583/cross_domain_security.png 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Abhimanyu Singh Rathore","github":"abhir9","avatar":null}}}}]},"markdownRemark":{"excerpt":"Identity is evolving, and developers are at the forefront of this transformation. Every day brings a new learning—adapting to new standards…","fields":{"slug":"/identity/developer-first-identity-provider-loginradius/"},"html":"<p>Identity is evolving, and developers are at the forefront of this transformation. Every day brings a new learning—adapting to new standards and refining approaches to building secure, seamless experiences.</p>\n<p>We’re here to support developers on that journey. We know how important simplicity, efficiency, and well-structured documentation are when working with identity and access management solutions. That’s why we’ve redesigned the <a href=\"https://www.loginradius.com/\">LoginRadius website</a>—to be faster, more intuitive, and developer-first in every way.</p>\n<p>The goal? Having them spend less time searching and more time building.</p>\n<h2 id=\"whats-new-and-improved-on-the-loginradius-website\" style=\"position:relative;\"><a href=\"#whats-new-and-improved-on-the-loginradius-website\" aria-label=\"whats new and improved on the loginradius website permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What’s New and Improved on the LoginRadius Website?</h2>\n<p>LoginRadius’ vision is to give developers a product that simplifies identity management so they can focus on building, deploying, and scaling their applications. To enhance this experience, we’ve spent the last few months redesigning our interface— making navigation more intuitive and reassuring that essential resources are easily accessible.</p>\n<p>Here’s a closer look at what’s new and why it’s important:</p>\n<h3 id=\"a-developer-friendly-dark-theme\" style=\"position:relative;\"><a href=\"#a-developer-friendly-dark-theme\" aria-label=\"a developer friendly dark theme permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>A Developer-Friendly Dark Theme</h3>\n<p><img src=\"/f46881583c7518a93bb24e94c32320de/a-developer-friendly-dark-theme.webp\" alt=\"This image shows how LoginRadius offers several authentication methods like traditional login, social login, passwordless login, passkeys and more in a dark mode.\">    </p>\n<p>Developers spend long hours working in dark-themed IDEs and terminals, so we’ve designed the LoginRadius experience to be developer-friendly and align with that preference.</p>\n<p>The new dark mode reduces eye strain, enhances readability, and provides a seamless transition between a coding environment and our platform. Our new design features a clean, modern aesthetic with a consistent color scheme and Barlow typography, ensuring better readability. High-quality graphics and icons are thoughtfully placed to enhance the content without adding visual clutter.</p>\n<p>So, whether you’re navigating our API docs or configuring authentication into your system, our improved interface will make those extended development hours more comfortable and efficient.</p>\n<h3 id=\"clear-categorization-for-loginradius-capabilities\" style=\"position:relative;\"><a href=\"#clear-categorization-for-loginradius-capabilities\" aria-label=\"clear categorization for loginradius capabilities permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Clear Categorization for LoginRadius Capabilities</h3>\n<p><img src=\"/e5358b82be414940f3fb146013845933/capabilities.webp\" alt=\"This image shows a breakdown of all the LoginRadius CIAM capabilities, including authentication, security, UX, scalability and multi-brand management.\"></p>\n<p>We’ve restructured our website to provide a straightforward breakdown of our customer identity and access management platform capabilities, helping you quickly find what you need:</p>\n<ul>\n<li>Authentication: Easily understand <a href=\"https://www.loginradius.com/blog/identity/authentication-option-for-your-product/\">how to choose the right login method</a>, from traditional passwords and OTPs to social login, federated SSO, and passkeys with few lines of code.</li>\n<li>Security: Implement no-code security features like bot detection, IP throttling, breached password alerts, DDoS protection, and adaptive MFA to safeguard user accounts.</li>\n<li>User Experience: Leverage AI builder, hosted pages, and drag-and-drop workflows to create smooth, branded sign-up and login experiences.</li>\n<li>High Performance &#x26; Scalability: Confidently scale with sub-100ms API response times, 100% uptime, 240K+ RPS, and 28+ global data center regions.</li>\n<li>Multi-Brand Management: Efficiently manage multiple identity apps, choosing isolated or shared data stores based on your brand’s unique needs.</li>\n</ul>\n<p>This structured layout ensures you can quickly understand each capability and how it integrates into your identity ecosystem.</p>\n<h3 id=\"developer-first-navigation\" style=\"position:relative;\"><a href=\"#developer-first-navigation\" aria-label=\"developer first navigation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Developer-First Navigation</h3>\n<p><img src=\"/a8c155c2b6faf3d5f4b4de4e2b14d763/developers-menu.webp\" alt=\"This image shows the LoginRadius menu bar, highlighting the developer dropdown.\">   </p>\n<p>We’ve been analyzing developer workflows to identify how you access key resources. That’s why we redesigned our navigation with one goal in mind: to reduce clicks and make essential resources readily available.</p>\n<p>The new LoginRadius structure puts APIs, SDKs, and integration guides right at the menu bar under the Developers dropdown so you can get started faster. Our Products, Solutions, and Customer Services are also clearly categorized, helping development teams quickly find the right tools and make informed decisions.</p>\n<h3 id=\"quick-understanding-of-integration-benefits\" style=\"position:relative;\"><a href=\"#quick-understanding-of-integration-benefits\" aria-label=\"quick understanding of integration benefits permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Quick Understanding of Integration Benefits</h3>\n<p><img src=\"/b2f9a964a2da0ea83e2f8596b833bba7/we-support-your-tech-stack.webp\" alt=\"This image shows a list of popular programming languages and frameworks offered by LoginRadius.\"></p>\n<p>Developers now have a clear view of the tech stack available with LoginRadius, designed to support diverse business needs.</p>\n<p>Our platform offers pre-built SDKs for Node.js, Python, Java, and more, making CIAM integration seamless across popular programming languages and frameworks.</p>\n<h2 id=\"over-to-you-now\" style=\"position:relative;\"><a href=\"#over-to-you-now\" aria-label=\"over to you now permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Over to You Now!</h2>\n<p>Check out our <a href=\"https://www.loginradius.com/\">revamped LoginRadius website</a> and see how the improved experience makes it easier to build, scale, and secure your applications.</p>\n<p>Do not forget to explore the improved navigation and API documentation, and get started with our free trial today. We’re excited to see what you’ll build with LoginRadius!</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n</style>","frontmatter":{"date":"February 21, 2025","updated_date":null,"description":"LoginRadius’ vision is to give developers a product that simplifies identity management so they can focus on building, deploying, and scaling their applications. To enhance this experience, we’ve redesigned our website interface, making navigation more intuitive and reassuring that essential resources are easily accessible.","title":"Revamped & Ready: Introducing the New Developer-First LoginRadius Website","tags":["Developer tools","API","Identity Management","User Authentication"],"pinned":true,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.7857142857142858,"src":"/static/80b4e4fbe176a10a327d273504607f32/58556/hero-section.webp","srcSet":"/static/80b4e4fbe176a10a327d273504607f32/61e93/hero-section.webp 200w,\n/static/80b4e4fbe176a10a327d273504607f32/1f5c5/hero-section.webp 400w,\n/static/80b4e4fbe176a10a327d273504607f32/58556/hero-section.webp 800w,\n/static/80b4e4fbe176a10a327d273504607f32/99238/hero-section.webp 1200w,\n/static/80b4e4fbe176a10a327d273504607f32/7c22d/hero-section.webp 1600w,\n/static/80b4e4fbe176a10a327d273504607f32/1258b/hero-section.webp 2732w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Rakesh Soni","github":"oyesoni","avatar":"rakesh-soni.jpg"}}}},"pageContext":{"limit":6,"skip":732,"currentPage":123,"type":"///","numPages":161,"pinned":"ee8a4479-3471-53b1-bf62-d0d8dc3faaeb"}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}