Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
4b353c7b
Commit
4b353c7b
authored
Nov 24, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #1297: CSRF not generated on error pages
parent
72dd86df
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
16 additions
and
10 deletions
+16
-10
error.md
docs/guide/error.md
+5
-5
Application.php
framework/yii/base/Application.php
+8
-0
ErrorHandler.php
framework/yii/base/ErrorHandler.php
+1
-3
Controller.php
framework/yii/web/Controller.php
+1
-1
ErrorAction.php
framework/yii/web/ErrorAction.php
+1
-1
No files found.
docs/guide/error.md
View file @
4b353c7b
...
...
@@ -22,14 +22,14 @@ return [
],
```
After it is done in case of error Yii will launch
`SiteController::actionError()`
. Since errors are converted to
exceptions we can get exception from error handler:
After it is done in case of error, Yii will launch
`SiteController::actionError()`
:
```
php
public
function
actionError
()
{
$exception
=
\Yii
::
$app
->
getErrorHandler
()
->
exception
;
$this
->
render
(
'myerror'
,
[
'message'
=>
$exception
->
getMessage
()]);
if
(
\Yii
::
$app
->
exception
!==
null
)
{
return
$this
->
render
(
'error'
,
[
'exception'
=>
\Yii
::
$app
->
exception
]);
}
}
```
...
...
@@ -48,7 +48,7 @@ public function actions()
```
After defining
`actions`
in
`SiteController`
as shown above you can create
`views/site/error.php`
. In the view there
are three varia
lb
es available:
are three varia
bl
es available:
-
`$name`
: the error name
-
`$message`
: the error message
...
...
framework/yii/base/Application.php
View file @
4b353c7b
...
...
@@ -127,6 +127,11 @@ abstract class Application extends Module
* ~~~
*/
public
$extensions
=
[];
/**
* @var \Exception the exception that is being handled currently. When this is not null,
* it means the application is handling some exception and extra care should be taken.
*/
public
$exception
;
/**
* @var string Used to reserve memory for fatal error handler.
...
...
@@ -487,6 +492,8 @@ abstract class Application extends Module
*/
public
function
handleException
(
$exception
)
{
$this
->
exception
=
$exception
;
// disable error capturing to avoid recursive errors while handling exceptions
restore_error_handler
();
restore_exception_handler
();
...
...
@@ -574,6 +581,7 @@ abstract class Application extends Module
if
(
ErrorException
::
isFatalError
(
$error
))
{
$exception
=
new
ErrorException
(
$error
[
'message'
],
$error
[
'type'
],
$error
[
'type'
],
$error
[
'file'
],
$error
[
'line'
]);
$this
->
exception
=
$exception
;
// use error_log because it's too late to use Yii log
error_log
(
$exception
);
...
...
framework/yii/base/ErrorHandler.php
View file @
4b353c7b
...
...
@@ -40,7 +40,7 @@ class ErrorHandler extends Component
/**
* @var string the route (e.g. 'site/error') to the controller action that will be used
* to display external errors. Inside the action, it can retrieve the error information
* by Yii::$app->e
rrorHandler->e
xception. This property defaults to null, meaning ErrorHandler
* by Yii::$app->exception. This property defaults to null, meaning ErrorHandler
* will handle the error display.
*/
public
$errorAction
;
...
...
@@ -96,8 +96,6 @@ class ErrorHandler extends Component
$response
->
getHeaders
()
->
removeAll
();
if
(
$useErrorView
&&
$this
->
errorAction
!==
null
)
{
// disable CSRF validation so that errorAction can run in case the error is caused by CSRF validation failure
Yii
::
$app
->
getRequest
()
->
enableCsrfValidation
=
false
;
$result
=
Yii
::
$app
->
runAction
(
$this
->
errorAction
);
if
(
$result
instanceof
Response
)
{
$response
=
$result
;
...
...
framework/yii/web/Controller.php
View file @
4b353c7b
...
...
@@ -91,7 +91,7 @@ class Controller extends \yii\base\Controller
public
function
beforeAction
(
$action
)
{
if
(
parent
::
beforeAction
(
$action
))
{
if
(
$this
->
enableCsrfValidation
&&
!
Yii
::
$app
->
getRequest
()
->
validateCsrfToken
())
{
if
(
$this
->
enableCsrfValidation
&&
Yii
::
$app
->
exception
===
null
&&
!
Yii
::
$app
->
getRequest
()
->
validateCsrfToken
())
{
throw
new
HttpException
(
400
,
Yii
::
t
(
'yii'
,
'Unable to verify your data submission.'
));
}
return
true
;
...
...
framework/yii/web/ErrorAction.php
View file @
4b353c7b
...
...
@@ -69,7 +69,7 @@ class ErrorAction extends Action
public
function
run
()
{
if
(
!
(
$exception
=
Yii
::
$app
->
getErrorHandler
()
->
exception
)
)
{
if
(
(
$exception
=
Yii
::
$app
->
exception
)
===
null
)
{
return
''
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment