← All articlesWeb security

How to restrict a database account for a read-only service and verify that writes are blocked

A service that only displays data needs a database account with permission to read only what it requires. But a read-only label in the configuration does not confirm that writes are blocked. You need a verifiable contract: which objects are accessible, which operations are allowed, and which the database itself must reject. We will cover configuration and author-proposed defensive checks using an entirely fictional database of our own with synthetic records.

Start with a minimal permissions contract

Define permissions around the application's tasks: first list the required operations and objects, then grant minimal access. The read service account needs read permissions only for the tables it requires. It does not need to insert, update or delete rows; application accounts should not receive administrative privileges either.

In this entirely fictional example, a web service displays a catalog of training products from our own test database. For the fictional_reader user, the contract allows SELECT from fictional_catalog. INSERT, UPDATE and DELETE are prohibited, and there are no administrative privileges. Other objects are outside the allowed set. This list becomes the basis for configuration and the acceptance criteria.

Separate service connections

Different web applications need different database users to separate their permissions. If a fictional product registration service runs alongside the catalog, its need to write must not expand fictional_reader's privileges. With a shared user, the database cannot enforce the separation described between these applications.

The boundary here is the connection's privileges, enforced by the database itself. You therefore need to test the account the service actually uses to connect in the test environment. The secrets for this connection should also receive the minimum permissions needed for their role. Choose the specific commands for creating users and granting permissions from your database system's documentation: this article provides no universal syntax.

Limit access to the required tables or a view

If the service needs an entire table, grant read access to that table according to the contract. When it needs only part of the data, you can grant SELECT on a view without access to the underlying table. Check the rules governing view permissions for your chosen database.

For the view option, include a separate author-proposed check using synthetic data: reading the allowed view succeeds, while direct reads of the underlying table and other excluded objects are rejected. If the database system supports writes through that view, separately verify that the corresponding operations are rejected. A restricted read result does not prove that data changes are blocked.

First confirm the connection and permitted reads

The following plan consists of author-proposed defensive tests, not an official OWASP procedure. Run it only against your own fictional database. Prepare several synthetic products with known identifiers and values in advance, and save the expected read result and the records' initial contents.

Connect using the service's configuration and credentials in this test environment. Use your chosen database system's facilities to confirm the actual connection user and effective permissions, including those inherited through roles. Do not include secrets in the report. Then perform the permitted read: the result must match the expected records and values. A connection error or a missing required row means the positive control has failed. A subsequent write rejection does not yet confirm the contract.

Verify that each operation is blocked and records remain unchanged

Under the same verified runtime user, separately attempt to insert a synthetic product, update an existing one and delete an existing one. Use valid data and test rows that actually exist: a format error or a missing target must not stand in for a permissions rejection. For each attempt, expect the database system itself to reject the operation specifically because of access permissions.

After each attempt, perform an independent verification read and compare the data with the saved initial state. After INSERT, the new row must be absent; after UPDATE, the existing row's values must match their initial values; after DELETE, the row must remain in place unchanged. Each operation requires both pieces of evidence: a permissions rejection and unchanged data.

A successful write followed by a rollback does not count as a pass: the database has already allowed the prohibited operation. An HTTP response alone is also insufficient, regardless of its status. Record the tested object, operation, database rejection and record comparison result in the report.

Correct permissions based on the results

If even one write is allowed, the contract is not met. Check the user's effective privileges, including roles, remove any write permissions and administrative access you find, and replace a shared account with separate accounts for each service. Then repeat the positive read control and the prohibited-operation checks: the correction must preserve required reads and block data changes.

The priorities are specific: first establish a separate connection with minimal permissions, then confirm permitted reads and rejection of all three write operations. For a view, also verify the access boundaries around the underlying objects. The result applies only to the operations, objects and configuration tested; it is not a statistical guarantee of security.

Understand the limits of the result

Minimal permissions limit the potential damage through the database account, but do not by themselves prevent SQL injection. This contract also does not replace separate checks of search parameterization, allowed SQL sorting, user access to API objects, input values, document state transitions or API key replacement. The procedures for those checks are outside this article's scope.

To examine your web product's defenses, you can start with a free website or web-product audit from CheckMyApp. The practical outcome of the work described here is a minimal service permissions contract and reproducible evidence that, on your own test data, required reads work, tested changes are rejected by the database, and records remain unchanged.

Sources and documentation

  1. SQL Injection Prevention Cheat Sheet
  2. Secrets Management Cheat Sheet
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free