Problem/Motivation

In #2966327: Limit what can be called by a callback in render arrays to reduce the risk of RCE we added TrustedCallbackInterface to protect against arbitrary user-supplied callbacks
However DateTime element has both #date_date_callbacks and #date_time_callbacks which can execute arbitrary functions

Steps to reproduce

Alter a date time element and add to either #date_date_callbacks or #date_time_callbacks e.g

$form['some_field']['widget'][0]['value']['#date_time_callbacks'][] = 'die';

Proposed resolution

Use the same trusted callback interface (\Drupal\Core\Render\Renderer::doCallback) that is used elsewhere.

Remaining tasks

Commit

User interface changes

API changes

Callbacks for #date_date_callbacks and #date_time_callbacks must implement TrustedCallbackInterface

Data model changes

Release notes snippet

Comments

larowlan created an issue. See original summary.

larowlan’s picture

Issue tags: +Security improvements
larowlan’s picture

Assigned: Unassigned » larowlan
Issue tags: +Bug Smash Initiative
larowlan’s picture

Assigned: larowlan » Unassigned
Status: Active » Needs review
StatusFileSize
new9.94 KB

I ran out of time to finish this, but in the spirit of sloppy sam, here's what I've got so far

basically need to finish off the test

larowlan’s picture

Issue tags: +#pnx-sprint
kim.pepper’s picture

StatusFileSize
new9.94 KB
new2.03 KB

Fixes the phpcs issues.

Status: Needs review » Needs work

The last submitted patch, 6: 3213572-6.patch, failed testing. View results

larowlan’s picture

Version: 9.1.x-dev » 9.2.x-dev
larowlan’s picture

Status: Needs work » Needs review
StatusFileSize
new3.3 KB
new10.14 KB
larowlan’s picture

Added a change notice

kim.pepper’s picture

Status: Needs review » Reviewed & tested by the community

Patch looks great.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work

I need to update the patch to use the specific change-notice, rather than the generic one

larowlan’s picture

Issue summary: View changes
Status: Needs work » Reviewed & tested by the community
Issue tags: +Novice
StatusFileSize
new10.14 KB

Updated the change notice links, leaving at RTBC because that's a trivial change

larowlan’s picture

Issue tags: -Novice
larowlan’s picture

Issue summary: View changes
xjm’s picture

Status: Reviewed & tested by the community » Needs work

Just some nits.

  1. +++ b/core/lib/Drupal/Core/Security/StaticTrustedCallbackHelper.php
    @@ -0,0 +1,47 @@
    +namespace Drupal\Core\Security;
    

    Formidable namespace!

  2. +++ b/core/lib/Drupal/Core/Security/StaticTrustedCallbackHelper.php
    @@ -0,0 +1,47 @@
    +   * @param $message
    

    Missing data type. It's presumably a string?

  3. +++ b/core/lib/Drupal/Core/Security/StaticTrustedCallbackHelper.php
    @@ -0,0 +1,47 @@
    +   * @see \Drupal\Core\Security\DoTrustedCallbackTrait::doTrustedCallback
    
    +++ b/core/tests/Drupal/KernelTests/Core/Datetime/DatetimeElementFormTest.php
    @@ -105,7 +136,46 @@ public function testDatetimeElement() {
    +   * Data provider for ::testDatetimeElementUntrustedCallbacks.
    

    Missing parens on the method name.

  4. +++ b/core/tests/Drupal/KernelTests/Core/Datetime/DatetimeElementFormTest.php
    @@ -12,14 +14,21 @@
    +   * Date flag.
    ...
    +   * Time flag.
    

    These docs are a little weak; can we spell out what these flags are used for?

  5. +++ b/core/tests/Drupal/KernelTests/Core/Datetime/DatetimeElementFormTest.php
    @@ -105,7 +136,46 @@ public function testDatetimeElement() {
    +   * @return array
    +   *   Test cases.
    

    Looks like it's a string[][] so we can be specific about that.

Thanks!

xjm’s picture

Version: 9.2.x-dev » 9.3.x-dev

Should be 9.3.x I think since it's raising new deprecations for arbitrary untrusted callbacks.

larowlan’s picture

Status: Needs work » Needs review
StatusFileSize
new4.31 KB
new10.15 KB

Fixed 1-4, I don't agree on 5, dataProviders can return anything - so leaving it generic in case of future refactoring

phenaproxima’s picture

StatusFileSize
new10.15 KB
new1.33 KB

Fixed spelling error.

tim.plunkett’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Datetime/Element/Datetime.php
    @@ -267,9 +273,8 @@ public static function processDatetime(&$element, FormStateInterface $form_state
    +          $message = sprintf('DateTime element #date_date_callbacks callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. Support for this callback implementation is deprecated in 9.3.0 and will be removed in Drupal 10.0.0. See https://www.drupal.org/node/3217966', Variable::callableToString($callback));
    
    @@ -299,9 +304,8 @@ public static function processDatetime(&$element, FormStateInterface $form_state
    +          $message = sprintf('DateTime element #date_time_callbacks callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. Support for this callback implementation is deprecated in 9.3.0 and will be removed in Drupal 10.0.0. See https://www.drupal.org/node/3217966', Variable::callableToString($callback));
    

    Nitpick, message should have drupal:9.3.0 and drupal:10.0.0

  2. +++ b/core/lib/Drupal/Core/Security/StaticTrustedCallbackHelper.php
    @@ -0,0 +1,47 @@
    +class StaticTrustedCallbackHelper {
    

    Are there any other places this should be used in the future? If so, any follow-ups worth opening?

phenaproxima’s picture

Status: Needs work » Needs review
StatusFileSize
new8.06 KB

Nice catch @tim.plunkett. Fixed the deprecation message.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, looks good! Other than #20.2, which shouldn't block commit, all feedback has been addressed.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 21: 3213572-21.patch, failed testing. View results

phenaproxima’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new10.17 KB

Whoops, had botched the previous patch (forgot to add the new file). Fixed in this patch, and restoring RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 24: 3213572-24.patch, failed testing. View results

phenaproxima’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new10.18 KB

Uggghhhh

xjm’s picture

Fixed 1-4, I don't agree on 5, dataProviders can return anything - so leaving it generic in case of future refactoring

FWIW I disagree with this; we can almost always be more specific than array and the array depth and content is useful information for using the data provider. The test method that it's a data provider for is also now (correctly) typehinted, so if we did expand the provider the contract would be broken and we wouldn't miss fixing the docs and typehint.

Leaving RTBC to discuss.

larowlan’s picture

StatusFileSize
new1.2 KB
new10.54 KB

Now with more docs

  • xjm committed fae3763 on 9.3.x
    Issue #3213572 by larowlan, phenaproxima, kim.pepper, xjm, tim.plunkett...
xjm’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 9.3.x only since it includes a new, dynamic deprecation; and published the CR. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

alexpott’s picture

This issue has caused a serious regression - see #3250335: #date_date_callbacks is broken in Drupal 9.3 - the feature no longer works.

alexpott’s picture

Another potentially tricky issue is that we missed a bit of date_date_callback because the same mechanism is used by \Drupal\Core\Datetime\Element\Datelist - see #3250349: \Drupal\Core\Datetime\Element\Datelist::processDatelist() does not use trusted callbacks - #date_date_callbacks only partially converted to TrustedCallbackInterface in 9.3.x