File: CPasswordComplexityValidator.php

package info (click to toggle)
zabbix 1%3A6.0.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 283,384 kB
  • sloc: sql: 868,673; ansic: 322,351; php: 235,311; javascript: 62,116; sh: 5,555; makefile: 2,257; java: 1,397; cpp: 662; xml: 49; perl: 41
file content (225 lines) | stat: -rw-r--r-- 5,722 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php declare(strict_types = 0);
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/


/**
 * Class for validating password complexity.
 */
class CPasswordComplexityValidator extends CValidator {

	/**
	 * File of common used passwords.
	 *
	 * @var string
	 */
	private const TOP_PASSWORDS_FILE = __DIR__.'/../../../data/top_passwords.txt';

	/**
	 * Strings forbidden to be part of validated password.
	 *
	 * @var array
	 */
	private $context_data = [];

	/**
	 * An options array.
	 *
	 * @var array
	 */
	private $options = [
		'passwd_min_length' => 0,
		'passwd_check_rules' => 0x00
	];

	/**
	 * Class constructor.
	 *
	 * @param array $options
	 * @param int   $options['passwd_min_length']   Minimum required password length.
	 * @param int   $options['passwd_check_rules']  Password complexity rules.
	 */
	public function __construct(array $options = []) {
		$this->options = $options + $this->options;
	}

	/**
	 * Validate password complexity.
	 *
	 * @param string $value  A password string to validate.
	 *
	 * @return bool
	 */
	public function validate($value) {
		$value = (string) $value;
		$this->setError('');

		if ($this->options['passwd_min_length'] > mb_strlen($value)) {
			$this->setError(_s('must be at least %1$d characters long', $this->options['passwd_min_length']));

			return false;
		}

		$check_case = ($this->options['passwd_check_rules'] & PASSWD_CHECK_CASE);
		if ($check_case && self::checkCase($value) === false) {
			$this->setError(_('must contain at least one lowercase and one uppercase Latin letter'));

			return false;
		}

		$check_digit = ($this->options['passwd_check_rules'] & PASSWD_CHECK_DIGITS);
		if ($check_digit && self::containsDigit($value) === false) {
			$this->setError(_('must contain at least one digit'));

			return false;
		}

		$check_special = ($this->options['passwd_check_rules'] & PASSWD_CHECK_SPECIAL);
		if ($check_special && self::containsSpecialCharacter($value) === false) {
			$this->setError(_('must contain at least one special character'));

			return false;
		}

		$check_simple = ($this->options['passwd_check_rules'] & PASSWD_CHECK_SIMPLE);
		if ($check_simple && $this->isSimple($value) === false) {
			$this->setError(_("must not contain user's name, surname or username"));

			return false;
		}

		if ($check_simple && self::checkIfPasswordIsCommonlyUsed($value) === false) {
			$this->setError(_('must not be one of common or context-specific passwords'));

			return false;
		}

		return true;
	}

	/**
	 * Set array of context specific strings forbidden to be part of validated password.
	 *
	 * @param array  $context_data  Indexed array of strings that are forbidden to be part of password.
	 */
	public function setContextData(array $context_data = []): void {
		$this->context_data = $context_data;
	}

	/**
	 * Check if string contains special character.
	 *
	 * @static
	 *
	 * @param string $password  Password to check.
	 *
	 * @return bool
	 */
	private static function containsSpecialCharacter(string $password): bool {
		$spec_chars = [
			' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>',
			'?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~'
		];

		foreach ($spec_chars as $char) {
			if (stripos($password, $char) !== false) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Check if string contains upper and lower case characters.
	 *
	 * @static
	 *
	 * @param string $password  Password to check.
	 *
	 * @return bool
	 */
	private static function checkCase(string $password): bool {
		return (preg_match('/^(?=.*[a-z])(?=.*[A-Z]).+$/', $password) == 1);
	}

	/**
	 * Check if string contains digit.
	 *
	 * @static
	 *
	 * @param string $password  Password to check.
	 *
	 * @return bool
	 */
	private static function containsDigit(string $password): bool {
		return (preg_match('/\d/', $password) == 1);
	}

	/**
	 * Check if string doesn't contain context specific substring.
	 *
	 * @param string $password  Password to check.
	 *
	 * @return bool
	 */
	private function isSimple(string $password): bool {
		foreach ($this->context_data as $context_string) {
			if (mb_stripos($password, $context_string) !== false) {
				return false;
			}
		}

		return true;
	}

	/**
	 * Check if string doesn't contain context specific substring.
	 *
	 * @static
	 *
	 * @param string $password  Password to check.
	 *
	 * @return bool
	 */
	private static function checkIfPasswordIsCommonlyUsed(string $password): bool {
		$password = base64_encode($password);

		if (($handle = fopen(self::TOP_PASSWORDS_FILE, 'r')) !== false) {
			// Skip disclaimer.
			$prev_line = null;
			while (($line = fgets($handle, 512)) !== false) {
				if ($prev_line === '' && trim($line) === '') {
					break;
				}
				$prev_line = trim($line);
			}

			// Check passwords.
			while (($line = fgets($handle, 512)) !== false) {
				if ($password === trim($line)) {
					return false;
				}
			}
			fclose($handle);
		}

		return true;
	}
}