Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Prevent infinite loops in Deprecation::notice() #10529

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Core/Config/Middleware/ExtensionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ protected function getExtraConfig($class, $classConfig, $excludeMiddleware)

$extensions = $extensionSourceConfig['extensions'];
foreach ($extensions as $extension) {
// Allow removing extensions via yaml config by setting named extension config to null
if ($extension === null) {
continue;
}

list($extensionClass, $extensionArgs) = ClassInfo::parse_class_spec($extension);
// Strip service name specifier
$extensionClass = strtok($extensionClass ?? '', '.');
Expand Down
5 changes: 5 additions & 0 deletions src/Core/Extensible.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ public function getExtensionInstances()

if ($extensions) {
foreach ($extensions as $extension) {
// Allow removing extensions via yaml config by setting named extension config to null
if ($extension === null) {
continue;
}

$name = $extension;
// Allow service names of the form "%$ServiceName"
if (substr($name ?? '', 0, 2) == '%$') {
Expand Down
10 changes: 10 additions & 0 deletions src/Dev/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class Deprecation
*/
public static $notice_level = null;

/**
* Used to prevent infinite call loops
*/
protected static $inside_notice = false;

/**
* Set the version that is used to check against the version passed to notice. If the ::notice version is
* greater than or equal to this version, a message will be raised
Expand Down Expand Up @@ -171,6 +176,10 @@ public static function set_enabled($enabled)
*/
public static function notice($atVersion, $string = '', $scope = Deprecation::SCOPE_METHOD)
{
if (static::$inside_notice) {
return;
}
static::$inside_notice = true;
if (!static::get_enabled()) {
return;
}
Expand Down Expand Up @@ -238,6 +247,7 @@ public static function notice($atVersion, $string = '', $scope = Deprecation::SC
user_error($string ?? '', $level ?? 0);
}
}
static::$inside_notice = false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Security/MemberAuthenticator/MemberAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function authenticate(array $data, HTTPRequest $request, ValidationResult
// Optionally record every login attempt as a {@link LoginAttempt} object
$this->recordLoginAttempt($data, $request, $member, $result->isValid());

if ($member) {
if ($member && $request->hasSession()) {
$request->getSession()->clear('BackURL');
}

Expand Down
48 changes: 48 additions & 0 deletions tests/php/Core/ExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace SilverStripe\Core\Tests;

use BadMethodCallException;
use ReflectionProperty;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Tests\ExtensionTest\NamedExtension;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\DataObject;

class ExtensionTest extends SapphireTest
{
protected function setUp(): void
{
parent::setUp();
// Reset extra_methods so that when we set NamedExtension to null it re-evaluates which methods are available
$reflectionProperty = new ReflectionProperty(DataObject::class, 'extra_methods');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue([]);
// Add named extension config like we would in yaml
Config::modify()->merge(DataObject::class, 'extensions', ['NamedExtension' => NamedExtension::class]);
}

public function testHasNamedExtension()
{
$this->assertTrue(DataObject::has_extension(NamedExtension::class));
$instance = new DataObject();
$this->assertTrue($instance->hasMethod('getTestValue'));
$this->assertSame('test', $instance->getTestValue());
}

public function testRemoveNamedExtension()
{
Config::modify()->merge(DataObject::class, 'extensions', ['NamedExtension' => null]);
$this->assertFalse(DataObject::has_extension(NamedExtension::class));
$instance = new DataObject();
$this->assertFalse($instance->hasMethod('getTestValue'));
}

public function testRemoveNamedExtensionException()
{
Config::modify()->merge(DataObject::class, 'extensions', ['NamedExtension' => null]);
$instance = new DataObject();
$this->expectException(BadMethodCallException::class);
$instance->getTestValue();
}
}
14 changes: 14 additions & 0 deletions tests/php/Core/ExtensionTest/NamedExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace SilverStripe\Core\Tests\ExtensionTest;

use SilverStripe\Core\Extension;
use SilverStripe\Dev\TestOnly;

class NamedExtension extends Extension implements TestOnly
{
public function getTestValue()
{
return 'test';
}
}