TwigSandboxPolicy.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Drupal\Core\Template;
  3. use Drupal\Core\Site\Settings;
  4. /**
  5. * Default sandbox policy for Twig templates.
  6. *
  7. * Twig's sandbox extension is usually used to evaluate untrusted code by
  8. * limiting access to potentially unsafe properties or methods. Since we do not
  9. * use ViewModels when passing objects to Twig templates, we limit what those
  10. * objects can do by whitelisting certain classes, method names, and method
  11. * names with an allowed prefix. All object properties may be accessed.
  12. */
  13. class TwigSandboxPolicy implements \Twig_Sandbox_SecurityPolicyInterface {
  14. /**
  15. * An array of whitelisted methods in the form of methodName => TRUE.
  16. *
  17. * @var array
  18. */
  19. protected $whitelisted_methods;
  20. /**
  21. * An array of whitelisted method prefixes -- any method starting with one of
  22. * these prefixes will be allowed.
  23. *
  24. * @var array
  25. */
  26. protected $whitelisted_prefixes;
  27. /**
  28. * An array of class names for which any method calls are allowed.
  29. *
  30. * @var array
  31. */
  32. protected $whitelisted_classes;
  33. /**
  34. * Constructs a new TwigSandboxPolicy object.
  35. */
  36. public function __construct() {
  37. // Allow settings.php to override our default whitelisted classes, methods,
  38. // and prefixes.
  39. $whitelisted_classes = Settings::get('twig_sandbox_whitelisted_classes', [
  40. // Allow any operations on the Attribute object as it is intended to be
  41. // changed from a Twig template, for example calling addClass().
  42. 'Drupal\Core\Template\Attribute',
  43. ]);
  44. // Flip the arrays so we can check using isset().
  45. $this->whitelisted_classes = array_flip($whitelisted_classes);
  46. $whitelisted_methods = Settings::get('twig_sandbox_whitelisted_methods', [
  47. // Only allow idempotent methods.
  48. 'id',
  49. 'label',
  50. 'bundle',
  51. 'get',
  52. '__toString',
  53. 'toString',
  54. ]);
  55. $this->whitelisted_methods = array_flip($whitelisted_methods);
  56. $this->whitelisted_prefixes = Settings::get('twig_sandbox_whitelisted_prefixes', [
  57. 'get',
  58. 'has',
  59. 'is',
  60. ]);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function checkSecurity($tags, $filters, $functions) {}
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function checkPropertyAllowed($obj, $property) {}
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function checkMethodAllowed($obj, $method) {
  74. foreach ($this->whitelisted_classes as $class => $key) {
  75. if ($obj instanceof $class) {
  76. return TRUE;
  77. }
  78. }
  79. // Return quickly for an exact match of the method name.
  80. if (isset($this->whitelisted_methods[$method])) {
  81. return TRUE;
  82. }
  83. // If the method name starts with a whitelisted prefix, allow it.
  84. // Note: strpos() is between 3x and 7x faster than preg_match in this case.
  85. foreach ($this->whitelisted_prefixes as $prefix) {
  86. if (strpos($method, $prefix) === 0) {
  87. return TRUE;
  88. }
  89. }
  90. throw new \Twig_Sandbox_SecurityError(sprintf('Calling "%s" method on a "%s" object is not allowed.', $method, get_class($obj)));
  91. }
  92. }