HostControl
API reference › yii › filters › HostControl
HostControl provides simple control over requested host name.
This filter provides protection against ‘host header’ attacks, allowing action execution only for specified host names.
Application configuration example:
return [ 'as hostControl' => [ 'class' => 'yii\filters\HostControl', 'allowedHosts' => [ 'example.com', '*.example.com', ], ], // ...];Controller configuration example:
use yii\web\Controller;use yii\filters\HostControl;
class SiteController extends Controller{ public function behaviors() { return [ 'hostControl' => [ 'class' => HostControl::class, 'allowedHosts' => [ 'example.com', '*.example.com', ], ], ]; }
// ...}Note: the best way to restrict allowed host names is usage of the web server ‘virtual hosts’ configuration. This filter should be used only if this configuration is not available or compromised.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$allowedHosts | array|Closure| | List of host names, which are allowed. Default: null. | HostControl |
$denyCallback | callable| | A callback that will be called if the current host does not match $allowedHosts. Default: null. | HostControl |
$except | array | List of action IDs that this filter should not apply to. Default: []. | yii\ |
$fallbackHostInfo | string| | Fallback host info (e.g. https://www.yiiframework.com) used when Request::$hostInfo is invalid. Default: ''. | HostControl |
$only | array | List of action IDs that this filter should apply to. Default: []. | yii\ |
$owner | yii\ | The owner of this behavior Default: null. | yii\ |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
afterAction() | This method is invoked right after an action is executed. | yii\ |
afterFilter() | yii\ | |
attach() | Attaches the behavior object to the component. | yii\ |
beforeAction() | This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action. | HostControl |
beforeFilter() | yii\ | |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detach() | Detaches the behavior object from the component. | yii\ |
events() | Declares event handlers for the $owner’s events. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
denyAccess() protected | Denies the access. | HostControl |
getActionId() protected | Returns an action ID by converting Action::$uniqueId into an ID relative to the module. | yii\ |
isActive() protected | Returns a value indicating whether the filter is active for the given action. | yii\ |
Property details
Section titled “Property details”$allowedHosts
Section titled “$allowedHosts ”public array|Closure|null $allowedHosts = nullList of host names, which are allowed.
Each host can be specified as a wildcard pattern. For example:
[ 'example.com', '*.example.com',]This field can be specified as a PHP callback of following signature:
function (\yii\base\Action $action) { //return array of strings}where $action is the current action object.
If this field is not set - no host name check will be performed.
$denyCallback
Section titled “$denyCallback ”public callable|null $denyCallback = nullA callback that will be called if the current host does not match $allowedHosts.
If not set, denyAccess() will be called.
The signature of the callback should be as follows:
function (\yii\base\Action $action)where $action is the current action object.
Note: while implementing your own host deny processing, make sure you avoid usage of the current requested host name, creation of absolute URL links, caching page parts and so on.
$fallbackHostInfo
Section titled “$fallbackHostInfo ”public string|null $fallbackHostInfo = ''Fallback host info (e.g. https://www.yiiframework.com) used when Request::$hostInfo is invalid.
This value will replace Request::$hostInfo before $denyCallback is called to make sure that
an invalid host will not be used for further processing. You can set it to null to leave Request::$hostInfo untouched.
Default value is empty string (this will result creating relative URLs instead of absolute).
Method details
Section titled “Method details”beforeAction()
Section titled “beforeAction() ”public function beforeAction(mixed $action): boolThis method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action.
Parameters
| Parameter | Type | Description |
|---|---|---|
$action | mixed | The action to be executed. |
Returns
bool: Whether the action should continue to be executed.
denyAccess()
Section titled “denyAccess() ”protected function denyAccess(yii\base\Action $action): mixedDenies the access.
The default implementation will display 404 page right away, terminating the program execution. You may override this method, creating your own deny access handler. While doing so, make sure you avoid usage of the current requested host name, creation of absolute URL links, caching page parts and so on.
Parameters
| Parameter | Type | Description |
|---|---|---|
$action | yii\ | The action to be executed. |
Returns
mixed
Throws