Skip to content

Commit 74e9649

Browse files
authored
fix: use https NC url in OP API requests (#992)
* fix: use https NC url in OP API requests Signed-off-by: Saw-jan <saw.jan.grg3e@gmail.com> * chore: add changelog entry Signed-off-by: Saw-jan <saw.jan.grg3e@gmail.com> * test: add phpunit test Signed-off-by: Saw-jan <saw.jan.grg3e@gmail.com> --------- Signed-off-by: Saw-jan <saw.jan.grg3e@gmail.com>
1 parent 5421da2 commit 74e9649

3 files changed

Lines changed: 79 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2626
### Fixed
2727

2828
- Fix: Handle projects whose parent project is unknown [#985](https://github.com/nextcloud/integration_openproject/pull/985)
29+
- Fix: Force HTTPS on Nextcloud base URL for OpenProject API requests [#992](https://github.com/nextcloud/integration_openproject/pull/992)
2930

3031
### Added
3132

lib/Service/OpenProjectAPIService.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,25 @@ public function getNotifications(string $userId): array {
246246
}
247247

248248
/**
249-
* wrapper around IURLGenerator::getBaseUrl() to make it easier to mock in tests
249+
* @return string
250250
*/
251-
public function getBaseUrl(): string {
252-
return $this->config->getSystemValueString('overwrite.cli.url');
251+
public function getNCBaseUrl(): string {
252+
$message = 'Invalid or missing "overwrite.cli.url" system configuration.';
253+
$ncUrl = $this->config->getSystemValueString('overwrite.cli.url');
254+
if (!$ncUrl) {
255+
$this->logger->error($message, ['app' => $this->appName]);
256+
return '';
257+
}
258+
259+
$parsedUrl = parse_url($ncUrl);
260+
if (!$parsedUrl || !isset($parsedUrl['scheme']) || !isset($parsedUrl['host'])) {
261+
$this->logger->error($message, ['app' => $this->appName]);
262+
return '';
263+
}
264+
if ($parsedUrl['scheme'] !== 'https') {
265+
$ncUrl = str_replace('http://', 'https://', $ncUrl);
266+
}
267+
return $ncUrl;
253268
}
254269

255270
/**
@@ -303,7 +318,7 @@ private function searchRequest(string $userId, array $filters, bool $onlyLinkabl
303318
if ($onlyLinkableWorkPackages) {
304319
$filters[] = [
305320
'linkable_to_storage_url' =>
306-
['operator' => '=', 'values' => [urlencode($this->getBaseUrl())]]
321+
['operator' => '=', 'values' => [urlencode($this->getNCBaseUrl())]]
307322
];
308323
}
309324

@@ -766,7 +781,7 @@ public function linkWorkPackageToFile(
766781
],
767782
'_links' => [
768783
'storageUrl' => [
769-
'href' => $this->getBaseUrl()
784+
'href' => $this->getNCBaseUrl()
770785
]
771786
]
772787
];
@@ -1520,7 +1535,7 @@ public function getAvailableOpenProjectProjects(string $userId, string $searchQu
15201535
}
15211536
$filters[] = [
15221537
'storageUrl' =>
1523-
['operator' => '=', 'values' => [$this->getBaseUrl()]],
1538+
['operator' => '=', 'values' => [$this->getNCBaseUrl()]],
15241539
'userAction' =>
15251540
['operator' => '&=', 'values' => ["file_links/manage", "work_packages/create"]]
15261541
];

tests/lib/Service/OpenProjectAPIServiceTest.php

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -803,10 +803,6 @@ private function getOpenProjectAPIService(
803803
$appManagerMock->method('isInstalled')->willReturn(true);
804804
}
805805

806-
$urlGeneratorMock = $this->getMockBuilder(IURLGenerator::class)->getMock();
807-
$urlGeneratorMock
808-
->method('getBaseUrl')
809-
->willReturn($baseUrl);
810806
$this->defaultConfigMock
811807
->method('getSystemValueString')
812808
->with($this->equalTo('overwrite.cli.url'))
@@ -817,7 +813,6 @@ private function getOpenProjectAPIService(
817813
'config' => $this->defaultConfigMock,
818814
'clientService' => $clientService,
819815
'rootFolder' => $storageMock,
820-
'urlGenerator' => $urlGeneratorMock,
821816
'appManager' => $appManagerMock,
822817
'tokenEventFactory' => $exchangeTokenMock,
823818
]);
@@ -831,21 +826,27 @@ private function getOpenProjectAPIService(
831826
*
832827
* @param array<string> $mockMethods
833828
* @param array<string, object> $constructParams
829+
* @param bool $mockBaseUrl
834830
*
835831
* @return OpenProjectAPIService|MockObject
836832
*/
837833
private function getOpenProjectAPIServiceMock(
838834
array $mockMethods = ['request'],
839835
array $constructParams = [],
836+
bool $mockBaseUrl = true
840837
): OpenProjectAPIService|MockObject {
841-
$mockMethods[] = 'getBaseUrl';
838+
if ($mockBaseUrl) {
839+
$mockMethods[] = 'getNCBaseUrl';
840+
}
842841
$constructArgs = $this->getOpenProjectAPIServiceConstructArgs($constructParams);
843842

844843
$mock = $this->getMockBuilder(OpenProjectAPIService::class)
845844
->setConstructorArgs($constructArgs)
846845
->onlyMethods($mockMethods)
847846
->getMock();
848-
$mock->method('getBaseUrl')->willReturn('https://nc.my-server.org');
847+
if ($mockBaseUrl) {
848+
$mock->method('getNCBaseUrl')->willReturn('https://nc.my-server.org');
849+
}
849850
return $mock;
850851
}
851852

@@ -3906,14 +3907,7 @@ public function testGetAvailableOpenProjectProjectsErrorResponse(): void {
39063907
* @return void
39073908
*/
39083909
public function testGetAvailableOpenProjectProjectsQueryOnly() {
3909-
$iUrlGeneratorMock = $this->getMockBuilder(IURLGenerator::class)->disableOriginalConstructor()->getMock();
3910-
$iUrlGeneratorMock->method('getBaseUrl')->willReturn('https%3A%2F%2Fnc.my-server.org');
3911-
$service = $this->getOpenProjectAPIServiceMock(
3912-
['request'],
3913-
[
3914-
'urlGenerator' => $iUrlGeneratorMock,
3915-
],
3916-
);
3910+
$service = $this->getOpenProjectAPIServiceMock(['request']);
39173911
$service->method('request')
39183912
->with(
39193913
'user', 'work_packages/available_projects',
@@ -5197,4 +5191,52 @@ public function testGetAppsName(string $appId, ?array $appInfo, string $expected
51975191
$this->assertSame($expected, $result);
51985192
}
51995193

5194+
/**
5195+
* @return array<mixed>
5196+
*/
5197+
public function getNCBaseUrlDataProvider() {
5198+
return [
5199+
'HTTP url' => [
5200+
'url' => 'http://test.nc.local',
5201+
'expected' => 'https://test.nc.local',
5202+
],
5203+
'HTTPS url' => [
5204+
'url' => 'https://test.nc.local',
5205+
'expected' => 'https://test.nc.local',
5206+
],
5207+
'invalid url' => [
5208+
'url' => 'invalid_url',
5209+
'expected' => '',
5210+
],
5211+
'empty url' => [
5212+
'url' => '',
5213+
'expected' => '',
5214+
],
5215+
];
5216+
}
5217+
5218+
/**
5219+
* @dataProvider getNCBaseUrlDataProvider
5220+
* @param string $url
5221+
* @param string $expected
5222+
* @return void
5223+
*/
5224+
public function testGetNCBaseUrl(string $url, string $expected): void {
5225+
$configMock = $this->createMock(IConfig::class);
5226+
$configMock
5227+
->method('getSystemValueString')
5228+
->with($this->equalTo('overwrite.cli.url'))
5229+
->willReturn($url);
5230+
5231+
$service = $this->getOpenProjectAPIServiceMock(
5232+
[],
5233+
[
5234+
'config' => $configMock,
5235+
],
5236+
false,
5237+
);
5238+
5239+
$baseUrl = $service->getNCBaseUrl();
5240+
$this->assertEquals($expected, $baseUrl);
5241+
}
52005242
}

0 commit comments

Comments
 (0)