HIGH missing authenticationaspnetcockroachdb

Missing Authentication in Aspnet with Cockroachdb

Missing Authentication in Aspnet with Cockroachdb — how this specific combination creates or exposes the vulnerability

Missing authentication in an ASP.NET API that connects to CockroachDB exposes endpoints that allow unauthenticated actors to interact directly with your database layer. Because the API does not enforce identity checks before building and executing SQL, an attacker can send crafted HTTP requests that trigger queries over CockroachDB without any user context or tenant isolation.

In this scenario, the ASP.NET application typically uses a connection string (or environment-derived configuration) to open a session to CockroachDB. If authorization is absent or misconfigured, endpoints that query or mutate data will execute with the privileges defined by that shared connection string. CockroachDB’s native support for role-based access control and tenant-level security means missing application-side authentication can lead to unauthorized schema or data access across tenants, especially in multi-tenant deployments.

Consider an endpoint designed to retrieve a user profile by ID. Without authentication, an attacker can iterate over numeric or UUID identifiers and issue SQL statements such as SELECT * FROM profiles WHERE id = $1 through the API. Because the service connection to CockroachDB often runs with broad permissions for simplicity, these queries succeed and return private information. The risk is amplified if the application uses shared database users rather than per-request authentication-bound roles, making it difficult to attribute actions or enforce least privilege.

Another common pattern is the use of unauthenticated health or debug endpoints that open a CockroachDB session to report connectivity or version details. These endpoints may inadvertently disclose cluster metadata or expose query execution paths that assist in reconnaissance. Because CockroachDB returns structured results for statements like SHOW DATABASES or SHOW TABLES, an unauthenticated caller can map the schema even when the API itself should be locked down.

In secure designs, authentication should gate every path that reaches CockroachDB. This includes validating tokens or session evidence in ASP.NET before constructing commands, ensuring each request uses a principle that maps to a CockroachDB user or role with narrowly scoped permissions. Without this control, the API becomes a conduit that bypasses identity checks and exposes the database directly to the network.

Cockroachdb-Specific Remediation in Aspnet — concrete code fixes

To remediate missing authentication, enforce identity validation in ASP.NET before any CockroachDB interaction. Use ASP.NET Core’s authentication and authorization middleware to ensure only verified principals reach data access code, and scope database permissions to the minimum required per role or tenant.

1) Enforce authentication and use parameterized queries

Ensure every controller or endpoint requiring identity checks is decorated with [Authorize]. Use parameterized queries to avoid injection and explicitly bind tenant or user context to the SQL sent to CockroachDB.

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Npgsql;

[ApiController]
[Route("api/[controller]")]
[Authorize]
public class ProfilesController : ControllerBase
{
    private readonly string _connectionString;

    public ProfilesController(IConfiguration config)
    {
        _connectionString = config.GetConnectionString("CockroachDb");
    }

    [HttpGet("{id:guid}")]
    public async Task<IActionResult> GetProfileAsync(Guid id)
    {
        await using var conn = new NpgsqlConnection(_connectionString);
        await conn.OpenAsync();
        await using var cmd = new NpgsqlCommand("SELECT id, display_name, email FROM profiles WHERE id = @id", conn);
        cmd.Parameters.AddWithValue("id", id);
        await using var reader = await cmd.ExecuteReaderAsync();
        if (await reader.ReadAsync())
        {
            var profile = new
            {
                Id = reader.GetGuid(0),
                DisplayName = reader.GetString(1),
                Email = reader.GetString(2)
            };
            return Ok(profile);
        }
        return NotFound();
    }
}

2) Use role-based connections and tenant scoping

Open connections with a role that matches the authenticated principal’s role, and include tenant identifiers in WHERE clauses to enforce isolation. CockroachDB supports SET ROLE and session variables that ASP.NET can use to align application identity with database privileges.

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Npgsql;

[ApiController]
[Route("api/tenant-data")]
[Authorize]
public class TenantDataController : ControllerBase
{
    private readonly string _baseConnectionString;

    public TenantDataController(IConfiguration config)
    {
        _baseConnectionString = config.GetConnectionString("CockroachDb");
    }

    [HttpGet("items")]
    public async Task<IActionResult> GetTenantItemsAsync()
    {
        var tenantId = User.FindFirst("tenant_id")?.Value;
        if (string.IsNullOrEmpty(tenantId))
        {
            return Forbid();
        }

        // Derive a per-tenant connection string or set session variables
        var csb = new NpgsqlConnectionStringBuilder(_baseConnectionString)
        {
            ApplicationName = $"tenant-{tenantId}"
        };
        await using var conn = new NpgsqlConnection(csb.ConnectionString);
        await conn.OpenAsync();
        await using var setRole = new NpgsqlCommand("SET ROLE tenant_reader", conn);
        await setRole.ExecuteNonQueryAsync();

        await using var cmd = new NpgsqlCommand(
            "SELECT id, name, value FROM tenant_items WHERE tenant_id = @tenantId", conn);
        cmd.Parameters.AddWithValue("tenantId", tenantId);
        await using var reader = await cmd.ExecuteReaderAsync();
        var items = new List<object>();
        while (await reader.ReadAsync())
        {
            items.Add(new { Id = reader.GetGuid(0), Name = reader.GetString(1), Value = reader.GetString(2) });
        }
        return Ok(items);
    }
}

3) Avoid shared service accounts; bind users to roles

Instead of a single service account, map authenticated users or roles to dedicated CockroachDB roles with scoped permissions. In ASP.NET, integrate policy-based authorization to enforce that a user can only query data tied to their identity or tenant.

services.AddAuthorization(options =>
{
    options.AddPolicy("TenantScope", policy =>
        policy.RequireAssertion(context =>
        {
            var tenantClaim = context.User.FindFirst("tenant_id");
            var dbRole = context.Resource as string?;
            // Ensure the user’s tenant matches the requested database role
            return !string.IsNullOrEmpty(tenantClaim?.Value) && dbRole == $"tenant_{tenantClaim.Value}_reader";
        }));
});

// In a controller:
[Authorize(Policy = "TenantScope")]
public async Task<IActionResult> SecureData()
{
    // Connection and queries proceed with tenant-bound role
}

Related CWEs: authentication

CWE IDNameSeverity
CWE-287Improper Authentication CRITICAL
CWE-306Missing Authentication for Critical Function CRITICAL
CWE-307Brute Force HIGH
CWE-308Single-Factor Authentication MEDIUM
CWE-309Use of Password System for Primary Authentication MEDIUM
CWE-347Improper Verification of Cryptographic Signature HIGH
CWE-384Session Fixation HIGH
CWE-521Weak Password Requirements MEDIUM
CWE-613Insufficient Session Expiration MEDIUM
CWE-640Weak Password Recovery HIGH

Frequently Asked Questions

How does middleBrick detect missing authentication in API endpoints?
middleBrick runs unauthenticated black-box scans and checks whether endpoints that should require credentials accept requests without authentication. It correlates findings with the API definition to highlight missing authentication controls and maps them to frameworks such as OWASP API Top 10.
Can middleBrick fix authentication issues in my ASP.NET API with CockroachDB?
middleBrick detects and reports missing authentication with remediation guidance. It does not fix, patch, block, or remediate issues. You should implement authentication and apply database-level role scoping as shown in the code examples.