Merge spliteUserRemote with fixRemoteUrlInShareWith

This commit is contained in:
Joas Schilling 2015-06-18 11:46:37 +02:00
parent d38a378b8c
commit 2b7e5f841a
3 changed files with 75 additions and 74 deletions

View File

@ -218,33 +218,25 @@ class Helper extends \OC\Share\Constants {
}
/**
* Extracts the necessary remote name from a given link
* Strips away a potential file names and trailing slashes:
* - http://localhost
* - http://localhost/
* - http://localhost/index.php
* - http://localhost/index.php/s/{shareToken}
*
* Strips away a potential file name, to allow
* - user
* - user@localhost
* - user@http://localhost
* - user@http://localhost/
* - user@http://localhost/index.php
* - user@http://localhost/index.php/s/{shareToken}
* all return: http://localhost
*
* @param string $shareWith
* @return string
*/
public static function fixRemoteURLInShareWith($shareWith) {
if (strpos($shareWith, '@')) {
list($user, $remote) = explode('@', $shareWith, 2);
protected static function fixRemoteURL($remote) {
$remote = str_replace('\\', '/', $remote);
if ($fileNamePosition = strpos($remote, '/index.php')) {
$remote = substr($remote, 0, $fileNamePosition);
}
$remote = rtrim($remote, '/');
$shareWith = $user . '@' . $remote;
}
return rtrim($shareWith, '/');
return $remote;
}
/**
@ -255,10 +247,36 @@ class Helper extends \OC\Share\Constants {
* @throws InvalidFederatedCloudIdException
*/
public static function splitUserRemote($id) {
$pos = strrpos($id, '@');
if (strpos($id, '@') === false) {
throw new InvalidFederatedCloudIdException('invalid Federated Cloud ID');
}
// Find the first character that is not allowed in user names
$id = str_replace('\\', '/', $id);
$posSlash = strpos($id, '/');
$posColon = strpos($id, ':');
if ($posSlash === false && $posColon === false) {
$invalidPos = strlen($id);
} else if ($posSlash === false) {
$invalidPos = $posColon;
} else if ($posColon === false) {
$invalidPos = $posSlash;
} else {
$invalidPos = min($posSlash, $posColon);
}
// Find the last @ before $invalidPos
$pos = $lastAtPos = 0;
while ($lastAtPos !== false && $lastAtPos <= $invalidPos) {
$pos = $lastAtPos;
$lastAtPos = strpos($id, '@', $pos + 1);
}
if ($pos !== false) {
$user = substr($id, 0, $pos);
$remote = substr($id, $pos + 1);
$remote = self::fixRemoteURL($remote);
if (!empty($user) && !empty($remote)) {
return array($user, $remote);
}

View File

@ -749,7 +749,8 @@ class Share extends Constants {
$token = \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(self::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_UPPER .
\OCP\Security\ISecureRandom::CHAR_DIGITS);
$shareWith = Helper::fixRemoteURLInShareWith($shareWith);
list($user, $remote) = Helper::splitUserRemote($shareWith);
$shareWith = $user . '@' . $remote;
$shareId = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token, $itemSourceName);
$send = false;
@ -1300,8 +1301,8 @@ class Share extends Constants {
$hookParams['deletedShares'] = $deletedShares;
\OC_Hook::emit('OCP\Share', 'post_unshare', $hookParams);
if ((int)$item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) {
$urlParts = explode('@', $item['share_with'], 2);
self::sendRemoteUnshare($urlParts[1], $item['id'], $item['token']);
list(, $remote) = Helper::splitUserRemote($item['share_with']);
self::sendRemoteUnshare($remote, $item['id'], $item['token']);
}
}
@ -2430,7 +2431,7 @@ class Share extends Constants {
list($user, $remote) = Helper::splitUserRemote($shareWith);
if ($user && $remote) {
$url = rtrim($remote, '/') . self::BASE_PATH_TO_SHARE_API . '?format=' . self::RESPONSE_FORMAT;
$url = $remote . self::BASE_PATH_TO_SHARE_API . '?format=' . self::RESPONSE_FORMAT;
$local = \OC::$server->getURLGenerator()->getAbsoluteURL('/');

View File

@ -50,44 +50,31 @@ class Test_Share_Helper extends \Test\TestCase {
$this->assertSame($expected, $result);
}
public function fixRemoteURLInShareWithData() {
$userPrefix = ['test@', 'na/me@'];
public function dataTestSplitUserRemote() {
$userPrefix = ['user@name', 'username'];
$protocols = ['', 'http://', 'https://'];
$remotes = [
'localhost',
'test:foobar@localhost',
'local.host',
'dev.local.host',
'dev.local.host/path',
'dev.local.host/at@inpath',
'127.0.0.1',
'::1',
'::192.0.2.128',
'::192.0.2.128/at@inpath',
];
$testCases = [
['test', 'test'],
['na/me', 'na/me'],
['na/me/', 'na/me'],
['na/index.php', 'na/index.php'],
['http://localhost', 'http://localhost'],
['http://localhost/', 'http://localhost'],
['http://localhost/index.php', 'http://localhost/index.php'],
['http://localhost/index.php/s/token', 'http://localhost/index.php/s/token'],
['http://test:foobar@localhost', 'http://test:foobar@localhost'],
['http://test:foobar@localhost/', 'http://test:foobar@localhost'],
['http://test:foobar@localhost/index.php', 'http://test:foobar@localhost'],
['http://test:foobar@localhost/index.php/s/token', 'http://test:foobar@localhost'],
];
$testCases = [];
foreach ($userPrefix as $user) {
foreach ($remotes as $remote) {
foreach ($protocols as $protocol) {
$baseUrl = $user . $protocol . $remote;
$baseUrl = $user . '@' . $protocol . $remote;
$testCases[] = [$baseUrl, $baseUrl];
$testCases[] = [$baseUrl . '/', $baseUrl];
$testCases[] = [$baseUrl . '/index.php', $baseUrl];
$testCases[] = [$baseUrl . '/index.php/s/token', $baseUrl];
$testCases[] = [$baseUrl, $user, $protocol . $remote];
$testCases[] = [$baseUrl . '/', $user, $protocol . $remote];
$testCases[] = [$baseUrl . '/index.php', $user, $protocol . $remote];
$testCases[] = [$baseUrl . '/index.php/s/token', $user, $protocol . $remote];
}
}
}
@ -95,29 +82,33 @@ class Test_Share_Helper extends \Test\TestCase {
}
/**
* @dataProvider fixRemoteURLInShareWithData
*/
public function testFixRemoteURLInShareWith($remote, $expected) {
$this->assertSame($expected, \OC\Share\Helper::fixRemoteURLInShareWith($remote));
}
/**
* @dataProvider dataTestSplitUserRemoteSuccess
* @dataProvider dataTestSplitUserRemote
*
* @param string $id
* @param string $remote
* @param string $expectedUser
* @param string $expectedRemote
* @param string $expectedUrl
*/
public function testSplitUserRemoteSuccess($id, $expectedUser, $expectedRemote) {
list($user, $remote) = \OC\Share\Helper::splitUserRemote($id);
$this->assertSame($expectedUser, $user);
$this->assertSame($expectedRemote, $remote);
public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) {
list($remoteUser, $remoteUrl) = \OC\Share\Helper::splitUserRemote($remote);
$this->assertSame($expectedUser, $remoteUser);
$this->assertSame($expectedUrl, $remoteUrl);
}
public function dataTestSplitUserRemoteSuccess() {
public function dataTestSplitUserRemoteError() {
return array(
array('user@server', 'user', 'server'),
array('user@name@server', 'user@name', 'server')
// Invalid path
array('user@'),
// Invalid user
array('@server'),
array('us/er@server'),
array('us:er@server'),
// Invalid splitting
array('user'),
array(''),
array('us/erserver'),
array('us:erserver'),
);
}
@ -130,13 +121,4 @@ class Test_Share_Helper extends \Test\TestCase {
public function testSplitUserRemoteError($id) {
\OC\Share\Helper::splitUserRemote($id);
}
public function dataTestSplitUserRemoteError() {
return array(
array('user@'),
array('@server'),
array('user'),
array(''),
);
}
}