Skip to content

Commit f708a4c

Browse files
committed
fix(CurrentUser): Properly get share token from non-legacy public webdav
requests Signed-off-by: Louis Chmn <louis@chmn.me> Assisted-by: ClaudeCode:claude-opus-5 Signed-off-by: Louis Chmn <louis@chmn.me>
1 parent f2e3142 commit f708a4c

1 file changed

Lines changed: 44 additions & 30 deletions

File tree

lib/CurrentUser.php

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,42 +79,56 @@ public function getCloudId(): string|null {
7979
* Check if the current request is via a public share link
8080
*/
8181
public function isPublicShareToken(): bool {
82-
/** @psalm-suppress NoInterfaceProperties */
83-
if (!empty($this->request->server['PHP_AUTH_USER'])) {
84-
$token = $this->request->server['PHP_AUTH_USER'];
85-
try {
86-
$share = $this->shareManager->getShareByToken($token);
87-
return $share->getShareType() === IShare::TYPE_LINK
88-
|| $share->getShareType() === IShare::TYPE_EMAIL;
89-
} catch (ShareNotFound $e) {
90-
// No share found for this token
91-
}
92-
}
93-
94-
return false;
82+
return $this->getPublicShare() !== null;
9583
}
9684

9785
/**
9886
* Get the cloud ID from the sharing token
9987
*/
100-
protected function getCloudIDFromToken() {
101-
/** @psalm-suppress NoInterfaceProperties */
102-
if (!empty($this->request->server['PHP_AUTH_USER'])) {
103-
$token = $this->request->server['PHP_AUTH_USER'];
104-
/**
105-
* Until https://github.com/nextcloud/server/pull/26681 is merged
106-
* @psalm-suppress InvalidCatch
107-
*/
108-
try {
109-
$share = $this->shareManager->getShareByToken($token);
110-
if ($share->getShareType() === IShare::TYPE_REMOTE) {
111-
return $share->getSharedWith();
112-
}
113-
} catch (ShareNotFound $e) {
114-
// No share, use the fallback
115-
}
88+
protected function getCloudIDFromToken(): string|null {
89+
$share = $this->getPublicShare();
90+
91+
if ($share === null || $share->getShareType() !== IShare::TYPE_REMOTE) {
92+
return null;
11693
}
11794

118-
return null;
95+
return $share->getSharedWith();
96+
}
97+
98+
protected function getPublicShare(): ?IShare {
99+
if (basename($this->request->getScriptName()) !== "public.php") {
100+
return null;
101+
}
102+
103+
$token = $this->getShareToken();
104+
if ($token === null) {
105+
return null;
106+
}
107+
108+
try {
109+
return $this->shareManager->getShareByToken($token);
110+
} catch (ShareNotFound $e) {
111+
return null;
112+
}
113+
}
114+
115+
protected function getShareToken(): ?string {
116+
// The legacy public endpoint receive the share token in the HTTP basic auth header.
117+
$authUser = (string) ($this->request->server["PHP_AUTH_USER"] ?? "");
118+
if ($authUser !== "") {
119+
return $authUser;
120+
}
121+
122+
// The current public endpoint receives the share token in the path.
123+
// Copied from apps/dav/lib/Connector/Sabre/PublicAuth::getToken()
124+
$path = $this->request->getPathInfo() ?: '';
125+
// ['', 'dav', 'files', 'token']
126+
$splittedPath = explode('/', $path);
127+
128+
if (count($splittedPath) < 4 || $splittedPath[3] === '') {
129+
return null;
130+
}
131+
132+
return $splittedPath[3];
119133
}
120134
}

0 commit comments

Comments
 (0)