الانتقال إلى المحتوى
الأمان (Security)Secure Coding· 7 دقائق للقراءة

Least Privilege Applies to Your App Too: Database Users, Service Accounts, Containers

Every bug in your app runs with your app's rights. Give the running code only what it uses, and keep schema changes and admin rights somewhere else.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

A penetration test finds a SQL injection in an old reporting endpoint. That alone would be a serious finding, but the report rates it critical: through that one parameter, the tester dropped the audit log table and added a new user to db_owner. The API's login was a member of db_owner because the app applies EF Core migrations at startup. In the review meeting, the tech lead's first reaction is: "But only our code uses that login."

The common misconception

The belief: the app's own database user or service account can safely have admin rights, because only our code uses it.

It sounds reasonable. The credentials are secret, no person logs in with them, and the code is yours and reviewed. Admin rights also remove a whole class of production surprises: no "permission denied" when a new feature needs a table, and Database.Migrate() at startup just works. Restricting the app looks like friction with no attacker in sight.

The flaw is in "only our code". Code with a bug in it is still your code, and it runs with exactly the rights you gave it.

What actually happens

Every bug inherits the app's rights

An injection, an , a vulnerable dependency or a leaked connection string all end the same way: someone else's instructions run under your app's identity. Parameterized queries, covered earlier in this path, stop the injection itself. decides what happens when something slips through anyway.

OWASP's Database Security Cheat Sheet states the rule for app accounts: they "should only have the minimal permissions required for the application to function". It is specific: "Do not use the built-in root, sa or SYS accounts", "Do not grant the account administrative rights over the database instance", and the account "should not be the owner of the database as this can lead to privilege escalation vulnerabilities." It also says each account should be "Used by a single application or service."

Running and deploying need different rights

Most apps get admin rights for one reason: schema changes. Microsoft's EF Core docs are direct about it: "Use a separate identity for deployment that has permission to change the schema. The identity used by the application at run time should normally have only the permissions the application needs to read and write data." On migrating at startup, they list the cost: "The application must have elevated access to modify the database schema." A or SQL script run by the pipeline, with its own login, removes that need.

Root in a container is still root

Containers have identities too. OWASP's Docker Security Cheat Sheet calls running as an unprivileged user "the best way to prevent privilege escalation attacks", and reminds you that even with rootless Docker "the shared kernel remains part of the security boundary", the point the article "Docker Is Not a VM" makes. .NET's Linux images ship a non-root user named app, but you opt in. Microsoft's .NET 8 notes say: "To run as non-root, add the following line at the end of your Dockerfile", and expose its UID as APP_UID for Kubernetes' runAsNonRoot check.

A service account is a login to your cluster

In Kubernetes, the pod's is the app's identity for the API server. The RBAC Good Practices page says "minimal RBAC rights should be assigned to users and service accounts", recommends namespaced RoleBindings over ClusterRoleBindings, warns against wildcards, which also grant "all object types which are created in the future", and suggests automountServiceAccountToken: false for pods that never call the API.

Two database identities, on a real SQL Server

SQL grants.sql
-- One identity for everything: the app, migrations and peopleCREATE USER shop_app FOR LOGIN shop_app;ALTER ROLE db_owner ADD MEMBER shop_app;

We ran both setups on a throwaway database in SQL Server 2022, then sent the tester's statements as each login. As a db_owner member, all of them succeeded: the audit table was gone, a Payload table appeared, and a new user helper sat in db_owner. As the least-privilege app login:

text
SELECT COUNT(*) FROM dbo.Orders;    -- 3DROP TABLE dbo.AuditLog;-- Msg 3701: Cannot drop the table 'AuditLog', because it does--           not exist or you do not have permission.CREATE TABLE dbo.Payload (Id int);-- Msg 262: CREATE TABLE permission denied in database 'ReadInW_sec2'.CREATE USER helper WITHOUT LOGIN;-- Msg 15247: User does not have permission to perform this action.

The app still reads and writes its data, so features keep working. The migrator login created a table, altered Orders and wrote its history row, but it couldn't create users either. The containers get the same treatment in the final stage of the Dockerfile:

Dockerfile Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:10.0WORKDIR /appCOPY --from=build /app/publish .USER $APP_UIDENTRYPOINT ["dotnet", "Shop.Api.dll"]
IdentityCommon over-grantWhat it usually needs
App's database usersa or db_ownerSELECT, INSERT, UPDATE, DELETE on its own schema
MigrationsShared with the appA separate login with DDL rights, used only by the pipeline
Container processRoot, the defaultA non-root user such as USER $APP_UID
Kubernetes service accountcluster-admin or wildcard rulesNo token, or a namespaced Role with explicit verbs

How to apply it in your project

  1. List every identity the app runs asDatabase logins, cloud and Kubernetes service accounts, the container user and API keys to other services. Write down what each one is granted today.
  2. Split run time from deploymentApply migrations from the pipeline with a bundle or a reviewed script, under a separate login. Remove Migrate() from startup and drop the app's DDL rights.
  3. Grant the app what it usesStart with data rights on the app's schema. Tighten tables that hold sensitive data with column or row rules, or put them behind views.
  4. Run containers as non-rootAdd USER $APP_UID to .NET images and set runAsNonRoot in the pod's security context. Keep the default port 8080: Microsoft notes that moving back to port 80 means "you can't run as non-root."
  5. Scope service accounts and review themDisable token mounting where the pod never calls the API, bind namespaced roles with explicit verbs, and review grants regularly, as both OWASP and Kubernetes advise.

Key takeaways

  • The app's identity is used by every bug in the app, not just by the code you meant to run.
  • Give the running app data rights only; never sa, db_owner or database ownership.
  • Run migrations from the pipeline with a separate login, so startup doesn't need DDL rights.
  • Containers should run as a non-root user; .NET images ship one you opt into with USER $APP_UID.
  • Give service accounts namespaced, explicit permissions, and no token when they don't call the API.

📚 المصادر

  1. Database Security Cheat Sheet — OWASP Cheat Sheet Series
  2. Applying Migrations — EF Core, Microsoft Learn
  3. Docker Security Cheat Sheet — OWASP Cheat Sheet Series
  4. What's new in containers for .NET 8 — Microsoft Learn
  5. Role Based Access Control Good Practices — Kubernetes

التعليقات (0)