Skip to content

Commit

Permalink
use https & http when unsure
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Dec 2, 2020
1 parent 50742f1 commit 7bef52e
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 33 deletions.
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions lib/Command/CirclesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->configService->setAppValue(ConfigService::TEST_NC_BASE, $input->getOption('url'));

if (!$this->testRequest($output, 'GET', 'core.CSRFToken.index')) {
$this->configService->setAppValue(ConfigService::TEST_NC_BASE, '');

return 0;
}

if (!$this->testRequest(
$output, 'POST', 'circles.GlobalScale.asyncBroadcast',
['token' => 'test-dummy-token']
)) {
$this->configService->setAppValue(ConfigService::TEST_NC_BASE, '');

return 0;
}

Expand Down Expand Up @@ -182,6 +186,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function testRequest(OutputInterface $o, string $type, string $route, array $args = []): bool {
$request = new NC19Request('', Request::type($type));
$this->configService->configureRequest($request, $route, $args);
$request->setFollowLocation(false);

$o->write('- ' . $type . ' request on ' . $request->getCompleteUrl() . ': ');
$this->doRequest($request);
Expand Down
1 change: 0 additions & 1 deletion lib/Command/MembersCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ private function findUserFromLookup(string $search, string &$instance = ''): str

$request = new NC19Request(ConfigService::GS_LOOKUP_USERS, Request::TYPE_GET);
$this->configService->configureRequest($request);
$request->setProtocols(['https', 'http']);
$request->basedOnUrl($lookup);
$request->addParam('search', $search);

Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/GlobalScaleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function asyncBroadcast(string $token) {
$this->async();
foreach ($wrappers as $wrapper) {
try {
$this->gsUpstreamService->broadcastWrapper($wrapper, $this->request->getServerProtocol());
$this->gsUpstreamService->broadcastWrapper($wrapper);
} catch (GSStatusException $e) {
}
}
Expand Down
3 changes: 0 additions & 3 deletions lib/Controller/MembersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,6 @@ public function searchGlobal(string $search, int $order) {
try {
$this->mustHaveFrontEndEnabled();

if ($search === 't') {
sleep(5);
}
$result = $this->searchService->searchGlobal($search);
} catch (\Exception $e) {
return
Expand Down
1 change: 0 additions & 1 deletion lib/Search/GlobalScaleUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public function search($search) {

$request = new NC19Request(ConfigService::GS_LOOKUP_USERS, Request::TYPE_GET);
$this->configService->configureRequest($request);
$request->setProtocols(['https', 'http']);
$request->basedOnUrl($lookup);
$request->addParam('search', $search);

Expand Down
35 changes: 31 additions & 4 deletions lib/Service/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,10 @@ public function isLocalInstance(string $instance): bool {
public function configureRequest(NC19Request $request, string $routeName = '', array $args = []) {
$this->configureRequestAddress($request, $routeName, $args);

if ($this->getForcedNcBase() === '') {
$request->setProtocols(['https', 'http']);
}

$request->setVerifyPeer($this->getAppValue(ConfigService::CIRCLES_SELF_SIGNED) !== '1');
$request->setLocalAddressAllowed(true);
}
Expand All @@ -613,11 +617,9 @@ private function configureRequestAddress(NC19Request $request, string $routeName
return;
}

$ncBase = ($this->getAppValue(self::TEST_NC_BASE)) ?
$this->getAppValue(self::TEST_NC_BASE) : $this->getForcedNcBase();

$ncBase = $this->getForcedNcBase();
if ($ncBase !== '') {
$absolute = rtrim($ncBase, '/') . $this->urlGenerator->linkToRoute($routeName, $args);
$absolute = $this->cleanLinkToRoute($ncBase, $routeName, $args);
} else {
$absolute = $this->urlGenerator->linkToRouteAbsolute($routeName, $args);
}
Expand All @@ -632,6 +634,10 @@ private function configureRequestAddress(NC19Request $request, string $routeName
* @return string
*/
private function getForcedNcBase(): string {
if ($this->getAppValue(self::TEST_NC_BASE) !== '') {
return $this->getAppValue(self::TEST_NC_BASE);
}

$fromConfig = $this->config->getSystemValue('circles.force_nc_base', '');
if ($fromConfig !== '') {
return $fromConfig;
Expand All @@ -640,5 +646,26 @@ private function getForcedNcBase(): string {
return $this->getAppValue(self::FORCE_NC_BASE);
}


/**
* sometimes, linkToRoute will include the base path to the nextcloud which will be duplicate with ncBase
*
* @param string $ncBase
* @param string $routeName
* @param array $args
*
* @return string
*/
private function cleanLinkToRoute(string $ncBase, string $routeName, array $args): string {
$link = $this->urlGenerator->linkToRoute($routeName, $args);
$forcedPath = rtrim(parse_url($ncBase, PHP_URL_PATH), '/');

if ($forcedPath !== '' && strpos($link, $forcedPath) === 0) {
$ncBase = substr($ncBase, 0, -strlen($forcedPath));
}

return rtrim($ncBase, '/') . $link;
}

}

19 changes: 4 additions & 15 deletions lib/Service/GSUpstreamService.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ public function newEvent(GSEvent $event): string {
* @param GSWrapper $wrapper
* @param string $protocol
*/
public function broadcastWrapper(GSWrapper $wrapper, string $protocol): void {
public function broadcastWrapper(GSWrapper $wrapper): void {
$status = GSWrapper::STATUS_FAILED;

try {
$this->broadcastEvent($wrapper->getEvent(), $wrapper->getInstance(), $protocol);
$this->broadcastEvent($wrapper->getEvent(), $wrapper->getInstance());
$status = GSWrapper::STATUS_DONE;
} catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException | RequestResultNotJsonException $e) {
}
Expand All @@ -192,7 +192,7 @@ public function broadcastWrapper(GSWrapper $wrapper, string $protocol): void {
* @throws RequestResultSizeException
* @throws RequestServerException
*/
public function broadcastEvent(GSEvent $event, string $instance, string $protocol = ''): void {
public function broadcastEvent(GSEvent $event, string $instance): void {
$this->signEvent($event);

if ($this->configService->isLocalInstance($instance)) {
Expand All @@ -202,12 +202,7 @@ public function broadcastEvent(GSEvent $event, string $instance, string $protoco
$path = $this->urlGenerator->linkToRoute('circles.GlobalScale.broadcast');
$request = new NC19Request($path, Request::TYPE_POST);
$this->configService->configureRequest($request);
$protocols = ['https', 'http'];
if ($protocol !== '') {
$protocols = [$protocol];
}
$request->setInstance($instance);
$request->setProtocols($protocols);
}

$request->setDataSerialize($event);
Expand Down Expand Up @@ -235,14 +230,9 @@ public function confirmEvent(GSEvent &$event): void {
$path = $this->urlGenerator->linkToRoute('circles.GlobalScale.event');

$request = new NC19Request($path, Request::TYPE_POST);
$request->basedOnUrl($owner->getInstance());
$this->configService->configureRequest($request);
$request->basedOnUrl($owner->getInstance());

if ($this->get('REQUEST_SCHEME', $_SERVER) !== '') {
$request->setProtocols([$_SERVER['REQUEST_SCHEME']]);
} else {
$request->setProtocols(['https', 'http']);
}
$request->setDataSerialize($event);

$result = $this->retrieveJson($request);
Expand Down Expand Up @@ -423,7 +413,6 @@ public function confirmCircleStatus(Circle $circle): bool {
$path = $this->urlGenerator->linkToRoute('circles.GlobalScale.status');
$request = new NC19Request($path, Request::TYPE_POST);
$this->configService->configureRequest($request);
$request->setProtocols(['https', 'http']);
$request->setDataSerialize($event);

$requestIssue = false;
Expand Down
3 changes: 1 addition & 2 deletions lib/Service/GlobalScaleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,8 @@ public function getInstances(bool $all = false): array {
try {
$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
$request = new NC19Request(ConfigService::GS_LOOKUP_INSTANCES, Request::TYPE_POST);
$request->setProtocols(['https', 'http']);
$request->basedOnUrl($lookup);
$this->configService->configureRequest($request);
$request->basedOnUrl($lookup);
$request->addData('authKey', $this->configService->getGSStatus(ConfigService::GS_KEY));

try {
Expand Down
1 change: 0 additions & 1 deletion lib/Service/MembersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,6 @@ private function getGlobalScaleUserDisplayName(string $ident): string {

$request = new NC19Request(ConfigService::GS_LOOKUP_USERS, Request::TYPE_GET);
$this->configService->configureRequest($request);
$request->setProtocols(['https', 'http']);
$request->basedOnUrl($lookup);
$request->addParam('search', $ident);
$request->addParam('exact', '1');
Expand Down
1 change: 0 additions & 1 deletion lib/Service/SharingFrameService.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ public function receiveFrame($token, $uniqueId, SharingFrame $frame) {
*/
public function initiateShare(string $circleUniqueId, string $frameUniqueId) {
$request = new NC19Request('', Request::TYPE_POST);
$request->setProtocols(['https', 'http']);
$this->configService->configureRequest($request, 'circles.Shares.initShareDelivery');
$request->addParam('circleId', $circleUniqueId);
$request->addParam('frameId', $frameUniqueId);
Expand Down

0 comments on commit 7bef52e

Please sign in to comment.