| 148 |
lars |
1 |
Security {#advanced.features.security}
|
|
|
2 |
========
|
|
|
3 |
|
|
|
4 |
Security is good for situations when you have untrusted parties editing
|
|
|
5 |
the templates e.g. via ftp, and you want to reduce the risk of system
|
|
|
6 |
security compromises through the template language.
|
|
|
7 |
|
|
|
8 |
The settings of the security policy are defined by properties of an
|
|
|
9 |
instance of the Smarty\_Security class. These are the possible settings:
|
|
|
10 |
|
|
|
11 |
- `$secure_dir` is an array of template directories that are
|
|
|
12 |
considered secure. [`$template_dir`](#variable.template.dir)
|
|
|
13 |
considered secure implicitly. The default is an empty array.
|
|
|
14 |
|
|
|
15 |
- `$trusted_dir` is an array of all directories that are considered
|
|
|
16 |
trusted. Trusted directories are where you keep php scripts that are
|
|
|
17 |
executed directly from the templates with
|
|
|
18 |
[`{insert}`](#language.function.insert.php). The default is an
|
|
|
19 |
empty array.
|
|
|
20 |
|
|
|
21 |
- `$trusted_uri` is an array of regular expressions matching URIs that
|
|
|
22 |
are considered trusted. This security directive used by
|
|
|
23 |
[`{fetch}`](#language.function.fetch) and
|
|
|
24 |
[`{html_image}`](#language.function.html.image). URIs passed to
|
|
|
25 |
these functions are reduced to `{$PROTOCOL}://{$HOSTNAME}` to allow
|
|
|
26 |
simple regular expressions (without having to deal with edge cases
|
|
|
27 |
like authentication-tokens).
|
|
|
28 |
|
|
|
29 |
The expression `'#https?://.*smarty.net$#i'` would allow accessing
|
|
|
30 |
the following URIs:
|
|
|
31 |
|
|
|
32 |
- `http://smarty.net/foo`
|
|
|
33 |
|
|
|
34 |
- `http://smarty.net/foo`
|
|
|
35 |
|
|
|
36 |
- `http://www.smarty.net/foo`
|
|
|
37 |
|
|
|
38 |
- `http://smarty.net/foo`
|
|
|
39 |
|
|
|
40 |
- `https://foo.bar.www.smarty.net/foo/bla?blubb=1`
|
|
|
41 |
|
|
|
42 |
but deny access to these URIs:
|
|
|
43 |
|
|
|
44 |
- `http://smarty.com/foo` (not matching top-level domain \"com\")
|
|
|
45 |
|
|
|
46 |
- `ftp://www.smarty.net/foo` (not matching protocol \"ftp\")
|
|
|
47 |
|
|
|
48 |
- `http://www.smarty.net.otherdomain.com/foo` (not matching end of
|
|
|
49 |
domain \"smarty.net\")
|
|
|
50 |
|
|
|
51 |
- `$static_classes` is an array of classes that are considered
|
|
|
52 |
trusted. The default is an empty array which allows access to all
|
|
|
53 |
static classes. To disable access to all static classes set
|
|
|
54 |
\$static\_classes = null.
|
|
|
55 |
|
|
|
56 |
- `$php_functions` is an array of PHP functions that are considered
|
|
|
57 |
trusted and can be used from within template. To disable access to
|
|
|
58 |
all PHP functions set \$php\_functions = null. An empty array (
|
|
|
59 |
\$php\_functions = array() ) will allow all PHP functions. The
|
|
|
60 |
default is array(\'isset\', \'empty\', \'count\', \'sizeof\',
|
|
|
61 |
\'in\_array\', \'is\_array\',\'time\',\'nl2br\').
|
|
|
62 |
|
|
|
63 |
- `$php_modifiers` is an array of PHP functions that are considered
|
|
|
64 |
trusted and can be used from within template as modifier. To disable
|
|
|
65 |
access to all PHP modifier set \$php\_modifier = null. An empty
|
|
|
66 |
array ( \$php\_modifier = array() ) will allow all PHP functions.
|
|
|
67 |
The default is array(\'escape\',\'count\').
|
|
|
68 |
|
|
|
69 |
- `$streams` is an array of streams that are considered trusted and
|
|
|
70 |
can be used from within template. To disable access to all streams
|
|
|
71 |
set \$streams = null. An empty array ( \$streams = array() ) will
|
|
|
72 |
allow all streams. The default is array(\'file\').
|
|
|
73 |
|
|
|
74 |
- `$allowed_modifiers` is an array of (registered / autoloaded)
|
|
|
75 |
modifiers that should be accessible to the template. If this array
|
|
|
76 |
is non-empty, only the herein listed modifiers may be used. This is
|
|
|
77 |
a whitelist.
|
|
|
78 |
|
|
|
79 |
- `$disabled_modifiers` is an array of (registered / autoloaded)
|
|
|
80 |
modifiers that may not be accessible to the template.
|
|
|
81 |
|
|
|
82 |
- `$allowed_tags` is a boolean flag which controls if constants can
|
|
|
83 |
function-, block and filter plugins that should be accessible to the
|
|
|
84 |
template. If this array is non-empty, only the herein listed
|
|
|
85 |
modifiers may be used. This is a whitelist.
|
|
|
86 |
|
|
|
87 |
- `$disabled_tags` is an array of (registered / autoloaded) function-,
|
|
|
88 |
block and filter plugins that may not be accessible to the template.
|
|
|
89 |
|
|
|
90 |
- `$allow_constants` is a boolean flag which controls if constants can
|
|
|
91 |
be accessed by the template. The default is \"true\".
|
|
|
92 |
|
|
|
93 |
- `$allow_super_globals` is a boolean flag which controls if the PHP
|
|
|
94 |
super globals can be accessed by the template. The default is
|
|
|
95 |
\"true\".
|
|
|
96 |
|
|
|
97 |
If security is enabled, no private methods, functions or properties of
|
|
|
98 |
static classes or assigned objects can be accessed (beginning with
|
|
|
99 |
\'\_\') by the template.
|
|
|
100 |
|
|
|
101 |
To customize the security policy settings you can extend the
|
|
|
102 |
Smarty\_Security class or create an instance of it.
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
<?php
|
|
|
106 |
require 'Smarty.class.php';
|
|
|
107 |
|
|
|
108 |
class My_Security_Policy extends Smarty_Security {
|
|
|
109 |
// disable all PHP functions
|
|
|
110 |
public $php_functions = null;
|
|
|
111 |
// allow everthing as modifier
|
|
|
112 |
public $php_modifiers = array();
|
|
|
113 |
}
|
|
|
114 |
$smarty = new Smarty();
|
|
|
115 |
// enable security
|
|
|
116 |
$smarty->enableSecurity('My_Security_Policy');
|
|
|
117 |
?>
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
<?php
|
|
|
121 |
require 'Smarty.class.php';
|
|
|
122 |
$smarty = new Smarty();
|
|
|
123 |
$my_security_policy = new Smarty_Security($smarty);
|
|
|
124 |
// disable all PHP functions
|
|
|
125 |
$my_security_policy->php_functions = null;
|
|
|
126 |
// allow everthing as modifier
|
|
|
127 |
$my_security_policy->php_modifiers = array();
|
|
|
128 |
// enable security
|
|
|
129 |
$smarty->enableSecurity($my_security_policy);
|
|
|
130 |
?>
|
|
|
131 |
|
|
|
132 |
|
|
|
133 |
<?php
|
|
|
134 |
require 'Smarty.class.php';
|
|
|
135 |
$smarty = new Smarty();
|
|
|
136 |
// enable default security
|
|
|
137 |
$smarty->enableSecurity();
|
|
|
138 |
?>
|
|
|
139 |
|
|
|
140 |
> **Note**
|
|
|
141 |
>
|
|
|
142 |
> Most security policy settings are only checked when the template gets
|
|
|
143 |
> compiled. For that reason you should delete all cached and compiled
|
|
|
144 |
> template files when you change your security settings.
|