Thilan Dissanayaka Web App Hacking Feb 11

Common Web Application Technologies

JWT - JSON Web Tokens

JWT is short for JSON Web Token. It is a compact and secure way to send information between two parties – like a client (browser) and a server.

We usually use JWTs to:

  • Log users in
  • Keep them logged in
  • Allow or deny access to parts of an app

JWTs are very common in modern web apps, especially in single-page apps (SPAs), mobile apps, and APIs.

A JWT has three parts, separated by a dot

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJ1c2VybmFtZSI6ImpvaG4iLCJpZCI6IjEyMyJ9.
AbC1234567890xYz

These parts are:

  • Header – Tells which algorithm is used (like HS256)
  • Payload – Contains the data (like user ID, username)
  • Signature – A hash created using a secret key, used to verify the token is not changed.

Why use JWT?

  • Stateless: The server doesn't need to remember sessions.
  • Portable: Can be used across web, mobile, and APIs.
  • Secure: Can be verified and signed (but keep secrets safe!).

JWTs can be secure if used correctly:

  • Use HTTPS always.
  • Keep your secret key hidden.
  • Don't store sensitive data like passwords in the token.
  • Set an expiration time for tokens.

Common Mistakes to Avoid

  • Storing JWTs in localStorage without thinking about XSS (cross-site scripting)
  • Not setting an expiry (exp) for the token
  • Using weak or public secret keys
  • Trusting data inside the token without verifying

Cookies

Cookies are small pieces of data stored on the browser by websites. They are used to remember information about the user, like:

  • Whether the user is logged in
  • User preferences (like language or theme)
  • Items in the shopping cart

What does a cookie look like? A cookie is just a name-value pair, like this:

token=abc123xyz;

The browser automatically sends cookies to the server with every request (for that domain).

Types of Cookie Options When a server sets a cookie, it can define options like:

Option What It Does
HttpOnly Stops JavaScript from reading the cookie (protects from XSS)
Secure Sends cookie only over HTTPS (not HTTP)
SameSite Controls cross-site sending (protects from CSRF)
Expires / Max-Age Controls how long the cookie stays alive
Path Controls which paths get the cookie
Feature Cookies (HttpOnly) Local Storage
Auto-sent to server Yes No
Accessible by JS No Yes
Secure from XSS Yes (HttpOnly) No
Secure from CSRF Needs SameSite Yes (not sent automatically)
Storage limit Small (\~4KB) Larger (\~5MB)

Local Storage

What is Local Storage? Local Storage is a feature in your web browser that allows websites to store data on your computer. It’s part of the Web Storage API.

Unlike cookies, local storage:

  • Is not sent to the server automatically
  • Can only be accessed using JavaScript
  • Can store more data (usually up to 5–10MB)

What can you store in localStorage? You can store things like:

JWT tokens

User preferences (theme, language)

Form inputs

Temporary app state

How to use localStorage (Examples)

// Save a value
localStorage.setItem('token', 'abc123');

// Get a value
const token = localStorage.getItem('token');

// Remove a value
localStorage.removeItem('token');

// Clear all local storage
localStorage.clear();

All values are stored as strings, so if you're saving objects, you must use JSON.stringify() and JSON.parse():

const user = { id: 1, name: 'Alice' };
localStorage.setItem('user', JSON.stringify(user));

const storedUser = JSON.parse(localStorage.getItem('user'));

Is localStorage safe for JWT? No, not really. Here's why:

Risk Description
XSS Attack If your site is vulnerable to XSS, a hacker can steal tokens from localStorage
Not Secure Tokens are not encrypted and visible in dev tools

So, while localStorage is convenient, it's not recommended for sensitive data like access tokens or JWTs unless you are 100% sure your site is protected from XSS.

When to Use Local Storage?

  • To store non-sensitive data like UI settings, dark mode, language preference.
  • In development or demo apps.
  • When you control the frontend and backend and are careful with security.

DOM - Document Object Model

It’s how your browser sees and works with a web page.

When you open an HTML file in your browser, it turns the HTML into a tree-like structure made up of objects. This structure is called the DOM.

Think of the DOM as a live map of your webpage that JavaScript can read and change.

Example: Here’s some simple HTML:

<html>
  <body>
    <h1>Hello, world!</h1>
    <p>This is a paragraph.</p>
  </body>
</html>

The DOM version of this would look like a tree:

Document
└── html
    └── body
        ├── h1
        │   └── "Hello, world!"
        └── p
            └── "This is a paragraph."

Each part of the HTML (like h1 or p) becomes a node (or object) in the DOM tree.

JavaScript uses the DOM to:

  • Read content from the page
  • Change text, style, or structure
  • Add or remove elements
  • Handle user actions (clicks, typing, etc.)

Common DOM operations with JavaScript

const title = document.querySelector('h1');

title.textContent = 'Welcome!';
title.style.color = 'blue';

const newParagraph = document.createElement('p');
newParagraph.textContent = 'New paragraph!';
document.body.appendChild(newParagraph);

AJAX - Asyncrhonous Javascript And XML

Using fetch() (modern and easy):

fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => {
    console.log(data); // Do something with the data
  });

Sending data (POST request):

fetch('https://api.example.com/login', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ username: 'thilan', password: '12345' })
})
  .then(res => res.json())
  .then(data => {
    console.log(data); // Show login result
  });

What makes AJAX special?

Feature Description
Asynchronous Doesn’t block or freeze the page
Fast Only updates parts of the page
Dynamic Enables live content and interactions
Smooth UX User experience is better and modern

Usages

  • Live search
  • chat
  • auto-save
  • dashboards, etc
ALSO READ
Database Indexing: Speeding Up Your Queries Like a Pro
Apr 26 Database Systems

In the world of databases, speed matters. Whether you're powering an e-commerce store, a social media app, or a business dashboard — users expect data to load instantly. That’s where database....

Build A Simple Web shell
Mar 23 Web App Hacking

A web shell is a type of code that hackers use to gain control over a web server. It is particularly useful for post-exploitation attacks, and there are various types of web shells available. Some of....

Basic concepts of Cryptography
May 03 Cryptography

Cryptography is the art and science of securing information. In today’s interconnected world, where data is constantly being transmitted across networks, cryptography plays a vital role in ensuring....

Introduction to Edge Computing
Mar 23 Computing Concepts

Edge computing is a distributed computing paradigm where computation and data storage are performed closer to the location where it is needed. Instead of relying solely on a centralized data center,....

GraphQL - Interview preparation guide
Oct 01 Interview Guides

## What is GraphQL? GraphQL is a query language for APIs and a runtime for executing those queries. It allows clients to request exactly the data they need, reducing over-fetching and....

Abstract Factory Pattern explained simply
Apr 26 Software Architecture

When you want to create **families of related objects** without specifying their concrete classes, the **Abstract Factory Pattern** is your best friend. --- ## What is the Abstract Factory....