Skip to content

Commit 709906d

Browse files
committed
ncp-web: Set domain explicitly to whatever was visited when activating NCP
1 parent d513873 commit 709906d

8 files changed

Lines changed: 338 additions & 444 deletions

File tree

.github/workflows/SelfTest.php

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\NotifyPush;
11+
12+
use OCA\NotifyPush\Queue\IQueue;
13+
use OCA\NotifyPush\Queue\RedisQueue;
14+
use OCP\App\IAppManager;
15+
use OCP\Http\Client\IClient;
16+
use OCP\Http\Client\IClientService;
17+
use OCP\IAppConfig;
18+
use OCP\IConfig;
19+
use OCP\IDBConnection;
20+
use OCP\Security\ISecureRandom;
21+
use Symfony\Component\Console\Output\OutputInterface;
22+
use Symfony\Component\HttpFoundation\IpUtils;
23+
use Psr\Log\LoggerInterface;
24+
25+
class SelfTest {
26+
public const ERROR_OTHER = 1;
27+
public const ERROR_TRUSTED_PROXY = 2;
28+
29+
private IClient $client;
30+
private int $cookie;
31+
private string $token;
32+
private LoggerInterface $logger;
33+
34+
public function __construct(
35+
IClientService $clientService,
36+
private IConfig $config,
37+
private IAppConfig $appConfig,
38+
private IQueue $queue,
39+
private IDBConnection $connection,
40+
private IAppManager $appManager,
41+
private ISecureRandom $random,
42+
private LoggerInterface $logger,
43+
) {
44+
$this->client = $clientService->newClient();
45+
$this->cookie = rand(1, (int)pow(2, 30));
46+
$this->token = $this->random->generate(32);
47+
$this->logger = $logger;
48+
}
49+
50+
private function getHttpOpts(): array {
51+
return ['nextcloud' => ['allow_local_address' => true], 'verify' => false, 'headers' => ['token' => $this->token]];
52+
}
53+
54+
public function test(string $server, OutputInterface $output, bool $ignoreProxyError = false): int {
55+
if ($this->queue instanceof RedisQueue) {
56+
$output->writeln('<info>✓ redis is configured</info>');
57+
} else {
58+
$output->writeln('<error>🗴 redis is not configured</error>');
59+
return self::ERROR_OTHER;
60+
}
61+
62+
if (strpos($server, 'http://') === 0) {
63+
$output->writeln('<comment>🗴 using unencrypted http for push server is strongly discouraged</comment>');
64+
} elseif (strpos($server, 'https://') !== 0) {
65+
$output->writeln('<error>🗴 malformed server url</error>');
66+
return self::ERROR_OTHER;
67+
}
68+
if (strpos($server, 'localhost') !== false) {
69+
$output->writeln('<comment>🗴 push server URL is set to localhost, the push server will not be reachable from other machines</comment>');
70+
}
71+
72+
$this->queue->getConnection()->set('test-token', $this->token);
73+
$this->queue->push('notify_test_cookie', $this->cookie);
74+
$this->appConfig->setValueInt('notify_push', 'cookie', $this->cookie);
75+
76+
try {
77+
$retrievedCookie = (int)$this->client->get($server . '/test/cookie', $this->getHttpOpts())->getBody();
78+
} catch (\Exception $e) {
79+
$msg = $e->getMessage();
80+
$output->writeln("<error>🗴 can't connect to push server: $msg</error>");
81+
return self::ERROR_OTHER;
82+
}
83+
84+
if ($this->cookie === $retrievedCookie) {
85+
$output->writeln('<info>✓ push server is receiving redis messages</info>');
86+
} else {
87+
$expected = $this->cookie;
88+
$output->writeln("<error>🗴 push server is not receiving redis messages (received $expected, got $retrievedCookie)</error>");
89+
return self::ERROR_OTHER;
90+
}
91+
92+
// test if the push server can load storage mappings from the db
93+
[$storageId, $count] = $this->getStorageIdForTest();
94+
// If no admin user was created during the installation, there are no oc_filecache and oc_mounts entries yet, so this check has to be skipped.
95+
if ($storageId !== null) {
96+
try {
97+
$this->logger->warn($server . '/test/mapping/' . $storageId . " :: " . print_r($this->getHttpOpts(), true));
98+
$resp = $this->client->get($server . '/test/mapping/' . $storageId, $this->getHttpOpts());
99+
$this->logger->warn($resp->getStatusCode() . ": " . $resp->getBody());
100+
$retrievedCount = (int)$resp->getBody();
101+
} catch (\Exception $e) {
102+
$msg = $e->getMessage();
103+
$output->writeln("<error>🗴 can't connect to push server: $msg</error>");
104+
return self::ERROR_OTHER;
105+
}
106+
107+
if ((int)$count === $retrievedCount) {
108+
$output->writeln('<info>✓ push server can load mount info from database</info>');
109+
} else {
110+
$output->writeln("<error>🗴 push server can't load mount info from database</error>");
111+
return self::ERROR_OTHER;
112+
}
113+
}
114+
115+
// test if the push server can reach nextcloud by having it request the cookie
116+
try {
117+
$response = $this->client->get($server . '/test/reverse_cookie', $this->getHttpOpts())->getBody();
118+
$retrievedCookie = (int)$response;
119+
120+
if ($this->cookie === $retrievedCookie) {
121+
$output->writeln('<info>✓ push server can connect to the Nextcloud server</info>');
122+
} else {
123+
$output->writeln("<error>🗴 push server can't connect to the Nextcloud server</error>");
124+
if (!is_numeric($response)) {
125+
$output->writeln("<error> $response</error>");
126+
}
127+
return self::ERROR_OTHER;
128+
}
129+
} catch (\Exception $e) {
130+
$msg = $e->getMessage();
131+
$output->writeln("<error>🗴 can't connect to push server: $msg</error>");
132+
return self::ERROR_OTHER;
133+
}
134+
135+
// test that the push server is a trusted proxy
136+
try {
137+
$resolvedRemote = $this->client->get($server . '/test/remote/1.2.3.4', $this->getHttpOpts())->getBody();
138+
} catch (\Exception $e) {
139+
$msg = $e->getMessage();
140+
$output->writeln("<error>🗴 can't connect to push server: $msg</error>");
141+
return self::ERROR_OTHER;
142+
}
143+
144+
if ($ignoreProxyError || $resolvedRemote === '1.2.3.4') {
145+
$output->writeln('<info>✓ push server is a trusted proxy</info>');
146+
} else {
147+
$trustedProxies = $this->config->getSystemValue('trusted_proxies', []);
148+
$headers = $this->config->getSystemValue('forwarded_for_headers', ['HTTP_X_FORWARDED_FOR']);
149+
$receivedHeader = $this->queue->getConnection()->get('notify_push_forwarded_header');
150+
$remote = $this->queue->getConnection()->get('notify_push_remote');
151+
152+
if (array_search('HTTP_X_FORWARDED_FOR', $headers) === false) {
153+
$output->writeln('<error>🗴 Nextcloud is configured to not use the `x-http-forwarded-for` header.</error>');
154+
$output->writeln("<error> Please add 'HTTP_X_FORWARDED_FOR' the the 'forwarded_for_headers' in your config.php.</error>");
155+
return self::ERROR_TRUSTED_PROXY;
156+
}
157+
158+
$output->writeln('<error>🗴 push server is not a trusted proxy by Nextcloud or another proxy in the chain.</error>');
159+
$output->writeln(" Nextcloud resolved the following client address for the test request: \"$resolvedRemote\" instead of the expected \"1.2.3.4\" test value.");
160+
$output->writeln(' The following trusted proxies are currently configured: ' . implode(', ', array_map(function (string $proxy) {
161+
return '"' . $proxy . '"';
162+
}, $trustedProxies)));
163+
$invalidConfig = array_filter($trustedProxies, function (string $proxy) {
164+
return !$this->isValidProxyConfig($proxy);
165+
});
166+
if ($invalidConfig) {
167+
$output->writeln('<error> of which the following seem to be invalid: ' . implode(', ', array_map(function (string $proxy) {
168+
return '"' . $proxy . '"';
169+
}, $invalidConfig)) . '</error>');
170+
}
171+
$output->writeln(" The following x-forwarded-for header was received by Nextcloud: \"$receivedHeader\"");
172+
$output->writeln(" from the following remote: $remote");
173+
$output->writeln('');
174+
175+
if ($receivedHeader) {
176+
$forwardedParts = array_map('trim', explode(',', $receivedHeader));
177+
$forwardedClient = $forwardedParts[0];
178+
$proxies = [$remote, ...array_reverse(array_slice($forwardedParts, 1))];
179+
$untrusted = $this->getFirstUntrustedIp($proxies, $trustedProxies);
180+
if ($untrusted) {
181+
$output->writeln(" <error>$untrusted is not trusted as a reverse proxy by Nextcloud</error>");
182+
$output->writeln(' See https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/reverse_proxy_configuration.html#defining-trusted-proxies for how to add trusted proxies.');
183+
} else {
184+
$output->writeln('<info>✓ All proxies in the chain appear to be trusted by Nextcloud</info>');
185+
if ($forwardedClient != '1.2.3.4') {
186+
$output->writeln("<comment> One of the proxies is the chain (probably $forwardedClient) seems to have stripped the x-forwarded-for header</comment>");
187+
$output->writeln(" Please configure the reverse proxy at $forwardedClient to not strip the x-forwarded-for header");
188+
}
189+
}
190+
} else {
191+
$output->writeln("<comment> No x-forwarded-for header was received by Nextcloud, $remote seems to be stripping the header from the request</comment>");
192+
$output->writeln(" Please configure the reverse proxy at $remote to not strip the x-forwarded-for header");
193+
}
194+
$output->writeln('');
195+
196+
$output->writeln(" If you're having issues getting the trusted proxy setup working, you can try bypassing any existing reverse proxy");
197+
$output->writeln(' in your setup by setting the `NEXTCLOUD_URL` environment variable to point directly to the internal Nextcloud webserver url');
198+
$output->writeln(' (You will still need the ip address of the push server added as trusted proxy)');
199+
return self::ERROR_TRUSTED_PROXY;
200+
}
201+
202+
// test that the binary is up to date
203+
try {
204+
$this->queue->getConnection()->del('notify_push_version');
205+
$response = $this->client->post($server . '/test/version', $this->getHttpOpts());
206+
if ($response === 'error') {
207+
$output->writeln('<error>🗴 failed to get binary version, check the push server output for more information</error>');
208+
return self::ERROR_OTHER;
209+
}
210+
usleep(10 * 1000);
211+
$binaryVersion = $this->queue->getConnection()->get('notify_push_version');
212+
if (!$binaryVersion) {
213+
throw new \Exception('push server didn\'t set expected redis key');
214+
}
215+
} catch (\Exception $e) {
216+
$msg = $e->getMessage();
217+
$output->writeln("<error>🗴 failed to get binary version: $msg</error>");
218+
return self::ERROR_OTHER;
219+
}
220+
$appVersion = $this->appManager->getAppVersion('notify_push');
221+
$appVersionNoMinor = substr($appVersion, 0, strrpos($appVersion, '.'));
222+
$binaryVersionNoMinor = substr($binaryVersion, 0, strrpos($binaryVersion, '.'));
223+
224+
if ($appVersionNoMinor === $binaryVersionNoMinor) {
225+
$output->writeln('<info>✓ push server is running the same version as the app</info>');
226+
} else {
227+
$output->writeln("<error>🗴 push server (version $binaryVersion) is not the same version as the app (version $appVersion).</error>");
228+
return self::ERROR_OTHER;
229+
}
230+
231+
return 0;
232+
}
233+
234+
private function getFirstUntrustedIp(array $ips, array $trusted): ?string {
235+
foreach ($ips as $ip) {
236+
if (str_starts_with($ip, '[') && str_ends_with($ip, ']')) {
237+
$ip = substr($ip, 1, -1);
238+
}
239+
if (!IpUtils::checkIp($ip, $trusted)) {
240+
return $ip;
241+
}
242+
}
243+
return null;
244+
}
245+
246+
private function getStorageIdForTest() {
247+
$query = $this->connection->getQueryBuilder();
248+
$query->select('storage_id', $query->func()->count())
249+
->from('mounts', 'm')
250+
->innerJoin('m', 'filecache', 'f', $query->expr()->eq('root_id', 'fileid'))
251+
->where($query->expr()->eq('path_hash', $query->createNamedParameter(md5(''))))
252+
->groupBy('storage_id')
253+
->setMaxResults(1);
254+
255+
return $query->executeQuery()->fetch(\PDO::FETCH_NUM);
256+
}
257+
258+
private function isValidProxyConfig(string $proxyConfig): bool {
259+
$cidrre = '/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\/([0-9]{1,2})$/';
260+
261+
if (filter_var($proxyConfig, FILTER_VALIDATE_IP) !== false) {
262+
return true;
263+
} else {
264+
return (bool)preg_match($cidrre, $proxyConfig);
265+
}
266+
}
267+
}

