Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,13 @@ public function getSftpUrl(?string $username = null, ?string $directory = null):
$directory = explode('/', trim($directory, '/'));
$directory = array_map(fn (string $part) => rawurlencode($part), $directory);
$directory = '/' . implode('/', $directory);

// SFTP clients only treat a URL path as a directory when it ends with a
// slash, otherwise they prompt to download the path as a file instead of
// opening it. See #2577.
if ($directory !== '/') {
$directory .= '/';
}
}

if ($directory === '/') {
Expand Down
60 changes: 60 additions & 0 deletions tests/Unit/Models/ServerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Tests\Unit\Models;

use App\Models\Node;
use App\Models\Server;
use App\Tests\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class ServerTest extends TestCase
{
#[DataProvider('sftpUrlDataProvider')]
public function test_sftp_url_points_at_the_given_directory(?string $directory, string $expectedPath): void
{
$server = $this->serverWithNode();

$this->assertSame("sftp://user.abcdefgh@node.example.com:2022{$expectedPath}", $server->getSftpUrl('user', $directory));
}

public static function sftpUrlDataProvider(): array
{
return [
'no directory' => [null, ''],
'root' => ['/', ''],
'single directory' => ['/data', '/data/'],
'directory without a leading slash' => ['data', '/data/'],
'directory with a trailing slash' => ['data/', '/data/'],
'nested directory' => ['/data/world', '/data/world/'],
'directory with encoded parts' => ['/data/my world', '/data/my%20world/'],
];
}

public function test_sftp_url_uses_the_node_sftp_alias_when_set(): void
{
$server = $this->serverWithNode(sftpAlias: 'sftp.example.com');

$this->assertSame('sftp://user.abcdefgh@sftp.example.com:2022/data/', $server->getSftpUrl('user', '/data'));
}

public function test_sftp_url_encodes_the_username(): void
{
$server = $this->serverWithNode();

$this->assertSame('sftp://user%20name.abcdefgh@node.example.com:2022/data/', $server->getSftpUrl('user name', '/data'));
}

private function serverWithNode(string $fqdn = 'node.example.com', ?string $sftpAlias = null): Server
{
$node = new Node();
$node->fqdn = $fqdn;
$node->daemon_sftp_alias = $sftpAlias;
$node->daemon_sftp = 2022;

$server = new Server();
$server->uuid_short = 'abcdefgh';
$server->setRelation('node', $node);

return $server;
}
}
Loading