Skip to content

fix(files_sharing): validate input in PublicPreviewController#getPreview - #61960

Open
kesselb wants to merge 1 commit into
masterfrom
bug-59229
Open

fix(files_sharing): validate input in PublicPreviewController#getPreview#61960
kesselb wants to merge 1 commit into
masterfrom
bug-59229

Conversation

@kesselb

@kesselb kesselb commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #59229

Summary

Local version of #59253

Return 400 Bad Request when the file parameter is empty and the shared node is a folder, instead of passing the folder itself to getPreview which triggers an internal server error.

Checklist

AI (if applicable)

  • The content of this PR was partly or fully generated using AI

@kesselb kesselb added this to the Nextcloud 35 milestone Jul 9, 2026
@kesselb kesselb self-assigned this Jul 9, 2026
@kesselb
kesselb requested review from a team and provokateurin as code owners July 9, 2026 20:10
@kesselb kesselb added the bug label Jul 9, 2026
@kesselb
kesselb requested review from ArtificialOwl and removed request for a team July 9, 2026 20:10
@kesselb kesselb added the 3. to review Waiting for reviews label Jul 9, 2026
@kesselb
kesselb requested review from come-nc and leftybournes July 9, 2026 20:10
@kesselb kesselb added 2. developing Work in progress and removed 3. to review Waiting for reviews labels Jul 9, 2026
@kesselb
kesselb force-pushed the bug-59229 branch 3 times, most recently from 436584a to 7c960b3 Compare August 4, 2026 13:49
@kesselb kesselb added 3. to review Waiting for reviews and removed 2. developing Work in progress labels Aug 4, 2026
@kesselb
kesselb requested review from miaulalala and a lite review from Copilot August 5, 2026 08:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Updates public preview input handling to return a 400 Bad Request for invalid folder-share file query values (e.g., empty or pointing to a folder), avoiding an internal server error and clarifying API behavior.

Changes:

  • Validate file for folder shares (reject empty values and folder targets) and add Forbidden handling for permission failures.
  • Add/extend unit tests covering invalid folder-share file inputs and MIME-icon fallback redirect behavior.
  • Update OpenAPI summaries/descriptions for the public preview endpoint to reflect folder vs single-file share behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
openapi.json Clarifies endpoint summary/description and file query parameter semantics.
apps/files_sharing/openapi.json Mirrors OpenAPI clarification within the app spec.
apps/files_sharing/lib/Controller/PublicPreviewController.php Adds input validation for folder shares and refines exception handling + MIME fallback gating.
apps/files_sharing/tests/Controller/PublicPreviewControllerTest.php Updates calls for new signature and adds tests for bad-request cases + MIME fallback redirect.
Suppressed comments (2)

openapi.json:1

  • The OpenAPI schema still sets "default": "" for file, but the controller now returns 400 for folder shares when file is empty. This default is misleading for API consumers and tooling that auto-populates query params. Consider removing the default entirely and/or adding an "example" like "path/to/file.jpg" to reflect valid usage.
    apps/files_sharing/openapi.json:1508
  • Same issue as the root OpenAPI spec: "default": "" conflicts with the new behavior (400 on empty file for folder shares). Remove the default and/or provide an example to prevent clients from sending invalid requests by default.
                    {
                        "name": "file",
                        "in": "query",
                        "description": "Relative path to a file inside a shared folder; ignored for single-file shares",
                        "schema": {
                            "type": "string",
                            "default": ""
                        }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +129 to +133
$previewFile = null;

try {
$node = $share->getNode();
if ($node instanceof Folder) {
$file = $node->get($file);
$shareNode = $share->getNode();
if ($shareNode instanceof Folder) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}

$previewFile = $shareNode->get($file);
Comment on lines +143 to +146
$previewFile = $shareNode;
}

$f = $this->previewManager->getPreview($file, $x, $y, !$a);
$response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
$preview = $this->previewManager->getPreview($previewFile, $x, $y, !$a);
Comment on lines +155 to 161
} catch (NotFoundException) {
// If a preview could not be generated for a resolved file, we can redirect to the mime icon if any
if ($mimeFallback && $previewFile instanceof File) {
if ($url = $this->mimeIconProvider->getMimeIconUrl($previewFile->getMimeType())) {
return new RedirectResponse($url);
}
}
Return 400 Bad Request when the file parameter is empty and the shared
node is a folder, instead of passing the folder itself to getPreview
which triggers an internal server error.

Also rename the local variable to $fileNode to prevent the catch block
from calling getMimeType() on the original string parameter when
get() throws NotFoundException.

Fixes #59229

Assisted-by: ClaudeCode:claude-opus-4-6
Assisted-by: ClaudeCode:claude-opus-5

Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>

# Conflicts:
#	apps/files_sharing/lib/Controller/PublicPreviewController.php
@kesselb

kesselb commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

Rebased after #56196.

@kesselb

kesselb commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

/backport to stable34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Improper input validation in PublicPreviewController triggers internal server error

4 participants