Lesson 3 of 3
Protection in transit
Listen to an AI summary of Protection in transit
Data travels from one computer to another across a network, a group of connected computers. Data on the way is called data in transit. A person on the network can read data in transit unless the data is scrambled.
What is missing encryption
Missing encryption (CWE-311) is sensitive data, such as a password, sent or stored in a form other people can read.
A password sent over HTTP
Sam signs in to the sports shop website on the wifi in a café. Sam's browser sends a request with Sam's password inside. HTTP sends the request as plain text. Every computer that passes the request along can read the password, including the café wifi.
Sam's browser
sends password=kettle42
- HTTP
The café wifi
reads password=kettle42
- HTTP
The sports shop server
reads password=kettle42
What is TLS
TLS is the encryption that protects data while the data travels between two computers. HTTPS is HTTP sent over TLS. A URL that starts with https:// tells the browser to use TLS.
TLS works like a locked box. The message is the thing inside the box, and the locked box is the encrypted message. The browser and the server agree a key that locks and unlocks the box. The café wifi passes the box along and has no key.
Over HTTPS, the café wifi reads scrambled data in place of Sam's password. TLS also lets the browser and the server detect a message that was changed on the way.
Sam's browser
sends password=kettle42
- HTTPS
The café wifi
reads x9$Tq2#Lm8&w
- HTTPS
The sports shop server
reads password=kettle42
What TLS does not protect
TLS protects data only on the way. The server reads the message in plain form and can save the data in a database. An earlier lesson covered how a server stores a password.
What is a certificate
A locked box protects the message only when the box goes to the right computer. A certificate is a file that proves a server is the real server for a host name. A host name is the name of the computer a website runs on, such as shop.example. Organisations that browsers trust issue certificates, and first confirm who owns the host name.
A certificate check is the step where a program confirms the certificate of a server before sending data. In the comparison, the certificate check is how the sender knows the box will reach the right person. A browser makes the certificate check for every HTTPS page. When the certificate check fails, the browser shows a warning and sends no data.
A certificate check switched off
The server of the sports shop sends card details to a payment provider, a company that takes card payments. The Python code on the server uses requests, ready-made Python code that sends a request and makes the certificate check.
A developer tries the checkout page against a test server at the payment provider. No trusted organisation issued the certificate of the test server, so the certificate check fails. The developer switches the check off, and the change reaches production, the live copy that real customers use. Look at the end of the last line of the Python.
url = "https://pay.example/charge" # the certificate check is on requests.post(url, data=card) # the certificate check is switched off requests.post(url, data=card, verify=False)
The first call makes the certificate check. The second call adds verify=False, which tells requests to skip the certificate check.
requests.post(url, data=card, verify=False)
- requests.post(send a request
- urlto the payment provider
- data=cardwith the card details
- verify=Falsecertificate check off
With the certificate check off, the code sends the card details to any server that answers. A person on the network can answer in place of the payment provider and read every card number. The name of the weakness is improper certificate validation (CWE-295).
Other programming languages have their own names for the same switch.
- Python
verify=False- JavaScript on a server
rejectUnauthorized: false- Go
InsecureSkipVerify: true
Connections between servers
The server of the sports shop also connects to other computers, such as the database. A person who gets onto the network between those computers can read any connection that has no TLS.
HTTPS on every visit
A person can type shop.example with no https:// in front. The browser can then send the first request over HTTP, which the café wifi can read and change. HSTS is a header in a response that tells the browser to use HTTPS for the website on every later visit. The name of the HSTS header is Strict-Transport-Security.
A session cookie gives the same access as the password. The cookie setting Secure makes the browser send the cookie only over HTTPS, as Sessions explains.
Sensitive data in a URL
TLS protects a URL only on the way. The browser saves every URL in the browser history, and the server can write every URL to a log, a list of what happened. Anyone who can open the history or the log can read a password in a URL, such as shop.example/signin?password=kettle42.
Saved copies of a response
A cache is a saved copy of a response, kept to use again later. On a shared computer in the library, the next person can open the browser's saved copy of Sam's order page. The response header Cache-Control: no-store tells the browser to keep no copy.
Examples of what to look for
- A URL that starts with
http://in code that sends a request. - A database connection with TLS switched off, such as
sslmode=disable. - A certificate check switched off, such as
verify=Falsein Python. - A removed
Strict-Transport-Securityheader. - A session cookie set without
Secure. - A password or a card number in a URL.
- A response with sensitive data and no
Cache-Control: no-storeheader. - A setting that allows SSL, TLS 1.0 or TLS 1.1.
Check your understanding
State what TLS protects and what TLS does not protect.
Show the answer
TLS protects data while the data travels between two computers. TLS does not protect data after the data arrives, such as data saved in a database or a log.
Which part of the last line of the Python example switches the certificate check off?
Show the answer
verify=False, which tellsrequeststo skip the certificate check.Give one reason why a password stays out of a URL even with HTTPS.
Show the answer
The browser saves the URL in the browser history, and the server writes the URL to a log. Other people can read the history and the log.