Network

Rest(ful) API
A REST API is an architectural style for client-server interaction. It uses standard HTTP methods to work with resources:
- GET – retrieve data (for example, a list of users).
- POST – create a new resource.
- PUT – completely replace an existing resource.
- PATCH – partially update a resource.
- DELETE – remove a resource.
Difference between PUT and PATCH
Suppose we want to edit a user:
{ "id": 1, "name": "Alice", "email": "alice@example.com", "age": 25 }
With PUT—even if only name
changes—you must send the full object with every field.
With PATCH you can send only the fields you want to update.
CDN
A Content Delivery Network is a set of servers distributed around the world that cache and serve static content (images, styles, JS, video, etc.).
DNS
DNS converts human-readable domain names into IP addresses.
Example: openai.com
→ 104.18.12.123
Security
CORS (Cross-Origin Resource Sharing)
- A browser security policy that restricts requests to another domain.
- If the server does not allow the client's domain in headers, the browser blocks the response.
- For example, when developing locally, a request from
localhost
will be blocked unless the backend addsAccess-Control-Allow-Origin: http://localhost:3000
. - Alternatively, use a reverse proxy: the dev server (Next/CRA/Vite) receives requests on its own
localhost
, makes the request to the real URL, and returns the response as if fromlocalhost
.
Preflight request
- For non-simple methods (e.g. POST with
Content-Type: application/json
) the browser first sends anOPTIONS
request to check if such access is allowed. - A preflight is sent if the request is not a “simple request”.
A simple request is:
- method: GET, POST, or HEAD;
Content-Type
:application/x-www-form-urlencoded
,multipart/form-data
, ortext/plain
;- no custom headers (e.g.
Authorization
,X-My-Header
).
CSRF (XSRF) (Cross-Site Request Forgery)
Attack: an attacker forces a browser to send a request to a trusted site with your cookies.
Example:
- in tab 1 you are logged in to
bank.com
- in tab 2 you open
evil.com
- it has a hidden form sending a request to
bank.com
- the browser attaches cookies automatically → the server thinks it was you.
Protection:
-
SameSite for cookies
- The
SameSite=Lax
orStrict
flag tells the browser: do not send cookies if the request comes from another site. - So when navigating from
evil.com
,bank.com
cookies are not attached.
- The
-
CSRF tokens
- The server generates a random token and embeds it in a form or provides it via API.
- The client must send it with each request.
- The attacker doesn't know the token → the request is rejected.
-
Checking headers
- The server inspects
Origin
orReferer
. - If the request didn't come from
bank.com
, it is blocked.
- The server inspects
XSS (Cross-Site Scripting)
- Injecting malicious scripts into a site via inputs or URL parameters.
Protection
- Escaping HTML (React does this automatically unless you disable it with the
dangerouslySetInnerHTML
prop)- replacing special characters like
<
and>
with<
and>
- replacing special characters like
- HttpOnly cookies
- The HttpOnly flag makes cookies inaccessible to JavaScript
- Example (Express)
app.post("/login", (req, res) => { // suppose the user successfully authenticated res.cookie("session", "abc123", { httpOnly: true, // ❗ JS on the front end cannot read it secure: true, // only via HTTPS sameSite: "Lax", // basic CSRF protection maxAge: 1000 * 60 * 60, // 1 hour }); res.send("Logged in"); });
- CSP (Content Security Policy)
- an HTTP header that tells the browser "which scripts, styles, images and other resources can be loaded and executed on this site"
- configured on the static file server (e.g. Nginx); in Next.js—in
next.config.js
HTTP(S)
- HTTP is the protocol by which the browser and server exchange data.
- HTTPS is the same HTTP but with TLS encryption on top.
What this provides:
- No one can spy on the data being sent (for example, a password).
- No one can silently modify server responses.
- The browser checks that the site is legitimate (via a certificate).
MITM – “Man in the middle” attack
An attacker inserts themselves between the browser and server, reading or modifying data. Common on open Wi‑Fi or with HTTP (or ws://
).
Protection
- Redirect from HTTP to HTTPS (configured in Nginx)
- Use
wss://
for WebSocket - CSP
- HSTS (HTTP Strict Transport Security)
- another header like CSP
- also configured on the static file server (e.g. Nginx); in Next.js—in
next.config.js
After entering a URL in the address bar
- The browser uses DNS to obtain the domain’s IP address.
- If HTTPS → TLS handshake (negotiate encryption, verify certificate).
- Request to the server → HTML is returned.
- CSS, JS, and images are loaded.
- DOM and CSSOM are built → layout → paint.
- JS makes the page interactive (hydration).