liveBook · Manning

Principle of least privilege

As explained in .NET in Action, Second Edition

Least privilege is an authorization principle that grants an identity only the permissions required for its tasks.

Least privilege limits a database identity to the permissions required by the application—no more. If an attacker exploits SQL injection, restricted credentials can limit damage such as dropping tables, reading schemas, or extracting data. section 9.4.1

Apply the principle alongside query safety:

  • Give each application connection string only the database and operations it needs, such as read-only access to one schema. section 9.4.1
  • Use SQL parameters so attacker-controlled titles remain values rather than executable SQL. section 9.4.1
  • Inspect generated SQL, because EF Core queries can differ by version and query shape. section 9.4.1

Limiting an injected query's blast radius

A title containing `atitle'); DELETE FROM Books; --` can alter an unsafe query. Parameterized queries prevent the input from becoming SQL, while a least-privileged connection can additionally prevent destructive operations if an injection reaches the database. section 9.4.1

Listing 9.6 LogSqlInterceptor logging SQL generated by EF Core
using System.Data.Common;                                  #1
using Microsoft.EntityFrameworkCore.Diagnostics;           #2
using Xunit.Abstractions;                                  #3
  
namespace ManningBooksApi.Tests;
  
public class LogSqlInterceptor : DbCommandInterceptor
{
  private readonly ITestOutputHelper _testOutput;          #4
  
  public LogSqlInterceptor(ITestOutputHelper testOutput)
    => _testOutput = testOutput;
  
  public override
    ValueTask<InterceptionResult<DbDataReader>>            #5
    ReaderExecutingAsync(                                  #6
      DbCommand command,
      CommandEventData eventData,
      InterceptionResult<DbDataReader> result,
      CancellationToken cancelToken)
  {
    _testOutput.WriteLine(command.CommandText.Trim());     #7
    return base.ReaderExecutingAsync(                      #8
      command, eventData, result, cancelToken);
  }
}

Use the SQL interceptor listing to inspect commands produced by EF Core and verify whether application queries use parameters. section 9.4.1

How .NET in Action, Second Edition covers it

How deep it goes
Explains scoped database authorization and its role in limiting SQL-injection damage, without detailing a complete permission configuration.
Examples
Uses malicious book-title input, destructive SQL statements, read-only schema access, and logged EF Core SQL as concrete examples.
What it assumes
Assumes familiarity with application database authentication, connection strings, and basic SQL queries.
What you can do after
Apply it via database connection identities that receive only the permissions required by the application.
How it connects
Connects database authorization with connection strings, SQL injection, SQL parameters, and EF Core query generation.

Also explained in

Compare how 17 books explain principle of least privilege →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.