limit incoming/outgoing requests

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/550/head
Maxence Lange 2019-05-30 14:04:15 -01:00
rodzic 3afc2e9bfd
commit b53e5209ab
8 zmienionych plików z 79 dodań i 16 usunięć

Wyświetl plik

@ -40,6 +40,7 @@ use OCA\Social\Exceptions\SignatureIsGoneException;
use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\ItemUnknownException;
use OCA\Social\Exceptions\UrlCloudException; use OCA\Social\Exceptions\UrlCloudException;
use OCA\Social\Service\CacheActorService; use OCA\Social\Service\CacheActorService;
use OCA\Social\Service\FediverseService;
use OCA\Social\Service\FollowService; use OCA\Social\Service\FollowService;
use OCA\Social\Service\ImportService; use OCA\Social\Service\ImportService;
use OCA\Social\Service\MiscService; use OCA\Social\Service\MiscService;
@ -61,6 +62,9 @@ class ActivityPubController extends Controller {
/** @var SocialPubController */ /** @var SocialPubController */
private $socialPubController; private $socialPubController;
/** @var FediverseService */
private $fediverseService;
/** @var CacheActorService */ /** @var CacheActorService */
private $cacheActorService; private $cacheActorService;
@ -85,6 +89,7 @@ class ActivityPubController extends Controller {
* *
* @param IRequest $request * @param IRequest $request
* @param SocialPubController $socialPubController * @param SocialPubController $socialPubController
* @param FediverseService $fediverseService
* @param CacheActorService $cacheActorService * @param CacheActorService $cacheActorService
* @param SignatureService $signatureService * @param SignatureService $signatureService
* @param StreamQueueService $streamQueueService * @param StreamQueueService $streamQueueService
@ -94,13 +99,14 @@ class ActivityPubController extends Controller {
*/ */
public function __construct( public function __construct(
IRequest $request, SocialPubController $socialPubController, IRequest $request, SocialPubController $socialPubController,
CacheActorService $cacheActorService, SignatureService $signatureService, FediverseService $fediverseService, CacheActorService $cacheActorService,
StreamQueueService $streamQueueService, ImportService $importService, SignatureService $signatureService, StreamQueueService $streamQueueService,
FollowService $followService, MiscService $miscService ImportService $importService, FollowService $followService, MiscService $miscService
) { ) {
parent::__construct(Application::APP_NAME, $request); parent::__construct(Application::APP_NAME, $request);
$this->socialPubController = $socialPubController; $this->socialPubController = $socialPubController;
$this->fediverseService = $fediverseService;
$this->cacheActorService = $cacheActorService; $this->cacheActorService = $cacheActorService;
$this->signatureService = $signatureService; $this->signatureService = $signatureService;
$this->streamQueueService = $streamQueueService; $this->streamQueueService = $streamQueueService;
@ -178,6 +184,7 @@ class ActivityPubController extends Controller {
$requestTime = 0; $requestTime = 0;
$origin = $this->signatureService->checkRequest($this->request, $requestTime); $origin = $this->signatureService->checkRequest($this->request, $requestTime);
$this->fediverseService->authorized($origin);
$activity = $this->importService->importFromJson($body); $activity = $this->importService->importFromJson($body);
if (!$this->signatureService->checkObject($activity)) { if (!$this->signatureService->checkObject($activity)) {
@ -221,6 +228,7 @@ class ActivityPubController extends Controller {
$requestTime = 0; $requestTime = 0;
$origin = $this->signatureService->checkRequest($this->request, $requestTime); $origin = $this->signatureService->checkRequest($this->request, $requestTime);
$this->fediverseService->authorized($origin);
// TODO - check the recipient <-> username // TODO - check the recipient <-> username
// $actor = $this->actorService->getActor($username); // $actor = $this->actorService->getActor($username);

Wyświetl plik

@ -48,6 +48,7 @@ use OCA\Social\Exceptions\RequestResultNotJsonException;
use OCA\Social\Exceptions\RequestResultSizeException; use OCA\Social\Exceptions\RequestResultSizeException;
use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\RequestServerException;
use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\UnauthorizedFediverseException;
use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Activity\Create; use OCA\Social\Model\ActivityPub\Activity\Create;
use OCA\Social\Model\ActivityPub\Activity\Delete; use OCA\Social\Model\ActivityPub\Activity\Delete;
@ -284,6 +285,8 @@ class ActivityService {
$this->signatureService->signRequest($request, $queue); $this->signatureService->signRequest($request, $queue);
$this->curlService->request($request); $this->curlService->request($request);
$this->requestQueueService->endRequest($queue, true); $this->requestQueueService->endRequest($queue, true);
} catch (UnauthorizedFediverseException $e) {
$this->requestQueueService->endRequest($queue, true);
} catch (RequestResultNotJsonException $e) { } catch (RequestResultNotJsonException $e) {
$this->requestQueueService->endRequest($queue, true); $this->requestQueueService->endRequest($queue, true);
} catch (ActorDoesNotExistException $e) { } catch (ActorDoesNotExistException $e) {

Wyświetl plik

@ -47,6 +47,7 @@ use OCA\Social\Exceptions\RequestResultSizeException;
use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\RequestServerException;
use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\ItemUnknownException;
use OCA\Social\Exceptions\UnauthorizedFediverseException;
use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Actor\Person;
@ -118,6 +119,7 @@ class CacheActorService {
* @throws SocialAppConfigException * @throws SocialAppConfigException
* @throws ItemUnknownException * @throws ItemUnknownException
* @throws RequestResultNotJsonException * @throws RequestResultNotJsonException
* @throws UnauthorizedFediverseException
*/ */
public function getFromId(string $id, bool $refresh = false): Person { public function getFromId(string $id, bool $refresh = false): Person {

Wyświetl plik

@ -43,6 +43,8 @@ use OCA\Social\Exceptions\RequestNetworkException;
use OCA\Social\Exceptions\RequestResultNotJsonException; use OCA\Social\Exceptions\RequestResultNotJsonException;
use OCA\Social\Exceptions\RequestResultSizeException; use OCA\Social\Exceptions\RequestResultSizeException;
use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\RequestServerException;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\UnauthorizedFediverseException;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
@ -96,13 +98,15 @@ class CacheDocumentService {
* @return string * @return string
* @throws CacheContentMimeTypeException * @throws CacheContentMimeTypeException
* @throws MalformedArrayException * @throws MalformedArrayException
* @throws NotFoundException
* @throws NotPermittedException * @throws NotPermittedException
* @throws RequestContentException * @throws RequestContentException
* @throws RequestNetworkException * @throws RequestNetworkException
* @throws RequestServerException
* @throws RequestResultSizeException
* @throws RequestResultNotJsonException * @throws RequestResultNotJsonException
* @throws NotFoundException * @throws RequestResultSizeException
* @throws RequestServerException
* @throws SocialAppConfigException
* @throws UnauthorizedFediverseException
*/ */
public function saveRemoteFileToCache(string $url, &$mime = '') { public function saveRemoteFileToCache(string $url, &$mime = '') {
@ -190,9 +194,11 @@ class CacheDocumentService {
* @throws MalformedArrayException * @throws MalformedArrayException
* @throws RequestContentException * @throws RequestContentException
* @throws RequestNetworkException * @throws RequestNetworkException
* @throws RequestServerException
* @throws RequestResultSizeException
* @throws RequestResultNotJsonException * @throws RequestResultNotJsonException
* @throws RequestResultSizeException
* @throws RequestServerException
* @throws SocialAppConfigException
* @throws UnauthorizedFediverseException
*/ */
public function retrieveContent(string $url) { public function retrieveContent(string $url) {
$url = parse_url($url); $url = parse_url($url);

Wyświetl plik

@ -48,6 +48,7 @@ use OCA\Social\Exceptions\RequestResultSizeException;
use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\RequestServerException;
use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\ItemUnknownException;
use OCA\Social\Exceptions\UnauthorizedFediverseException;
use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Actor\Person;
class CurlService { class CurlService {
@ -64,6 +65,9 @@ class CurlService {
/** @var ConfigService */ /** @var ConfigService */
private $configService; private $configService;
/** @var FediverseService */
private $fediverseService;
/** @var MiscService */ /** @var MiscService */
private $miscService; private $miscService;
@ -79,10 +83,14 @@ class CurlService {
* CurlService constructor. * CurlService constructor.
* *
* @param ConfigService $configService * @param ConfigService $configService
* @param FediverseService $fediverseService
* @param MiscService $miscService * @param MiscService $miscService
*/ */
public function __construct(ConfigService $configService, MiscService $miscService) { public function __construct(
ConfigService $configService, FediverseService $fediverseService, MiscService $miscService
) {
$this->configService = $configService; $this->configService = $configService;
$this->fediverseService = $fediverseService;
$this->miscService = $miscService; $this->miscService = $miscService;
} }
@ -94,9 +102,11 @@ class CurlService {
* @throws InvalidResourceException * @throws InvalidResourceException
* @throws RequestContentException * @throws RequestContentException
* @throws RequestNetworkException * @throws RequestNetworkException
* @throws RequestResultNotJsonException
* @throws RequestResultSizeException * @throws RequestResultSizeException
* @throws RequestServerException * @throws RequestServerException
* @throws RequestResultNotJsonException * @throws SocialAppConfigException
* @throws UnauthorizedFediverseException
*/ */
public function webfingerAccount(string $account): array { public function webfingerAccount(string $account): array {
$account = $this->withoutBeginAt($account); $account = $this->withoutBeginAt($account);
@ -144,6 +154,7 @@ class CurlService {
* @throws SocialAppConfigException * @throws SocialAppConfigException
* @throws ItemUnknownException * @throws ItemUnknownException
* @throws RequestResultNotJsonException * @throws RequestResultNotJsonException
* @throws UnauthorizedFediverseException
*/ */
public function retrieveAccount(string $account): Person { public function retrieveAccount(string $account): Person {
$result = $this->webfingerAccount($account); $result = $this->webfingerAccount($account);
@ -178,9 +189,11 @@ class CurlService {
* @throws MalformedArrayException * @throws MalformedArrayException
* @throws RequestContentException * @throws RequestContentException
* @throws RequestNetworkException * @throws RequestNetworkException
* @throws RequestServerException
* @throws RequestResultSizeException
* @throws RequestResultNotJsonException * @throws RequestResultNotJsonException
* @throws RequestResultSizeException
* @throws RequestServerException
* @throws SocialAppConfigException
* @throws UnauthorizedFediverseException
*/ */
public function retrieveObject($id): array { public function retrieveObject($id): array {
@ -205,11 +218,14 @@ class CurlService {
* @return mixed * @return mixed
* @throws RequestContentException * @throws RequestContentException
* @throws RequestNetworkException * @throws RequestNetworkException
* @throws RequestResultNotJsonException
* @throws RequestResultSizeException * @throws RequestResultSizeException
* @throws RequestServerException * @throws RequestServerException
* @throws RequestResultNotJsonException * @throws SocialAppConfigException
* @throws UnauthorizedFediverseException
*/ */
public function request(Request $request) { public function request(Request $request) {
$this->fediverseService->authorized($request->getAddress());
$this->maxDownloadSizeReached = false; $this->maxDownloadSizeReached = false;
$curl = $this->initRequest($request); $curl = $this->initRequest($request);

Wyświetl plik

@ -46,6 +46,7 @@ use OCA\Social\Exceptions\RequestResultNotJsonException;
use OCA\Social\Exceptions\RequestResultSizeException; use OCA\Social\Exceptions\RequestResultSizeException;
use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\RequestServerException;
use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\UnauthorizedFediverseException;
use OCA\Social\Exceptions\UrlCloudException; use OCA\Social\Exceptions\UrlCloudException;
use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Actor\Person;
use OCA\Social\Model\ActivityPub\Object\Document; use OCA\Social\Model\ActivityPub\Object\Document;
@ -117,6 +118,7 @@ class DocumentService {
* @throws CacheDocumentDoesNotExistException * @throws CacheDocumentDoesNotExistException
* @throws MalformedArrayException * @throws MalformedArrayException
* @throws RequestResultNotJsonException * @throws RequestResultNotJsonException
* @throws SocialAppConfigException
*/ */
public function cacheRemoteDocument(string $id, bool $public = false) { public function cacheRemoteDocument(string $id, bool $public = false) {
$document = $this->cacheDocumentsRequest->getById($id, $public); $document = $this->cacheDocumentsRequest->getById($id, $public);
@ -169,6 +171,8 @@ class DocumentService {
$this->cacheDocumentsRequest->endCaching($document); $this->cacheDocumentsRequest->endCaching($document);
} catch (RequestContentException $e) { } catch (RequestContentException $e) {
$this->cacheDocumentsRequest->deleteById($id); $this->cacheDocumentsRequest->deleteById($id);
} catch (UnauthorizedFediverseException $e) {
$this->cacheDocumentsRequest->deleteById($id);
} catch (RequestNetworkException $e) { } catch (RequestNetworkException $e) {
$this->cacheDocumentsRequest->endCaching($document); $this->cacheDocumentsRequest->endCaching($document);
} catch (RequestServerException $e) { } catch (RequestServerException $e) {
@ -189,6 +193,7 @@ class DocumentService {
* @throws CacheDocumentDoesNotExistException * @throws CacheDocumentDoesNotExistException
* @throws MalformedArrayException * @throws MalformedArrayException
* @throws RequestResultNotJsonException * @throws RequestResultNotJsonException
* @throws SocialAppConfigException
*/ */
public function getFromCache(string $id, bool $public = false) { public function getFromCache(string $id, bool $public = false) {
$document = $this->cacheRemoteDocument($id, $public); $document = $this->cacheRemoteDocument($id, $public);

Wyświetl plik

@ -31,6 +31,7 @@ declare(strict_types=1);
namespace OCA\Social\Service; namespace OCA\Social\Service;
use Exception; use Exception;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\UnauthorizedFediverseException; use OCA\Social\Exceptions\UnauthorizedFediverseException;
@ -68,6 +69,7 @@ class FediverseService {
* *
* @return bool * @return bool
* @throws UnauthorizedFediverseException * @throws UnauthorizedFediverseException
* @throws SocialAppConfigException
*/ */
public function authorized(string $address): bool { public function authorized(string $address): bool {
if ($this->getAccessType() === if ($this->getAccessType() ===
@ -78,11 +80,11 @@ class FediverseService {
if ($this->getAccessType() === if ($this->getAccessType() ===
$this->configService->accessTypeList['WHITELIST'] $this->configService->accessTypeList['WHITELIST']
&& $this->isListed($address)) { && ($this->isListed($address) || $this->isLocal($address))) {
return true; return true;
} }
throw new UnauthorizedFediverseException(); throw new UnauthorizedFediverseException('Unauthorized Fediverse');
} }
@ -95,7 +97,7 @@ class FediverseService {
return; return;
} }
throw new UnauthorizedFediverseException(); throw new UnauthorizedFediverseException('Jailed Fediverse');
} }
@ -122,6 +124,19 @@ class FediverseService {
} }
/**
* @param string $address
*
* @return bool
* @throws SocialAppConfigException
*/
public function isLocal(string $address): bool {
$local = $this->configService->getCloudAddress(true);
return ($local === $address);
}
/** /**
* @return array * @return array
*/ */

Wyświetl plik

@ -48,6 +48,7 @@ use OCA\Social\Exceptions\RequestResultNotJsonException;
use OCA\Social\Exceptions\RequestResultSizeException; use OCA\Social\Exceptions\RequestResultSizeException;
use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\RequestServerException;
use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\UnauthorizedFediverseException;
use OCA\Social\Model\ActivityPub\Object\Note; use OCA\Social\Model\ActivityPub\Object\Note;
use OCA\Social\Model\ActivityPub\Stream; use OCA\Social\Model\ActivityPub\Stream;
use OCA\Social\Model\StreamQueue; use OCA\Social\Model\StreamQueue;
@ -259,6 +260,12 @@ class StreamQueueService {
. $e->getMessage(), 1 . $e->getMessage(), 1
); );
$cache->removeItem($item->getUrl()); $cache->removeItem($item->getUrl());
} catch (UnauthorizedFediverseException $e) {
$this->miscService->log(
'Error caching stream: ' . json_encode($item) . ' ' . get_class($e) . ' '
. $e->getMessage(), 1
);
$cache->removeItem($item->getUrl());
} catch (RequestNetworkException $e) { } catch (RequestNetworkException $e) {
$this->miscService->log( $this->miscService->log(
'Error caching stream: ' . json_encode($item) . ' ' . get_class($e) . ' ' 'Error caching stream: ' . json_encode($item) . ' ' . get_class($e) . ' '
@ -299,6 +306,7 @@ class StreamQueueService {
* @throws RequestResultSizeException * @throws RequestResultSizeException
* @throws RequestServerException * @throws RequestServerException
* @throws SocialAppConfigException * @throws SocialAppConfigException
* @throws UnauthorizedFediverseException
*/ */
private function cacheItem(CacheItem &$item) { private function cacheItem(CacheItem &$item) {