@@ -25,7 +25,7 @@ access control should be used on this request. The following ``access_control``
25
25
options are used for matching:
26
26
27
27
* ``path ``: a regular expression (without delimiters)
28
- * ``ip `` or ``ips ``: netmasks are also supported
28
+ * ``ip `` or ``ips ``: netmasks are also supported (can be a comma-separated string)
29
29
* ``port ``: an integer
30
30
* ``host ``: a regular expression
31
31
* ``methods ``: one or many methods
@@ -37,6 +37,9 @@ Take the following ``access_control`` entries as an example:
37
37
.. code-block :: yaml
38
38
39
39
# config/packages/security.yaml
40
+ parameters :
41
+ env(TRUSTED_IPS) : ' 10.0.0.1, 10.0.0.2'
42
+
40
43
security :
41
44
# ...
42
45
access_control :
@@ -45,6 +48,10 @@ Take the following ``access_control`` entries as an example:
45
48
- { path: '^/admin', roles: ROLE_USER_HOST, host: symfony\.com$ }
46
49
- { path: '^/admin', roles: ROLE_USER_METHOD, methods: [POST, PUT] }
47
50
51
+ # ips can be comma-separated, which is especially useful when using env variables
52
+ - { path: '^/admin', roles: ROLE_USER_IP, ips: '%env(TRUSTED_IPS)%' }
53
+ - { path: '^/admin', roles: ROLE_USER_IP, ips: [127.0.0.1, ::1, '%env(TRUSTED_IPS)%'] }
54
+
48
55
.. code-block :: xml
49
56
50
57
<!-- config/packages/security.xml -->
@@ -57,18 +64,31 @@ Take the following ``access_control`` entries as an example:
57
64
http://symfony.com/schema/dic/security
58
65
https://symfony.com/schema/dic/security/security-1.0.xsd" >
59
66
67
+ <srv : parameters >
68
+ <srv : parameter key =" env(TRUSTED_IPS)" >10.0.0.1, 10.0.0.2</parameter >
69
+ </srv : parameters >
70
+
60
71
<config >
61
72
<!-- ... -->
62
73
<rule path =" ^/admin" role =" ROLE_USER_IP" ip =" 127.0.0.1" />
63
74
<rule path =" ^/admin" role =" ROLE_USER_PORT" ip =" 127.0.0.1" port =" 8080" />
64
75
<rule path =" ^/admin" role =" ROLE_USER_HOST" host =" symfony\.com$" />
65
76
<rule path =" ^/admin" role =" ROLE_USER_METHOD" methods =" POST, PUT" />
77
+
78
+ <!-- ips can be comma-separated, which is especially useful when using env variables -->
79
+ <rule path =" ^/admin" role =" ROLE_USER_IP" ip =" %env(TRUSTED_IPS)%" />
80
+ <rule path =" ^/admin" role =" ROLE_USER_IP" >
81
+ <ip >127.0.0.1</ip >
82
+ <ip >::1</ip >
83
+ <ip >%env(TRUSTED_IPS)%</ip >
84
+ </rule >
66
85
</config >
67
86
</srv : container >
68
87
69
88
.. code-block :: php
70
89
71
90
// config/packages/security.php
91
+ $container->setParameter('env(TRUSTED_IPS)', '10.0.0.1, 10.0.0.2');
72
92
$container->loadFromExtension('security', [
73
93
// ...
74
94
'access_control' => [
@@ -92,10 +112,30 @@ Take the following ``access_control`` entries as an example:
92
112
'path' => '^/admin',
93
113
'roles' => 'ROLE_USER_METHOD',
94
114
'methods' => 'POST, PUT',
95
- ]
115
+ ],
116
+
117
+ // ips can be comma-separated, which is especially useful when using env variables
118
+ [
119
+ 'path' => '^/admin',
120
+ 'roles' => 'ROLE_USER_IP',
121
+ 'ips' => '%env(TRUSTED_IPS)%',
122
+ ],
123
+ [
124
+ 'path' => '^/admin',
125
+ 'roles' => 'ROLE_USER_IP',
126
+ 'ips' => [
127
+ '127.0.0.1',
128
+ '::1',
129
+ '%env(TRUSTED_IPS)%',
130
+ ],
131
+ ],
96
132
],
97
133
]);
98
134
135
+ .. versionadded :: 5.2
136
+
137
+ Support for comma-separated IP addresses was introduced in Symfony 5.2.
138
+
99
139
For each incoming request, Symfony will decide which ``access_control ``
100
140
to use based on the URI, the client's IP address, the incoming host name,
101
141
and the request method. Remember, the first rule that matches is used, and
@@ -133,73 +173,6 @@ if ``ip``, ``port``, ``host`` or ``method`` are not specified for an entry, that
133
173
:ref: `Deny access in PHP code <security-securing-controller >` if you want
134
174
to disallow access based on ``$_GET `` parameter values.
135
175
136
- .. versionadded :: 5.2
137
-
138
- Environment variables can be used to pass comma separated ip addresses
139
- (as a single value or as one of array values):
140
-
141
- .. configuration-block ::
142
-
143
- .. code-block :: yaml
144
-
145
- # config/packages/security.yaml
146
- parameters :
147
- env(TRUSTED_IPS) : ' 10.0.0.1, 10.0.0.2'
148
- security :
149
- # ...
150
- access_control :
151
- - { path: '^/admin', ips: '%env(TRUSTED_IPS)%' }
152
- - { path: '^/admin', ips: [127.0.0.1, ::1, '%env(TRUSTED_IPS)%'] }
153
-
154
- .. code-block :: xml
155
-
156
- <!-- config/packages/security.xml -->
157
- <?xml version =" 1.0" encoding =" UTF-8" ?>
158
- <srv : container xmlns =" http://symfony.com/schema/dic/security"
159
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
160
- xmlns : srv =" http://symfony.com/schema/dic/services"
161
- xsi : schemaLocation =" http://symfony.com/schema/dic/services
162
- https://symfony.com/schema/dic/services/services-1.0.xsd
163
- http://symfony.com/schema/dic/security
164
- https://symfony.com/schema/dic/security/security-1.0.xsd" >
165
-
166
- <parameters >
167
- <parameter key =" env(TRUSTED_IPS)" >10.0.0.1, 10.0.0.2</parameter >
168
- </parameters >
169
-
170
- <config >
171
- <!-- ... -->
172
- <rule path =" ^/admin" ip =" %env(TRUSTED_IPS)%" />
173
- <rule path =" ^/admin" >
174
- <ip >127.0.0.1</ip >
175
- <ip >::1</ip >
176
- <ip >%env(TRUSTED_IPS)%</ip >
177
- </rule >
178
- </config >
179
- </srv : container >
180
-
181
- .. code-block :: php
182
-
183
- // config/packages/security.php
184
- $container->setParameter('env(TRUSTED_IPS)', '10.0.0.1, 10.0.0.2');
185
- $container->loadFromExtension('security', [
186
- // ...
187
- 'access_control' => [
188
- [
189
- 'path' => '^/admin',
190
- 'ips' => '%env(TRUSTED_IPS)%',
191
- ],
192
- [
193
- 'path' => '^/admin',
194
- 'ips' => [
195
- '127.0.0.1',
196
- '::1',
197
- '%env(TRUSTED_IPS)%',
198
- ],
199
- ],
200
- ],
201
- ]);
202
-
203
176
.. _security-access-control-enforcement-options :
204
177
205
178
2. Access Enforcement
0 commit comments