<?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: CREDO23</title>
    <description>The latest articles on DEV Community by CREDO23 (@credo23).</description>
    <link>https://dev.to/credo23</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%2F865213%2F6ae1c3f4-2964-44d0-82cd-8fdfa94a0c30.jpg</url>
      <title>DEV Community: CREDO23</title>
      <link>https://dev.to/credo23</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/credo23"/>
    <language>en</language>
    <item>
      <title>Everything You Need to Know About SSL Stripping attacks &amp; HTTP Strict Transport Secure</title>
      <dc:creator>CREDO23</dc:creator>
      <pubDate>Wed, 05 Jul 2023 08:55:23 +0000</pubDate>
      <link>https://dev.to/kadea-academy/everything-you-need-to-know-about-ssl-stripping-attacks-http-strict-transport-secure-4con</link>
      <guid>https://dev.to/kadea-academy/everything-you-need-to-know-about-ssl-stripping-attacks-http-strict-transport-secure-4con</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the last post, we learnt a lot about SSL/TLS (SSL certificates, SSL handshakes, and so on) and saw how important it is in safeguarding online communication and protecting sensitive data.But i stated that &lt;strong&gt;despite&lt;/strong&gt; the secure connection established by the SSL/TLS protocol, this communication remains &lt;strong&gt;vulnerable to malicious attacks because&lt;/strong&gt; it is over the &lt;strong&gt;internet&lt;/strong&gt;. In this article we are going to talks about SSL attacks, especially the striping attack also known as  &lt;strong&gt;SSL downgrade or HTTP downgrade attacks;&lt;/strong&gt; we will cover everything you need to know.&lt;/p&gt;

&lt;p&gt;Before I begin, let me discuss the internet vulnerability.&lt;/p&gt;

&lt;h2&gt;
  
  
  How is the Internet vulnerable?
&lt;/h2&gt;

&lt;p&gt;Many early network protocols that now form part of the Internet infrastructure were designed without security in mind. This was mainly because cybersecurity was not a top priority at the time. Examples of such protocols include HTTP (Hypertext Transfer Protocol), which is used for web browsing, SMTP (Simple Mail Transfer Protocol), which is used for email, and FTP (File Transfer Protocol), which is used for file transfers. These protocols were originally developed with the assumption that all participants on the network were trustworthy, and did not include any mechanisms for authentication, encryption, or integrity protection.&lt;/p&gt;

&lt;p&gt;The focus was primarily on making communication between computers possible, and &lt;strong&gt;security measures were often considered an afterthought.&lt;/strong&gt; As a result, attackers can exploit these vulnerabilities to intercept, modify, or steal sensitive data and information.&lt;/p&gt;

&lt;p&gt;To address these issues, newer protocols and security protocols such as SSL/TLS (Transport Layer Security), SSH (Secure Shell), and IPsec (Internet Protocol Security) have been developed to provide security services to Internet communications.&lt;/p&gt;

&lt;p&gt;However, older protocols that were not designed with security in mind are still in use today, and many organizations continue to rely on them.&lt;/p&gt;

&lt;p&gt;💡 As new technologies and applications are being developed, new vulnerabilities and attack vectors emerge, which makes it difficult for security experts to keep up and protect against all potential threats. That is why the internet remains vulnerable.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a network attack?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fae3v5fx2djvojovgwq5u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fae3v5fx2djvojovgwq5u.png" alt="image that describe a network attack" width="435" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A network attack is an attempt to gain unauthorized access to an organization’s network, with the objective of stealing data or performing other malicious activity. There are two main types of network attacks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Passive:&lt;/strong&gt; Attackers gain access to a network and can monitor or steal sensitive information without making any changes to the data, leaving it intact.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Active:&lt;/strong&gt; Attackers not only gain unauthorized access but also modify data, either deleting, encrypting, or otherwise harming it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A Brief History of SSL Stripping
&lt;/h2&gt;

&lt;p&gt;The SSL stripping vulnerability was discovered in 2009 by &lt;strong&gt;Moxie Marlinspike&lt;/strong&gt;, a prominent American computer security researcher. He brought out details of how SSL stripping attacks can be executed without anyone ever noticing them making them a serious threat to the digital security of both regular users and businesses.&lt;/p&gt;