.github/workflows/build-lxd.yml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ jobs:
267267
set -x
268268
export GECKODRIVER_PATH="$GECKODRIVER_PATH"
269269
export FF_BINARY_PATH="$(which firefox)"
270+
sudo "$LXC" exec ncp -- /usr/local/bin/ncc config:system:set overwrite.cli.url --value "https://nextcloudpi.local"
270271
sudo "$LXC" exec ncp -- bash -c 'tail -f /var/log/ncp.log' |& awk '{ print "NCP::" $0 }' &
271272
../.venv/bin/python activation_tests.py --no-gui "nextcloudpi.local" 443 4443 || {
272273
echo "Activation test failed!"
@@ -319,7 +320,6 @@ jobs:
319320
320321
sudo "$LXC" exec ncp -- bash -c "DBG=x ncp-update ${UPDATE_ARGS[*]}"
321322
sudo "$LXC" exec ncp -- /usr/local/bin/ncc status
322-
sudo "$LXC" exec ncp -- /usr/local/bin/ncc notify_push:self-test
323323
324324
if [[ "$current_nc_version" =~ "$latest_nc_version".* ]]
325325
then
@@ -367,6 +367,8 @@ jobs:
367367
sudo "$LXC" exec ncp -- bash -c 'tail -f /var/log/ncp.log' |& awk '{ print "NCP::" $0 }' &
368368
export GECKODRIVER_PATH="$GECKODRIVER_PATH"
369369
export FF_BINARY_PATH="$(which firefox)"
370+
sudo "$LXC" exec ncp -- /usr/local/bin/ncc notify_push:self-test
371+
sudo "$LXC" file push --uid 33 --gid 33 ../.github/workflows/SelfTest.php ncp/var/www/nextcloud/apps/notify_push/lib/SelfTest.php
370372
../.venv/bin/python nextcloud_tests.py --no-gui "nextcloudpi.local" 443 4443 || {
371373
echo "Nextcloud test failed!"
372374
echo "Geckodriver logs:"
@@ -380,8 +382,24 @@ jobs:
380382
sudo "$LXC" exec ncp -- cat "$datadir/nextcloud.log" || true
381383
exit 1
382384
}
383-
USE_INCUS="$USE_INCUS" ../.venv/bin/python system_tests.py --non-interactive || {
385+
sudo "$LXC" exec ncp -- /usr/local/bin/ncc notify_push:self-test || {
386+
datadir="$(sudo "$LXC" exec ncp -- ncc config:system:get datadirectory)"
387+
echo "ncp.log: "
388+
sudo "$LXC" exec ncp -- bash -c "tail -n20 /var/log/ncp.log" || true
389+
echo "================"
390+
echo "nextcloud log: "
391+
sudo "$LXC" exec ncp -- cat "$datadir/nextcloud.log" || true
392+
exit 1
393+
}
394+
USE_INCUS="$USE_INCUS" ../.venv/bin/python system_tests.py --skip-update-test --non-interactive || {
384395
echo "System test failed!"
396+
sudo "$LXC" exec ncp -- /usr/local/bin/ncc notify_push:self-test || :;
397+
echo "ncp.log: "
398+
sudo "$LXC" exec ncp -- bash -c "tail -n20 /var/log/ncp.log" || true
399+
echo "================"
400+
echo "nextcloud log: "
401+
datadir="$(sudo "$LXC" exec ncp -- ncc config:system:get datadirectory)"
402+
sudo "$LXC" exec ncp -- cat "$datadir/nextcloud.log" || true
385403
exit 1
386404
}
387405
sudo "$LXC" stop ncp

0 commit comments

Comments
 (0)