Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad Request (#400) - Unable to verify your data submission #4497

Closed
mbman opened this issue Jul 28, 2014 · 49 comments
Closed

Bad Request (#400) - Unable to verify your data submission #4497

mbman opened this issue Jul 28, 2014 · 49 comments
Assignees
Milestone

Comments

@mbman
Copy link
Contributor

mbman commented Jul 28, 2014

Updated my advanced app to latest Yii using Composer some 12 hours ago (previous update was maybe a day earlier), and since then all forms stopped working.
It seems Yii can't validate the csrf cookie it generates.

Yii 2.0.0-dev
PHP 5.4.4-14+deb7u11

Log: http://pastebin.com/i9Ta8W5y
Request: http://pastebin.com/5wpALxCy
Headers: http://pastebin.com/KMKbvr3V

@samdark samdark added this to the 2.0 RC milestone Jul 28, 2014
@dynasource
Copy link
Member

I remember this error too after an composer update. It also went away as fast as it came. Could it be a cookie or browser cache reset

@cebe
Copy link
Member

cebe commented Jul 28, 2014

Have you done this?

https://github.com/yiisoft/yii2/blob/master/framework/UPGRADE.md
Please update your main layout file by adding this line in the section: . This change is needed because yii\web\View no longer automatically generates CSRF meta tags due to issue #3358.

@mbman
Copy link
Contributor Author

mbman commented Jul 28, 2014

Yes, I had to do it on a previous update and it worked until now. I've even cloned a fresh copy of the latest advanced app and the same thing happens.

@cebe
Copy link
Member

cebe commented Jul 28, 2014

looks like you have to clear your cookies from the browser. your data looks like the cookie CSRF tag has been generated by an old version of Secuitry class.

@cebe cebe closed this as completed Jul 28, 2014
@cebe cebe removed this from the 2.0 RC milestone Jul 28, 2014
@mbman
Copy link
Contributor Author

mbman commented Jul 28, 2014

Deleted the cookies, tried incognito window, other browser. Still the same.

@cebe
Copy link
Member

cebe commented Jul 28, 2014

okay, this is weird. Have you tried with the basic app contact form? Or which form did you use to reproduce it?

@cebe cebe reopened this Jul 28, 2014
@mbman
Copy link
Contributor Author

mbman commented Jul 28, 2014

Frontend signup, login, backend login and contact - neither works.

@mbman
Copy link
Contributor Author

mbman commented Jul 28, 2014

Shouldn't it fail automated testing for issues like this?

@mbman
Copy link
Contributor Author

mbman commented Jul 28, 2014

It seems $this->getCookies()->getValue($this->csrfParam) in request is returning null, as if the cookies aren't passing yii's validation. They are present in raw $_COOKIE.

@cebe
Copy link
Member

cebe commented Jul 28, 2014

I am unable to reproduce it with neither basic nor advanced application here...

@samdark
Copy link
Member

samdark commented Jul 28, 2014

@cebe cebe added this to the 2.0 RC milestone Jul 29, 2014
@qiangxue
Copy link
Member

@mbman Will you be able to set a breakpoint in Security::validateData() and see why the validation fails? Or could you please paste your cookie value and the cookie validation key?

@samdark
Copy link
Member

samdark commented Jul 29, 2014

The issue seems to be storing binary data in cookie as @qiangxue noted.

In order to fix it we need to convert binary hash to string and then convert it back when reading. I think base64_encode and base64_decode should be OK for it.

@samdark samdark self-assigned this Jul 29, 2014
@mbman
Copy link
Contributor Author

mbman commented Jul 29, 2014

Here you go:
'cookieValidationKey' => 'hl-1nd93H281efs',

I'll set the breakpoint and send the cookie data ASAP

@samdark
Copy link
Member

samdark commented Jul 29, 2014

A good place to check with debugger is https://github.com/yiisoft/yii2/blob/master/framework/web/Request.php#L1195

@mbman
Copy link
Contributor Author

mbman commented Jul 29, 2014

$_COOKIE value at the breakpoint @samdark suggested:

array (size=3)
  '_identity' => string 'a856387a22edee80aed59ef85fef670ba09878469941e9bb0c0f5f26f5ec1480s:46:"[1,"Ts3tQePZVtoNzS_EdKEV9Ofxn7RpQF6L",2592000]";' (length=118)
  'PHPSESSID' => string 'eu82n5eds0pr95dpok77v7p200' (length=26)
  '_csrf' => string '‚08ćĘŮ­PüśĹO�ŃŐâT�á�…†Bś�eµâ�?    Fs:32:"UwXqTJ63PJlpJEk13QuvQ8ye-WM2LwCe";' (length=72)

@samdark
Copy link
Member

samdark commented Jul 29, 2014

Yup. _csrf seems to be binary.

https://github.com/yiisoft/yii2/blob/master/framework/web/Request.php#L1306 is generating raw token but we're trying to decode it at https://github.com/yiisoft/yii2/blob/master/framework/web/Request.php#L1341

@qiangxue
Copy link
Member

what's your cookievalidationkey?

@metalagman
Copy link
Contributor

key is sfjBvtU7vXsWLsiGfkHY75mIYsqLwATl

@qiangxue
Copy link
Member

Your cookie is valid. Is your problem the same as @mbman's?

@metalagman
Copy link
Contributor

pretty sure it is
I suppose that problem is not with code, problem is os-specific
I'm getting error on FPM PHP 5.4.4-14+deb7u12 (cli) (built: Jun 30 2014 18:42:58)
and dont on PHP Version 5.4.26 on windows

@metalagman
Copy link
Contributor

some debugging info:

 $_POST = [
    '_csrf' => 'MnhwRE13c0lKECIKD09HMGozBwx1Axh6Yyk7EQgTQD9TEyAjOx9GcQ=='
    'Account' => [
        'name' => '...'
        'password' => '...'
        'rememberMe' => '1'
    ]
]

$_COOKIE = [
    '_csrf' => '4558ce18737862b0acc221aef149bd36b66d17c6bc1fd7b23b93d458d7126f34s:32:\"xhRNB84yXKwH8tk3QQKUEd3vakPgvh58\";'
]

$_SERVER = [
    ...
    'HTTP_COOKIE' => '_csrf=4558ce18737862b0acc221aef149bd36b66d17c6bc1fd7b23b93d458d7126f34s%3A32%3A%22xhRNB84yXKwH8tk3QQKUEd3vakPgvh58%22%3B'
    ...
]

@mbman
Copy link
Contributor Author

mbman commented Jul 30, 2014

Updated the code to latest version, cleared cookies and the problem is still there.
Csrf values:

$_POST: NUMyTEo2ODRmDmcVA05CW010BC47QFV5YhBVE3ldSgdbOgIeLwFJeA==
$_COOKIE: 1fc0738c337cd4eddab5c35a9280dfacc7f73aea1fbf11d3f5453ccf61c2e5e4s:32:\"SMUYIxzox76bqvmMWSg_3kr3ny0Re7qL\";

@mbman
Copy link
Contributor Author

mbman commented Jul 30, 2014

Vagrant server used:

@zlakomanoff
Copy link
Contributor

mb_substr with null length parameter return empty string
security.php in validateData function
$pureData always is empty string

http://php.net//manual/en/function.mb-substr.php (first comment)

@samdark samdark reopened this Jul 30, 2014
@samdark
Copy link
Member

samdark commented Jul 30, 2014

@onepeopleprojects why length is 0?

samdark added a commit that referenced this issue Jul 30, 2014
Fixes #4497: Bad Request, Unable to verify your data submission
@samdark
Copy link
Member

samdark commented Jul 30, 2014

Just merged another fix. Is it better now?

@zlakomanoff
Copy link
Contributor

Excellent speed )) yes, it work ))

