Skip to main content

mass-assignment

Mass Assignment

When developers first started building web applications backed by databases, they often had to write raw SQL queries manually. This approach was repetitive, error-prone, and hard to maintain. To solve this, modern frameworks introduced Object-Relational Mapping (ORM) systems, which allow developers to interact with database records as objects instead of writing SQL directly.

Mass Assignment is a security vulnerability that occurs when an application automatically binds user input to object properties without proper filtering or restrictions. This can lead to unauthorized modification of sensitive fields, privilege escalation, and data breaches.

Common Technologies Affected

Mass Assignment is particularly prevalent in web frameworks that provide automatic data binding. Some common frameworks where this vulnerability can occur include:

  • Ruby on Rails (Active Record)
  • Node.js (Express + Mongoose)
  • Laravel (PHP)
  • Django (Python)
  • Spring Boot (Java)
  • ASP.NET Core (C# Model Binding)

Example of Mass Assignment Vulnerability

Vulnerable Code (Node.js + Mongoose)

// User schema in Mongoose
const UserSchema = new mongoose.Schema({
username: String,
email: String,
role: { type: String, default: "user" } // Shouldn't be modifiable by users
});
const User = mongoose.model("User", UserSchema);

// Insecure Route: Allows unrestricted updates
app.post("/update", async (req, res) => {
await User.updateOne({ _id: req.user.id }, req.body);
res.send("Updated successfully");
});

Issue: If an attacker submits { "role": "admin" }, they can escalate privileges.

Secure Code (Whitelist Approach)

app.post("/update", async (req, res) => {
const safeFields = { username: req.body.username, email: req.body.email }; // Only allow these fields
await User.updateOne({ _id: req.user.id }, safeFields);
res.send("Updated successfully");
});