You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactory.php
+8-32Lines changed: 8 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -19,38 +19,11 @@ public static function getTwigEnvironment(Connection $connection = null) {
19
19
20
20
$twig = new \Twig_Environment($stringLoader, $options);
21
21
22
-
if ($connection !== null) {
23
-
24
-
$twig->getExtension('core')->setEscaper('sql', function(\Twig_Environment$env, $string, $charset) use ($connection) {
25
-
var_dump($string);
26
-
// ARGH! Escapers seems to be not called if $string is empty!
27
-
if ($string === null) {
28
-
return"null";
29
-
} else {
30
-
return$connection->quote($string);
31
-
}
32
-
});
33
-
34
-
// SQL identifier (table or column names....)
35
-
$twig->getExtension('core')->setEscaper('sqli', function(\Twig_Environment$env, $string, $charset) use ($connection) {
36
-
return$connection->quoteIdentifier($string);
37
-
});
38
-
39
-
} else {
40
-
$twig->getExtension('core')->setEscaper('sql', function(\Twig_Environment$env, $string, $charset) use ($connection) {
41
-
if ($string === null) {
42
-
return"null";
43
-
} else {
44
-
return"'".addslashes($string)."'";
45
-
}
46
-
});
47
-
48
-
$twig->getExtension('core')->setEscaper('sqli', function(\Twig_Environment$env, $string, $charset) use ($connection) {
49
-
// Note: we don't know how to escape backticks in a column name. In order to avoid injection,
50
-
// we remove any backticks.
51
-
return"`".str_replace('`', '', $string)."`";
52
-
});
53
-
}
22
+
// Default escaper will throw an exception. This is because we want to use SQL parameters instead of Twig.
23
+
// This ahs a number of advantages, especially in terms of caching.
24
+
$twig->getExtension('core')->setEscaper('sql', function(\Twig_Environment$env, $string, $charset) use ($connection) {
25
+
thrownewForbiddenTwigParameterInSqlException('You cannot use Twig expressions (like "{{ id }}"). Instead, you should use SQL parameters (like ":id"). Twig integration is limited to Twig statements (like "{% for .... %}"');
0 commit comments