views:

108

answers:

5

I'm doing a security audit on a fairly large php application and was wondering where I should include my user-input validation.

Should I validate the data, then send the clean data off to the back-end functions or should I rely on each function to do it's own validation? Or even both?

Is there any standard or best-practice for this sort of thing?

Currently the app does both inconsistently and I'll like to make things more consistent.

+2  A: 

I think if you can do both, and time / resources are not an issue, why not?

Raven Dreamer
+6  A: 

You should definitely validate the data from the outside as soon as possible. Depending on the architecture, backend validation inside the responsible functions can be a second step, but don't depend on backend validation but validate the data when it comes in to your application.

The pros with validation inside functions as a complement to the previous validation is that it's easier (and safer) to maintain the system because (sloppier) developers after you can't break the application. If you have an application with plugin support, e.g. for third party plugins, safe functions is a must also.

Emil Vikström
One problem with this is that when the application when it comes to your application is that you might not know how the variable is being used. For this reason its generally best to sanitize immediately before use, this also makes it **a lot easier** for security based peer review.
Rook
+2  A: 

It depends on the scope/definition of the application. But traditionally, your functions are used in may places $object->doSomething() does just that. By relying on validation in there, you prevent the ability to doSomething() of your OWN acccord, ya know?

Too, if you keep validation outside you can easily manage it. No need to hunt it down in that particular internal function. Keep it OOP, but more like

$data = $validator->sanitizeSomething($data); $object->doSomething($data);

this keeps your validation rules separate and easy to manaage as well as your internal functions.

To elaborate, say you have a db object that adds an array to the table:

class db {
   function addRow($table, $associativeArray) {
      // primitive i know, just an example 
   }
}

would you want your validation in there?

function addRow($table, $associativeArray) {
    if( isset( $assiciativeArray['description'] ) {
       // validate
    }
}

would be silly - you'd want that in the object you're working in

class product {
   function update() {
       if( $this->validate() ) {
          $this->db->addRow($this->toArray()); // or something, you get the idea, ya?
       }
   }
   function validate() {
      if( $this->description != "") {
         return true;
      }
      return false;
   }
}
Dan Heberden
+7  A: 

Both is the better answer. Data validation should happen in every function that will be handling the data to avoid the problem of Hope Driven Development (HDD)

MANCHUCK
I have strong sympathy with this point of view! It's really The Right Way To Do It. This is where PHP becomes a headache to work with, because the weak typing forces you to create your own checks just to make sure all function arguments are the right type =(
Emil Vikström
But there has to be a balance. If i make a class with a function that takes _only an array. That class if for a particular object, that function a particular task. So if i send it a string and it's expecting an array, that's not HDD - that's me being dumb. It gets into the bigger picture: is the application well documented? are there strong publicly available methods that gracefully encapsulate the functionality?
Dan Heberden
+2  A: 

Validating at the backend is like screening passengers after they have boarded the plane. The whole point of validation is to prevent injecting elements that might choke up your app. So you must validate before you enter the gate :)

Babiker
Following that analogy if you don't trust the people screening at the gate to always do their jobs properly every single time it's safer to do a double-check before the plane takes off :p But definitely should catch bad input earlier rather than later, but to be doubly sure check both.
Davy8