How to fix Mass Assignment
in Dart Frog
Executive Summary
Mass Assignment in Dart Frog occurs when an application takes user-provided JSON and maps it directly to a database model or internal state without filtering. In Dart Frog's functional routing style, this usually happens when developers use a generic 'fromJson' constructor on an incoming request body. An attacker can inject unexpected fields—like 'is_admin', 'permissions', or 'balance'—to escalate privileges or manipulate data that should be read-only.
The Vulnerable Pattern
// routes/profile/update.dart FutureonRequest(RequestContext context) async { final body = await context.request.json() as Map ; // VULNERABLE: Blindly updating the user object with whatever the client sent. // If the client sends {“is_premium”: true}, this code will process it. final user = User.fromJson(body); await database.users.update(user);
return Response.json(body: user.toJson()); }
The Secure Implementation
The fix involves decoupling the external API contract from the internal data model. By manually extracting fields or using a dedicated 'UpdateDTO' class, you create a whitelist of allowed attributes. Even if an attacker sends extra keys in the JSON payload, the Dart Frog handler ignores them because they are not explicitly mapped to the 'copyWith' method or the update logic. This ensures that sensitive fields like 'id', 'role', or 'created_at' remain immutable from the perspective of the client.
// routes/profile/update.dart FutureonRequest(RequestContext context) async { final body = await context.request.json() as Map ; final currentUser = context.read (); // SECURE: Explicitly extract only the fields the user is allowed to change. // Use a DTO (Data Transfer Object) or manual mapping to enforce a whitelist. final allowedUpdate = { ‘displayName’: body[‘displayName’] as String?, ‘bio’: body[‘bio’] as String?, };
final updatedUser = currentUser.copyWith( displayName: allowedUpdate[‘displayName’] ?? currentUser.displayName, bio: allowedUpdate[‘bio’] ?? currentUser.bio, );
await database.users.save(updatedUser); return Response.json(body: updatedUser.toJson()); }
Your Dart Frog API
might be exposed to Mass Assignment
74% of Dart Frog apps fail this check. Hackers use automated scanners to find this specific flaw. Check your codebase before they do.
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.