HIGH integrity failuresecho go

Integrity Failures in Echo Go

How Integrity Failures Manifests in Echo Go

Integrity failures in Echo Go applications occur when the server fails to properly validate that a user has permission to modify or access specific resources. In Echo Go, these failures often manifest through improper authorization checks in route handlers, particularly when dealing with user-specific data like profiles, orders, or content.

The most common pattern involves Echo handlers that retrieve a resource ID from the URL or request body, query the database, and return results without verifying the authenticated user owns that resource. For example:

func getUserProfile(c echo.Context) error {
    id := c.Param("id")
    user := database.GetUser(id) // No ownership check
    return c.JSON(http.StatusOK, user)
}

This handler allows any authenticated user to view any profile by simply changing the ID parameter. The integrity failure occurs because the application trusts the client-provided identifier without validating the relationship between the authenticated user and the requested resource.

Another Echo Go-specific manifestation involves middleware ordering. If authentication middleware runs after authorization checks, the application might process requests without knowing who the user is:

func getUserOrders(c echo.Context) error {
    id := c.Param("user_id")
    orders := database.GetOrdersByUserID(id) // Auth not yet verified
    return c.JSON(http.StatusOK, orders)
}

// Middleware applied AFTER handler registration
e := echo.New()
e.GET("/users/:user_id/orders", getUserOrders)
e.Use(middleware.JWT())

This ordering bug allows unauthenticated users to enumerate orders by user ID, creating a significant data exposure vulnerability.

Echo Go's parameter binding can also introduce integrity failures when struct tags aren't properly configured. Consider this handler:

type UpdateProfileRequest struct {
    UserID string `json:"user_id" validate:"required"`
    Bio     string `json:"bio"`
}

func updateProfile(c echo.Context) error {
    var req UpdateProfileRequest
    if err := c.Bind(&req); err != nil {
        return err
    }
    
    // Critical integrity failure: trusts client-provided UserID
    database.UpdateUserProfile(req.UserID, req.Bio)
    return c.NoContent(http.StatusOK)
}

Here, a malicious user could modify user_id in the request body to update any user's profile, not just their own. The integrity violation stems from the application accepting client-provided identifiers for resource ownership.

Echo Go-Specific Detection

Detecting integrity failures in Echo Go requires both manual code review and automated scanning. For manual detection, look for these Echo-specific patterns:

Route Parameter Handling: Search for handlers that use c.Param(), c.QueryParam(), or c.Bind() to retrieve identifiers, then query databases without ownership validation. Pay special attention to Echo's path parameter binding where IDs appear directly in URLs.

Middleware Ordering: Verify authentication middleware is registered before any handlers that access protected resources. Echo processes middleware in registration order, so this is a common source of integrity failures:

e := echo.New()
// WRONG: Auth middleware after handler
e.GET("/api/users/:id", getUser)
e.Use(middleware.JWT())

// CORRECT: Auth middleware before handler
e.Use(middleware.JWT())
e.GET("/api/users/:id", getUser)

Context Value Usage: Echo stores authenticated user information in the context. Integrity failures often occur when handlers ignore this and use client-provided values instead:

func currentUserID(c echo.Context) string {
    return c.Get("user_id").(string) // Echo's standard pattern
}

// Integrity failure: uses param instead of context
func getProfile(c echo.Context) error {
    id := c.Param("id")
    // Should use: id := currentUserID(c)
    return c.JSON(http.StatusOK, database.GetUser(id))
}

Automated Scanning with middleBrick: middleBrick's Echo Go scanner specifically tests for integrity failures by attempting authenticated requests with manipulated identifiers. The scanner tries authenticated requests with:

  • Modified ID parameters (incrementing numeric IDs, changing UUIDs)
  • Swapped user identifiers in request bodies
  • Cross-user resource access attempts

The scanner reports when it successfully accesses resources owned by other users, providing concrete evidence of integrity failures. For Echo Go applications, middleBrick also checks middleware ordering by analyzing the application structure and identifying potential authentication bypass scenarios.

Echo Go-Specific Remediation

