Skip to main content

Data uniqueness not properly verified

Need

Ensure proper validation of data uniqueness

Context

  • Usage of Go 1.16 for building efficient and scalable applications
  • Usage of gin-gonic/gin for building web applications in Go

Description

Non compliant code

package main

import (
"github.com/gin-gonic/gin"
)

type User struct {
ID string `json:"id"`
Name string `json:"name"`
}

var users []User

func main() {
r := gin.Default()
r.POST("/user", CreateUser)
r.Run()
}

func CreateUser(c *gin.Context) {
var newUser User
if err := c.ShouldBindJSON(&newUser); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

users = append(users, newUser)

c.JSON(200, newUser)
}

The vulnerable code above is a simple Go application using the Gin framework. It has a slice of User objects and a CreateUser endpoint that accepts JSON data to create a new user.

The vulnerability lies in the CreateUser function. The function accepts a JSON payload, binds it to a User struct and appends it to the users slice without checking if a user with the same ID already exists. This allows an attacker to create multiple users with the same ID, thus not ensuring the uniqueness of the data.

In a real-world scenario, this could lead to serious issues, such as data inconsistency, overwrite of user data, and potential security breaches. For instance, if the application uses the user ID to manage permissions or access controls, an attacker could create a new user with the same ID as an existing user to gain the same permissions or access.

Steps

  • Implement a validation mechanism to check the uniqueness of the data before adding it to the users list.
  • Use a unique identifier, such as the user ID, to ensure that each user is unique.
  • Before adding a new user, iterate through the existing users list and check if the new user's identifier already exists.
  • If the identifier already exists, return an error response indicating that the data is not unique.
  • If the identifier is unique, proceed with adding the new user to the users list.

Compliant code

package main

import (
"github.com/gin-gonic/gin"
)

type User struct {
ID string `json:"id"`
Name string `json:"name"`
}

var users []User

func main() {
r := gin.Default()
r.POST("/user", CreateUser)
r.Run()
}

func CreateUser(c *gin.Context) {
var newUser User
if err := c.ShouldBindJSON(&newUser); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

// Check for uniqueness of the user ID
for _, user := range users {
if user.ID == newUser.ID {
c.JSON(400, gin.H{"error": "User ID already exists"})
return
}
}

users = append(users, newUser)

c.JSON(200, newUser)
}

The updated code now includes a validation mechanism to ensure the uniqueness of the user ID before adding a new user to the users list.

In the CreateUser function, after binding the incoming JSON to the newUser struct, we iterate through the existing users list. For each user in the list, we compare the existing user's ID with the new user's ID. If we find a match, it means the new user's ID is not unique. In this case, we return a 400 status code with an error message indicating that the user ID already exists.

If we iterate through the entire users list without finding a matching ID, it means the new user's ID is unique. We then proceed to append the new user to the users list and return a 200 status code along with the new user's data.

This validation mechanism ensures that each user in the users list has a unique ID, thereby mitigating the risk of an attacker reusing or regenerating information.

References