Learn As I Learn - Technology, Product and Cybersecurity
Learn As I Learn - Technology, Product and Cybersecurity
Akanksha Pathak
Series 2: Ep 10: Defense in Depth example
11 minutes Posted Oct 28, 2023 at 3:01 pm.
0:00
11:26
Download MP3
Show notes

Dive into the world of cybersecurity with our podcast, exploring defense in depth strategies through real-life examples. Discover the power of client and server-side validation techniques, safeguarding digital landscapes one layer at a time.


Client side validation:

// Client-side validation for a registration form

function validateForm() {

    var username =

document.forms["registrationForm"]["username"].value;

    var password =

document.forms["registrationForm"]["password"].value;

   

    // Check if

username and password are not empty

    if (username ==

"" || password == "") {

       

alert("Username and password must be filled out");

        return false;

// Prevent form submission

    }

   

    // Check if

password is at least 8 characters long

    if

(password.length < 8) {

       

alert("Password must be at least 8 characters long");

        return false;

// Prevent form submission

    }

}


Server side validation:


const express = require('express');

const bodyParser = require('body-parser');

const app = express();

 

// Middleware to parse incoming JSON requests

app.use(bodyParser.json());

 

// POST endpoint for user registration

app.post('/register', (req, res) => {

    const

username = req.body.username;

    const

password = req.body.password;

 

    //

Server-side validation

    if

(!username || !password) {

        return

res.status(400).json({ error: "Username and password are required"
});

    }

 

    // Perform

further validation checks and save user data to the database

    // ...

 

    return

res.status(200).json({ message: "Registration successful" });

});

 

// Start the server

app.listen(3000, () => {

   

console.log('Server is running on port 3000');

});