Securing the Site

Goals

What the Browser Checks

What is the difference between HTTP and HTTPS?

What is a certificate, and what is a certificate authority?

Installing mkcert

How do I install mkcert on macOS, Windows, and Linux?

What does mkcert -install do, and when do I need to run it?

Generating and Using a Certificate

How do I generate a certificate for localhost using mkcert?

How do I start a Litestar server with HTTPS enabled?

i
litestar run --ssl-certfile localhost.pem --ssl-keyfile localhost-key.pem --app server:app

Check Understanding

You run mkcert localhost but forget to run mkcert -install first. What does the browser show when you open https://localhost:8000?

The browser shows a security warning and refuses to load the page. mkcert -install is what registers mkcert's certificate authority with your machine's trust store. Without it, the browser has never heard of the CA that signed your certificate, so it treats the certificate as untrusted.

A classmate sends you their localhost-key.pem file so you can run the same server. You copy the file, start the server, and open https://localhost:8000, but the browser still shows a security warning. What is wrong?

The certificate is only trusted on the machine where mkcert -install was run. Your classmate's CA is in their machine's trust store, not yours. You need to run mkcert -install on your own machine and then run mkcert localhost to generate a fresh certificate that your browser will accept.

What does the padlock icon in the browser actually guarantee about the sightings application?

The padlock means the connection between the browser and the server is encrypted, and that the server presented a certificate signed by a CA the browser trusts. It says nothing about whether the application itself is correct, complete, or free of security bugs. A server that leaks data through poorly written queries or missing access controls is still dangerous even when the padlock is showing.

The litestar run command below is supposed to start a secure server, but the browser shows a connection error. What is wrong?

The two file arguments are swapped. --ssl-certfile should point to localhost.pem (the certificate) and --ssl-keyfile should point to localhost-key.pem (the private key). Passing the key where the certificate is expected causes the TLS handshake to fail before the browser can connect.

litestar run --ssl-certfile localhost-key.pem --ssl-keyfile localhost.pem --app server:app

Exercises

Generate a Certificate for Both localhost and 127.0.0.1

Run mkcert localhost 127.0.0.1 to create a single certificate that covers both addresses. Start the server with this certificate and confirm that both https://localhost:8000 and https://127.0.0.1:8000 show the padlock.

Add Key Files to .gitignore

Open or create a .gitignore file in your project directory and add a line that prevents any file ending in -key.pem from being committed. Run git status after creating a certificate and confirm that localhost-key.pem does not appear as an untracked file.

Inspect a Certificate

Visit https://localhost:8000 in Chrome or Firefox, click the padlock icon, and find the option to view the certificate details. Identify the fields that show the issuer (which should be the mkcert CA you installed), the subject (which should be localhost), and the expiry date.