BE6: Protecting the API

Lesson 4 of 5 · reading 15–25 minutes; practice and project time is on the module page

Secrets, encryption and personal data

🎯You'll be able to

Keep secrets out of code, protect data in transit and at rest, store passwords correctly, and limit what personal data you keep and log.

🏠The everyday picture

A bank keeps the locker master key in a safe, not taped under the counter. Your locker's contents are listed only by number in the visitor register, never described.

📘What it really is

📝A worked example

A log line from the starter, during an error:

POST /v1/orders body={"phone":"+91 90000 00042","address":"12 Laxmi Road"} auth=Bearer eyJhbGciOi...

Problems: a phone number, an address and a full bearer token (which anyone reading the logs could reuse until it expires).

Fixed log line from the finished kit's error handler:

2026-11-01T07:12:03.120Z POST /v1/orders request_id=7f3a customer_id=1 error=<message and stack>

Expected outcome: logs that help debugging with IDs and timings, but contain no secrets, tokens or personal details.

🌍Where it's used

📝Case scenario

A developer commits a .env file with the production database password to a public repository "for a minute". Automated scanners find it within hours, and the database is copied. Deleting the commit doesn't help: the password must be rotated immediately, access logs checked, and a pre-commit secret scan and .gitignore entry added.

🖱️Try it

Search the kit's files for anything that looks like a secret (grep -ri "secret\|password" --include=*.js .). For each hit, decide: practice value, real secret, or neither. Then write a logging rule for TiffinBox: which fields may be logged, which may not. Success: every hit is a labelled practice value, and your rule names at least four fields to keep out of logs.

⚠️Common mistakes

🛠️For your project

The project pages refuse evidence text that looks like a secret; Project 1's marking guide has a safety item for secrets in code and logs.

✅Quick check

Which hashing methods suit stored passwords?

Show the answer

Slow, salted password hashes: Argon2id, scrypt or bcrypt.

A secret was committed and then deleted in the next commit. Is it safe?

Show the answer

No: it's still in history and may already be copied. Rotate it.

← PreviousRate limits
ProLearners Assist