Passed
Push — master ( 2187f8...15d39c )
by Joas
11:45 queued 12s
created

RequestRemoteAddress::setFileInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\WorkflowEngine\Check;
23
24
25
use OCP\IL10N;
26
use OCP\IRequest;
27
use OCP\WorkflowEngine\ICheck;
28
29
class RequestRemoteAddress implements ICheck {
30
31
	/** @var IL10N */
32
	protected $l;
33
34
	/** @var IRequest */
35
	protected $request;
36
37
	/**
38
	 * @param IL10N $l
39
	 * @param IRequest $request
40
	 */
41
	public function __construct(IL10N $l, IRequest $request) {
42
		$this->l = $l;
43
		$this->request = $request;
44
	}
45
46
	/**
47
	 * @param string $operator
48
	 * @param string $value
49
	 * @return bool
50
	 */
51
	public function executeCheck($operator, $value) {
52
		$actualValue = $this->request->getRemoteAddress();
53
		$decodedValue = explode('/', $value);
54
55
		if ($operator === 'matchesIPv4') {
56
			return $this->matchIPv4($actualValue, $decodedValue[0], $decodedValue[1]);
0 ignored issues
show
Bug introduced by
$decodedValue[1] of type string is incompatible with the type integer expected by parameter $bits of OCA\WorkflowEngine\Check...oteAddress::matchIPv4(). ( Ignorable by Annotation )

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

56
			return $this->matchIPv4($actualValue, $decodedValue[0], /** @scrutinizer ignore-type */ $decodedValue[1]);
Loading history...
57
		} else if ($operator === '!matchesIPv4') {
58
			return !$this->matchIPv4($actualValue, $decodedValue[0], $decodedValue[1]);
59
		} else if ($operator === 'matchesIPv6') {
60
			return $this->matchIPv6($actualValue, $decodedValue[0], $decodedValue[1]);
0 ignored issues
show
Bug introduced by
$decodedValue[1] of type string is incompatible with the type integer expected by parameter $bits of OCA\WorkflowEngine\Check...oteAddress::matchIPv6(). ( Ignorable by Annotation )

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

60
			return $this->matchIPv6($actualValue, $decodedValue[0], /** @scrutinizer ignore-type */ $decodedValue[1]);
Loading history...
61
		} else {
62
			return !$this->matchIPv6($actualValue, $decodedValue[0], $decodedValue[1]);
63
		}
64
	}
65
66
	/**
67
	 * @param string $operator
68
	 * @param string $value
69
	 * @throws \UnexpectedValueException
70
	 */
71
	public function validateCheck($operator, $value) {
72
		if (!in_array($operator, ['matchesIPv4', '!matchesIPv4', 'matchesIPv6', '!matchesIPv6'])) {
73
			throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
74
		}
75
76
		$decodedValue = explode('/', $value);
77
		if (count($decodedValue) !== 2) {
78
			throw new \UnexpectedValueException($this->l->t('The given IP range is invalid'), 2);
79
		}
80
81
		if (in_array($operator, ['matchesIPv4', '!matchesIPv4'])) {
82
			if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
83
				throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 3);
84
			}
85
			if ($decodedValue[1] > 32 || $decodedValue[1] <= 0) {
86
				throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 4);
87
			}
88
		} else {
89
			if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
90
				throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 3);
91
			}
92
			if ($decodedValue[1] > 128 || $decodedValue[1] <= 0) {
93
				throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 4);
94
			}
95
		}
96
	}
97
98
	/**
99
	 * Based on http://stackoverflow.com/a/594134
100
	 * @param string $ip
101
	 * @param string $rangeIp
102
	 * @param int $bits
103
	 * @return bool
104
	 */
105
	protected function matchIPv4($ip, $rangeIp, $bits) {
106
		$rangeDecimal = ip2long($rangeIp);
107
		$ipDecimal = ip2long($ip);
108
		$mask = -1 << (32 - $bits);
109
		return ($ipDecimal & $mask) === ($rangeDecimal & $mask);
110
	}
111
112
	/**
113
	 * Based on http://stackoverflow.com/a/7951507
114
	 * @param string $ip
115
	 * @param string $rangeIp
116
	 * @param int $bits
117
	 * @return bool
118
	 */
119
	protected function matchIPv6($ip, $rangeIp, $bits) {
120
		$ipNet = inet_pton($ip);
121
		$binaryIp = $this->ipv6ToBits($ipNet);
122
		$ipNetBits = substr($binaryIp, 0, $bits);
123
124
		$rangeNet = inet_pton($rangeIp);
125
		$binaryRange = $this->ipv6ToBits($rangeNet);
126
		$rangeNetBits = substr($binaryRange, 0, $bits);
127
128
		return $ipNetBits === $rangeNetBits;
129
	}
130
131
	/**
132
	 * Based on http://stackoverflow.com/a/7951507
133
	 * @param string $packedIp
134
	 * @return string
135
	 */
136
	protected function ipv6ToBits($packedIp) {
137
		$unpackedIp = unpack('A16', $packedIp);
138
		$unpackedIp = str_split($unpackedIp[1]);
139
		$binaryIp = '';
140
		foreach ($unpackedIp as $char) {
141
			$binaryIp .= str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT);
142
		}
143
		return str_pad($binaryIp, 128, '0', STR_PAD_RIGHT);
144
	}
145
146
	/**
147
	 * returns a list of Entities the checker supports. The values must match
148
	 * the class name of the entity.
149
	 *
150
	 * An empty result means the check is universally available.
151
	 *
152
	 * @since 18.0.0
153
	 */
154
	public function supportedEntities(): array {
155
		return [];
156
	}
157
158
	/**
159
	 * returns whether the operation can be used in the requested scope.
160
	 *
161
	 * Scope IDs are defined as constants in OCP\WorkflowEngine\IManager. At
162
	 * time of writing these are SCOPE_ADMIN and SCOPE_USER.
163
	 *
164
	 * For possibly unknown future scopes the recommended behaviour is: if
165
	 * user scope is permitted, the default behaviour should return `true`,
166
	 * otherwise `false`.
167
	 *
168
	 * @since 18.0.0
169
	 */
170
	public function isAvailableForScope(int $scope): bool {
171
		return true;
172
	}
173
}
174