TwigSandboxPolicy.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. protected $whitelisted_methods = NULL;
  18. /**
  19. * An array of whitelisted method prefixes -- any method starting with one of
  20. * these prefixes will be allowed.
  21. */
  22. protected $whitelisted_prefixes = NULL;
  23. /**
  24. * An array of class names for which any method calls are allowed.
  25. */
  26. protected $whitelisted_classes = NULL;
  27. /**
  28. * Constructs a new TwigSandboxPolicy object.
  29. */
  30. public function __construct() {
  31. // Allow settings.php to override our default whitelisted classes, methods,
  32. // and prefixes.
  33. $whitelisted_classes = Settings::get('twig_sandbox_whitelisted_classes', [
  34. // Allow any operations on the Attribute object as it is intended to be
  35. // changed from a Twig template, for example calling addClass().
  36. 'Drupal\Core\Template\Attribute',
  37. ]);
  38. // Flip the arrays so we can check using isset().
  39. $this->whitelisted_classes = array_flip($whitelisted_classes);
  40. $whitelisted_methods = Settings::get('twig_sandbox_whitelisted_methods', [
  41. // Only allow idempotent methods.
  42. 'id',
  43. 'label',
  44. 'bundle',
  45. 'get',
  46. '__toString',
  47. 'toString',
  48. ]);
  49. $this->whitelisted_methods = array_flip($whitelisted_methods);
  50. $this->whitelisted_prefixes = Settings::get('twig_sandbox_whitelisted_prefixes', [
  51. 'get',
  52. 'has',
  53. 'is',
  54. ]);
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function checkSecurity($tags, $filters, $functions) {}
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function checkPropertyAllowed($obj, $property) {}
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function checkMethodAllowed($obj, $method) {
  68. foreach ($this->whitelisted_classes as $class => $key) {
  69. if ($obj instanceof $class) {
  70. return TRUE;
  71. }
  72. }
  73. // Return quickly for an exact match of the method name.
  74. if (isset($this->whitelisted_methods[$method])) {
  75. return TRUE;
  76. }
  77. // If the method name starts with a whitelisted prefix, allow it.
  78. // Note: strpos() is between 3x and 7x faster than preg_match in this case.
  79. foreach ($this->whitelisted_prefixes as $prefix) {
  80. if (strpos($method, $prefix) === 0) {
  81. return TRUE;
  82. }
  83. }
  84. throw new \Twig_Sandbox_SecurityError(sprintf('Calling "%s" method on a "%s" object is not allowed.', $method, get_class($obj)));
  85. }
  86. }