smartyfy.mrindigi.com

Databases & SQL Master Course — 141. Database Trust Boundaries

Lesson 141 Database Trust Boundaries

Prerequisites and objectives

Complete the concurrency practice lab and understand composite keys. You will map the boundaries between a browser, an application service, and a database; separate authentication, authorization, and query safety; and test tenant-scoped reads and writes using fictional support tickets.

Identify who is trusted to decide what

A browser can send a ticket identity, a search string, and a claimed tenant. None of those values becomes authoritative merely because it arrived through the application’s interface. The service should establish the caller’s identity using its authentication system, derive the permitted tenant or account scope, and then execute only an authorized operation through its database access layer.

The database connection identity is another boundary. A service commonly uses a database account shared across many application users. That account’s ability to read a table does not automatically prove that the current caller may read every row. Conversely, a correctly filtered query does not justify giving the service an unrestricted administrative account.

Authentication asks who the caller is. Authorization asks which operations and records that caller may access. Parameter binding separates SQL syntax from values. These controls support one another but answer different questions. A perfectly parameterized query can still leak another tenant’s records when it omits the authorization predicate.

Map the access path and exposed data

For the fictional ticket service, the browser asks for a ticket, the service obtains an authorized tenant from trusted request context, and the query uses both tenant and ticket identity. The response contains only approved fields. Internal notes are deliberately excluded from the public response even though the service process may have access to them for a different responsibility.

Trust-boundary responsibilities
Boundary Trusted responsibility Do not assume
Browser to service Authenticate and authorize the request A supplied tenant is authoritative
Service to database Use scoped queries and bounded privileges One database account identifies every end user
Database to response Project approved fields and audit outcomes Every selected column should reach the browser
CREATE TABLE lab_ticket141(tenant TEXT NOT NULL,ticket_id INTEGER NOT NULL,title TEXT NOT NULL,internal_note TEXT NOT NULL,PRIMARY KEY(tenant,ticket_id));
INSERT INTO lab_ticket141 VALUES('T1',7,'Alpha repair','Internal A'),('T2',7,'Beta repair','Internal B'),('T2',8,'Gamma repair','Internal C');

Worked practice

Exercise 1

Inspect the result of looking up ticket seven without tenant scope.

Worked solution
-- Connection A
SELECT tenant,ticket_id,title FROM lab_ticket141 WHERE ticket_id=7 ORDER BY tenant;

Two tenants have ticket seven, so the unscoped query returns both. A globally unique ticket identity would remove this particular duplicate but would not remove the authorization requirement: knowing an identifier still does not establish permission. The deliberate failure case makes the missing boundary visible.

Exercise 2

Fetch only the ticket permitted by an authenticated T1 context.

Worked solution
-- Connection A
SELECT ticket_id,title FROM lab_ticket141 WHERE tenant='T1' AND ticket_id=7;
-- Connection A
SELECT ticket_id,title FROM lab_ticket141 WHERE tenant='T1' AND ticket_id=8;

The authorized T1 lookup returns Alpha repair, while identity eight returns no row in that scope. The service should not replace T1 with a browser-supplied T2 merely to satisfy the request. In application code, both values are bound through the driver, but only the service-derived tenant expresses the caller’s authority.

Exercise 3

Apply the same boundary to a write rather than protecting reads alone.

Worked solution
-- Connection A
BEGIN;
-- Connection A
UPDATE lab_ticket141 SET title='Changed' WHERE tenant='T1' AND ticket_id=8;
-- Connection A
SELECT title FROM lab_ticket141 WHERE tenant='T2' AND ticket_id=8;
-- Connection A
ROLLBACK;

The attempted T1 write matches zero rows and leaves the T2 ticket unchanged. An application must inspect that outcome instead of reporting success merely because SQL syntax was valid. The verification SELECT uses privileged lab visibility to assert the fixture’s state; it is not the response query that an unauthorized caller should receive.

Exercise 4

Construct a tenant export with an explicit response projection.

