You open a site, and the browser shows a red warning page about the certificate, the site's proof of identity. Plenty of people click "proceed" right away. Behind that warning is one check that failed. That check is what keeps your password secret on its way from the browser to the server.
That check is part of HTTPS: HTTP wrapped in TLS (Transport Layer Security), the layer that encrypts data between the browser and the server. Without HTTPS, everything sent, passwords and cookies included, can be read by anyone on the same network as you, for example on cafe Wi-Fi. Picture mailing a postcard: the mail carrier and anyone who holds it can read it. HTTPS turns that postcard into a letter in a sealed envelope that only the right recipient can open. The postcard is the data sent over plain HTTP, the mail carrier is the network the data passes through, and the envelope is TLS encryption.
Three things TLS guarantees
TLS gives three guarantees at once on every connection:
- Confidentiality (encryption): the data is scrambled, so someone in the middle, such as your ISP (your internet provider) or an attacker on the same Wi-Fi, can't read it.
- Integrity: the data can't be changed in secret on the way. If a single byte changes, the browser knows and rejects it.
- Authentication: the server proves it really is
bank.example, not an impostor. This is the certificate's job.
Modern browser features like service workers (scripts that can make a site work offline) and geolocation (the user's location) also run only on HTTPS. Google also gives HTTPS sites a small boost when ranking search results.
The TLS 1.3 handshake: agreeing a key before exchanging data
Before the first data is sent, the browser and server do a handshake: a short introduction to agree on a shared secret key. The latest version, TLS 1.3, finishes it in a single round trip (1-RTT). In the Lab, the handshake to bank.example runs step by step:
| Step | Direction | Contents |
|---|---|---|
| 1. ClientHello | browser to server | TLS 1.3 version, a list of ciphers, and the browser's key share |
| 2. ServerHello | server to browser | the chosen cipher, and the server's key share |
| 3. Certificate + CertificateVerify | server to browser | the bank.example certificate (signed by a CA, a certificate issuer), then the server's signature over the handshake |
| 4. Finished | server then browser | the server sends Finished, the browser checks the certificate and the signature, then sends Finished |
| 5. Application data | both ways | all data encrypted with the session key |
- ClientHello already carries a key share: a public number for computing the shared key. The list of ciphers is the list of encryption methods the browser knows. The key share in this first message is what makes TLS 1.3 fast. In TLS 1.2, the handshake took an extra round trip.
- ServerHello picks a cipher and sends the server's key share. From these two key shares, both sides compute the same session key (the key that scrambles this connection's data, not a login session), without ever sending that key itself. This pattern is called a key exchange, and the method TLS 1.3 uses is ECDHE.
- Certificate holds the public key of
bank.example, a key that everyone may see. A certificate is public, so anyone can send it. The proof that this server owns it is in CertificateVerify: a signature over the whole handshake, made with the private key that matches that public key. Only the real server has that private key. - After Finished, all data (step 5) is encrypted with the session key. From here on, TLS uses symmetric encryption (the same single key on both sides), because it's far faster than the cryptography the key exchange used.
So TLS uses two kinds of cryptography: asymmetric (a pair of a public key and a private key) only at the start, to agree on a key and to prove the server's identity, then symmetric encryption for all the data after.
The keys used in the key exchange are made fresh for each connection and thrown away after the handshake (ephemeral, used once). This gives Perfect Forward Secrecy (PFS): if the server's private key leaks in the future, recorded data from past connections still can't be opened, because their keys are already gone.
The certificate and the chain of trust
A certificate is the server's digital ID card. It holds the domain name (bank.example), the server's public key, a validity period, and a signature from its issuer. That issuer is a Certificate Authority (CA): a party the browser trusts.
The browser doesn't trust the bank.example certificate on its own. It follows a chain of signatures up to a root it already trusts:
bank.example (leaf: the site's certificate)
signed by
intermediate CA (an intermediate certificate)
signed by
ISRG Root X1 (root CA, already in the browser)
- The leaf is the site's certificate. It's signed by the intermediate, not by the root directly.
- The intermediate links the leaf and the root. The server sends the leaf and the intermediate.
- The root is in the built-in list of the browser or the operating system. If the chain reaches a trusted root, the certificate is valid. If the chain breaks in the middle or reaches an unknown root, the browser refuses it and shows a warning.
The browser also refuses if the domain name doesn't match, the certificate has expired, or a signature is invalid. Free certificates come from Let's Encrypt, usually through Certbot (a program that requests and installs certificates), and renew automatically.
HSTS: forcing HTTPS
Even when a site is already HTTPS, a visit sometimes still starts on http://, for example when someone types the address without https://, and is then redirected to https://. That http:// request is not encrypted. The HSTS header closes that gap for later visits: it asks the browser to remember to always use HTTPS for this domain. The very first visit to the site is still exposed, because the header has not been received yet. To close that gap too, the domain is added to the HSTS preload list, a list of domains built into browsers (through hstspreload.org). One requirement: the header must also include preload (the example below doesn't).
Strict-Transport-Security: max-age=31536000; includeSubDomains
max-age=31536000means remember for one year (in seconds).includeSubDomainsapplies the rule to all subdomains too.- After this header is received once, the browser changes
http://tohttps://by itself, without ever sending the unsafe request.
Recognising a safe URL
This is a simple check that tells three kinds of URL apart by their protocol, the start of the URL before ://:
function isSecureURL(url) {
const lower = url.toLowerCase(); // check case-insensitively
if (lower.startsWith("https://")) return "secure";
if (lower.startsWith("http://")) return "insecure";
return "invalid"; // not http/https
}
console.log(isSecureURL("https://example.com")); // secure
console.log(isSecureURL("http://example.com")); // insecure
console.log(isSecureURL("ftp://files.com")); // invalid
- Turn the URL into lower case first with
toLowerCase(), soHTTPS://is recognised too. The reason:startsWithis case-sensitive. https://in front means safe: there's TLS.http://means unsafe: plain data that can be read on the way.- Anything that doesn't start with either (
ftp://,mailto:) isn't HTTP at all, so the result is"invalid".
In the exercise, your function is tested with these five calls:
| Call | Result |
|---|---|
isSecureURL("https://example.com") |
"secure" |
isSecureURL("http://example.com") |
"insecure" |
isSecureURL("ftp://files.com") |
"invalid" |
isSecureURL("HTTPS://SECURE.COM") |
"secure" |
isSecureURL("mailto:[email protected]") |
"invalid" |
⚠️ What gets missed about HTTPS
- Forgetting
toLowerCase().isSecureURL("HTTPS://...")returns"invalid", becausestartsWith("https://")is case-sensitive. Turn it into lower case first. - Thinking
http://is fine on localhost but forgetting production. In production (the server that real users use), everything must be HTTPS, and everyhttp://is redirected tohttps://. - Ignoring certificate warnings. An "invalid certificate" warning often means someone is intercepting your connection, or the certificate has expired. Don't just click "proceed".
- Equating HTTPS with "a safe site". HTTPS only guarantees the connection is encrypted. A phishing site (a fake site that copies a real one to steal data) can have HTTPS too.
TL;DR: HTTPS is HTTP over TLS, the layer that guarantees confidentiality (the data is scrambled), integrity (the data can't be changed in secret) and authentication (the server proves who it is). The TLS 1.3 handshake, an introduction to agree on a key, finishes in one round trip. The browser and server exchange key shares (public numbers) in the ClientHello and ServerHello messages, to compute a shared session key, the key used to scramble the data. The server sends a certificate (the server's digital ID card) and a signature that proves it owns the certificate (CertificateVerify), then all data is encrypted symmetrically (the same single key on both sides) with the session key. The browser checks the certificate through a chain of signatures from the leaf (the site's certificate) to the intermediate (a go-between certificate) to a root the browser trusts. Free certificates come from Let's Encrypt. The HSTS header (Strict-Transport-Security) forces the browser to always use HTTPS from the second visit on. The first visit is protected by HSTS preload, a list of domains built into browsers. isSecureURL, the function from the exercise, returns "secure" for https://, "insecure" for http://, and "invalid" for anything else, after turning the URL into lower case.
⚡ A postcard becomes a sealed envelope
Meaning: Without HTTPS, the data you send is like a postcard: anyone on the same network can read it. HTTPS is HTTP wrapped in TLS (a layer of encryption), like a letter in a sealed envelope. TLS guarantees three things at once: confidentiality (the data is scrambled), integrity (it can't be changed in secret on the way), and authentication (the server proves it really is the site you meant to reach, such as bank.example).
⚡ A one-round-trip handshake
Guess first: before exchanging data, the browser and server do a handshake (an introduction) to agree on a session key, the key used to scramble the data. Is that session key sent over the network?
Never. The browser and server send each other key shares (public numbers), then each computes the same session key on its own. This method is called ECDHE.
Meaning: TLS 1.3 finishes the handshake in one round trip (one trip there and back). After that, all data is encrypted symmetrically (the same single key on both sides) with the session key, because that's far faster than the method used in the handshake.
⚡ The chain of trust
Write:
bank.example (leaf: the site's certificate)
signed by
intermediate CA (an intermediate certificate)
signed by
ISRG Root X1 (root CA, already in the browser)
Meaning: A certificate is the server's digital ID card. The browser follows this chain of signatures up to a root CA, an issuer the browser already trusts. If the chain is broken, the domain name doesn't match, or the certificate has expired, the browser refuses it and shows a warning.
⚡ HSTS and a safe URL
Write:
Strict-Transport-Security: max-age=31536000; includeSubDomains
Meaning: Once this HSTS header is received, the browser remembers to always use HTTPS for the domain that sent it, for one year (max-age is in seconds). A URL that starts with http:// is unsafe, so don't send a password through it. In the exercise, isSecureURL returns "insecure" for such a URL. Turn the URL into lower case first with toLowerCase().