Skip to content

Commit 88176be

Browse files
authored
Merge pull request #2458 from nextcloud/backport/2431/stable33
[stable33] fix: only write activites for actual public uploads
2 parents 8142f82 + ec6473e commit 88176be

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed

lib/CurrentUser.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,31 @@ public function getCloudId() {
102102
return $this->cloudId;
103103
}
104104

105+
/**
106+
* Check if the current request is via a public share link
107+
*/
108+
public function isPublicShareToken(): bool {
109+
/** @psalm-suppress NoInterfaceProperties */
110+
if (!empty($this->request->server['PHP_AUTH_USER'])) {
111+
$token = $this->request->server['PHP_AUTH_USER'];
112+
try {
113+
$share = $this->shareManager->getShareByToken($token);
114+
return $share->getShareType() === IShare::TYPE_LINK
115+
|| $share->getShareType() === IShare::TYPE_EMAIL;
116+
} catch (ShareNotFound $e) {
117+
// No share found for this token
118+
}
119+
}
120+
121+
return false;
122+
}
123+
105124
/**
106125
* Get the cloud ID from the sharing token
107126
* @return string|null
108127
*/
109128
protected function getCloudIDFromToken() {
129+
/** @psalm-suppress NoInterfaceProperties */
110130
if (!empty($this->request->server['PHP_AUTH_USER'])) {
111131
$token = $this->request->server['PHP_AUTH_USER'];
112132
/**

lib/FilesHooks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function fileCreate($path) {
8282
return;
8383
}
8484

85-
if ($this->currentUser->getUserIdentifier() !== '') {
85+
if ($this->currentUser->getUserIdentifier() !== '' || !$this->currentUser->isPublicShareToken()) {
8686
$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, 'created_self', 'created_by');
8787
} else {
8888
$this->addNotificationsForFileAction($path, Files_Sharing::TYPE_PUBLIC_UPLOAD, '', 'created_public');

tests/FilesHooksTest.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,42 @@ protected function getUserMock(string $uid): IUser {
168168

169169
public static function dataFileCreate(): array {
170170
return [
171-
['user', 'created_self', 'created_by', Files::TYPE_SHARE_CREATED],
172-
['', '', 'created_public', Files_Sharing::TYPE_PUBLIC_UPLOAD],
171+
['user', false, 'created_self', 'created_by', Files::TYPE_SHARE_CREATED],
172+
['', true, '', 'created_public', Files_Sharing::TYPE_PUBLIC_UPLOAD],
173+
['', false, 'created_self', 'created_by', Files::TYPE_SHARE_CREATED],
173174
];
174175
}
175176

176177
#[DataProvider('dataFileCreate')]
177-
public function testFileCreate(string $currentUser, string $selfSubject, string $othersSubject, string $type): void {
178-
$filesHooks = $this->getFilesHooks([
179-
'addNotificationsForFileAction',
180-
], $currentUser);
178+
public function testFileCreate(string $currentUser, bool $isPublicShare, string $selfSubject, string $othersSubject, string $type): void {
179+
$currentUserMock = $this->createMock(CurrentUser::class);
180+
$currentUserMock->method('getUID')->willReturn($currentUser);
181+
$currentUserMock->method('getUserIdentifier')->willReturn($currentUser);
182+
$currentUserMock->method('isPublicShareToken')->willReturn($isPublicShare);
183+
184+
$logger = $this->createMock(LoggerInterface::class);
185+
186+
$filesHooks = $this->getMockBuilder(FilesHooks::class)
187+
->setConstructorArgs([
188+
$this->activityManager,
189+
$this->data,
190+
$this->settings,
191+
$this->groupManager,
192+
$this->view,
193+
$this->rootFolder,
194+
$this->shareHelper,
195+
Server::get(IDBConnection::class),
196+
$this->urlGenerator,
197+
$logger,
198+
$currentUserMock,
199+
$this->userMountCache,
200+
$this->config,
201+
$this->notificationGenerator,
202+
$this->tagManager,
203+
$this->teamManager,
204+
])
205+
->onlyMethods(['addNotificationsForFileAction'])
206+
->getMock();
181207

182208
$filesHooks->expects($this->once())
183209
->method('addNotificationsForFileAction')

tests/psalm-baseline.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="6.12.1@e71404b0465be25cf7f8a631b298c01c5ddd864f">
2+
<files psalm-version="6.15.1@28dc127af1b5aecd52314f6f645bafc10d0e11f9">
33
<file src="lib/BackgroundJob/RemoteActivity.php">
44
<UndefinedClass>
55
<code><![CDATA[ClientException]]></code>
66
</UndefinedClass>
77
</file>
8-
<file src="lib/CurrentUser.php">
9-
<NoInterfaceProperties>
10-
<code><![CDATA[$this->request->server]]></code>
11-
</NoInterfaceProperties>
12-
</file>
138
<file src="lib/Data.php">
149
<UndefinedDocblockClass>
1510
<code><![CDATA[$favoriteFilter->filterFavorites($query);]]></code>

0 commit comments

Comments
 (0)