GuardAPI Logo
GuardAPI

Fix NoSQL Injection in Revel

NoSQL injection in the Revel framework typically manifests when developers blindly trust user-supplied parameters to construct MongoDB queries. In Go, using 'bson.M' with unvalidated input allows attackers to inject operators like '$ne', '$gt', or '$where', leading to authentication bypass or full data exfiltration. As a researcher, I see this most often when 'c.Params' is mapped directly into a query object without schema enforcement.

The Vulnerable Pattern

func (c App) Login() revel.Result {
    // DANGER: Taking raw input and dropping it into a bson.M map
    user := c.Params.Get("username")
    pass := c.Params.Get("password")
query := bson.M{
    "username": user,
    "password": pass,
}

var result User
err := collection.Find(query).One(&result)
// If attacker sends username[$ne]=1, the query becomes {username: {$ne: 1}}
// This bypasses authentication entirely.
return c.RenderJSON(result)

}

The Secure Implementation

The vulnerability stems from the MongoDB driver's ability to parse nested maps as query operators. To remediate, you must: 1. Explicitly cast inputs to primitive types (e.g., string) to prevent them from being interpreted as objects. 2. Use 'bson.D' (ordered document) or 'bson.E' instead of 'bson.M' to maintain strict control over the query structure. 3. Implement a Data Transfer Object (DTO) struct for incoming requests and validate it using Revel's validation suite before passing it to the database layer.

func (c App) Login() revel.Result {
    username := c.Params.Get("username")
    password := c.Params.Get("password")
// SECURE: Enforce strict types and use a structured filter
// This ensures the driver treats the input as a literal string value
filter := bson.D{
    {Key: "username", Value: string(username)},
    {Key: "password", Value: string(password)},
}

var result User
err := collection.Find(filter).One(&result)
if err != nil {
    return c.RenderError(err)
}
return c.RenderJSON(result)

}

System Alert • ID: 9170
Target: Revel API
Potential Vulnerability

Your Revel API might be exposed to NoSQL Injection

74% of Revel apps fail this check. Hackers use automated scanners to find this specific flaw. Check your codebase before they do.

RUN FREE SECURITY DIAGNOSTIC
GuardLabs Engine: ONLINE

Free Tier • No Credit Card • Instant Report

Verified by Ghost Labs Security Team

This content is continuously validated by our automated security engine and reviewed by our research team. Ghost Labs analyzes over 500+ vulnerability patterns across 40+ frameworks to provide up-to-date remediation strategies.