HIGH integrity failureschibasic auth

Integrity Failures in Chi with Basic Auth

Integrity Failures in Chi with Basic Auth — how this specific combination creates or exposes the vulnerability

Chi is a lightweight HTTP framework for the Crystal programming language. When Basic Auth is used without additional integrity controls, the protocol itself carries credentials in an easily decoded form on every request. An attacker who can observe or modify traffic—such as through a compromised network, a malicious proxy, or an unencrypted redirect—can strip or alter the Authorization header. Because Chi routes typically map directly to controller actions, this can lead to Integrity Failures where an attacker changes the intended recipient of a request or injects parameters that the server processes with higher privileges.

In Chi, routes are often defined as a chain of nested pipelines. If a pipeline applies Basic Auth only at an outer level and then delegates to inner routes that perform sensitive state changes (for example, updating a user’s email or toggling an administrative flag), the lack of per-action integrity checks means tampered requests may bypass intended authorization logic. For instance, an attacker who can modify the request path or headers may exploit route precedence or pattern-matching ambiguities to reach an endpoint that was assumed to be protected by the outer pipeline. The scanner’s BOLA/IDOR and BFLA/Privilege Escalation checks would flag this as a risk because the combination of weak transport integrity and broad privilege assignment allows horizontal or vertical escalation.

Basic Auth also increases exposure because the credentials are static across requests. If an attacker can inject a query parameter or header that influences routing or business logic, they might leverage the authenticated context to perform actions on behalf of the user without direct credential reuse. This aligns with the OWASP API Top 10 category 1: Broken Object Level Authorization, where object references and permissions are not consistently validated per request. The scanner’s Property Authorization and Input Validation checks look for missing guarantees that each handler enforces its own integrity rules, rather than relying on pipeline-level assumptions.

When an API specification is provided, middleBrick cross-references the OpenAPI/Swagger definition with runtime findings. If the spec describes securitySchemes using Basic Auth but does not enforce strict path or parameter integrity, the report will highlight mismatches between declared protections and actual endpoint behavior. For example, a PUT to /users/{id} that does not validate that the authenticated user matches {id} is a classic Integrity Failure. The scanner’s Inventory Management and Data Exposure checks will further flag whether sensitive data is transmitted without encryption, compounding the integrity risk.

Basic Auth-Specific Remediation in Chi — concrete code fixes

Remediation focuses on ensuring each request validates both identity and integrity, rather than relying on outer pipeline guards. In Chi, use per-action checks and avoid assuming that a prior pipeline step has sufficiently constrained the request. Always pair Basic Auth with explicit verification that the authenticated principal is allowed to operate on the target resource.

use HTTP::BasicAuth;
use Chi::Router;

# Define a secure authentication helper
proc verify-user($user, $password --> Bool) {
    # Use constant-time comparison and a secure store
    my $stored = fetch-user-hash($user);
    return False unless $stored;
    return $stored eq crypt($password, $stored);
}

# Apply authentication at the route level with per-action checks
my $router = Chi::Router.new;

$router.get('/users/{id}', :auth-Basic, -> $req, $id {
    my $user = $req.user;
    # Integrity check: ensure the authenticated user matches the resource
    return 403 unless $user.id == $id;
    # Proceed only if integrity passes
    return { status => 'ok', user => $user };
});

$router.put('/users/{id}', :auth-Basic, -> $req, $id {
    my $user = $req.user;
    # Integrity and authorization check
    return 403 unless $user.id == $id;
    # Validate input strictly
    my $email = $req.params // '';
    return 400 unless $email ~~ /^ <[ \w @.- ]+ > $/;
    # Perform the update
    update-user-email($id, $email);
    return { status => 'updated' };
});

# Optional: enforce HTTPS to protect Basic Auth in transit
$router = Chi::Router.new;
$router.before: -> $req {
    return 400 unless $req.secure; # require TLS
    return 401 unless verify-user($req.user.name, $req.headers.substr(6));
}

For applications using the middleBrick CLI, you can scan from terminal with middlebrick scan <url> to validate that your endpoints enforce these checks. If you integrate the GitHub Action, add API security checks to your CI/CD pipeline and fail builds if risk scores drop below your chosen threshold. Teams using the Pro plan gain continuous monitoring, so Chi-based APIs are scanned on a configurable schedule with alerts for integrity regressions.

Additionally, prefer transport-level integrity mechanisms such as TLS and avoid embedding secrets in URLs. Combine per-request ownership checks with schema validation to ensure that parameters like {id} cannot be abused to escalate privileges or bypass intended scoping. The scanner’s LLM/AI Security checks will also flag endpoints that expose authentication logic or leak system prompts, further reducing the attack surface.

Frequently Asked Questions

Why does Basic Auth in Chi increase the risk of Integrity Failures even when credentials are correct?
Basic Auth transmits credentials on every request but does not guarantee request integrity. An attacker who can modify headers or paths may redirect an authenticated request to a different resource or parameter, and if Chi routes do not enforce per-action ownership checks, the authenticated context can be abused to perform unauthorized operations.
How does middleBrick help detect Integrity Failures when Basic Auth is used in Chi?
middleBrick runs 12 security checks in parallel, including BOLA/IDOR, Property Authorization, and Input Validation. It cross-references OpenAPI/Swagger specs with runtime behavior to identify mismatches where Basic Auth is present but per-request integrity controls are missing, and it reports findings with severity and remediation guidance.