&lt;p&gt;By the end of this article, you'll have a complete understanding of ****SSL striping attacks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpznx92ujh4khas3rzjgb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpznx92ujh4khas3rzjgb.gif" alt="Let’s goo" width="396" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s goo&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an SSL stripping attack?
&lt;/h2&gt;

&lt;p&gt;Every connection to a website requires an application protocol, which is either HTTP or HTTPS. HTTP is less secure because it transmits information in plaintext, whereas HTTPS is more secure because it encrypts all information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SSL stripping attacks&lt;/strong&gt; are a type of attack in which hackers downgrade a web connection from the more secure HTTPS (Hyper Text Transfer Protocol Secure) to the less secure HTTP (Hyper Text Transfer Protocol).&lt;/p&gt;

&lt;p&gt;This attack occurs when the hacker intervenes in the communication as (&lt;strong&gt;a man-in-the-middle&lt;/strong&gt;) between a user and the website server. The hacker sits in the middle of the connection, connecting himself to the target site and connecting the user to their servers. All the traffic from the victim’s machine is routed via a proxy server that is created by the hacker. This allows the hacker to see everything the user sends in unencrypted form.&lt;/p&gt;

&lt;p&gt;💡 A proxy server is a server that acts as an intermediary between a client (such as a user's computer) and other servers on the internet. The client sends its requests to the proxy server, which in turn forwards them to the appropriate servers, and vice versa.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does an SSL stripping attack work?
&lt;/h2&gt;

&lt;p&gt;When you type an address in your browser’s address bar, your browser first connects to the target site over an insecure connection (HTTP). The site then usually responds with a redirect to use a secure protocol (HTTPS) after the &lt;strong&gt;SSL handshake&lt;/strong&gt; succeeds. &lt;/p&gt;

&lt;p&gt;What if :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There were a way to get around the &lt;strong&gt;SSL handshake&lt;/strong&gt; ?

&lt;ul&gt;
&lt;li&gt;For example, attack the transition from the unsecure connection to the secure connection ?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbz0wojdeunerfg2woewi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbz0wojdeunerfg2woewi.gif" alt="a way to get around the SSL handshake?" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Yes ! It is possible and that is what we call &lt;strong&gt;SSL Stripping attack&lt;/strong&gt; !&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkhcfne4hf03k2cou7byh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkhcfne4hf03k2cou7byh.png" alt="man in the middle attack (ssl stripping)" width="650" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once hackers have got the traffic (the handshake session), they create a &lt;strong&gt;fake valid certificate&lt;/strong&gt;, mimicking the target web server. To do that, hackers use another type of man-in-the-middle attack to intercept the connection between the legitimate user and the legitimate server, for example &lt;em&gt;ARP spoofing&lt;/em&gt;, &lt;em&gt;IP spoofing&lt;/em&gt; or &lt;em&gt;DNS cache poisoning&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;After inserting themselves in the connection and create a proxy server, if the legitimate user attempt to connect to the legitimate website server, the user will establish a connection with the server fully controlled by the hacker. The hacker’s server then relays all traffic to the target website’s server and back, allowing the hacker to read and modify information along the way.&lt;/p&gt;

&lt;p&gt;If the victim wants to use HTTPS when visiting the site, their browser will expect the attacker’s server to present an SSL/TLS certificate for the domain. This requires the attacker to generate a certificate for the target website server and send it to the victim’s browser.&lt;/p&gt;

&lt;p&gt;After providing a certificate (fake) to the browser, another challenge is the &lt;strong&gt;Certificate Authority !&lt;/strong&gt; Browser trusts in certificates signed by a trusted &lt;strong&gt;&lt;a href="https://www.acunetix.com/blog/articles/tls-ssl-certificates-part-4/" rel="noopener noreferrer"&gt;&lt;/a&gt;Certificate Authority (CA).&lt;/strong&gt; If a certificate is not signed by a trusted CA, your browser will show a &lt;strong&gt;clear warning&lt;/strong&gt; and may even refuse to open a page.&lt;/p&gt;

&lt;p&gt;💡 Operating systems such as Microsoft Windows and Linux come with a built-in set of trusted Certificate Authorities, but you can always manually add new ones.&lt;/p&gt;

&lt;p&gt;For this attack to succeed, hackers need to add their CA to the trusted certificate store in your operating system. This part has to be done through other attack vectors, such as &lt;strong&gt;HTTPS Phishing&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario
&lt;/h2&gt;

&lt;p&gt;In this scenario, the target is the foobank.com website, and you are the victim.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The hacker makes a targeted phishing attack against you utilizing JavaScript-related vulnerabilities, causing you to download and install their CA certificate. This allows the hacker to generate TLS/SSL certificates that will be accepted by your browser without warning messages.&lt;/li&gt;
&lt;li&gt;The hacker configures their web server to act as a proxy. It accepts all connections to foobank.com and relays them to the real foobank.com website, and does the same with all the responses from the foobank.com website to you.&lt;/li&gt;
&lt;li&gt;Your computer is configured to use the DNS caches of your local provider. The hacker performs a DNS cache poisoning (DNS spoofing) attack against your provider’s DNS cache servers, causing your computer’s local cache to store the hacker’s IP address as the IP of foobank*.com*. Until this information expires in your local cache, you will be connecting to the hacker-controlled IP address every time you try to visit foobank.com in your browser.&lt;/li&gt;
&lt;li&gt;Now, when you type *https//&lt;a href="http://www.*foobank.com%C2%A0in" rel="noopener noreferrer"&gt;www.*foobank.com in&lt;/a&gt; the address bar, your browser looks up the IP address of *&lt;a href="http://www.*foobank.com%C2%A0in" rel="noopener noreferrer"&gt;www.*foobank.com in&lt;/a&gt; your computer’s local cache and finds the hacker’s IP address. The browser then connects to the hacker’s server and accepts a fake SSL/TLS certificate for foobank.com. No warning is shown because the browser successfully verifies the fake SSL/TLS certificate using the hacker’s CA certificate installed earlier.&lt;/li&gt;
&lt;li&gt;Your browser creates an SSL connection to the hacker’s server, and you now have secure, encrypted communications between your browser and the hacker’s server. However, all SSL traffic is decrypted by the hacker, logged and then separately relayed to the real foobank web server via a server-side secure connection. Neither you nor the foobank web server have any way of knowing that this is a MITM attack.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;💡 Unfortunately, after inserting their own certificate in your computer by &lt;strong&gt;HTTPS Phishing,&lt;/strong&gt;  browsers won’t display any SSL Certificate warning pop-ups, and the victims have no clue that such an attack is going on ☹.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is HTST ?
&lt;/h2&gt;

&lt;p&gt;HSTS stands for HTTP Strict Transport Security. It is a web security policy mechanism that helps to protect websites against various types of attacks, particularly those related to HTTPS downgrade attacks (striping attacks) and certificate forgery. It prevents hackers from intercepting web traffic and downgrading the connection to HTTP, replacing the secure HTTPS connection with an insecure one. Not only that, but it also protects against attacks where an attacker tries to use fraudulent or forged digital certificates to impersonate an HTTPS website. In the next section we will see how HSTS aids in preventing SSL attacks.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does HSTS aid in preventing attacks like SSL stripping attacks?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffagkm7kxqrv2myl0r6n6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffagkm7kxqrv2myl0r6n6.png" alt="HTTP Strict Transport Security" width="742" height="452"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HTTP Strict Transport Security was defined as a standard  web security in 2012 in &lt;a href="https://tools.ietf.org/html/rfc6797" rel="noopener noreferrer"&gt;RFC 6797&lt;/a&gt;. The primary goal of creating this standard was to help avoid &lt;a href="https://www.acunetix.com/blog/articles/man-in-the-middle-attacks/" rel="noopener noreferrer"&gt;man-in-the-middle (MITM)&lt;/a&gt; attacks that use &lt;em&gt;SSL stripping&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Websites using HSTS often do not accept clear text HTTP, either by rejecting connections over HTTP or systematically redirecting users to HTTPS.&lt;/p&gt;

&lt;p&gt;When  you type &lt;a href="http://foobank.com" rel="noopener noreferrer"&gt;foobank.com&lt;/a&gt;  in your browser and press enter, the browser will first try to use an HTTP tunnel and then the server will respond and redirect  to an HTTPS tunnel if SSL handshake succeds. Hackers insert themselves in the communication when the tunnel is ensecure (thus HTTP). So, if we can prevent the browser to use the HTTP tunnel, we can prevent hackers from inserting themselves in the communication as a man-in-the-middle.&lt;/p&gt;

&lt;p&gt;The HSTS Policy is communicated by the server to the user agent (browser) via an HTTP response header field named &lt;code&gt;Strict-Transport-Security&lt;/code&gt;. HSTS Policy specifies a period of time during which the user should only access the server in a secure tunnel (HTTPS).&lt;/p&gt;

&lt;p&gt;But this protection applies after a user has visited the site at least once, using the principle of "&lt;strong&gt;trust on first use&lt;/strong&gt;". The way this protection works is that when a user enter or select a URL to the site that specifies HTTP, the user will be redirected directely to HTTPS before making a request to the website server, which prevents the HTTP man-in-the-middle attack from occurring.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;Trust on first use&lt;/strong&gt; (&lt;strong&gt;TOFU&lt;/strong&gt;), or &lt;strong&gt;trust upon first use&lt;/strong&gt; (&lt;strong&gt;TUFU&lt;/strong&gt;), is an authentication scheme used by client software which needs to establish a trust relationship with an unknown or not-yet-trusted endpoint. In a TOFU model, the client will try to look up the endpoint's identifier, usually either the public identity key of the endpoint, or the fingerprintof said identity key, in its local trust database. If no identifier exists yet for the endpoint, the client software will either prompt the user to confirm they have verified the purported identifier is authentic, or if manual verification is not assumed to be possible in the protocol, the client will simply trust the identifier which was given and record the trust relationship into its trust database. If in a subsequent connection a different identifier is received from the opposing endpoint, the client software will consider it to be untrusted.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Strict-Transport-Security&lt;/code&gt; header gives specific instructions to the browser.&lt;/p&gt;

&lt;p&gt;A server send a header field such that future requests to the domain for the next year use only HTTPS: &lt;code&gt;Strict-Transport-Security: max-age=31536000&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;So, from now on, every connection to the site and its subdomains for the next year &lt;strong&gt;&lt;em&gt;(31536000 seconds from the moment this header is received)&lt;/em&gt;&lt;/strong&gt; must be an HTTPS connection. &lt;strong&gt;HTTP connections are not allowed at all&lt;/strong&gt;. If the browser receives a request to load a resource using HTTP, it must try an HTTPS request instead. If HTTPS is not available, the connection must be terminated.&lt;/p&gt;

&lt;p&gt;When a web application issues HSTS Policy to user agents, conformant user agents behave as follows :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Automatically turn any insecure links referencing the web application into secure links (e.g. &lt;code&gt;http://example.com/some/page/&lt;/code&gt; will be modified to &lt;code&gt;https://example.com/some/page/&lt;/code&gt; &lt;em&gt;before&lt;/em&gt; accessing the server).&lt;/li&gt;
&lt;li&gt;If the security of the connection cannot be ensured (e.g. the server's TLS Certificate is not trusted), the user agent must terminate the connection and should not allow the user to access the web application.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;💡 This will work only if the hacker didn’t strip the header on the first visit to the site.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ldhr6zh7izuigfj2v0k.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ldhr6zh7izuigfj2v0k.gif" alt="This will work only if the hacker didn’t strip the header on the first visit to the site." width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;It is crucial for both website owners and internet users to be aware of the potential threat posed by SSL stripping attacks and the importance of implementing HSTS (HTTP Strict Transport Security).&lt;/p&gt;

&lt;p&gt;By enabling HSTS (HTTP Strict Transport Security) on websites, users' browsers are instructed to only establish secure TLS/SSL connections, thereby mitigating the risks of SSL stripping attacks. HSTS accomplishes this by informing the browser to remember and automatically upgrade any non-secure HTTP requests to HTTPS, ensuring end-to-end encryption.&lt;/p&gt;

&lt;p&gt;I hope you found the information I provided to be useful. I constantly make an effort to deliver the highest quality information.&lt;/p&gt;

&lt;p&gt;I want to thank you one again for reading. I much appreciate your support and being here!&lt;/p&gt;

&lt;p&gt;Regards,&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>api</category>
      <category>security</category>
    </item>
    <item>
      <title>Understanding SSL/TLS: The Key to Secure Online Communication</title>
      <dc:creator>CREDO23</dc:creator>
      <pubDate>Tue, 06 Jun 2023 06:46:40 +0000</pubDate>
      <link>https://dev.to/kadea-academy/understanding-ssltls-the-key-to-secure-online-communication-19a1</link>
      <guid>https://dev.to/kadea-academy/understanding-ssltls-the-key-to-secure-online-communication-19a1</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;With the increasing use of internet for various activities such as online shopping, banking, social networking, etc; security has become crucial for users.&lt;/p&gt;

&lt;p&gt;When a client is communicating with a server, there can be a transfer of sensitive information such as login credentials, credit card information or others information that are supposed to be private ! In that case, the communication must be secure.&lt;/p&gt;

&lt;p&gt;One of the critical components of online security is SSL. In this article, We are going to learn how  does SSL work,  and in the next article we will see how it can be compromised by hackers and how  to protect it against stripping attacks. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is SSL / TLS&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;SSL&lt;/strong&gt; stands for Secure Socket Layer, a security protocol that establish a secure and safe connection  between two systems.&lt;/p&gt;

&lt;p&gt;It makes sure that any information transferred between two systems  is not readable by using encryption algorithms to scramble it.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;TLS&lt;/strong&gt; stands for Transport Layer Security. It is just an update of SSL. It is more secure. So as much as we will discuss SSL and continuously refer to it throughout this article, we will also discuss TLS.&lt;/p&gt;

&lt;p&gt;In this article, we are going to focus on Client - Server system. A communication between a web browser (chrome,  Firefox, ...) as a client and a web server.&lt;/p&gt;

&lt;h2&gt;
  
  
  The basic of Client &lt;strong&gt;&lt;em&gt;- Server Communication&lt;/em&gt;&lt;/strong&gt;*
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The communication between web browsers and web servers is the foundation of the internet.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is just a simple request-response process in which the browser requests information from the server and the server responds with the requested information. This communication is facilitated by a set of protocols.&lt;/p&gt;

&lt;p&gt;💡 Protocols are the rules and standards that govern the communication between two systems.&lt;/p&gt;

&lt;p&gt;The most  commonly protocol used for web browser and web server communication is the Hyper Text Transfer Protocol (HTTP).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This communication &lt;em&gt;is essential for the functioning of the internet, but it also presents some **challenges.&lt;/em&gt;* As this communication is done over the internet, it is vulnerable to malicious &lt;strong&gt;attacks&lt;/strong&gt;. One of the biggest challenges is ensuring that the communication between those two systems is secure.*&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How does the SSL / TLS work ?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqbj49gxzxt6hlkodqwwv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqbj49gxzxt6hlkodqwwv.png" alt=" " width="305" height="165"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As discussed, the primary purpose of SSL is to provide a secure transport-layer connection between two endpoints. In our case, between a website server and the client browser.&lt;/p&gt;

&lt;p&gt;When the browser is trying to interact with a secured web server via the SSL protocol, an SSL certificate is needed to ensure that a secure connection is established. If the certificate is valid, the browser said to be “SSL enabled”, it will have a padlock icon just before the URL and will begin with “HTTPS” rather than “HTTP”.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is an SSL certificate ?
&lt;/h3&gt;

&lt;p&gt;An SSL certificate is a file hosted in the website’s  server, it contains :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The domain name that the certificate was issued for&lt;/li&gt;
&lt;li&gt;Which person, organization, or device it was issued to&lt;/li&gt;
&lt;li&gt;Which certificate authority issued it (CA)&lt;/li&gt;
&lt;li&gt;The certificate authority's digital signature&lt;/li&gt;
&lt;li&gt;Associated subdomains&lt;/li&gt;
&lt;li&gt;Issue  and expiration date of the certificate&lt;/li&gt;
&lt;li&gt;The public key&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To view an SSL certificate's details, you can click on the padlock symbol located within the browser bar.&lt;/p&gt;

&lt;p&gt;If a device tries to communicate with this server (secured), the device will use this file to obtain the information listed out above.&lt;/p&gt;

&lt;p&gt;Now, we know what is an SSL certificate and its content; let’s  see how a secure connection take place:&lt;/p&gt;

&lt;p&gt;The process works like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A browser  attempts to connect to a server (secured with SSL).&lt;/li&gt;
&lt;li&gt;The browser  requests that the server identifies itself (request the SSL certificate).&lt;/li&gt;
&lt;li&gt;The server sends the browser a copy of its SSL certificate in response.&lt;/li&gt;
&lt;li&gt;The browser checks to see whether it trusts the SSL certificate. If it does, it signals this to the server.&lt;/li&gt;
&lt;li&gt;The server then returns a digitally signed acknowledgment to start an SSL encrypted session.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;💡 This process is called “&lt;strong&gt;SSL handshake&lt;/strong&gt;” and it takes place in milliseconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  The SSL/TLS &lt;strong&gt;Handshake&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s understand some SSL/TLS terminologies before delve into the specifics of SSL/TLS handshake :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cipher suite&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6h9ry53stnyz9h4tdzks.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6h9ry53stnyz9h4tdzks.png" alt=" " width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A cipher is a cryptographic algorithm, a procedure used to encrypt and decrypt data.&lt;/p&gt;

&lt;p&gt;Ciphers operate by encrypting the original message (&lt;strong&gt;plaintext)&lt;/strong&gt; via the algorithm’s rules (i.e., the encryption key) to produce what’s known as &lt;strong&gt;ciphertext&lt;/strong&gt;. The ciphertext contains all the information of the original plaintext message but appears as a random string of data. It cannot be read by anyone who doesn’t have the key.&lt;/p&gt;

&lt;p&gt;The cipher use a symmetric encryption (when the same key is used for both encryption and decryption), or an asymmetric encryption (when different keys are used for encryption and decryption).&lt;/p&gt;

&lt;p&gt;A cipher suite is just a suite of cipher 🙃, in our case:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Key exchange&lt;/strong&gt; algorithms, such as RSA, DH, ECDH, DHE, ECDHE, or PSK : to exchange keys  securely.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authentication/Digital Signature&lt;/strong&gt; algorithm, like RSA, ECDSA, or DSA : to ensure that a message was sent by an entity that claims to have sent it, to ensure that the message has not been modified.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bulk encryption&lt;/strong&gt; algorithms, like AES, CHACHA20, Camellia, or ARIA : to encrypt messages *&lt;strong&gt;&lt;em&gt;exchanged *&lt;/em&gt;&lt;/strong&gt;between *&lt;strong&gt;&lt;em&gt;clients *&lt;/em&gt;&lt;/strong&gt;and ****servers, it is a more secured way to encrypt a large amount of data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Message Authentication Code&lt;/strong&gt; algorithms, such as SHA-256, and POLY1305 : to authenticate a message, to confirm that the message came from the stated sender and has not been changed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;During the handshake, the browser and the server will agree about a cipher suite to use (set of algorithms) to establish a secure connection. If the client and server do not agree on a cipher suite, no connection will be made. Once the cipher suite is agreed upon, they will proceed with the key exchange.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;*&lt;strong&gt;&lt;em&gt;Authentication&lt;/em&gt;&lt;/strong&gt;*&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5seaj4lqir9z7mx6351e.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5seaj4lqir9z7mx6351e.gif" alt=" " width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Authentication lets each party in a communication verify that the other party is who they say they are. &lt;/p&gt;

&lt;p&gt;The client uses the server’s certificate to authenticate the identity the certificate claims to represent.&lt;/p&gt;

&lt;p&gt;To authenticate the server, the client must receive a &lt;strong&gt;YES&lt;/strong&gt; answer to these questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is today’s date within the validity period?&lt;/li&gt;
&lt;li&gt;Is the issuing CA a trusted CA?&lt;/li&gt;
&lt;li&gt;Does the issuing CA’s public key validate the issuer’s digital signature?&lt;/li&gt;
&lt;li&gt;Does the domain name in the server’s certificate match the domain name of the server itself?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 A CA is an outside organization, a trusted third party, that generates and gives out SSL certificates. The CA will also digitally sign the certificate with their own private key, allowing client devices to verify it.&lt;/p&gt;

&lt;p&gt;We've got a grasp on what a cipher is and how the client authenticates the server, so let's have a look at the process of SSL/TLS handshake.&lt;/p&gt;

&lt;p&gt;The procedure for establishing an SSL/TLS connection depends on the encryption method employed, but the fundamental procedure is as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The SSL client will send the server a “&lt;em&gt;Client Hello&lt;/em&gt;” message that details the client’s configuration settings, including the SSL/TLS version, the cipher suites it supports,  and a string of random data referred to as “&lt;em&gt;client random.&lt;/em&gt;”&lt;/li&gt;
&lt;li&gt; The SSL server sends back a “&lt;em&gt;Server Hello&lt;/em&gt;” message containing its own public key, digital certificate, the cryptographic algorithm agreement (selected by the server from the client-supplied list of algorithms), and the “&lt;em&gt;server random&lt;/em&gt;”.&lt;/li&gt;
&lt;li&gt;The client performs authentication by contacting the server’s certificate authority (CA) to validate the web server’s digital certificate. This confirms the authenticity of the web server, thus, establishing trust.&lt;/li&gt;
&lt;li&gt;The client uses the extracted  public key from the verified certificate and generates a 48-bit string called the &lt;strong&gt;premaster secret&lt;/strong&gt;. The premaster secret is then encrypted using the extracted public key and is sent to the server. The premaster secret will be used for both client and server to generate the symmetric keys used for the secured session.&lt;/li&gt;
&lt;li&gt;The SSL/TLS server decrypts the premaster secret using its private key.&lt;/li&gt;
&lt;li&gt;Both client and server generate session keys from the client random, the server random, and the premaster secret. They should arrive at the same results  (&lt;strong&gt;a shared key&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;Next, the client sends an encrypted “&lt;em&gt;finished&lt;/em&gt;” message using the shared secret key. This message says that the client’s part of the handshake is complete.&lt;/li&gt;
&lt;li&gt;Finally, an encrypted “&lt;em&gt;finished&lt;/em&gt;” message is sent back to the client from the server using the previously agreed shared secret key, which indicates the end of the server’s side of the handshake.&lt;/li&gt;
&lt;li&gt;Once the SSL/TLS handshake and negotiation is done, the server and the client communication continues, i.e., they begin to share files and messages using the session keys (&lt;strong&gt;symmetric encryption&lt;/strong&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;SSL/TLS plays a crucial role in securing online communication and protecting sensitive data. It is important for websites and applications to implement SSL/TLS protocols to ensure the trust and confidence of users. As technology advances, it is also important to stay up-to-date with the latest SSL/TLS versions and best practices to maintain a secure online environment.&lt;/p&gt;

&lt;p&gt;I hope that the information I have shared has been informative, helpful and thought-provoking. I always strive to provide the best content possible.&lt;/p&gt;

&lt;p&gt;Once again, thank you so much for reading. Your presence and support mean a lot to me!&lt;/p&gt;

&lt;p&gt;See you soon!&lt;/p&gt;

&lt;p&gt;Best regards,&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to validate form inputs in an Expressjs app with joi package</title>
      <dc:creator>CREDO23</dc:creator>
      <pubDate>Mon, 31 Oct 2022 19:06:37 +0000</pubDate>
      <link>https://dev.to/kadea-academy/how-to-make-form-validation-in-server-side-built-with-expressjs-basic--5c74</link>
      <guid>https://dev.to/kadea-academy/how-to-make-form-validation-in-server-side-built-with-expressjs-basic--5c74</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This tutorial requires knowledge of using the expressjs framework&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you have been building web applications using an Express framework or any other Node.js framework, validation plays a crucial role in any web app which requires you to validate the request &lt;strong&gt;body&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why do we need server-side validation?&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The client side validation is not enough , it is within reach of the client.&lt;/li&gt;
&lt;li&gt;A user can turn off client-side JavaScript validation and manipulate the data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this tutorial, you’ll learn how to validate form in an Express.js app using an open source and popular module called joi.  We will be able to avoid data type errors that can be done by a client .&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to &lt;a href="https://joi.dev/api/?v=17.6.1#introduction" rel="noopener noreferrer"&gt;joi&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The definition on the official web site says : &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;joi lets you describe your data using a simple, intuitive, and readable schema.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;h2&gt;
  
  
  General use of &lt;a href="https://joi.dev/api/?v=17.6.1#general-usage" rel="noopener noreferrer"&gt;joi&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Usage is a two steps process:&lt;br&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, a schema is constructed using the provided types 
 and constraints:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;           &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Joi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Joi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
                &lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Joi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;number&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
                &lt;span class="na"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Joi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;email&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
           &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that joi schema objects are immutable which means &lt;br&gt;
   every additional rule added (e.g. .min(5)) will return a &lt;br&gt;
   new schema object.&lt;br&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Second, the value is validated against the defined 
 schema:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;           &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; 
                &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
                &lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;243&lt;/span&gt;
                &lt;span class="na"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bakerathierry@gmail.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If the input is valid, then req.body will be assigned to &lt;br&gt;
   result. &lt;br&gt;
   If the input is invalid, an explicit error will be thrown &lt;br&gt;
   and assigned  to result.&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In this tutorial we will be using its asynchronous &lt;br&gt;
   validation method &lt;em&gt;validateAsync&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;



&lt;p&gt;Let's take a look at a basic user route without any validation module to create a user: &lt;code&gt;/route/user&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userController&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;Now in user controller &lt;code&gt;./controllers/user&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./models/user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

   &lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

   &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;

      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;

      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userName&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;phone&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 

         &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;({...&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

         &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;savedUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

         &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;savedUser&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

            &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
             &lt;span class="na"&gt;msg&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User created successfully&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="na"&gt;data&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;savedUser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="na"&gt;error&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="na"&gt;success&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
             &lt;span class="p"&gt;}))&lt;/span&gt;

         &lt;span class="p"&gt;}&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code is just a basic example of validating fields on your own.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;You can handle some validations in your user model using Mongoose. For best practices, we want to make sure validation happens before business logic.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;joi will take care of all these validations and the sanitization of inputs as well.&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;With npm
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;insatll&lt;/span&gt; &lt;span class="nx"&gt;joi&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;With Yarn
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="nx"&gt;yarn&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="nx"&gt;joi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now , let's create a separate file in validation folder which content our schema validation , we have to include the validation module (joi) in this file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;joi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;joi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;joi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;userName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Joi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="na"&gt;phone&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Joi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;number&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;optionnal&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="na"&gt;email&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Joi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;email&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
         &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userSchema&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more details about joi , go to &lt;a href="https://joi.dev/api/?v=17.6.1#general-usage" rel="noopener noreferrer"&gt;joi&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;In this schema , we mention that :&lt;/em&gt;&lt;br&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;the userName must be :&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;a string (   .string( )   )&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;required (   .required()   )&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;the phone number must be :&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;a number (   .number( )   )&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;the email must be :&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;a string (   .string( )   )&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;a valid email (  .email( )   )&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;required (   .required( )   )&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;



&lt;p&gt;As you can see we use .optional( ) method to mention that the field can be missed ( not required )&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;** &lt;em&gt;Now we can use our schema whenever we want in the app .&lt;/em&gt; **&lt;/p&gt;



&lt;p&gt;Let's use that in the user controller &lt;code&gt;/controllers/user&lt;/code&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./models/user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userRegisterValidation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
         &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./validation/userRegister&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

   &lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

   &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;

      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;         
          &lt;span class="k"&gt;await&lt;/span&gt; 
          &lt;span class="nx"&gt;userRegisterValidation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;validateAsync&lt;/span&gt;&lt;span class="p"&gt;({...&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 

         &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;({...&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

         &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;savedUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

         &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;savedUser&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

            &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
             &lt;span class="na"&gt;msg&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User created successfully&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="na"&gt;data&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;savedUser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="na"&gt;error&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="na"&gt;success&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
             &lt;span class="p"&gt;}))&lt;/span&gt;

         &lt;span class="p"&gt;}&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If email is missed , a validation error will be thrown :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
    &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="nl"&gt;message&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; The email is required &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If phone is not a number :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
    &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="nl"&gt;message&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; The phone must be a number &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;And so one ...&lt;/p&gt;



&lt;p&gt;As you can see this module ( joi ) is helpful when it becomes to validate the client's form in server side , it maintain the code quality as well , but focused on business logic .&lt;/p&gt;



&lt;p&gt;I have tried my best and hope I covered enough to explain it so that you can get started.&lt;/p&gt;



&lt;p&gt;If you encounter any problems, feel free to comment below.&lt;br&gt;
I would be happy to help :)&lt;/p&gt;

</description>
      <category>express</category>
      <category>validation</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
