Description
Copying an entire client-supplied JSON object into a user record with Object.assign can overwrite properties managed by the server.
Potential impact
Changes to account privileges or internal settings may expose sensitive data or change service behavior.
Remediation
List the editable properties on the server and exclude permission fields. Before saving, validate each allowed value’s type and any rules the application enforces.
Examples
Before
The database lookup and save helpers are omitted.
javascript
// Before
function updateUser(untrustedInput) {
let user = getUserFromDB();
// Merge unvalidated input
Object.assign(user, JSON.parse(untrustedInput));
saveUserToDB(user);
}
After
javascript
// After
function updateUser(untrustedInput) {
let user = getUserFromDB();
const allowed = ['nickname', 'email'];
const data = JSON.parse(untrustedInput);
// Merge only allowed fields
for(const key of allowed) {
if(data[key] !== undefined) {
user[key] = data[key];
}
}
saveUserToDB(user);
}
The loop copies only nickname and email when their values are not undefined. The example leaves value validation to the application.