@prawee
Copy link

prawee commented Jul 30, 2014

great! it work.

@dynasource
Copy link
Member

this patch also fixes a Codeception issue:

  • when json_encoding the cookie
  • when using the debug option

@samdark
Copy link
Member

samdark commented Aug 3, 2014

@dynasource are the issues mentioned in our or codeception trackers? If so, would you please give links so I'll close these?

@dynasource
Copy link
Member

none. It was an observation yesterday. I knew about this topic, so a composer update was worth a try (and it was). I just wanted to mention it for people searching.

@queejie
Copy link

queejie commented Oct 27, 2014

I am suddenly getting the "Unable to verify your data submission" on a $.ajax() call that was working fine. I haven't changed the config or the $.ajax() call. I just ran a "composer update", and I cleared the cookies in my browser. If I set enableCsrfValidation to FALSE it works fine. I'm just getting my feet wet with Yii2 and composer. Thanks in advance for any suggestions on how to diagnose this.

@kartik-v
Copy link
Contributor

I am getting the same error on a fresh update... need to check.

UPDATE: Confirm that the error occurs on the new update for ajax/post submissions. If I revert back my yiisoft/yii2 to older version it works.

@MEGApixel23
Copy link

Have same problem. Reinstall project but ajax-requests still returns 400 error

@mbman
Copy link
Contributor Author

mbman commented Dec 8, 2014

@MEGApixel23 do you send the _csrf token in the ajax request?

@MEGApixel23
Copy link

@mbman no, but it works on previous Yii2 version. I tried to send _csrf token and it works. So what I must rewrite all my ajax requests in whole project? May be there is another solution?

@metalagman
Copy link
Contributor

@MEGApixel23 just disable csrf validation in your ajax controller

@samgiety
Copy link

Still happens on version 2.0.2. The cookie is not created on first request. I have a workaround: Extend View::endPage():

class View extends \yii\web\View {

    public function endPage($ajaxMode = false)
    {
        \Yii::$app->getResponse()->sendCookies();
        parent::endPage($ajaxMode);
    }
}

@cebe
Copy link
Member

cebe commented Jan 18, 2015

@gorellnet please open a new issue if there is a problem, this one is already closed for 2.0rc

@yiisoft yiisoft locked and limited conversation to collaborators Jan 18, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests