Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a MVC-model, whose responsibility is it to sanitize input?

A simple question: I have a Model-View-Controller setup, with Models accessing a SQL database. In which part should I sanitize/check for malformed incoming data?

like image 600
Esa Avatar asked Dec 15 '08 11:12

Esa


People also ask

What does sanitize input related to?

Input sanitization is a cybersecurity measure of checking, cleaning, and filtering data inputs from users, APIs, and web services of any unwanted characters and strings to prevent the injection of harmful codes into the system.

Which can be considered form data sanitization?

Sanitizing data can be done by removing or replacing contextually-dangerous characters, such as by using a whitelist or escaping the input data. While it may not be intuitive, even data that a user submits to their own area on a site should be validated.

Should the controller interact with the database?

The controller should not need to know anything about the underlying structure of the model in order to interact with it. The model should have no knowledge of how the data is to be displayed (e.g., formatting) or the workflow.


1 Answers

It's important to keep error handling as low as possible in the stack, but supplemental in other parts. If you keep the sanitizing in the controller, you could break the model by swapping out the controller with a looser one, but you can never break the model by being strict higher up in the stack. Keep the sanitizing low in the stack for consistency, and high in the stack for user feedback.

like image 69
Egil Avatar answered Nov 16 '22 00:11

Egil