← PHP / Laravel · Contents

PHP-057 / Intermediate

Exposing entire models in API responses, including sensitive attributes

A01:2021 Broken Access Control · CWE-200 Exposure of Sensitive Information

Why is this a problem?

The JSON includes password, remember_token and internal flags.

How to fix it

php
class User extends Model { protected $hidden = ['password', 'remember_token']; }
return UserResource::collection(User::all());  // Specify fields using API Resource

Further reading