<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nosh Ghazanfar</title>
    <description>The latest articles on DEV Community by Nosh Ghazanfar (@nosherwan).</description>
    <link>https://dev.to/nosherwan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F80910%2F62897e31-f32c-4d88-84c7-f315585aaeb7.png</url>
      <title>DEV Community: Nosh Ghazanfar</title>
      <link>https://dev.to/nosherwan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nosherwan"/>
    <language>en</language>
    <item>
      <title>Effective Security &amp; Authentication for a modern full stack Web Application</title>
      <dc:creator>Nosh Ghazanfar</dc:creator>
      <pubDate>Sat, 08 Apr 2023 10:22:49 +0000</pubDate>
      <link>https://dev.to/nosherwan/effective-security-authentication-for-a-modern-full-stack-web-application-3dm6</link>
      <guid>https://dev.to/nosherwan/effective-security-authentication-for-a-modern-full-stack-web-application-3dm6</guid>
      <description>&lt;p&gt;Over the past month, I have been researching ways to secure web applications without relying on third-party solutions, particularly for SAAS solutions that don't deal with highly sensitive information, such as banks. My goal has been to identify simple &amp;amp; straightforward methods to ensure the security of such web applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;EDIT&lt;/strong&gt;: My original strategy of not using refresh tokens had a flaw; hence the article has been updated by using refresh tokens inside http only secure cookies.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: This article is for developers who do not want to use oauth2 or similar authentication solutions as a separate server is required for authentication in them. This is for baked in authentication that can be provided as part of REST or graphQL apis.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Below is the proposed solution.&lt;/p&gt;

&lt;p&gt;There are two parts to any web application:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Front-end&lt;/li&gt;
&lt;li&gt;Back-end&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Most of my recent projects personal or work wise have a front-end framework essentially doing SSR or server side rendering.&lt;br&gt;
For the back-end I use a GraphQL server.&lt;/p&gt;

&lt;p&gt;Recently I started updating strategies for both options &amp;amp; I felt it was time I refreshed my approach to security.&lt;/p&gt;

&lt;h2&gt;
  
  
  Secure your connections
&lt;/h2&gt;

&lt;p&gt;The first rule of web security is serve everything over https. For that you are required to secure both your front &amp;amp; back end servers with SSL/TLS certificates.&lt;/p&gt;

&lt;h2&gt;
  
  
  Front-end
&lt;/h2&gt;

&lt;p&gt;For the SSR its all about configuring content security policy or CSP. Provide CSP header with a good policy to get relative security from XSS attacks.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;An example of a CSP policy:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Content-Security-Policy: default-src 'none'; script-src 'self' https://ajax.googleapis.com; style-src ‘self' ‘unsafe-inline’ fonts.googleapis.com; img-src ‘self' ‘data’; font-src ‘self' fonts.gstatic.com; connect-src 'self' https://api.example.com; frame-src 'self'; object-src 'none'; media-src 'self';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the above example we are disabling everything by default and then gradually adding each source explicitly. For example we have added google fonts support; &amp;amp; we are allowing to make calls to &lt;a href="https://api.example.com"&gt;https://api.example.com&lt;/a&gt;. For more detailed explanation review the links below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy"&gt;MDN CSP Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://content-security-policy.com/"&gt;Content Security Policy Website&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Back-end
&lt;/h3&gt;

