This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SecureUrlRuleFilter.php
205 lines (189 loc) · 6.62 KB
/
SecureUrlRuleFilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\https;
use Yii;
use yii\base\ActionFilter;
use yii\base\InvalidConfigException;
use yii\web\UrlRule;
/**
* SecureUrlRuleFilter is an action filter, which adjusts [[\yii\web\UrlManager::rules]] to automatically create URLs with
* correct protocol 'http' or 'https'.
* After being applied it ensures [[\yii\web\UrlManager::createUrl()]] will automatically create absolute URL with leading protocol
* in case it miss matches current one.
*
* This filter can be used at module (application) level or at controller level.
*
* Application configuration example:
*
* ```php
* return [
* 'as secureUrlRules' => [
* 'class' => 'yii2tech\https\SecureUrlRuleFilter',
* 'secureOnlyRoutes' => [
* 'auth/login',
* 'site/signup',
* ],
* 'secureExceptRoutes' => [
* 'site/index',
* 'help/<action>',
* ],
* ],
* // ...
* ];
* ```
*
* Note: this filter will take affect only if [[UrlManager::enablePrettyUrl]] is enabled.
*
* Attention: once applied this filter changes the state of related [[UrlManager]], this may break some features,
* like parsing URL.
*
* @see UrlManager
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 1.0
*/
class SecureUrlRuleFilter extends ActionFilter
{
/**
* @var boolean|callable whether the filter is enabled or not.
* You may use this field for quick disabling of the filter, based on debug mode or environment.
* This value can be a callable, which returns actual boolean result for the check.
*/
public $enabled = true;
/**
* @var string|\yii\web\UrlManager URL manager to be processed. This can be either instance of [[\yii\web\UrlManager]] or
* corresponding application component name.
*/
public $urlManager = 'urlManager';
/**
* @var array list of the URL routes, which should be secure-only ('https' protocol).
* Routes should be specified in the same way they are used in [[\yii\web\UrlManager::rules]], including placeholders
* like `<controller>` and `<action>`. Route can be specified as wildcards, e.g. `auth/*`.
* For example:
*
* ```
* [
* 'auth/login',
* 'payment/<action>',
* 'some-module/<controller>/<action>',
* 'credit-card/*',
* ]
* ```
*
* URL rules, which have host specification or 'parse-only' mode will not be affected.
*/
public $secureOnlyRoutes = [];
/**
* @var array list of the URL routes, which should not be secure ('http' protocol).
* Routes should be specified in the same way they are used in [[\yii\web\UrlManager::rules]], including placeholders
* like `<controller>` and `<action>`. Route can be specified as wildcards, e.g. `auth/*`.
* For example:
*
* ```
* [
* 'site/index',
* 'help/<action>',
* 'some-module/<controller>/<action>',
* 'pages/*',
* ]
* ```
*
* URL rules, which have host specification or 'parse-only' mode will not be affected.
*/
public $secureExceptRoutes = [];
/**
* @inheritdoc
*/
public function beforeAction($action)
{
if (!$this->isEnabled()) {
return true;
}
if (Yii::$app->getRequest()->getIsSecureConnection() && !empty($this->secureExceptRoutes)) {
$urlManager = $this->getUrlManager();
$host = str_replace('https://', 'http://', $urlManager->getHostInfo());
$this->processUrlRules($urlManager->rules, $this->secureExceptRoutes, $host);
return true;
}
if (!empty($this->secureOnlyRoutes)) {
$urlManager = $this->getUrlManager();
$host = str_replace('http://', 'https://', $urlManager->getHostInfo());
$this->processUrlRules($urlManager->rules, $this->secureOnlyRoutes, $host);
}
return true;
}
/**
* @return boolean whether filter is enabled or not.
*/
protected function isEnabled()
{
if (is_bool($this->enabled)) {
return $this->enabled;
}
return call_user_func($this->enabled);
}
/**
* Returns [[\yii\web\UrlManager]] corresponding to [[urlManager]] value.
* @return \yii\web\UrlManager URL manager instance.
* @throws InvalidConfigException on invalid [[urlManager]].
*/
protected function getUrlManager()
{
if (is_string($this->urlManager)) {
return Yii::$app->get($this->urlManager);
} elseif (!is_object($this->urlManager)) {
throw new InvalidConfigException('"' . get_class($this) . '::urlManager" must be instance of "yii\web\UrlManager" or application component name.');
}
return $this->urlManager;
}
/**
* @param UrlRule[] $rules URL rules to be processed.
* @param array $routes routes to be matched.
* @param string $host host to be applied.
*/
private function processUrlRules($rules, $routes, $host)
{
$urlRuleTemplateProperty = $this->getUrlRuleTemplateReflection();
foreach ($rules as $rule) {
if ($this->isRuleMatch($rule, $routes)) {
$rule->host = $host;
$newTemplate = $rule->host . $urlRuleTemplateProperty->getValue($rule);
$urlRuleTemplateProperty->setValue($rule, $newTemplate);
}
}
}
/**
* Returns reflection for the `_template` property of the [[UrlRule]] class.
* Sets it to be accessible.
* @return \ReflectionProperty reflection of the property.
*/
private function getUrlRuleTemplateReflection()
{
$urlRuleReflection = new \ReflectionClass('yii\web\UrlRule');
$urlRuleTemplateProperty = $urlRuleReflection->getProperty('_template');
$urlRuleTemplateProperty->setAccessible(true);
return $urlRuleTemplateProperty;
}
/**
* Checks if URL rule matches the condition for processing and the given routes list.
* @param UrlRule $rule URL rule instance.
* @param array $routes routes to be checked against.
* @return boolean whether the URL rule matching.
*/
private function isRuleMatch($rule, $routes)
{
if (!empty($rule->host) || $rule->mode === UrlRule::PARSING_ONLY) {
return false;
}
foreach ($routes as $route) {
if (fnmatch($route, $rule->route)) {
return true;
}
}
return false;
}
}