Database Security
Purpose
Protect the data store itself: who can connect, what the app account can do, how injection is made impossible, how sensitive data is classified and encrypted, and how access is audited. Complements ../../backend/backend-security (app layer) and feeds ../../security-review.
When to Use
- When provisioning a database, hardening an existing one, or handling new sensitive data classes.
- Not as the release audit itself (
../../security-review).
Inputs
- Data-sensitivity map (
../../backend/backend-security), tenancy model (../../backend/ownership-authorization).
- Deployment topology (who/what can reach the database), compliance constraints.
Discovery Questions
- What data classes live here (credentials, PII, payment/financial, health) and what do compliance rules demand of each?
- Who connects: app, migrations, humans, analytics — with what privileges each?
- Is the database network-reachable from anywhere beyond the app (it shouldn't be)?
Responsibilities
- Least-privilege accounts: the app's runtime account gets CRUD on its schema only — no DDL, no superuser (migrations run under a separate, deploy-scoped account —
database-migrations); humans get personal, audited, read-limited access; analytics gets replicas/views, not the primary's write account.
- Network isolation: private network/VPC only, no public listener; TLS on connections; access from production paths only — dev machines don't hold prod credentials.
- Credentials: secret store/env per
../../backend/backend-security, rotated, never in code/logs/dumps; per-environment separation absolute.
- Injection prevention: parameterized queries only — ORMs default to this, raw escape hatches (
$queryRaw, sql templates, Mongo operator injection via unvalidated objects) hold the discipline (../../backend/backend-validation blocks operator-shaped input like {$gt: ''}).
- Encryption: at rest (storage/provider level), in transit (TLS), and application-level encryption for the truly sensitive fields (tokens, secrets users store with you) — with key management stated; hashing where reversibility isn't needed (
../../backend/backend-authentication credentials).
- PII discipline: classify columns/fields; minimize collection; define retention + deletion paths (account-deletion actually deletes/anonymizes — including backups policy awareness
backup-recovery); PII never in logs (../../backend/backend-observability) or dev seeds (seed-data).
- Tenant isolation depth: scoped queries are the floor (
ownership-authorization); evaluate row-level security or per-tenant schemas where the risk profile demands database-enforced isolation — recorded decision either way.
- Auditing: who connected, admin/DDL actions, bulk exports — logged and reviewable.
Required Workflow
- Classify data; map compliance demands per class.
- Define accounts + privileges per connector; isolate the network.
- Verify parameterized-only access across the data layer (audit raw usages).
- Set encryption per class (rest/transit/field) with key management.
- Define PII retention/deletion paths.
- Record the tenant-isolation depth decision; wire access auditing.
- Hand to
../../security-review before release.
Decision Rules
- The app account can't do what the app never does — DDL rights in runtime accounts are standing self-harm.
- Any string-built query is a defect regardless of "we control that input."
- Field-level encryption is for data whose leak survives a database dump; don't encrypt-everything into unqueryable mush — classify first.
- Real production data never seeds lower environments (
seed-data).
Rules
- Never print/commit credentials while working; flag + rotate on discovery.
- Every raw-query usage is inventoried and reviewed.
- Deletion paths are tested, not asserted.
Anti-Patterns
- App connecting as superuser/owner "temporarily forever."
- Database on a public IP with password auth.
- String interpolation in the one legacy raw query nobody reviews.
- PII in logs, fixtures, and three abandoned analytics exports.
- "Encrypted at rest" (disk) presented as the answer to field-sensitivity questions.
- Account deletion that flips a boolean and keeps everything.
Validation Checklist
Definition of Done
A recorded database-security posture — least-privilege access, isolated network, injection-proof data layer, class-appropriate encryption, tested PII deletion, explicit tenant-isolation depth, and auditing — queued for security review.
Related Skills
../../backend/backend-security, ../../security-review, ../../backend/ownership-authorization, ../../backend/backend-validation, ../../backend/backend-authentication, backup-recovery, seed-data, database-migrations, ../../environment-audit.
Related Knowledge
../../../knowledge/ (data classes, compliance regime, topology).
Related References
../../../references/database/security/ (classification table, when populated).
Context Loading Guidance
- Requires: sensitivity map, tenancy model, topology, connector list.
- Does not require: application feature code, query-by-query review (sample raw usages).
- May load:
backup-recovery (backup encryption/PII), ../../security-review (hand-off).
- Stop when: the posture is recorded and handed to review.
Token Efficiency Guidance
The classification table (field class → encryption → retention → access) plus the account-privilege matrix carry the design; never echo actual secrets or real data while working.
1---2name: database-security3description: Use to plan database security — least-privilege credentials, network isolation, injection prevention (parameterized-only), encryption at rest/in transit, PII classification and minimization, tenant isolation at the data layer, and access auditing.4---56# Database Security78## Purpose910Protect the data store itself: who can connect, what the app account can do, how injection is made impossible, how sensitive data is classified and encrypted, and how access is audited. Complements `../../backend/backend-security` (app layer) and feeds `../../security-review`.1112## When to Use1314- When provisioning a database, hardening an existing one, or handling new sensitive data classes.15- **Not** as the release audit itself (`../../security-review`).1617## Inputs1819- Data-sensitivity map (`../../backend/backend-security`), tenancy model (`../../backend/ownership-authorization`).20- Deployment topology (who/what can reach the database), compliance constraints.2122## Discovery Questions2324- What data classes live here (credentials, PII, payment/financial, health) and what do compliance rules demand of each?25- Who connects: app, migrations, humans, analytics — with what privileges each?26- Is the database network-reachable from anywhere beyond the app (it shouldn't be)?2728## Responsibilities2930- **Least-privilege accounts**: the app's runtime account gets CRUD on its schema only — no DDL, no superuser (migrations run under a separate, deploy-scoped account — `database-migrations`); humans get personal, audited, read-limited access; analytics gets replicas/views, not the primary's write account.31- **Network isolation**: private network/VPC only, no public listener; TLS on connections; access from production paths only — dev machines don't hold prod credentials.32- **Credentials**: secret store/env per `../../backend/backend-security`, rotated, never in code/logs/dumps; per-environment separation absolute.33- **Injection prevention**: parameterized queries *only* — ORMs default to this, raw escape hatches (`$queryRaw`, `sql` templates, Mongo operator injection via unvalidated objects) hold the discipline (`../../backend/backend-validation` blocks operator-shaped input like `{$gt: ''}`).34- **Encryption**: at rest (storage/provider level), in transit (TLS), and **application-level encryption for the truly sensitive fields** (tokens, secrets users store with you) — with key management stated; hashing where reversibility isn't needed (`../../backend/backend-authentication` credentials).35- **PII discipline**: classify columns/fields; minimize collection; define retention + deletion paths (account-deletion actually deletes/anonymizes — including backups policy awareness `backup-recovery`); PII never in logs (`../../backend/backend-observability`) or dev seeds (`seed-data`).36- **Tenant isolation depth**: scoped queries are the floor (`ownership-authorization`); evaluate row-level security or per-tenant schemas where the risk profile demands database-enforced isolation — recorded decision either way.37- **Auditing**: who connected, admin/DDL actions, bulk exports — logged and reviewable.3839## Required Workflow40411. Classify data; map compliance demands per class.422. Define accounts + privileges per connector; isolate the network.433. Verify parameterized-only access across the data layer (audit raw usages).444. Set encryption per class (rest/transit/field) with key management.455. Define PII retention/deletion paths.466. Record the tenant-isolation depth decision; wire access auditing.477. Hand to `../../security-review` before release.4849## Decision Rules5051- The app account can't do what the app never does — DDL rights in runtime accounts are standing self-harm.52- Any string-built query is a defect regardless of "we control that input."53- Field-level encryption is for data whose leak survives a database dump; don't encrypt-everything into unqueryable mush — classify first.54- Real production data never seeds lower environments (`seed-data`).5556## Rules5758- Never print/commit credentials while working; flag + rotate on discovery.59- Every raw-query usage is inventoried and reviewed.60- Deletion paths are tested, not asserted.6162## Anti-Patterns6364- App connecting as superuser/owner "temporarily forever."65- Database on a public IP with password auth.66- String interpolation in the one legacy raw query nobody reviews.67- PII in logs, fixtures, and three abandoned analytics exports.68- "Encrypted at rest" (disk) presented as the answer to field-sensitivity questions.69- Account deletion that flips a boolean and keeps everything.7071## Validation Checklist7273- [ ] Data classified; compliance mapped.74- [ ] Least-privilege accounts per connector; migration account separate.75- [ ] Network private; TLS enforced.76- [ ] Parameterized-only verified; raw usages inventoried.77- [ ] Encryption per class + key management recorded.78- [ ] PII retention/deletion paths defined and tested.79- [ ] Tenant-isolation depth decision recorded; auditing wired.8081## Definition of Done8283A recorded database-security posture — least-privilege access, isolated network, injection-proof data layer, class-appropriate encryption, tested PII deletion, explicit tenant-isolation depth, and auditing — queued for security review.8485## Related Skills8687`../../backend/backend-security`, `../../security-review`, `../../backend/ownership-authorization`, `../../backend/backend-validation`, `../../backend/backend-authentication`, `backup-recovery`, `seed-data`, `database-migrations`, `../../environment-audit`.8889## Related Knowledge9091`../../../knowledge/` (data classes, compliance regime, topology).9293## Related References9495`../../../references/database/security/` (classification table, when populated).9697## Context Loading Guidance9899- **Requires:** sensitivity map, tenancy model, topology, connector list.100- **Does not require:** application feature code, query-by-query review (sample raw usages).101- **May load:** `backup-recovery` (backup encryption/PII), `../../security-review` (hand-off).102- **Stop when:** the posture is recorded and handed to review.103104## Token Efficiency Guidance105106The classification table (field class → encryption → retention → access) plus the account-privilege matrix carry the design; never echo actual secrets or real data while working.