How to Configure Codex SQL Server Least Privilege

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up codex sql server least privilege by creating a dedicated database user. Then you grant that user only the exact table permissions needed. Engineering teams often give coding agents broad owner roles to move fast. Broad roles let an automated agent drop tables or alter rows by mistake. Limiting permissions at the database level stops these bad writes before they happen. How codex sql server least privilege works in practice In SQL Server, a database user is the identity of a login i

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up codex sql server least privilege by creating a dedicated database user. Then you grant that user only the exact table permissions needed. Engineering teams often give coding agents broad owner roles to move fast. Broad roles let an automated agent drop tables or alter rows by mistake. Limiting permissions at the database level stops these bad writes before they happen.

How codex sql server least privilege works in practice

In SQL Server, a database user is the identity of a login inside a database. When an agent needs many databases on an instance, teams create a login in master. Then they map a database user to that login.

If the agent only needs one database, a contained database user is an option. This user has a password and is not tied to a login in master. An admin must first enable contained databases on the server and the database.

After creating the user, admins assign roles. SQL Server offers fixed database roles with preset permissions that you cannot change. The db_owner role can run all maintenance tasks and can even drop the database. Never give db_owner to an AI coding agent.

Instead, teams often start with the db_datareader role. Members of db_datareader can read all data from all user tables and views. To block changes, admins can also add the user to db_denydatawriter. That role stops the user from adding, changing, or deleting data.

What native roles solve and what they leave open

Native SQL Server roles decide who can log in and which objects they can reach. Giving an agent db_datareader and db_denydatawriter stops accidental deletes and table drops. It is a strong first step for database safety.

Still, fixed roles apply to the whole database. A user in db_datareader can read every user table and view. If a table holds emails, phone numbers, card numbers, or national IDs, the agent reads them. You can add db_denydatareader to block all user tables, or grant permissions per table.

Least Privilege Principle + Kubernetes API Server Access: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Even with per-table grants, native roles do not inspect individual queries in flight. Once a user has read rights on a table, it can pull every column by default. Native roles also do not mask sensitive fields in query results. If an agent queries a customer table, raw emails and card numbers return in full. Finally, local connection strings still hold a password on the developer machine or agent host.

Steps to tighten database permissions for agents

Start by creating a separate database user just for the coding agent. Do not share human admin accounts or app service accounts with the agent. Sharing accounts makes it hard to track which queries the agent ran.

Next, avoid broad fixed roles if your database holds sensitive tables. Instead of db_datareader, create a custom role and grant read rights only on safe views. Leave tables with national IDs or card numbers out of that custom role. Add the agent user to db_denydatawriter so it can never modify rows.

Finally, review what happens after the query leaves the database. Native database roles check access at the object level when a query starts. They do not rewrite matched fields in results or remove secrets from client machines.

Add runtime guardrails beside SQL Server

Role grants and network rules are needed setup for SQL Server. Still, an allowed user or agent tool can send a bad delete or read raw customer fields over a valid session. In practice, hoop.dev sits inline in the TDS protocol beside the database as a sidecar for your SQL Server. For every query an agent or human runs in production, hoop.dev decides whether to pass, mask, block, or escalate it for review.

With hoop.dev SQL Server controls, rules refuse banned statements before they run, hold risky queries for human review, and mask matched sensitive values in supported result columns in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for HIPAA, PCI DSS, SOX, and GDPR audits. See the source on GitHub.

Frequently asked questions

Should I use a login in master or a contained database user for Codex?

Use a contained database user if the agent only needs one or a few databases. Use a login in master if the agent must reach many databases on the instance.

Does the db_datareader role hide sensitive columns from an AI agent?

No, members of db_datareader can read all data from all user tables and views. To limit table access, grant permissions on specific tables or views instead of using db_datareader.

Why should I avoid giving db_owner to a coding agent?

Members of db_owner can run all maintenance tasks and can drop the entire database. An AI agent with db_owner could delete production schemas or change security settings by mistake.

The query runs. The emails stay hidden.