
Least Privilege Applies to Your App Too: Database Users, Service Accounts, Containers
- كاتب موثّق
محتويات المقال
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
Database.Migrate() at startup just works. Restricting the app looks like friction with no attacker in sight.
What actually happens
Every bug inherits the app's rights
Running and deploying need different rights
Root in a container is still root
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
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
-- One identity for everything: the app, migrations and peopleCREATE USER shop_app FOR LOGIN shop_app;ALTER ROLE db_owner ADD MEMBER shop_app;-- The running app: read and write data, nothing elseCREATE USER shop_app FOR LOGIN shop_app;GRANT SELECT, INSERT, UPDATE, DELETE ON SCHEMA::dbo TO shop_app;-- The pipeline: schema changes, used only during deploymentCREATE USER shop_migrator FOR LOGIN shop_migrator;ALTER ROLE db_ddladmin ADD MEMBER shop_migrator;ALTER ROLE db_datareader ADD MEMBER shop_migrator;ALTER ROLE db_datawriter ADD MEMBER shop_migrator;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:
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.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:
FROM mcr.microsoft.com/dotnet/aspnet:10.0WORKDIR /appCOPY --from=build /app/publish .USER $APP_UIDENTRYPOINT ["dotnet", "Shop.Api.dll"]sa or db_owner | SELECT, INSERT, UPDATE, DELETE on its own schema | |
USER $APP_UID | ||
cluster-admin or wildcard rules | Role with explicit verbs |
How to apply it in your project
List every identity the app runs as Database logins, cloud and Kubernetes service accounts, the container user and API keys to other services. Write down what each one is granted today. Split run time from deployment Apply 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.Grant the app what it uses Start with data rights on the app's schema. Tighten tables that hold sensitive data with column or row rules, or put them behind views. Run containers as non-root Add USER $APP_UIDto .NET images and setrunAsNonRootin 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."Scope service accounts and review them Disable 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_owneror 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.
التعليقات (0)