Skip to main content
This lesson walks through a database security audit for an Express login demo application. The objective is to inspect every database interaction, surface security gaps, and produce prioritized remediation steps so the app can be safely promoted to production. Initial CLI session used to launch the auditing tool:
We follow a structured checklist covering parameterized queries, secret management, permissions, encryption, PII controls, timeouts/pools, and audit/logging.
A dark-themed Visual Studio Code window with a file explorer on the left and a central terminal/editor pane showing a database security checklist (parameterized queries, connection string security, user permissions, encryption, PII handling, timeouts, connection pool settings, etc.). The project file tree and a highlighted security report file are visible in the sidebar.

High-level audit focus areas

  • Parameterized queries / safe ORM usage to prevent SQL/NoSQL injection
  • Connection string protection, TLS/SSL, and secret management
  • Database user permissions (principle of least privilege)
  • Encryption at rest and TLS in transit
  • PII handling, retention, and logging hygiene
  • Query timeouts, connection-pool limits, and transaction discipline
  • Audit logging for sensitive operations and schema changes
  • NoSQL injection hardening and row/tenant isolation for multi-tenant apps
Principle of least privilege: give each DB account only the privileges it needs — separate read-only, migration, and admin accounts; avoid SUPERUSER-level credentials unless explicitly required. We verify that critical fields are minimized, encrypted or tokenized where needed, and that PII is redacted from logs. Example checklist items (condensed):
  • Verify parameterized queries or ORM APIs are used instead of concatenating user input.
  • Ensure connection strings and credentials are stored in a secrets manager and rotated.
  • Confirm DB connections use TLS/SSL (validate server certificate).
  • Validate query timeouts and pool limits are configured at driver and server levels.
  • Confirm audit logging for schema and privilege changes, failed logins, and sensitive table access.
  • Enforce row-level security (or server-side tenant scoping) for multi-tenant data isolation.
A Visual Studio Code window with the explorer on the left and a text file open in the main pane. The file shows a numbered security checklist covering items like audit logging, NoSQL injection hardening, row/tenant isolation, TLS, secret management, and schema controls.

Row / tenant isolation

NoSQL sanitization (Mongoose example)

For MongoDB/Mongoose, avoid passing raw user input directly into query filters:
See Mongoose migration docs for sanitizeFilter: https://mongoosejs.com/docs/migration.html#sanitizeFilter

TLS / secret management and schema controls to check

Example: enabling SSL/TLS in node-postgres Pool (illustrative)

See node-postgres pooling docs: https://node-postgres.com/features/pooling
A dark-themed Visual Studio Code window showing a project explorer on the left and an editor/terminal pane with a security-audit checklist for an Express app in the center. The text highlights critical flags (e.g., direct string concatenation) and lists a structured findings/report template.

Critical flags to watch for

  • Direct string concatenation with user input in SQL or NoSQL queries — extremely high severity.
  • Passing user JSON directly into NoSQL query filters without sanitization (see OWASP NoSQL Injection: https://owasp.org/www-community/attacks/NoSQL_Injection).
  • Hard-coded credentials or placeholder secrets committed to the repository (e.g., JWT_SECRET=your_jwt_secret_key_here, DB_PASSWORD=your_db_password).

Useful audit commands (examples)

  • Search for environment files and secrets:
  • Check for console logging (may leak PII):
  • Generate a bcrypt test hash:
See bcrypt: https://www.npmjs.com/package/bcrypt

Audit findings (executive summary)

  • Strengths: parameterized queries, bcrypt password hashing, and input validation (express-validator) are implemented.
  • Critical gaps: no TLS/SSL for DB connections, placeholder secrets in .env, missing connection pool limits/timeouts, and no centralized audit logging.
  • Risk Score (current): 6.5 / 10 (Medium-High). After remediation, target ~2.5 / 10 (Low Risk).

Example critical finding (evidence & remediation)

  • CRITICAL: No TLS/SSL Encryption in Transit
    • Evidence (example): config/database.js:3-9 — Pool configuration missing SSL settings.
    • Impact: Cleartext DB credentials and payloads on the network (CWE-319: https://cwe.mitre.org/data/definitions/319.html).
    • Priority fixes:
      1. Enable TLS/SSL for DB connections and validate certificates.
      2. Remove .env from git and add .env.example.
      3. Replace placeholder secrets with strong values stored in a secrets manager.
      4. Add pool limits, timeouts, and resource caps.

Security checklist assessment

Risk score justification (summary)

  • Missing TLS and credential management are the largest contributors to risk (+3).
  • Good fundamentals (parameterized queries, hashing, validation) reduce risk (-2).
  • Missing enterprise controls (audit, timeouts, monitoring) add risk (+1).
  • Completing immediate remediation should significantly lower overall risk.

Conclusion & recommendations

  • The app uses solid fundamentals against injection and password attacks, but it is not production-ready as-is.
  • Complete immediate remediation items (TLS, secrets, pool/timeouts, logging hygiene) before production deployment.
  • Audit every place that constructs queries — a single unsafe concatenation can be catastrophic.
  • Enforce server-side tenant scoping for multi-tenant apps and ensure backups/exports scrub or encrypt PII.
Do not commit secrets or placeholder credentials to source control. Replace them with values stored in a proper secrets manager and use .env.example for local setup instructions.
Parameterized queries and input validation significantly reduce injection risk — maintain these practices while hardening transport, secrets, and logging layers.
Next: API and infrastructure security auditing — focus on authentication flows, token handling, rate limiting, and network-level protections (VPCs, security groups, firewall rules). References and further reading:

Watch Video