From 2420d38b57e1ea3a4235078e1898a8c4c384ff4f Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 17 Apr 2023 10:43:35 +0100 Subject: [PATCH] Issue #3274867 by longwave, jungle, andypost, alexpott, smustgrave: Add TrustedCallback attribute --- .../Core/Security/Attribute/TrustedCallback.php | 9 +++++++++ .../Core/Security/DoTrustedCallbackTrait.php | 16 +++++++++++++--- .../Core/Security/DoTrustedCallbackTraitTest.php | 8 ++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 core/lib/Drupal/Core/Security/Attribute/TrustedCallback.php diff --git a/core/lib/Drupal/Core/Security/Attribute/TrustedCallback.php b/core/lib/Drupal/Core/Security/Attribute/TrustedCallback.php new file mode 100644 index 00000000000..039fb11aaa3 --- /dev/null +++ b/core/lib/Drupal/Core/Security/Attribute/TrustedCallback.php @@ -0,0 +1,9 @@ +getAttributes(TrustedCallback::class); + } } elseif ($callback instanceof \Closure) { $safe_callback = TRUE; diff --git a/core/tests/Drupal/Tests/Core/Security/DoTrustedCallbackTraitTest.php b/core/tests/Drupal/Tests/Core/Security/DoTrustedCallbackTraitTest.php index 91b72fe3717..cab6e8d68d9 100644 --- a/core/tests/Drupal/Tests/Core/Security/DoTrustedCallbackTraitTest.php +++ b/core/tests/Drupal/Tests/Core/Security/DoTrustedCallbackTraitTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\Core\Security; +use Drupal\Core\Security\Attribute\TrustedCallback; use Drupal\Core\Security\TrustedCallbackInterface; use Drupal\Core\Security\DoTrustedCallbackTrait; use Drupal\Core\Security\UntrustedCallbackException; @@ -33,8 +34,10 @@ class DoTrustedCallbackTraitTest extends UnitTestCase { $tests['closure'] = [$closure]; $tests['TrustedCallbackInterface_object'] = [[new TrustedMethods(), 'callback'], TrustedInterface::class]; + $tests['TrustedCallbackInterface_object_attribute'] = [[new TrustedMethods(), 'attributeCallback'], TrustedInterface::class]; $tests['TrustedCallbackInterface_static_string'] = ['\Drupal\Tests\Core\Security\TrustedMethods::callback', TrustedInterface::class]; $tests['TrustedCallbackInterface_static_array'] = [[TrustedMethods::class, 'callback'], TrustedInterface::class]; + $tests['TrustedCallbackInterface_static_array_attribute'] = [[TrustedMethods::class, 'attributeCallback'], TrustedInterface::class]; $tests['extra_trusted_interface_object'] = [[new TrustedObject(), 'callback'], TrustedInterface::class]; $tests['extra_trusted_interface_static_string'] = ['\Drupal\Tests\Core\Security\TrustedObject::callback', TrustedInterface::class]; $tests['extra_trusted_interface_static_array'] = [[TrustedObject::class, 'callback'], TrustedInterface::class]; @@ -140,6 +143,11 @@ class TrustedMethods implements TrustedCallbackInterface { return 'test'; } + #[TrustedCallback] + public static function attributeCallback() { + return 'test'; + } + public static function unTrustedCallback() { return 'test'; }