From 367d4d78d7476c6894514aeb97f9aced89c9edec Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Wed, 29 May 2024 18:30:05 +0200 Subject: [PATCH 1/8] Use an event for the spam check during registration --- .../files/lib/bootstrap/com.woltlab.wcf.php | 5 +++ .../user/RegistrationSpamChecking.class.php | 27 ++++++++++++++++ .../files/lib/form/RegisterForm.class.php | 32 +++++++++---------- ...istrationSpamCheckingSfsListener.class.php | 28 ++++++++++++++++ 4 files changed, 76 insertions(+), 16 deletions(-) create mode 100644 wcfsetup/install/files/lib/event/user/RegistrationSpamChecking.class.php create mode 100644 wcfsetup/install/files/lib/system/event/listener/RegistrationSpamCheckingSfsListener.class.php diff --git a/wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php b/wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php index 8a7b127088a..1c29b4a72a7 100644 --- a/wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php +++ b/wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php @@ -33,6 +33,11 @@ static function (\wcf\event\session\PreserveVariablesCollecting $event) { \wcf\system\event\listener\UsernameValidatingCheckCharactersListener::class ); + $eventHandler->register( + \wcf\event\user\RegistrationSpamChecking::class, + \wcf\system\event\listener\RegistrationSpamCheckingSfsListener::class + ); + $eventHandler->register( \wcf\event\package\PackageListChanged::class, static function () { diff --git a/wcfsetup/install/files/lib/event/user/RegistrationSpamChecking.class.php b/wcfsetup/install/files/lib/event/user/RegistrationSpamChecking.class.php new file mode 100644 index 00000000000..fd1fbf309b4 --- /dev/null +++ b/wcfsetup/install/files/lib/event/user/RegistrationSpamChecking.class.php @@ -0,0 +1,27 @@ + + * @since 6.1 + */ +final class RegistrationSpamChecking implements IInterruptableEvent +{ + use TInterruptableEvent; + + public function __construct( + public readonly string $username, + public readonly string $email, + public readonly string $ipAddress + ) { + } +} diff --git a/wcfsetup/install/files/lib/form/RegisterForm.class.php b/wcfsetup/install/files/lib/form/RegisterForm.class.php index ccce21d216a..01d2a12e207 100644 --- a/wcfsetup/install/files/lib/form/RegisterForm.class.php +++ b/wcfsetup/install/files/lib/form/RegisterForm.class.php @@ -6,11 +6,11 @@ use wcf\acp\form\UserAddForm; use wcf\action\EmailValidationAction; use wcf\action\UsernameValidationAction; -use wcf\data\blacklist\entry\BlacklistEntry; use wcf\data\object\type\ObjectType; use wcf\data\user\group\UserGroup; use wcf\data\user\User; use wcf\data\user\UserAction; +use wcf\event\user\RegistrationSpamChecking; use wcf\system\captcha\CaptchaHandler; use wcf\system\email\Email; use wcf\system\email\mime\MimePartFacade; @@ -89,6 +89,7 @@ class RegisterForm extends UserAddForm * list of fields that have matches in the blacklist * @var string[] * @since 5.2 + * @deprecated 6.1 */ public $blacklistMatches = []; @@ -108,6 +109,11 @@ class RegisterForm extends UserAddForm */ public bool $termsConfirmed = false; + /** + * @since 6.1 + */ + private RegistrationSpamChecking $spamCheckEvent; + /** * @inheritDoc */ @@ -210,17 +216,12 @@ public function validate() throw new UserInputException('registrationStartTime', []); } - if (BLACKLIST_SFS_ENABLE) { - $this->blacklistMatches = BlacklistEntry::getMatches( - $this->username, - $this->email, - UserUtil::getIpAddress() + $this->spamCheckEvent = new RegistrationSpamChecking($this->username, $this->email, UserUtil::getIpAddress()); + EventHandler::getInstance()->fire($this->spamCheckEvent); + if ($this->spamCheckEvent->defaultPrevented() && BLACKLIST_SFS_ACTION === 'block') { + throw new NamedUserException( + WCF::getLanguage()->getDynamicVariable('wcf.user.register.error.blacklistMatches') ); - if (!empty($this->blacklistMatches) && BLACKLIST_SFS_ACTION === 'block') { - throw new NamedUserException( - WCF::getLanguage()->getDynamicVariable('wcf.user.register.error.blacklistMatches') - ); - } } if (REGISTER_ENABLE_DISCLAIMER && !$this->termsConfirmed) { @@ -407,7 +408,7 @@ public function save() // generate activation code $addDefaultGroups = true; if ( - !empty($this->blacklistMatches) + $this->spamCheckEvent->defaultPrevented() || (REGISTER_ACTIVATION_METHOD & User::REGISTER_ACTIVATION_USER && !$registerVia3rdParty) || (REGISTER_ACTIVATION_METHOD & User::REGISTER_ACTIVATION_ADMIN) ) { @@ -425,7 +426,6 @@ public function save() 'username' => $this->username, 'email' => $this->email, 'password' => $this->password, - 'blacklistMatches' => (!empty($this->blacklistMatches)) ? JSON::encode($this->blacklistMatches) : '', 'signatureEnableHtml' => 1, ]), 'groups' => $this->groupIDs, @@ -442,11 +442,11 @@ public function save() WCF::getSession()->changeUser($user); // activation management - if (REGISTER_ACTIVATION_METHOD == User::REGISTER_ACTIVATION_NONE && empty($this->blacklistMatches)) { + if (REGISTER_ACTIVATION_METHOD == User::REGISTER_ACTIVATION_NONE && !$this->spamCheckEvent->defaultPrevented()) { $this->message = 'wcf.user.register.success'; UserGroupAssignmentHandler::getInstance()->checkUsers([$user->userID]); - } elseif (REGISTER_ACTIVATION_METHOD & User::REGISTER_ACTIVATION_USER && empty($this->blacklistMatches)) { + } elseif (REGISTER_ACTIVATION_METHOD & User::REGISTER_ACTIVATION_USER && !$this->spamCheckEvent->defaultPrevented()) { // registering via 3rdParty leads to instant activation if ($registerVia3rdParty) { $this->message = 'wcf.user.register.success'; @@ -463,7 +463,7 @@ public function save() $email->send(); $this->message = 'wcf.user.register.success.needActivation'; } - } elseif (REGISTER_ACTIVATION_METHOD & User::REGISTER_ACTIVATION_ADMIN || !empty($this->blacklistMatches)) { + } elseif (REGISTER_ACTIVATION_METHOD & User::REGISTER_ACTIVATION_ADMIN || $this->spamCheckEvent->defaultPrevented()) { $this->message = 'wcf.user.register.success.awaitActivation'; } diff --git a/wcfsetup/install/files/lib/system/event/listener/RegistrationSpamCheckingSfsListener.class.php b/wcfsetup/install/files/lib/system/event/listener/RegistrationSpamCheckingSfsListener.class.php new file mode 100644 index 00000000000..03110c4a0aa --- /dev/null +++ b/wcfsetup/install/files/lib/system/event/listener/RegistrationSpamCheckingSfsListener.class.php @@ -0,0 +1,28 @@ + + * @since 6.1 + */ +final class RegistrationSpamCheckingSfsListener +{ + public function __invoke(RegistrationSpamChecking $event): void + { + if (!\BLACKLIST_SFS_ENABLE) { + return; + } + + if (BlacklistEntry::getMatches($event->username, $event->email, $event->ipAddress) !== []) { + $event->preventDefault(); + } + } +} From 0db6a2a3dd22dbbb02d101f9a919c42af6071156 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Thu, 30 May 2024 17:15:39 +0200 Subject: [PATCH 2/8] Move the settings for SFS to the anti-spam category --- com.woltlab.wcf/option.xml | 16 +++++----------- wcfsetup/install/lang/de.xml | 2 -- wcfsetup/install/lang/en.xml | 2 -- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/com.woltlab.wcf/option.xml b/com.woltlab.wcf/option.xml index 2a82335759e..12baa7070f8 100644 --- a/com.woltlab.wcf/option.xml +++ b/com.woltlab.wcf/option.xml @@ -166,17 +166,6 @@ security.general 15 - - security - - - security.blacklist - 1 - - - security.blacklist - 2 - security @@ -186,6 +175,9 @@ security.antispam + + security.antispam + security @@ -1613,5 +1605,7 @@ DESC:wcf.global.sortOrder.descending - +