Back to results for “SQL/Sequelize)”
Node.js › Authentication & Authorization

What is the principle of least privilege, and how does it apply to designing an authorization system for a multi-tenant Node.js SaaS application?

Advanced

Answer

The principle of least privilege states that any user, service, or process should be granted only the minimum access necessary to perform its function, and nothing more -- in a multi-tenant SaaS context, this means enforcing strict tenant isolation (a user from Tenant A should never be able to access Tenant B's data, even accidentally through a missing filter), scoping database queries and API tokens to a specific tenant ID by default, and giving even administrative roles the narrowest permissions actually required for their job rather than blanket superuser access.
// Middleware enforcing tenant scoping on every query automatically
function scopeToTenant(req, res, next) {
  req.tenantId = req.user.tenantId;
  next();
}

async function getOrders(req) {
  // Every query is forced through the authenticated user's own tenant scope
  return db.orders.find({ tenantId: req.tenantId });
}
Real-world example A multi-tenant project-management SaaS enforces tenant scoping at the database-query layer itself (not just in route middleware), so that even a bug in a specific route handler that forgets to filter by tenant would still be caught by the underlying data-access layer, preventing any possibility of cross-tenant data leakage.

Common follow-ups: What's the difference between enforcing tenant isolation at the application layer versus at the database layer (like PostgreSQL row-level security)?;How do you audit an existing codebase to verify least-privilege is actually being enforced consistently?