-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
feat(files_sharing): configure public share link base URL #62146
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
Open
camr0
wants to merge
8
commits into
nextcloud:master
Choose a base branch
from
camr0:public-share-base-url
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+442
−61
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8647de1
feat(sharing): configure public share link base URL
camr0 aa26dcc
test(files_sharing): cover public share link copy URLs
camr0 8792966
test(files_sharing): fix public share link test style
camr0 973d683
fix(files_sharing): generate file request share URLs
camr0 26a0cb4
fix(files_sharing): keep router-generated path for public share URLs
camr0 5e60a60
fix(sharebymail): use configured public base URL in share emails
camr0 7860e5d
chore(files_sharing): compile assets
camr0 eded848
fix(sharebymail): gracefully handle provider resolution failure
camr0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Files_Sharing; | ||
|
|
||
| use OC\Core\AppInfo\ConfigLexicon; | ||
| use OCP\IAppConfig; | ||
| use OCP\IURLGenerator; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| class PublicShareUrlGenerator { | ||
| private bool $invalidBaseUrlLogged = false; | ||
|
|
||
| public function __construct( | ||
| private IAppConfig $appConfig, | ||
| private IURLGenerator $urlGenerator, | ||
| private LoggerInterface $logger, | ||
| ) { | ||
| } | ||
|
|
||
| public function getUrl(string $token): string { | ||
| $baseUrl = $this->getBaseUrl(); | ||
| if ($baseUrl === null) { | ||
| return $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]); | ||
| } | ||
|
|
||
| // Keep the router as the single source of truth for the share link path, | ||
| // including a potential '/index.php' front controller prefix on instances | ||
| // without pretty URLs, and only swap the instance's webroot for the | ||
| // configured base URL. | ||
| $path = $this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', ['token' => $token]); | ||
| $webroot = $this->urlGenerator->getWebroot(); | ||
| if ($webroot !== '' && str_starts_with($path, $webroot)) { | ||
| $path = substr($path, strlen($webroot)); | ||
| } | ||
|
|
||
| return $baseUrl . $path; | ||
| } | ||
|
|
||
| private function getBaseUrl(): ?string { | ||
| $baseUrl = trim($this->appConfig->getValueString('core', ConfigLexicon::SHARE_LINK_BASE_URL, '')); | ||
| if ($baseUrl === '') { | ||
| return null; | ||
| } | ||
|
|
||
| if (filter_var($baseUrl, FILTER_VALIDATE_URL) !== false) { | ||
| $parts = parse_url($baseUrl); | ||
| if (is_array($parts) | ||
| && isset($parts['host']) | ||
| && in_array($parts['scheme'] ?? '', ['http', 'https'], true) | ||
| && !isset($parts['user']) | ||
| && !isset($parts['pass']) | ||
| && !isset($parts['query']) | ||
| && !isset($parts['fragment'])) { | ||
| return rtrim($baseUrl, '/'); | ||
| } | ||
| } | ||
|
|
||
| // Do not log the configured value itself, it may contain credentials | ||
| if (!$this->invalidBaseUrlLogged) { | ||
| $this->invalidBaseUrlLogged = true; | ||
| $this->logger->warning( | ||
| 'Ignoring invalid {configKey} app config value, falling back to the default base URL for public share links. Expected an absolute http(s) URL without credentials, query or fragment.', | ||
| ['app' => 'files_sharing', 'configKey' => ConfigLexicon::SHARE_LINK_BASE_URL], | ||
| ); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
apps/files_sharing/src/components/PublicShareLinkComponents.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| import type * as Router from '@nextcloud/router' | ||
|
|
||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import NewFileRequestDialogFinish from './NewFileRequestDialog/NewFileRequestDialogFinish.vue' | ||
| import SharingEntryLink from './SharingEntryLink.vue' | ||
|
|
||
| const generateUrl = vi.hoisted(() => vi.fn()) | ||
| const getBaseUrl = vi.hoisted(() => vi.fn()) | ||
| vi.mock('@nextcloud/router', async (importOriginal) => ({ | ||
| ...await importOriginal<typeof Router>(), | ||
| generateUrl, | ||
| getBaseUrl, | ||
| })) | ||
|
|
||
| const components = [ | ||
| ['SharingEntryLink', SharingEntryLink], | ||
| ['NewFileRequestDialogFinish', NewFileRequestDialogFinish], | ||
| ] as const | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| describe.each(components)('%s public share link', (_name, component) => { | ||
| it('uses the complete URL returned by the server', () => { | ||
| generateUrl.mockReturnValue('http://nextcloud.local/s/token') | ||
|
|
||
| const shareLink = component.computed.shareLink.call({ | ||
| share: { | ||
| token: 'token', | ||
| url: 'https://public-gateway.example/s/token', | ||
| }, | ||
| }) | ||
|
|
||
| expect(shareLink).toBe('https://public-gateway.example/s/token') | ||
| expect(generateUrl).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('falls back to the existing URL generation when the server URL is absent', () => { | ||
| getBaseUrl.mockReturnValue('/nextcloud') | ||
| generateUrl.mockReturnValue('http://nextcloud.local/nextcloud/s/token') | ||
|
|
||
| const shareLink = component.computed.shareLink.call({ | ||
| share: { token: 'token' }, | ||
| }) | ||
|
|
||
| expect(shareLink).toBe('http://nextcloud.local/nextcloud/s/token') | ||
| expect(generateUrl).toHaveBeenCalledWith('/s/{token}', { token: 'token' }, { baseURL: '/nextcloud' }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| import { describe, expect, it } from 'vitest' | ||
| import Share from './Share.ts' | ||
|
|
||
| describe('Share', () => { | ||
| it('exposes the complete public link URL returned by the server', () => { | ||
| const share = new Share({ | ||
| id: 1, | ||
| share_type: 3, | ||
| url: 'https://public-gateway.example/prefix/s/token', | ||
| }) | ||
|
|
||
| expect(share.url).toBe('https://public-gateway.example/prefix/s/token') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it in RoutingWeirdness?