Skip to content

Commit 7eb9f6f

Browse files
committed
feat: Throw an error when trying to listen to no longer used signals
Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent 13583e8 commit 7eb9f6f

4 files changed

Lines changed: 100 additions & 16 deletions

File tree

lib/private/Files/Filesystem.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ class Filesystem {
150150
public const signal_delete_mount = 'delete_mount';
151151
public const signal_param_mount_type = 'mounttype';
152152
public const signal_param_users = 'users';
153+
public const signal_setup = 'setup';
154+
public const signal_pre_setup = 'preSetup';
155+
public const signal_post_init_mountpoints = 'post_initMountPoints';
153156

154157
private static ?StorageFactory $loader = null;
155158

lib/private/Files/Node/HookConnector.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,25 @@ public function __construct(
4444
}
4545

4646
public function viewToNode() {
47-
Util::connectHook('OC_Filesystem', 'write', $this, 'write');
48-
Util::connectHook('OC_Filesystem', 'post_write', $this, 'postWrite');
47+
Util::connectHook(Filesystem::CLASSNAME, Filesystem::signal_write, $this, 'write');
48+
Util::connectHook(Filesystem::CLASSNAME, Filesystem::signal_post_write, $this, 'postWrite');
4949

50-
Util::connectHook('OC_Filesystem', 'create', $this, 'create');
51-
Util::connectHook('OC_Filesystem', 'post_create', $this, 'postCreate');
50+
Util::connectHook(Filesystem::CLASSNAME, 'create', $this, 'create');
51+
Util::connectHook(Filesystem::CLASSNAME, 'post_create', $this, 'postCreate');
5252

53-
Util::connectHook('OC_Filesystem', 'delete', $this, 'delete');
54-
Util::connectHook('OC_Filesystem', 'post_delete', $this, 'postDelete');
53+
Util::connectHook(Filesystem::CLASSNAME, 'delete', $this, 'delete');
54+
Util::connectHook(Filesystem::CLASSNAME, 'post_delete', $this, 'postDelete');
5555

56-
Util::connectHook('OC_Filesystem', 'rename', $this, 'rename');
57-
Util::connectHook('OC_Filesystem', 'post_rename', $this, 'postRename');
56+
Util::connectHook(Filesystem::CLASSNAME, 'rename', $this, 'rename');
57+
Util::connectHook(Filesystem::CLASSNAME, 'post_rename', $this, 'postRename');
5858

59-
Util::connectHook('OC_Filesystem', 'copy', $this, 'copy');
60-
Util::connectHook('OC_Filesystem', 'post_copy', $this, 'postCopy');
59+
Util::connectHook(Filesystem::CLASSNAME, 'copy', $this, 'copy');
60+
Util::connectHook(Filesystem::CLASSNAME, 'post_copy', $this, 'postCopy');
6161

62-
Util::connectHook('OC_Filesystem', 'touch', $this, 'touch');
63-
Util::connectHook('OC_Filesystem', 'post_touch', $this, 'postTouch');
62+
Util::connectHook(Filesystem::CLASSNAME, 'touch', $this, 'touch');
63+
Util::connectHook(Filesystem::CLASSNAME, 'post_touch', $this, 'postTouch');
6464

65-
Util::connectHook('OC_Filesystem', 'read', $this, 'read');
65+
Util::connectHook(Filesystem::CLASSNAME, 'read', $this, 'read');
6666
}
6767

6868
public function write($arguments) {

lib/private/legacy/OC_Hook.php

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,82 @@
55
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
66
* SPDX-License-Identifier: AGPL-3.0-only
77
*/
8+
9+
use OC\Files\Filesystem;
810
use OC\ServerNotAvailableException;
911
use OCP\HintException;
12+
use OCP\IConfig;
1013
use OCP\Server;
14+
use OCP\Share;
1115
use Psr\Log\LoggerInterface;
1216

1317
class OC_Hook {
1418
public static $thrownExceptions = [];
1519

16-
private static $registered = [];
20+
private static array $registered = [];
21+
22+
private static array $allowList = [
23+
[Filesystem::CLASSNAME, Filesystem::signal_create],
24+
[Filesystem::CLASSNAME, Filesystem::signal_update],
25+
[Filesystem::CLASSNAME, Filesystem::signal_write],
26+
[Filesystem::CLASSNAME, Filesystem::signal_post_create],
27+
[Filesystem::CLASSNAME, Filesystem::signal_post_update],
28+
[Filesystem::CLASSNAME, Filesystem::signal_post_write],
29+
[Filesystem::CLASSNAME, Filesystem::signal_create],
30+
[Filesystem::CLASSNAME, Filesystem::signal_update],
31+
[Filesystem::CLASSNAME, Filesystem::signal_write],
32+
[Filesystem::CLASSNAME, Filesystem::signal_post_create],
33+
[Filesystem::CLASSNAME, Filesystem::signal_post_update],
34+
[Filesystem::CLASSNAME, Filesystem::signal_post_write],
35+
[Filesystem::CLASSNAME, Filesystem::signal_delete_mount],
36+
[Filesystem::CLASSNAME, Filesystem::signal_create_mount],
37+
[Filesystem::CLASSNAME, Filesystem::signal_setup],
38+
[Filesystem::CLASSNAME, Filesystem::signal_pre_setup],
39+
[Filesystem::CLASSNAME, Filesystem::signal_post_init_mountpoints],
40+
[Filesystem::CLASSNAME, 'umount'],
41+
['\OCA\Files_Sharing\API\Server2Server', 'preLoginNameUsedAsUserName'],
42+
[Share::class,'share_link_access'],
43+
[Share::class,'pre_unshare'],
44+
[Share::class,'post_unshare'],
45+
[Share::class,'post_unshareFromSelf'],
46+
[Share::class,'pre_shared'],
47+
[Share::class,'post_shared'],
48+
['\OC\Share','verifyExpirationDate'],
49+
[Share::class,'post_set_expiration_date'],
50+
[Share::class,'post_update_password'],
51+
[Share::class,'post_update_permissions'],
52+
['\OC\Files\Storage\Shared','fopen'],
53+
['\OC\Files\Storage\Shared','file_get_contents'],
54+
['\OC\Files\Storage\Shared','file_put_contents'],
55+
['\OCA\Files_Trashbin\Trashbin','post_moveToTrash'],
56+
['\OCA\Files_Trashbin\Trashbin','post_restore'],
57+
['\OCP\Trashbin','preDeleteAll'],
58+
['\OCP\Trashbin','deleteAll'],
59+
['\OCP\Versions','rollback'],
60+
['\OCP\Versions','preDelete'],
61+
['\OCP\Versions','delete'],
62+
['OC_User','pre_createUser'],
63+
['OC_User','post_createUser'],
64+
['OC_User','pre_deleteUser'],
65+
['OC_User','post_deleteUser'],
66+
['OC_User','pre_setPassword'],
67+
['OC_User','post_setPassword'],
68+
['OC_User','pre_login'],
69+
['OC_User','post_login'],
70+
['OC_User','logout'],
71+
['OC_User','changeUser'],
72+
['\OC\User','assignedUserId'],
73+
['\OC\User','preUnassignedUserId'],
74+
['\OC\User','postUnassignedUserId'],
75+
['\OC\Files\Cache\Scanner','scan_file'],
76+
['\OC\Files\Cache\Scanner','post_scan_file'],
77+
['Scanner','removeFromCache'],
78+
['Scanner','addToCache'],
79+
['Scanner','correctFolderSize'],
80+
['\OC\Core\LostPassword\Controller\LostController','post_passwordReset'],
81+
['\OC\Core\LostPassword\Controller\LostController','pre_passwordReset'],
82+
['\OCA\Files_Sharing\API\Server2Server','preLoginNameUsedAsUserName'],
83+
];
1784

1885
/**
1986
* connects a function to a hook
@@ -29,6 +96,20 @@ class OC_Hook {
2996
* TODO: write example
3097
*/
3198
public static function connect($signalClass, $signalName, $slotClass, $slotName) {
99+
static $debug = null;
100+
if ($debug === null) {
101+
$debug = Server::get(IConfig::class)->getSystemValueBool('debug');
102+
}
103+
if ($debug) {
104+
$found = array_find(self::$allowList, function ($allowed) use ($signalClass, $signalName) {
105+
[$allowedClass, $allowedSignal] = $allowed;
106+
return $allowedClass === $signalClass && $allowedSignal === $signalName;
107+
}) !== null;
108+
109+
if (!$found) {
110+
throw new \RuntimeException("The signal $signalClass::$signalName is no longer emitted in server. Listening to it is NOOP.");
111+
}
112+
}
32113
// If we're trying to connect to an emitting class that isn't
33114
// yet registered, register it
34115
if (!array_key_exists($signalClass, self::$registered)) {

lib/private/legacy/OC_User.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static function setupBackends() {
147147
public static function loginWithApache(IApacheBackend $backend): bool {
148148
$uid = $backend->getCurrentUserId();
149149
$run = true;
150-
OC_Hook::emit('OC_User', 'pre_login', ['run' => &$run, 'uid' => $uid, 'backend' => $backend]);
150+
Util::emitHook('OC_User', 'pre_login', ['run' => &$run, 'uid' => $uid, 'backend' => $backend]);
151151

152152
if ($uid) {
153153
if (self::getUser() !== $uid) {
@@ -196,7 +196,7 @@ public static function loginWithApache(IApacheBackend $backend): bool {
196196
// completed before we can safely create the users folder.
197197
// For example encryption needs to initialize the users keys first
198198
// before we can create the user folder with the skeleton files
199-
OC_Hook::emit(
199+
Util::emitHook(
200200
'OC_User',
201201
'post_login',
202202
[

0 commit comments

Comments
 (0)