&lt;p&gt;For our authentication strategy we will keep the following requirements in mind.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cookies required:&lt;/strong&gt;&lt;br&gt;
A front-end framework that does SSR would be easily able to render on the server if cookies were used for authentication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CSRF token for security:&lt;/strong&gt;&lt;br&gt;
cookie usage requires safety for CSRF attacks on post update &amp;amp; delete requests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;JWT token generation:&lt;/strong&gt;&lt;br&gt;
JWT access tokens will be used for authentication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Short lived access tokens:&lt;/strong&gt;&lt;br&gt;
Access tokens need to be refreshed after short period of time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Refresh tokens:&lt;/strong&gt;&lt;br&gt;
Refresh tokens will have to be used for short lived access tokens.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Logout capability from the server:&lt;/strong&gt;&lt;br&gt;
For a reasonable SAAS offering there needs to be a way to logout users who’s access token is compromised, refresh tokens will serve that purpose.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: JWT tokens are a topic in itself, if you are not familiar with them use the link below to get a better understanding. JWTs are tokens that have a signature portion that can be verified by the server to see that the token has not been tampered with. The claims that are part of the JWT are not secret if the token is compromised and can be read by anyone; hence data that needs to be kept secret should not be saved in them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Reference below links for more details:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#synchronizer-token-pattern"&gt;CSRF Prevention&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jwt.io/introduction/"&gt;What is JWT&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The strategy:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The client requests authentication via username password to the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server validates that username password against a database table with hashed passwords &amp;amp; generates a signed JWT.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: Visible user passwords should never be stored without hashing them &amp;amp; providing a salt value for the hash via a library such as bcrypt. This is a separate topic &amp;amp; beyond the scope of this article.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;The server then generates three cookies;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;An http only cookie that contains a signed JWT with claims such as expiry date &amp;amp; the user details required by the client. As this is a secure cookie javascript will not be able to access it at all &amp;amp; it will be transported on every request back to the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It creates a refresh token possibly a guid value that expires potentially in 3 months time &amp;amp; stores it against the user record in the user table. It will be sent to the client in another http only cookie. The client won’t have to do anything.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A non http only cookie readable by the client that contains a x-csrf-token. This token will be returned by the client in a custom header. This is for protection against csrf attacks.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Scenario:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4XkgPQVy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8dqp6x9lmhmspmrrzec.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4XkgPQVy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8dqp6x9lmhmspmrrzec.jpeg" alt="Authentication Scenario" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets look at a scenario how this strategy plays out.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The client logs in with username password. On success the server creates a GUID as a refresh token and stores it in the database table against the user record as a list as the user can login from multiple devices. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server sends the access token containing the JWT &amp;amp; the refresh token as the GUID in two http only secure cookies that will automatically be transported back on each request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The client on subsequent post, delete or update requests sends back the x-csrf-token value by reading it from the readable cookie sent by the server. As a third party is not able to add custom headers to a request this tells the server that the request is not compromised.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On every request the server verify’s the access token/JWT; checks the x-csrf-token value is the same one as in the sent cookie; and verifies that a refresh token value exists.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the access token expires lets say every 15 minutes; the refresh token is verified &amp;amp; another access token/JWT is generated by the server &amp;amp; sent back in the http only cookie that will overwrite the previous one on the client. To increase security the refresh token can be regenerated again.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If a certain users access token is stolen or compromised and we need to log them out we invalidate the refresh token by looking up that value in the list stored against that user in the database table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The next request sent with that access token will fail and the user will have to re-login.&lt;br&gt;
This way the user will also be logged out of all the device where the refresh token is from. To be safe the whole list of refresh tokens can be removed to log that user out of all devices.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;Securing a web application requires a comprehensive approach that covers both front-end and back-end security. Implementing security measures, such as HTTPS, CSP, access token &amp;amp; refresh token management, CSRF protection &amp;amp; short lived access tokens can help protect against attacks and keep your users' data safe.&lt;/p&gt;

&lt;p&gt;I hope I have been able to describe how we can mitigate all these issues with a reasonable &amp;amp; simple authentication strategy.&lt;/p&gt;

&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@mr_williams_photography?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Micah Williams&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>security</category>
      <category>cookies</category>
      <category>authentication</category>
    </item>
    <item>
      <title>What Web Framework to Follow in 2023?</title>
      <dc:creator>Nosh Ghazanfar</dc:creator>
      <pubDate>Wed, 01 Feb 2023 23:32:47 +0000</pubDate>
      <link>https://dev.to/nosherwan/what-web-framework-to-follow-in-2023-3jjd</link>
      <guid>https://dev.to/nosherwan/what-web-framework-to-follow-in-2023-3jjd</guid>
      <description>&lt;p&gt;As a &lt;em&gt;full stack dev&lt;/em&gt; with huge amount of experience under my belt, I feel like I have come full circle in the web development world. I started out with .NET starting with web forms, then MVC frameworks to SPA frameworks like Angular &amp;amp; then &lt;em&gt;React&lt;/em&gt;, &amp;amp; in recent years &lt;em&gt;Next.js&lt;/em&gt; which goes back to Server Side Rendering.&lt;/p&gt;