Worked solution
-- Connection A
SELECT ticket_id,title FROM lab_ticket141 WHERE tenant='T1' ORDER BY ticket_id;

The export has one row and only two approved columns. SELECT star would couple the response to every future schema addition, including internal notes or new sensitive fields. Apply the same scope to counts, aggregates, downloads, search suggestions, and background jobs; a secure detail page does not compensate for an unscoped export endpoint.

Database architecture changes the enforcement boundary

SQLite runs in the application’s process and accesses database files directly. Its ordinary SQL interface does not provide PostgreSQL-style server roles and GRANT statements. Protection therefore depends heavily on operating-system access, process boundaries, and carefully controlled application operations. A view can organize approved columns, but a process that can freely open the file and query base tables is not confined merely because a view exists.

Client-server databases offer account and privilege mechanisms, and some support row-level security. Those mechanisms require deliberate configuration and testing. Do not assume a database account, a view, or a row policy automatically understands an application’s tenant context. Connection reuse also means request-specific context must not leak into the next request.

Review the complete path, including failure paths

Draw every data route: normal reads, edits, reports, imports, exports, maintenance jobs, and backup access. Assign an owner to each boundary and record the least information needed at that point. A support worker may need a ticket title without needing unrestricted table export or permission to change schema.

Return a deliberate missing-or-forbidden response policy that does not accidentally reveal cross-tenant existence. Log an operation identifier and outcome suitable for investigation, while avoiding full internal notes or credentials in routine logs. Error handling and observability are data-access paths too.

These SQL experiments verify predicate and projection behavior only. They do not implement or test authentication, network security, account provisioning, or an entire web application. A complete security review must exercise those surrounding controls and adversarial requests through the real authorized interface.

Pitfalls and summary

Do not trust a tenant value simply because it is an integer or a UUID. Do not confuse bound parameters with authorization. Do not protect SELECT while leaving UPDATE, exports, and counts unscoped. Map the caller, service identity, database capabilities, row scope, and returned fields as separate decisions, then test both permitted and denied outcomes.

Knowledge check

Question 1

Which source should determine the tenant predicate for this service?

  1. Any tenant value supplied in the browser request
  2. The tenant of the first matching ticket row
  3. The tenant scope authorized from the authenticated request context
  4. The database account name without application authorization
Answer and explanation

C. The service must derive authority from trusted identity and policy, not a caller’s arbitrary claim.

Question 2

Can a parameterized query still disclose another tenant’s data?

  1. No, binding automatically adds tenant predicates
  2. No, prepared statements automatically identify the end user
  3. Only if values are concatenated, because binding also supplies authorization
  4. Yes, if its authorization scope is missing or wrong
Answer and explanation

D. Binding protects query structure, while row authorization remains a separate responsibility.

Question 3

Why use an explicit export projection?

  1. To make every exported row automatically authorized
  2. To return only approved fields and avoid accidental exposure after schema changes
  3. To replace the need for a tenant WHERE clause
  4. To automatically mask sensitive values inside every selected column
Answer and explanation

B. Column selection and row scope are complementary controls.

Question 4

What does the zero-row scoped UPDATE show?

  1. No row matched the authorized write predicate in this fixture
  2. The T2 ticket was successfully changed
  3. The SQL engine rejected the statement as malformed
  4. The caller gained permission by knowing identity eight
Answer and explanation

A. A syntactically successful statement may still perform no authorized change.

Question 5

Why is an SQLite view alone not a boundary against a process with unrestricted file access?

  1. Views automatically delete all base-table privileges in SQLite
  2. SQLite authenticates every view user with a server role
  3. The view encrypts the database file separately
  4. That process can also open and query the underlying database directly
Answer and explanation

D. Embedded access relies on surrounding process and filesystem protections rather than server-style GRANT enforcement.

Execution scope

These scenarios were executed with SQLite 3.53.1 using separate connections to newly created disposable local database files. No existing or external database was used. Each scenario starts from a fresh fixture. Python was an internal verification harness, not a learner prerequisite. Follow the labeled connection order in SQL clients that open the same disposable file.

Primary references