I’m trying to upload a file using YUI Uploader (javascript + flash) and everthing seems to work fine until I enable CSRF prevention (‘enableCsrfValidation’=>true).
From my uploader script using POST request I send current user session ID and CSRF token like this:
When trying to upload a file I get Error 400 (The CSRF token could not be verified). If I disable CSRF validation, then everything works fine. I’ve seen some similar issues and suggestions on this forum, but none of them solved my problem.
Ok, I’ve made some more research on validateCsrfToken function of CHttpRequest class (/yii-1.1.3/framework/web/CHttpRequeset.php). Here is the function:
public function validateCsrfToken($event)
{
if($this->getIsPostRequest())
{
// only validate POST requests
$cookies=$this->getCookies();
if($cookies->contains($this->csrfTokenName) && isset($_POST[$this->csrfTokenName]))
{
$tokenFromCookie=$cookies->itemAt($this->csrfTokenName)->value;
$tokenFromPost=$_POST[$this->csrfTokenName];
$valid=$tokenFromCookie===$tokenFromPost;
}
else
$valid=false;
if(!$valid)
throw new CHttpException(400,Yii::t('yii','The CSRF token could not be verified.'));
}
}
When I try to upload a file print_r($this->getCookies()) in the function itself returns nothing: