OWASP 2017 A3: Sensitive Data Exposure
Encryption is cheap. The expensive part is the data you kept and forgot.
Sensitive Data Exposure is a security vulnerability in which an application does not protect data that would hurt a person if it leaked, where it is stored or while it travels. It is number three on the OWASP Top 10 2017. In 2013 it was number six. It did not change because the bug got worse. It moved up because the cost of getting it wrong did, and because the ground underneath it shifted. The web encrypted itself, browsers started naming the sites that did not, and a regulator in Europe put a price on a careless leak that a company can actually feel.
This item is not a single coding mistake. It is the question you are left with after an attacker is already past the door, or sitting on the same network as your user. When your application holds something that would hurt a person if it leaked, is it protected in a way that still works after the breach? I wrote the 2013 version of this item in an earlier essay. What follows is what changed by 2018, and the defenses that match the new ground.
What changed from 2013
Three things.
HTTPS became the default, and the browser started saying so. In 2013, serving a login form over plain HTTP was common and drew no visible warning. By 2018 that is over. Free certificates made encryption a non-excuse, and on July 24, 2018, Chrome 68 began marking every plain HTTP page with a “Not secure” label in the address bar, for all users, on every page, not only on pages with a password field. Google reported that encrypted traffic on Android had risen from 42 percent to 76 percent across those years, and that 83 of the top 100 sites served HTTPS by default. Plain HTTP is now something a user is told to distrust.
A leak got expensive. The European Union’s General Data Protection Regulation took effect on May 25, 2018. It changed sensitive data from a reputational problem into a budget line. It requires notifying a regulator within 72 hours of discovering a breach, and it carries fines reaching the higher of 20 million euros or 4 percent of a company’s worldwide annual revenue. It also gives a direct reason to encrypt: if the leaked data was unintelligible to the thief, for example because it was properly encrypted, the duty to notify every affected person can fall away. Encryption stopped being only a control and became a way to cap the damage.
The scope narrowed. In 2013 this item carried some baggage that was really about configuration, such as leaky stack traces. The 2017 edition hands those to Security Misconfiguration and keeps A3 on the thing it was always about: data in transit and at rest, and credentials. The OWASP text calls it the most common impactful attack of the preceding years, and names the usual root cause bluntly: the data was not encrypted at all.
Classify, then keep less
The instinct is to jump to algorithms. Start one step earlier, because the safest record is the one you never stored.
Walk your data and sort it into buckets: public, internal, personal (names, emails, addresses), and regulated (payment cards, health data, government identifiers, credentials). For each regulated field, ask a question most teams skip: do we actually need to keep this? A card number you tokenize through your payment processor and never store is a card number that cannot leak. The last four digits are enough to show a customer which card they used. A date of birth you check once at signup and then discard is not in the breach.
The General Data Protection Regulation (GDPR), Europe’s privacy law, gives this a name, data minimisation, but it is simply good engineering: every field you store is a field you must defend forever. Most breaches that made headlines in 2017 and 2018 hurt more than they had to because the company kept data it did not need, in a form it did not protect.
Protecting data in transit
Encryption on the wire is settled technology. The mistakes now are in enforcement, not in the cipher.
Serve everything over HTTPS, including the first request. The classic attack is not breaking TLS; it is catching the user before TLS starts. A user types example.com, the browser tries http:// first, and an attacker on the same network answers that one unprotected request and keeps the whole session on plain HTTP. The fix is HTTP Strict Transport Security (HSTS), a header that tells the browser to never use plain HTTP for your domain again:
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
max-age is one year in seconds. includeSubDomains covers every host under your domain, which closes the gap where an attacker forces a victim to http://anything.example.com to plant a cookie. preload lets you submit your domain to a list that ships inside the browser, so even the very first visit, before the browser has ever seen your header, is forced to HTTPS.
One warning that the usual advice leaves out: the preload list is close to a one-way door. Getting off it reaches users only through a browser update months later, and until then every subdomain must serve valid HTTPS, including internal ones that are not public. Add preload only when you are sure every host under your domain will speak HTTPS for the foreseeable future. Turn it on by mistake on a domain with a legacy HTTP subservice, and you have taken that service offline for months.
Encryption at rest that actually protects
Here is the question that separates real encryption at rest from the checkbox kind: where does the key live? Full-disk or database-level transparent encryption, where the database decrypts every row automatically, protects against exactly one threat: a stolen physical disk. It does nothing against the attacker who holds a valid database login or an injection bug, because to that attacker the database helpfully decrypts everything on the way out. The OWASP text makes this point directly in its first attack scenario.
Encryption protects a field only when the key is not sitting next to the data, and not in the code. The pattern that works is envelope encryption with a key management service (KMS). A master key lives inside the KMS and never leaves it. The KMS hands your application a fresh single-use data key, both in the clear and wrapped under the master key. You encrypt the field with the data key, store the wrapped key beside the ciphertext, and throw the clear key away. To read the field back, you ask the KMS to unwrap the key. An attacker who steals the database gets ciphertext and wrapped keys that are useless without the KMS, and the KMS can require authentication and log every unwrap.
Vulnerable code
# The key is in the source, so a leaked repository leaks every record.
KEY = b"0123456789abcdef0123456789abcdef"
def encrypt_ssn(ssn):
nonce = os.urandom(12)
return nonce + AESGCM(KEY).encrypt(nonce, ssn.encode(), None)
A key in the source is a key in version control, in every developer’s laptop, in every backup of the repository, and in the container image. The encryption is real and the protection is zero.
Fixed code
import base64, os, struct
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
# In production: kms = boto3.client("kms"). The master key never leaves the KMS.
# generate_data_key returns a fresh key both in the clear and wrapped under the master key.
def encrypt_field(kms, key_id, plaintext):
resp = kms.generate_data_key(KeyId=key_id, KeySpec="AES_256")
data_key, wrapped_key = resp["Plaintext"], resp["CiphertextBlob"]
nonce = os.urandom(12)
ciphertext = AESGCM(data_key).encrypt(nonce, plaintext.encode(), None)
del data_key # drop the clear key as soon as we are done with it
record = struct.pack(">H", len(wrapped_key)) + wrapped_key + nonce + ciphertext
return base64.b64encode(record).decode() # store this; the master key stays in the KMS
def decrypt_field(kms, blob):
raw = base64.b64decode(blob)
n = struct.unpack(">H", raw[:2])[0]
wrapped_key, nonce, ciphertext = raw[2:2 + n], raw[2 + n:14 + n], raw[14 + n:]
data_key = kms.decrypt(CiphertextBlob=wrapped_key)["Plaintext"]
return AESGCM(data_key).decrypt(nonce, ciphertext, None).decode()
Run it against a stand-in KMS and you should see three things. A value round-trips correctly. Two encryptions of the same input produce different output, because each uses a fresh data key and nonce, so an attacker cannot tell which rows hold the same value. And flipping one byte of the stored blob makes decryption fail rather than return wrong data, because AES-GCM, the form of AES encryption used here, authenticates what it decrypts. The length prefix on the wrapped key matters: real KMS blobs are not a fixed size, and code that assumes they are breaks the first time the KMS changes its format.
Passwords: slow, and getting slower
Password hashing is a special case of data at rest, and the standard moved. In 2013 the advice was bcrypt or scrypt. By 2018 the direction is clear: use a slow, memory-hard function, one that is expensive in memory as well as time, so an attacker with a rack of graphics cards cannot parallelize the cracking cheaply. The Password Hashing Competition settled on Argon2 as its winner in 2015, and PHP 7.2 (released at the end of 2017) added it as a built-in option alongside bcrypt. The OWASP A3 text lists Argon2, scrypt, bcrypt and PBKDF2, in that order of preference.
In PHP the whole thing is one call, and the important part is the upgrade path:
<?php
// Hashing: let the platform pick and tune the algorithm.
$hash = password_hash($password, PASSWORD_DEFAULT);
// On login: verify, then transparently upgrade old hashes to the current standard.
if (password_verify($password, $stored)) {
if (password_needs_rehash($stored, PASSWORD_DEFAULT)) {
$stored = password_hash($password, PASSWORD_DEFAULT);
// save $stored back to the user row
}
// log the user in
}
password_needs_rehash is the piece most teams miss. You cannot re-hash a password you do not have in the clear, so you upgrade it at the one moment you do: when the user next logs in and hands it to you. Over a few weeks your table migrates itself from old hashes to new, with no password resets. Raise the cost setting and password_needs_rehash reports every older, cheaper bcrypt hash as needing a rehash, so the upgrade happens on its own.
Two failures the usual advice misses
Secrets in source code and logs. The encrypted field above is undone if the key sits in the repository, and in 2018 that is a leading cause of real breaches, not a hypothetical. The access key that opened Uber’s cloud storage in its 2016 breach, disclosed in November 2017, was found in a code repository; the data behind it was stored unencrypted, so one key read out 25.6 million names and email addresses and 600,000 driver’s license numbers. Keep secrets in a secrets manager or injected environment, never in the code, and scan your history for the ones already there.
Logs are the quieter version. Twitter asked all 330 million of its users to change their passwords in May 2018 after a bug wrote them to an internal log in the clear, before the hashing step ran. GitHub disclosed a near-identical logging bug around the same time. The passwords were hashed in the database and still leaked, because a log statement upstream never got the message. Scrub sensitive fields before they reach a log line, and treat logs as data that can leak like any other.
Caching sensitive responses. A page that shows a user’s statement or personal details should not be stored by the browser or an intermediate cache. Say so explicitly:
Cache-Control: no-store
Pair it with a session cookie marked Secure, HttpOnly and SameSite, so the cookie never travels over plain HTTP and is not readable from script:
Set-Cookie: session=...; Secure; HttpOnly; SameSite=Lax; Path=/
The common mistake is Cache-Control: no-cache, which does not mean “do not store.” It means “revalidate before reuse,” and the sensitive page can still sit on disk on a shared machine. For data you do not want kept, the directive is no-store.
How to test your own application
- Hit every page over
http://and watch what happens. It must redirect to HTTPS, and the HTTPS response must carry an HSTS header with a longmax-age. Check subdomains too. - Run your site through a TLS checker and confirm it refuses old protocols and weak ciphers.
- Open a sensitive page, then read its response headers. Look for
Cache-Control: no-storeand aSecure; HttpOnlysession cookie. - Grep the code and the full Git history for keys and passwords:
AKIAfor AWS keys,BEGIN PRIVATE KEY,password =,secret. Finding one in history means rotating it, not just deleting the line. - Pull a sample of regulated fields straight from the database. If you can read a card number, a government identifier or a password in the clear, your encryption at rest is not protecting against the attacker who gets that far.
- For each regulated field, ask who can decrypt it and where that key lives. If the answer is “the database, automatically,” you have disk encryption, not field encryption.
Checklist
- Every regulated field classified, and the ones you do not need are not stored.
- HTTPS everywhere, with HSTS;
preloadonly once every subdomain is committed to HTTPS. - Field-level encryption with keys in a KMS, not on the disk beside the data and not in the code.
- Passwords hashed with a slow, memory-hard function, upgraded on next login.
- No secrets in source, history or logs; sensitive fields scrubbed before logging.
Cache-Control: no-storeandSecure; HttpOnlycookies on sensitive pages.
Sensitive Data Exposure moved up the list because the world made it matter more, not because the engineering got harder. The tools are all here and mostly free: a certificate, an HSTS header, a key management service, one hashing call. The work that is left is the unglamorous part: deciding what you truly need to keep, and protecting the rest as if the breach already happened. For the data on this list, assuming the breach is the only honest way to design.