| Conditions | 8 |
| Paths | 7 |
| Total Lines | 41 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 18 | public function handle($request, Closure $next) |
||
| 19 | { |
||
| 20 | if (!config('recaptcha.enabled')) return next($request); |
||
| 21 | |||
| 22 | $response_domain = null; |
||
| 23 | |||
| 24 | if ($request->has('g-recaptcha-response')) { |
||
| 25 | $response = $request->get('g-recaptcha-response'); |
||
| 26 | |||
| 27 | $client = new \GuzzleHttp\Client(); |
||
| 28 | $res = $client->post('https://www.google.com/recaptcha/api/siteverify', [ |
||
| 29 | 'form_params' => [ |
||
| 30 | 'secret' => config('recaptcha.secret_key'), |
||
| 31 | 'response' => $response, |
||
| 32 | ], |
||
| 33 | ]); |
||
| 34 | |||
| 35 | if ($res->getStatusCode() === 200) { |
||
| 36 | $result = json_decode($res->getBody()); |
||
| 37 | |||
| 38 | $response_domain = $result->hostname; |
||
| 39 | |||
| 40 | // Compare the domain received by google with the app url |
||
| 41 | $domain_verified = false; |
||
| 42 | if (config('recaptcha.verify_domain')) { |
||
| 43 | $matches; |
||
|
|
|||
| 44 | preg_match('/^(?:https?:\/\/)?((?:www\.)?[^:\/\n]+)/', config('app.url'), $matches); |
||
| 45 | $domain = $matches[1]; |
||
| 46 | $domain_verified = $response_domain === $domain; |
||
| 47 | } |
||
| 48 | |||
| 49 | if ($result->success && (!config('recaptcha.verify_domain') || $domain_verified)) { |
||
| 50 | return $next($request); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | // Emit an event and return to the previous view with an error (only the captcha error will be shown!) |
||
| 56 | event(new FailedCaptcha($request->ip(), $response_domain)); |
||
| 57 | return back()->withErrors(['g-recaptcha-response' => trans('strings.captcha_invalid')])->withInput(); |
||
| 58 | } |
||
| 59 | } |
||
| 60 |
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.