&lt;p&gt;Although I have spent a lot of time developing front-ends with React &amp;amp; loved working with it, I feel it is at a stage where it has become too complicated as its time has passed and it is trying to continuously evolve due to modern architecture demands.&lt;/p&gt;

&lt;p&gt;Next.js was great in the early days until I started having issues with &lt;em&gt;re-hydrating&lt;/em&gt; server side data for the client, still haven't found a trivial solution for this. I am not comfortable using too much magic like &lt;em&gt;React Query&lt;/em&gt;, I have used &lt;em&gt;Redux&lt;/em&gt; in most of my apps in the past but with Server Side rendering &amp;amp; hydration it seems a bit overkill.&lt;/p&gt;

&lt;p&gt;So late last year I started looking at a lot of the promising new frameworks that have popped up. In order to excel at SSR most of the new frameworks have a Meta portion to it, just like Next.js is trying to be for React.&lt;/p&gt;

&lt;p&gt;My considerations were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I need less worries about re-renders. Potentially fine grained reactivity should take care of this.&lt;/li&gt;
&lt;li&gt;I need trivial SSR/SSG from the framework.&lt;/li&gt;
&lt;li&gt;No partial or full hydration headaches, when to hydrate on the server so the app passes on the data to client code.&lt;/li&gt;
&lt;li&gt;Dont need third party packages to monitor, fetch update data. I like less magic.&lt;/li&gt;
&lt;li&gt;Fast rendering with good lighthouse scores.&lt;/li&gt;
&lt;li&gt;Coding experience closer to vanilla JavaScript/TypeScript paradigms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Following is a brief list of frameworks I have come across: (Please keep in mind as I have not completely explored all aspects of these frameworks so I may miss features)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Qwik&lt;/strong&gt; is a framework that I have been impressed with as it delivers constant minimal JavaScript on first load &amp;amp; then lazy loads JS via web workers. It has &lt;strong&gt;Reactivity&lt;/strong&gt; which means it updates injected variables without the need for re-renders. It also has a meta portion called &lt;strong&gt;QwikCity&lt;/strong&gt; providing server functions SSR &amp;amp; SSG &amp;amp; does not require &lt;em&gt;hydration&lt;/em&gt; as it has &lt;strong&gt;resumeablility&lt;/strong&gt;. The syntax and concepts are very much like React so it is comfortable to pick up. The last time I looked they had some breaking changes &amp;amp; I feel it is not ready for prime time yet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SolidJs&lt;/strong&gt; is another great framework that has react like syntax, fine grained &lt;strong&gt;Reactivity&lt;/strong&gt; &amp;amp; fast rendering so theoretically less worries about changing state and no worries about re-renders. It has a meta framework called &lt;strong&gt;SolidStart&lt;/strong&gt; that provides server functions &amp;amp; server side rendering as well apart from plethora of other features. However SolidStart is in early development as well. I have heard issues about passing reactive variables to child components that dont get them updated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are people who swear by &lt;strong&gt;Svelte&lt;/strong&gt; &amp;amp; &lt;strong&gt;SvelteKit&lt;/strong&gt;.&lt;br&gt;
It is the one that started fine grained &lt;strong&gt;reactivity&lt;/strong&gt; trend. It is fast and compiles the code to delivery efficient JS at compile time. SvelteKit is the meta framework for Svelte that provides runtime for Svelte applications. With increasing application size however the size of the build increases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Astro&lt;/strong&gt; is another extremely popular website build tool.&lt;br&gt;
To me it seems more like other meta frameworks that is popularizing the &lt;strong&gt;islands&lt;/strong&gt; architecture. It is efficient in rendering fast static content at runtime and only fetching &lt;em&gt;javaScript&lt;/em&gt; for islands as they are required. On top of this it allows one to bring their own view library in it like React, SolidJs, Svelte etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All the above frameworks / libraries / tools are very promising however my vast experience with the react.js eco system tells me its never a bed or roses. So I am undecided as to which one of these technologies I should invest my time in for a deep dive that will be a good investment.&lt;/p&gt;

&lt;p&gt;If you were in my shoes what would you do, please provide your feedback in the comments section.&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
  </channel>
</rss>
