Passed
Push — master ( b93e1e...342765 )
by Roeland
34:24 queued 23:05
created

RefreshWebcalService::getRandomCalendarObjectUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020, Thomas Citharel <[email protected]>
7
 *
8
 * @author Georg Ehrke <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 * @author Thomas Citharel <[email protected]>
11
 *
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
namespace OCA\DAV\CalDAV\WebcalCaching;
30
31
use Exception;
32
use GuzzleHttp\HandlerStack;
33
use GuzzleHttp\Middleware;
34
use OCA\DAV\CalDAV\CalDavBackend;
35
use OCP\AppFramework\Utility\ITimeFactory;
36
use OCP\Http\Client\IClientService;
37
use OCP\IConfig;
38
use OCP\ILogger;
39
use Psr\Http\Message\RequestInterface;
40
use Psr\Http\Message\ResponseInterface;
41
use Sabre\DAV\Exception\BadRequest;
42
use Sabre\DAV\PropPatch;
43
use Sabre\DAV\Xml\Property\Href;
44
use Sabre\VObject\Component;
45
use Sabre\VObject\DateTimeParser;
46
use Sabre\VObject\InvalidDataException;
47
use Sabre\VObject\ParseException;
48
use Sabre\VObject\Reader;
49
use Sabre\VObject\Splitter\ICalendar;
50
use Sabre\VObject\UUIDUtil;
51
use function count;
52
53
class RefreshWebcalService {
54
55
	/** @var CalDavBackend */
56
	private $calDavBackend;
57
58
	/** @var IClientService */
59
	private $clientService;
60
61
	/** @var IConfig */
62
	private $config;
63
64
	/** @var ILogger */
65
	private $logger;
66
67
	public const REFRESH_RATE = '{http://apple.com/ns/ical/}refreshrate';
68
	public const STRIP_ALARMS = '{http://calendarserver.org/ns/}subscribed-strip-alarms';
69
	public const STRIP_ATTACHMENTS = '{http://calendarserver.org/ns/}subscribed-strip-attachments';
70
	public const STRIP_TODOS = '{http://calendarserver.org/ns/}subscribed-strip-todos';
71
72
	/**
73
	 * RefreshWebcalJob constructor.
74
	 *
75
	 * @param CalDavBackend $calDavBackend
76
	 * @param IClientService $clientService
77
	 * @param IConfig $config
78
	 * @param ILogger $logger
79
	 */
80
	public function __construct(CalDavBackend $calDavBackend, IClientService $clientService, IConfig $config, ILogger $logger) {
81
		$this->calDavBackend = $calDavBackend;
82
		$this->clientService = $clientService;
83
		$this->config = $config;
84
		$this->logger = $logger;
85
	}
86
87
	/**
88
	 * @param string $principalUri
89
	 * @param string $uri
90
	 */
91
	public function refreshSubscription(string $principalUri, string $uri) {
92
		$subscription = $this->getSubscription($principalUri, $uri);
93
		$mutations = [];
94
		if (!$subscription) {
95
			return;
96
		}
97
98
		$webcalData = $this->queryWebcalFeed($subscription, $mutations);
99
		if (!$webcalData) {
100
			return;
101
		}
102
103
		$stripTodos = ($subscription[self::STRIP_TODOS] ?? 1) === 1;
104
		$stripAlarms = ($subscription[self::STRIP_ALARMS] ?? 1) === 1;
105
		$stripAttachments = ($subscription[self::STRIP_ATTACHMENTS] ?? 1) === 1;
106
107
		try {
108
			$splitter = new ICalendar($webcalData, Reader::OPTION_FORGIVING);
0 ignored issues
show
Bug introduced by
$webcalData of type string is incompatible with the type resource expected by parameter $input of Sabre\VObject\Splitter\ICalendar::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
			$splitter = new ICalendar(/** @scrutinizer ignore-type */ $webcalData, Reader::OPTION_FORGIVING);
Loading history...
109
110
			// we wait with deleting all outdated events till we parsed the new ones
111
			// in case the new calendar is broken and `new ICalendar` throws a ParseException
112
			// the user will still see the old data
113
			$this->calDavBackend->purgeAllCachedEventsForSubscription($subscription['id']);
114
115
			while ($vObject = $splitter->getNext()) {
116
				/** @var Component $vObject */
117
				$compName = null;
118
119
				foreach ($vObject->getComponents() as $component) {
120
					if ($component->name === 'VTIMEZONE') {
121
						continue;
122
					}
123
124
					$compName = $component->name;
125
126
					if ($stripAlarms) {
127
						unset($component->{'VALARM'});
128
					}
129
					if ($stripAttachments) {
130
						unset($component->{'ATTACH'});
131
					}
132
				}
133
134
				if ($stripTodos && $compName === 'VTODO') {
135
					continue;
136
				}
137
138
				$uri = $this->getRandomCalendarObjectUri();
139
				$calendarData = $vObject->serialize();
140
				try {
141
					$this->calDavBackend->createCalendarObject($subscription['id'], $uri, $calendarData, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
142
				} catch(BadRequest $ex) {
143
					$this->logger->logException($ex);
144
				}
145
			}
146
147
			$newRefreshRate = $this->checkWebcalDataForRefreshRate($subscription, $webcalData);
148
			if ($newRefreshRate) {
149
				$mutations[self::REFRESH_RATE] = $newRefreshRate;
150
			}
151
152
			$this->updateSubscription($subscription, $mutations);
153
		} catch(ParseException $ex) {
154
			$subscriptionId = $subscription['id'];
155
156
			$this->logger->logException($ex);
157
			$this->logger->warning("Subscription $subscriptionId could not be refreshed due to a parsing error");
158
		}
159
	}
160
161
	/**
162
	 * loads subscription from backend
163
	 *
164
	 * @param string $principalUri
165
	 * @param string $uri
166
	 * @return array|null
167
	 */
168
	public function getSubscription(string $principalUri, string $uri) {
169
		$subscriptions = array_values(array_filter(
170
			$this->calDavBackend->getSubscriptionsForUser($principalUri),
171
			function($sub) use ($uri) {
172
				return $sub['uri'] === $uri;
173
			}
174
		));
175
176
		if (count($subscriptions) === 0) {
177
			return null;
178
		}
179
180
		return $subscriptions[0];
181
	}
182
183
	/**
184
	 * gets webcal feed from remote server
185
	 *
186
	 * @param array $subscription
187
	 * @param array &$mutations
188
	 * @return null|string
189
	 */
190
	private function queryWebcalFeed(array $subscription, array &$mutations) {
191
		$client = $this->clientService->newClient();
192
193
		$didBreak301Chain = false;
194
		$latestLocation = null;
195
196
		$handlerStack = HandlerStack::create();
197
		$handlerStack->push(Middleware::mapRequest(function (RequestInterface $request) {
198
			return $request
199
				->withHeader('Accept', 'text/calendar, application/calendar+json, application/calendar+xml')
200
				->withHeader('User-Agent', 'Nextcloud Webcal Crawler');
201
		}));
202
		$handlerStack->push(Middleware::mapResponse(function(ResponseInterface $response) use (&$didBreak301Chain, &$latestLocation) {
203
			if (!$didBreak301Chain) {
204
				if ($response->getStatusCode() !== 301) {
205
					$didBreak301Chain = true;
206
				} else {
207
					$latestLocation = $response->getHeader('Location');
208
				}
209
			}
210
			return $response;
211
		}));
212
213
		$allowLocalAccess = $this->config->getAppValue('dav', 'webcalAllowLocalAccess', 'no');
214
		$subscriptionId = $subscription['id'];
215
		$url = $this->cleanURL($subscription['source']);
216
		if ($url === null) {
217
			return null;
218
		}
219
220
		if ($allowLocalAccess !== 'yes') {
221
			$host = strtolower(parse_url($url, PHP_URL_HOST));
222
			// remove brackets from IPv6 addresses
223
			if (strpos($host, '[') === 0 && substr($host, -1) === ']') {
224
				$host = substr($host, 1, -1);
225
			}
226
227
			// Disallow localhost and local network
228
			if ($host === 'localhost' || substr($host, -6) === '.local' || substr($host, -10) === '.localhost') {
229
				$this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules");
230
				return null;
231
			}
232
233
			// Disallow hostname only
234
			if (substr_count($host, '.') === 0) {
235
				$this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules");
236
				return null;
237
			}
238
239
			if ((bool)filter_var($host, FILTER_VALIDATE_IP) && !filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
240
				$this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules");
241
				return null;
242
			}
243
244
			// Also check for IPv6 IPv4 nesting, because that's not covered by filter_var
245
			if ((bool)filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && substr_count($host, '.') > 0) {
246
				$delimiter = strrpos($host, ':'); // Get last colon
247
				$ipv4Address = substr($host, $delimiter + 1);
248
249
				if (!filter_var($ipv4Address, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
250
					$this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules");
251
					return null;
252
				}
253
			}
254
		}
255
256
		try {
257
			$params = [
258
				'allow_redirects' => [
259
					'redirects' => 10
260
				],
261
				'handler' => $handlerStack,
262
			];
263
264
			$user = parse_url($subscription['source'], PHP_URL_USER);
265
			$pass = parse_url($subscription['source'], PHP_URL_PASS);
266
			if ($user !== null && $pass !== null) {
267
				$params['auth'] = [$user, $pass];
268
			}
269
270
			$response = $client->get($url, $params);
271
			$body = $response->getBody();
272
273
			if ($latestLocation) {
274
				$mutations['{http://calendarserver.org/ns/}source'] = new Href($latestLocation);
275
			}
276
277
			$contentType = $response->getHeader('Content-Type');
278
			$contentType = explode(';', $contentType, 2)[0];
279
			switch($contentType) {
280
				case 'application/calendar+json':
281
					try {
282
						$jCalendar = Reader::readJson($body, Reader::OPTION_FORGIVING);
283
					} catch(Exception $ex) {
284
						// In case of a parsing error return null
285
						$this->logger->debug("Subscription $subscriptionId could not be parsed");
286
						return null;
287
					}
288
					return $jCalendar->serialize();
289
290
				case 'application/calendar+xml':
291
					try {
292
						$xCalendar = Reader::readXML($body);
293
					} catch(Exception $ex) {
294
						// In case of a parsing error return null
295
						$this->logger->debug("Subscription $subscriptionId could not be parsed");
296
						return null;
297
					}
298
					return $xCalendar->serialize();
299
300
				case 'text/calendar':
301
				default:
302
					try {
303
						$vCalendar = Reader::read($body);
304
					} catch(Exception $ex) {
305
						// In case of a parsing error return null
306
						$this->logger->debug("Subscription $subscriptionId could not be parsed");
307
						return null;
308
					}
309
					return $vCalendar->serialize();
310
			}
311
		} catch(Exception $ex) {
312
			$this->logger->logException($ex);
313
			$this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error");
314
315
			return null;
316
		}
317
	}
318
319
	/**
320
	 * check if:
321
	 *  - current subscription stores a refreshrate
322
	 *  - the webcal feed suggests a refreshrate
323
	 *  - return suggested refreshrate if user didn't set a custom one
324
	 *
325
	 * @param array $subscription
326
	 * @param string $webcalData
327
	 * @return string|null
328
	 */
329
	private function checkWebcalDataForRefreshRate($subscription, $webcalData) {
330
		// if there is no refreshrate stored in the database, check the webcal feed
331
		// whether it suggests any refresh rate and store that in the database
332
		if (isset($subscription[self::REFRESH_RATE]) && $subscription[self::REFRESH_RATE] !== null) {
333
			return null;
334
		}
335
336
		/** @var Component\VCalendar $vCalendar */
337
		$vCalendar = Reader::read($webcalData);
338
339
		$newRefreshRate = null;
340
		if (isset($vCalendar->{'X-PUBLISHED-TTL'})) {
341
			$newRefreshRate = $vCalendar->{'X-PUBLISHED-TTL'}->getValue();
342
		}
343
		if (isset($vCalendar->{'REFRESH-INTERVAL'})) {
344
			$newRefreshRate = $vCalendar->{'REFRESH-INTERVAL'}->getValue();
345
		}
346
347
		if (!$newRefreshRate) {
348
			return null;
349
		}
350
351
		// check if new refresh rate is even valid
352
		try {
353
			DateTimeParser::parseDuration($newRefreshRate);
354
		} catch(InvalidDataException $ex) {
355
			return null;
356
		}
357
358
		return $newRefreshRate;
359
	}
360
361
	/**
362
	 * update subscription stored in database
363
	 * used to set:
364
	 *  - refreshrate
365
	 *  - source
366
	 *
367
	 * @param array $subscription
368
	 * @param array $mutations
369
	 */
370
	private function updateSubscription(array $subscription, array $mutations) {
371
		if (empty($mutations)) {
372
			return;
373
		}
374
375
		$propPatch = new PropPatch($mutations);
376
		$this->calDavBackend->updateSubscription($subscription['id'], $propPatch);
377
		$propPatch->commit();
378
	}
379
380
	/**
381
	 * This method will strip authentication information and replace the
382
	 * 'webcal' or 'webcals' protocol scheme
383
	 *
384
	 * @param string $url
385
	 * @return string|null
386
	 */
387
	private function cleanURL(string $url) {
388
		$parsed = parse_url($url);
389
		if ($parsed === false) {
390
			return null;
391
		}
392
393
		if (isset($parsed['scheme']) && $parsed['scheme'] === 'http') {
394
			$scheme = 'http';
395
		} else {
396
			$scheme = 'https';
397
		}
398
399
		$host = $parsed['host'] ?? '';
400
		$port = isset($parsed['port']) ? ':' . $parsed['port'] : '';
401
		$path = $parsed['path'] ?? '';
402
		$query = isset($parsed['query']) ? '?' . $parsed['query'] : '';
403
		$fragment = isset($parsed['fragment']) ? '#' . $parsed['fragment'] : '';
404
405
		$cleanURL = "$scheme://$host$port$path$query$fragment";
406
		// parse_url is giving some weird results if no url and no :// is given,
407
		// so let's test the url again
408
		$parsedClean = parse_url($cleanURL);
409
		if ($parsedClean === false || !isset($parsedClean['host'])) {
410
			return null;
411
		}
412
413
		return $cleanURL;
414
	}
415
416
	/**
417
	 * Returns a random uri for a calendar-object
418
	 *
419
	 * @return string
420
	 */
421
	public function getRandomCalendarObjectUri():string {
422
		return UUIDUtil::getUUID() . '.ics';
423
	}
424
}
425