Total Complexity | 100 |
Total Lines | 631 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 1 | Features | 0 |
Complex classes like InputValidation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InputValidation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class InputValidation extends \core\common\Entity |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * returns a simple HTML <p> element with basic explanations about what was |
||
35 | * wrong with the input |
||
36 | * |
||
37 | * @param string $customtext explanation provided by the validator function |
||
38 | * @return string |
||
39 | */ |
||
40 | private function inputValidationError($customtext) |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Is this a known Federation? Optionally, also check if the authenticated |
||
50 | * user is a federation admin of that federation |
||
51 | * @param mixed $input the ISO code of the federation |
||
52 | * @param string|NULL $owner the authenticated username, optional |
||
53 | * @return \core\Federation |
||
54 | * @throws Exception |
||
55 | */ |
||
56 | public function existingFederation($input, $owner = NULL) |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Is this a known Federation? Optionally, also check if the authenticated |
||
82 | * user is a federation admin of that federation |
||
83 | * @param mixed $input the ISO code of the federation |
||
84 | * @param string|NULL $owner the authenticated username, optional |
||
85 | * @return array(\core\Federation, string) |
||
86 | * @throws Exception |
||
87 | */ |
||
88 | public function existingFederationInt($input, $owner = NULL) |
||
89 | { |
||
90 | $cat = new \core\CAT(); // initialises Entity static members |
||
91 | $fedIdentifiers = array_keys($cat->knownFederations); |
||
92 | if (!in_array(strtoupper($input), $fedIdentifiers)) { |
||
93 | throw new Exception($this->inputValidationError(sprintf("This %s does not exist!", \core\common\Entity::$nomenclature_fed))); |
||
94 | } |
||
95 | // totally circular, but this hopefully *finally* make Scrutinizer happier |
||
96 | $correctIndex = array_search(strtoupper($input), $fedIdentifiers); |
||
97 | $postFed = $fedIdentifiers[$correctIndex]; |
||
98 | $temp = new \core\Federation($postFed); |
||
99 | if ($owner === NULL) { |
||
100 | return [$temp,'readonly']; |
||
101 | } |
||
102 | $user = new \core\User($owner); |
||
103 | foreach ($temp->listFederationAdmins() as $oneowner) { |
||
104 | if ($oneowner == $owner) { |
||
105 | return [$temp, 'fullaccess']; |
||
106 | } |
||
107 | } |
||
108 | if ($user->isSuperadmin()|| $user->isSupport()) { |
||
109 | $this->loggerInstance->debug(4, "You are the superadmin/support\n"); |
||
110 | return [$temp,'readonly']; |
||
111 | } |
||
112 | throw new Exception($this->inputValidationError(sprintf("User is not %s administrator!", \core\common\Entity::$nomenclature_fed))); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Is this a known IdP? Optionally, also check if the authenticated |
||
117 | * user is an admin of that IdP |
||
118 | * It is a wrapper around existingIdPInt. |
||
119 | * |
||
120 | * @param mixed $input the numeric ID of the IdP in the system |
||
121 | * @param string $owner the authenticated username, optional |
||
122 | * @param \core\Federation $claimedFedBinding if set, cross-check that IdP belongs to specified federation (useful in admin API mode) |
||
123 | * @return \core\IdP |
||
124 | * @throws Exception |
||
125 | */ |
||
126 | public function existingIdP($input, $owner = NULL, $claimedFedBinding = NULL) |
||
127 | { |
||
128 | $clean = $this->integer($input); |
||
129 | if ($clean === FALSE) { |
||
130 | throw new Exception($this->inputValidationError("Value for IdP is not an integer!")); |
||
131 | } |
||
132 | |||
133 | $checkResult = $this->existingIdPInt($input, $owner, $claimedFedBinding); |
||
134 | $this->loggerInstance->debug(5, $checkResult, "existingIdP:", "\n"); |
||
135 | if ($checkResult[1] == 'fullaccess') { |
||
136 | return $checkResult[0]; |
||
137 | } |
||
138 | if ($owner == NULL && $checkResult[1] == 'nouser') { |
||
139 | return $checkResult[0]; |
||
140 | } |
||
141 | return false; |
||
142 | } |
||
143 | |||
144 | |||
145 | /** |
||
146 | * Is this a known IdP? Optionally, also check if the authenticated |
||
147 | * user is an admin of that IdP or a federation admin for the parent federation |
||
148 | * federaton admins superadmin and support get read-only access, superadmins get readonly access as well |
||
149 | * @param mixed $input the numeric ID of the IdP in the system |
||
150 | * @param string $owner the authenticated username, optional |
||
151 | * @param \core\Federation $claimedFedBinding if set, cross-check that IdP belongs to specified federation (useful in admin API mode) |
||
152 | * @return array(\core\IdP, string) |
||
153 | * @throws Exception |
||
154 | */ |
||
155 | public function existingIdPInt($input, $owner = NULL, $claimedFedBinding = NULL) |
||
156 | { |
||
157 | $clean = $this->integer($input); |
||
158 | if ($clean === FALSE) { |
||
159 | throw new Exception($this->inputValidationError("Value for IdP is not an integer!")); |
||
160 | } |
||
161 | $temp = new \core\IdP($input); // constructor throws an exception if NX, game over |
||
162 | if ($owner !== NULL) { // check if the authenticated user is allowed to see this institution |
||
163 | $user = new \core\User($owner); |
||
164 | foreach ($temp->listOwners() as $oneowner) { |
||
165 | if ($oneowner['ID'] == $owner) { |
||
166 | return [$temp, 'fullaccess']; |
||
167 | } |
||
168 | } |
||
169 | if ($user->isFederationAdmin($temp->federation)) { |
||
170 | $this->loggerInstance->debug(4, "You are fed admin for this IdP\n"); |
||
171 | return [$temp,'readonly']; |
||
172 | } |
||
173 | if ($user->isSuperadmin() || $user->isSupport()) { |
||
174 | $this->loggerInstance->debug(4, "You are the superadmin/support\n"); |
||
175 | return [$temp,'readonly']; |
||
176 | } |
||
177 | throw new Exception($this->inputValidationError("This IdP identifier is not accessible!")); |
||
178 | } |
||
179 | if ($claimedFedBinding !== NULL && strtoupper($temp->federation) != strtoupper($claimedFedBinding->tld)) { |
||
180 | throw new Exception($this->inputValidationError("This IdP does not belong to the claimed federation!")); |
||
181 | } |
||
182 | return [$temp,'nouser']; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Checks if the input refers to a known Profile. Optionally also takes an |
||
187 | * IdP identifier and then checks if the Profile belongs to the referenced |
||
188 | * IdP |
||
189 | * |
||
190 | * @param mixed $input the numeric ID of the Profile in the system |
||
191 | * @param int|NULL $idpIdentifier the numeric ID of the IdP in the system, optional |
||
192 | * @return \core\AbstractProfile |
||
193 | * @throws Exception |
||
194 | */ |
||
195 | public function existingProfile($input, $idpIdentifier = NULL) |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Checks if the input refers to a known DeploymentManaged. Optionally also takes an |
||
211 | * IdP identifier and then checks if the Profile belongs to the referenced |
||
212 | * IdP |
||
213 | * |
||
214 | * @param mixed $input the numeric ID of the Deployment in the system |
||
215 | * @param \core\IdP $idp the IdP |
||
216 | * @return \core\DeploymentManaged |
||
217 | * @throws Exception |
||
218 | */ |
||
219 | public function existingDeploymentManaged($input, $idp) |
||
220 | { |
||
221 | $clean = $this->integer($input); |
||
222 | if ($clean === FALSE) { |
||
223 | throw new Exception("Non-integer was passed to Profile validator!"); |
||
224 | } |
||
225 | $temp = new \core\DeploymentManaged($idp, $clean); // constructor throws an exception if NX, game over |
||
226 | |||
227 | if ($temp->institution != $idp->identifier) { |
||
228 | throw new Exception($this->inputValidationError("The profile does not belong to the IdP!")); |
||
229 | } |
||
230 | return $temp; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Checks if this is a device known to the system |
||
235 | * @param mixed $input the name of the device (index in the Devices.php array) |
||
236 | * @return string returns the same string on success, throws an Exception on failure |
||
237 | * @throws Exception |
||
238 | */ |
||
239 | public function existingDevice($input) |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Checks if the input was a valid string. |
||
252 | * |
||
253 | * @param mixed $input a string to be made SQL-safe |
||
254 | * @param boolean $allowWhitespace whether some whitespace (e.g. newlines should be preserved (true) or redacted (false) |
||
255 | * @return string the massaged string |
||
256 | * @throws Exception |
||
257 | */ |
||
258 | public function string($input, $allowWhitespace = FALSE) |
||
259 | { |
||
260 | // always chop out invalid characters, and surrounding whitespace |
||
261 | $retvalStep0 = iconv("UTF-8", "UTF-8//TRANSLIT", $input); |
||
262 | if ($retvalStep0 === FALSE) { |
||
263 | throw new Exception("iconv failure for string sanitisation. With TRANSLIT, this should never happen!"); |
||
264 | } |
||
265 | $retvalStep1 = trim($retvalStep0); |
||
266 | // if some funny person wants to inject markup tags, remove them |
||
267 | if ($retvalStep1 !== '') { |
||
268 | $retval = htmlspecialchars(strip_tags($retvalStep1), ENT_NOQUOTES); |
||
269 | if ($retval === '') { |
||
270 | throw new Exception("filter_var failure for string sanitisation."); |
||
271 | } |
||
272 | } else { |
||
273 | $retval = ''; |
||
274 | } |
||
275 | // unless explicitly wanted, take away intermediate disturbing whitespace |
||
276 | // a simple "space" is NOT disturbing :-) |
||
277 | if ($allowWhitespace === FALSE) { |
||
278 | $afterWhitespace = preg_replace('/(\0|\r|\x0b|\t|\n)/', '', $retval); |
||
279 | } else { |
||
280 | // even if we allow whitespace, not pathological ones! |
||
281 | $afterWhitespace = preg_replace('/(\0|\r|\x0b)/', '', $retval); |
||
282 | } |
||
283 | if (is_array($afterWhitespace)) { |
||
284 | throw new Exception("This function has to be given a string and returns a string. preg_replace has generated an array instead!"); |
||
285 | } |
||
286 | return (string) $afterWhitespace; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Is this an integer, or a string that represents an integer? |
||
291 | * |
||
292 | * @param mixed $input the raw input |
||
293 | * @return boolean|int returns the input, or FALSE if it is not an integer-like value |
||
294 | */ |
||
295 | public function integer($input) |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Is this a string representing a potentially more than 64-Bit length integer? |
||
305 | * |
||
306 | * @param string $input the input data which is possibly a really large integer |
||
307 | * @return boolean|string returns the input, or FALSE if it is not an integer-like string |
||
308 | */ |
||
309 | public function hugeInteger($input) |
||
310 | { |
||
311 | if (is_numeric($input)) { |
||
312 | return $input; |
||
313 | } |
||
314 | return FALSE; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Checks if the input is the hex representation of a Consortium OI (i.e. three |
||
319 | * or five bytes) |
||
320 | * |
||
321 | * @param mixed $input the raw input |
||
322 | * @return boolean|string returns the input, or FALSE on validation failure |
||
323 | */ |
||
324 | public function consortiumOI($input) |
||
325 | { |
||
326 | $shallow = $this->string($input); |
||
327 | if (strlen($shallow) != 6 && strlen($shallow) != 10) { |
||
328 | return FALSE; |
||
329 | } |
||
330 | if (!preg_match("/^[a-fA-F0-9]+$/", $shallow)) { |
||
331 | return FALSE; |
||
332 | } |
||
333 | return $shallow; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Is the input an NAI realm? Throws HTML error and returns FALSE if not. |
||
338 | * |
||
339 | * @param mixed $input the input to check |
||
340 | * @return boolean|string returns the realm, or FALSE if it was malformed |
||
341 | */ |
||
342 | public function realm($input) |
||
343 | { |
||
344 | \core\common\Entity::intoThePotatoes(); |
||
345 | if (strlen($input) == 0) { |
||
346 | echo $this->inputValidationError(_("Realm is empty!")); |
||
347 | \core\common\Entity::outOfThePotatoes(); |
||
348 | return FALSE; |
||
349 | } |
||
350 | |||
351 | // basic string checks |
||
352 | $check = $this->string($input); |
||
353 | // list of things to check, and the error they produce |
||
354 | $pregCheck = [ |
||
355 | "/@/" => _("Realm contains an @ sign!"), |
||
356 | "/^\./" => _("Realm begins with a . (dot)!"), |
||
357 | "/\.$/" => _("Realm ends with a . (dot)!"), |
||
358 | "/ /" => _("Realm contains spaces!"), |
||
359 | ]; |
||
360 | |||
361 | // bark on invalid constructs |
||
362 | foreach ($pregCheck as $search => $error) { |
||
363 | if (preg_match($search, $check) == 1) { |
||
364 | echo $this->inputValidationError($error); |
||
365 | \core\common\Entity::outOfThePotatoes(); |
||
366 | return FALSE; |
||
367 | } |
||
368 | } |
||
369 | |||
370 | if (preg_match("/\./", $check) == 0) { |
||
371 | echo $this->inputValidationError(_("Realm does not contain at least one . (dot)!")); |
||
372 | \core\common\Entity::outOfThePotatoes(); |
||
373 | return FALSE; |
||
374 | } |
||
375 | |||
376 | // none of the special HTML entities should be here. In case someone wants |
||
377 | // to mount a CSS attack by providing something that matches the realm constructs |
||
378 | // below but has interesting stuff between, mangle the input so that these |
||
379 | // characters do not do any harm. |
||
380 | \core\common\Entity::outOfThePotatoes(); |
||
381 | return htmlentities($check, ENT_QUOTES); |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * could this be a valid username? |
||
386 | * |
||
387 | * Only checks correct form, not if the user actually exists in the system. |
||
388 | * |
||
389 | * @param mixed $input the username |
||
390 | * @return string echoes back the input string, or throws an Exception if bogus |
||
391 | * @throws Exception |
||
392 | */ |
||
393 | public function syntaxConformUser($input) |
||
394 | { |
||
395 | $retvalStep0 = iconv("UTF-8", "UTF-8//TRANSLIT", $input); |
||
396 | if ($retvalStep0 === FALSE) { |
||
397 | throw new Exception("iconv failure for string sanitisation. With TRANSLIT, this should never happen!"); |
||
398 | } |
||
399 | $retvalStep1 = trim($retvalStep0); |
||
400 | |||
401 | $retval = preg_replace('/(\0|\r|\x0b|\t|\n)/', '', $retvalStep1); |
||
402 | if ($retval != "" && !ctype_print($retval)) { |
||
403 | throw new Exception($this->inputValidationError("The user identifier is not an ASCII string!")); |
||
404 | } |
||
405 | |||
406 | return $retval; |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * could this be a valid token? |
||
411 | * |
||
412 | * Only checks correct form, not if the token actually exists in the system. |
||
413 | * @param mixed $input the raw input |
||
414 | * @return string echoes back the input string, or throws an Exception if bogus |
||
415 | * @throws Exception |
||
416 | */ |
||
417 | public function token($input) |
||
418 | { |
||
419 | $retval = $input; |
||
420 | if ($input != "" && preg_match('/[^0-9a-fA-F]/', $input) != 0) { |
||
421 | throw new Exception($this->inputValidationError("Token is not a hexadecimal string!")); |
||
422 | } |
||
423 | return $retval; |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Is this be a valid coordinate vector on one axis? |
||
428 | * |
||
429 | * @param mixed $input a numeric value in range of a geo coordinate [-180;180] |
||
430 | * @return string returns back the input if all is good; throws an Exception if out of bounds or not numeric |
||
431 | * @throws Exception |
||
432 | */ |
||
433 | public function coordinate($input) |
||
434 | { |
||
435 | $oldlocale = setlocale(LC_NUMERIC, 0); |
||
436 | setlocale(LC_NUMERIC, "en_GB"); |
||
437 | if (!is_numeric($input)) { |
||
438 | throw new Exception($this->inputValidationError("Coordinate is not a numeric value!")); |
||
439 | } |
||
440 | setlocale(LC_NUMERIC, $oldlocale); |
||
441 | // lat and lon are always in the range of [-180;+180] |
||
442 | if ($input < -180 || $input > 180) { |
||
443 | throw new Exception($this->inputValidationError("Coordinate is out of bounds. Which planet are you from?")); |
||
444 | } |
||
445 | return $input; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Is this a valid coordinate pair in JSON encoded representation? |
||
450 | * |
||
451 | * @param mixed $input the string to be checked: is this a serialised array with lat/lon keys in a valid number range? |
||
452 | * @return string returns $input if checks have passed; throws an Exception if something's wrong |
||
453 | * @throws Exception |
||
454 | */ |
||
455 | public function coordJsonEncoded($input) |
||
456 | { |
||
457 | $tentative = json_decode($input, true); |
||
458 | if (is_array($tentative)) { |
||
459 | if (isset($tentative['lon']) && isset($tentative['lat']) && $this->coordinate($tentative['lon']) && $this->coordinate($tentative['lat'])) { |
||
460 | return $input; |
||
461 | } |
||
462 | } |
||
463 | throw new Exception($this->inputValidationError("Wrong coordinate encoding (2.0 uses JSON, not serialize)!")); |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * This checks the state of a HTML GET/POST "boolean". |
||
468 | * |
||
469 | * If not checked, no value is submitted at all; if checked, has the word "on". |
||
470 | * Anything else is a big error. |
||
471 | * |
||
472 | * @param mixed $input the string to test |
||
473 | * @return boolean TRUE if the input was "on". It is not possible in HTML to signal "off" |
||
474 | * @throws Exception |
||
475 | */ |
||
476 | public function boolean($input) |
||
477 | { |
||
478 | if ($input != "on") { |
||
479 | throw new Exception($this->inputValidationError("Unknown state of boolean option!")); |
||
480 | } |
||
481 | return TRUE; |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * checks if we have the strings "IdP" "SP" or "IdPSP" |
||
486 | * |
||
487 | * @param string $partTypeRaw the string to be validated as participant type |
||
488 | * @return string validated result |
||
489 | * @throws Exception |
||
490 | */ |
||
491 | public function partType($partTypeRaw) |
||
502 | } |
||
503 | } |
||
504 | |||
505 | const TABLEMAPPING = [ |
||
506 | "IdP" => "institution_option", |
||
507 | "Profile" => "profile_option", |
||
508 | "FED" => "federation_option", |
||
509 | ]; |
||
510 | |||
511 | /** |
||
512 | * Is this a valid database reference? Has the form <tablename>-<rowID> and there |
||
513 | * needs to be actual data at that place |
||
514 | * |
||
515 | * @param string $input the reference to check |
||
516 | * @return boolean|array the reference split up into "table" and "rowindex", or FALSE |
||
517 | */ |
||
518 | public function databaseReference($input) |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * is this a valid hostname? |
||
533 | * |
||
534 | * @param mixed $input the raw input |
||
535 | * @return boolean|string echoes the hostname, or FALSE if bogus |
||
536 | */ |
||
537 | public function hostname($input) |
||
538 | { |
||
539 | // is it a valid IP address (IPv4 or IPv6), or a hostname? |
||
540 | if (filter_var($input, FILTER_VALIDATE_IP) || filter_var($input, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) { |
||
541 | // if it's a verified IP address or hostname then it does not contain |
||
542 | // rubbish of course. But just to be sure, run htmlspecialchars around it |
||
543 | return htmlspecialchars($input, ENT_QUOTES); |
||
544 | } |
||
545 | return FALSE; |
||
546 | } |
||
547 | |||
548 | /** |
||
549 | * is this a valid email address? |
||
550 | * |
||
551 | * @param mixed $input the raw input |
||
552 | * @return boolean|string echoes the mail address, or FALSE if bogus |
||
553 | */ |
||
554 | public function email($input) |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * is this a well-formed SMS number? Light massaging - leading + will be removed |
||
566 | * @param string $input the raw input |
||
567 | * @return boolean|string |
||
568 | */ |
||
569 | public function sms($input) |
||
570 | { |
||
571 | $number = str_replace(' ', '', str_replace(".", "", str_replace("+", "", $input))); |
||
572 | if (!is_numeric($number)) { |
||
573 | return FALSE; |
||
574 | } |
||
575 | return $number; |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * Is this is a language we support? If not, sanitise to our configured default language. |
||
580 | * |
||
581 | * @param mixed $input the candidate language identifier |
||
582 | * @return string |
||
583 | * @throws Exception |
||
584 | */ |
||
585 | public function supportedLanguage($input) |
||
597 | } |
||
598 | |||
599 | /** |
||
600 | * Makes sure we are not receiving a bogus option name. The called function throws |
||
601 | * an assertion if the name is not known. |
||
602 | * |
||
603 | * @param mixed $input the unvetted option name |
||
604 | * @return string |
||
605 | */ |
||
606 | public function optionName($input) |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * Checks to see if the input is a valid image of sorts |
||
614 | * |
||
615 | * @param mixed $binary blob that may or may not be a parseable image |
||
616 | * @return boolean |
||
617 | */ |
||
618 | public function image($binary) |
||
633 | } |
||
634 | |||
635 | /** |
||
636 | * searches for values in GET and POST, and filters the value according to |
||
637 | * which kind of data is expected |
||
638 | * |
||
639 | * @param string $varName name of the variable in GET/POST |
||
640 | * @param string $filter which type of filter to apply (safe_text / int) |
||
641 | * @return NULL|string|integer the returned value |
||
642 | */ |
||
643 | public function simpleInputFilter($varName, $filter) |
||
661 | } |
||
662 | |||
663 | } |
||
664 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths