Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
hosts
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
14
100.00% covered (success)
100.00%
1 / 1
 list
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 allowed
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
13
1<?php
2// This file is part of Moodle - https://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <https://www.gnu.org/licenses/>.
16
17namespace message_webpush\local;
18
19/**
20 * Which push services the site sends to. A subscription endpoint comes from the browser, so it is checked before it is
21 * stored and again before each send: never an internal address, never plain HTTP.
22 *
23 * @package    message_webpush
24 * @copyright  2026 DevLMS
25 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27class hosts {
28    /**
29     * Push services of the major browsers: Pushpad's list (github.com/pushpad/known-push-services, production hosts)
30     * and Apple's documentation (https://*.push.apple.com).
31     */
32    public const KNOWN = [
33        'android.googleapis.com',
34        'fcm.googleapis.com',
35        'jmt17.google.com',
36        'updates.push.services.mozilla.com',
37        '*.notify.windows.com',
38        '*.push.apple.com',
39    ];
40
41    /**
42     * The hosts in use: the known ones plus any the administrator added.
43     *
44     * @return string[]
45     */
46    public static function list(): array {
47        $extra = preg_split('/[\s,]+/', strtolower(trim((string)get_config('message_webpush', 'extrahosts'))));
48        return array_values(array_unique(array_merge(self::KNOWN, array_filter($extra))));
49    }
50
51    /**
52     * Whether an endpoint is an https address on a push service in the list.
53     *
54     * @param string $endpoint
55     * @return bool
56     */
57    public static function allowed(string $endpoint): bool {
58        $parts = parse_url($endpoint);
59        if (
60            !$parts || ($parts['scheme'] ?? '') !== 'https' || empty($parts['host']) || isset($parts['user']) ||
61                isset($parts['pass']) || (isset($parts['port']) && (int)$parts['port'] !== 443)
62        ) {
63            return false;
64        }
65        $host = strtolower($parts['host']);
66        foreach (self::list() as $rule) {
67            if (str_starts_with($rule, '*.')) {
68                // A wildcard covers subdomains only, never the bare domain or a look-alike suffix.
69                if (str_ends_with($host, substr($rule, 1)) && strlen($host) > strlen($rule) - 1) {
70                    return true;
71                }
72            } else if ($host === $rule) {
73                return true;
74            }
75        }
76        return false;
77    }
78}