Remediating integrity failures in Echo Go requires implementing proper authorization checks throughout your application. The most effective approach uses Echo's middleware system and context values to centralize ownership validation.

Centralized Authorization Middleware: Create a middleware that validates resource ownership before processing requests:

func authorizeResourceOwner(resourceType string, idParam string) echo.MiddlewareFunc {
    return func(next echo.HandlerFunc) echo.HandlerFunc {
        return func(c echo.Context) error {
            // Get authenticated user from context
            userID := c.Get("user_id").(string)
            
            // Get resource ID from URL parameter
            resourceID := c.Param(idParam)
            
            // Validate ownership based on resource type
            if !database.ValidateOwnership(resourceType, resourceID, userID) {
                return echo.NewHTTPError(http.StatusForbidden, "Access denied")
            }
            
            return next(c)
        }
    }
}

// Usage in route definitions
e := echo.New()
e.Use(middleware.JWT())

// Apply authorization middleware to specific routes
e.GET("/api/users/:user_id/profile", getProfile,
    authorizeResourceOwner("user", "user_id"))

e.PUT("/api/users/:user_id/profile", updateProfile,
    authorizeResourceOwner("user", "user_id"))

Echo Context Extension: Extend Echo's context to include authorization helpers:

type authContext struct {
    echo.Context
}

func (a *authContext) currentUserID() string {
    return a.Get("user_id").(string)
}

func (a *authContext) requireOwnership(resourceID string) error {
    if a.currentUserID() != resourceID {
        return echo.NewHTTPError(http.StatusForbidden, "Access denied")
    }
    return nil
}

// Custom context middleware
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
    return func(c echo.Context) error {
        return next(&authContext{c})
    }
})

Handler Pattern for Integrity: Implement handlers that always use context-based user identification:

func getOwnProfile(c echo.Context) error {
    ctx := c.(*authContext)
    userID := ctx.currentUserID()
    
    // Only access resources for the authenticated user
    profile := database.GetUser(userID)
    return c.JSON(http.StatusOK, profile)
}

func updateOwnProfile(c echo.Context) error {
    ctx := c.(*authContext)
    userID := ctx.currentUserID()
    
    var req UpdateProfileRequest
    if err := c.Bind(&req); err != nil {
        return err
    }
    
    // Ignore any user_id in request body - use authenticated user
    if err := ctx.requireOwnership(userID); err != nil {
        return err
    }
    
    database.UpdateUserProfile(userID, req.Bio)
    return c.NoContent(http.StatusOK)
}

Echo-Specific Validation: Use Echo's validation middleware to reject requests with suspicious parameters:

type ProfileRequest struct {
    UserID string `json:"user_id" validate:"omitempty,uuid"
    Bio     string `json:"bio"`
}

// Custom validator to reject user_id in update requests
e.Validator = &customValidator{}

type customValidator struct{}

func (cv *customValidator) Validate(i interface{}) error {
    v := validator.New()
    if err := v.Struct(i); err != nil {
        return err
    }
    
    // Additional check: reject user_id in update requests
    if req, ok := i.(*ProfileRequest); ok {
        if req.UserID != "" {
            return errors.New("user_id must be empty in update requests")
        }
    }
    return nil
}

Frequently Asked Questions

How can I test my Echo Go application for integrity failures?

Use middleBrick's automated scanner to test your Echo Go API endpoints. The scanner attempts authenticated requests with manipulated identifiers to detect when users can access resources they don't own. Additionally, manually test by attempting to access /api/users/1/profile when authenticated as user 2, or modifying request bodies to change user IDs. Echo Go's Echo framework provides helpful debugging tools - enable Echo's logger to track parameter values and ensure they match authenticated user context.

What's the difference between authentication and authorization in Echo Go?

Authentication verifies who the user is (via JWT, sessions, etc.), while authorization determines what they can access. In Echo Go, authentication typically happens in middleware that sets user information in the context. Authorization is the subsequent check that validates whether the authenticated user has permission to access a specific resource. Integrity failures occur when applications skip authorization checks and trust client-provided identifiers, allowing authenticated users to access any resource by manipulating IDs in URLs or request bodies.