5b46006e5ce90448a6eb96e1b65b3f97c71682eb
bernd input-filtering

bernd authored 16 years ago

1) <?php
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

2) /*
3) This file belongs to the Webinterface of schokokeks.org Hosting
4) 
Bernd Wurst Copyright year update

Bernd Wurst authored 6 years ago

5) Written 2008-2018 by schokokeks.org Hosting, namely
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

6)   Bernd Wurst <bernd@schokokeks.org>
7)   Hanno Böck <hanno@schokokeks.org>
8) 
9) To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
10) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

11) You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

12) http://creativecommons.org/publicdomain/zero/1.0/
13) 
14) Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
15) */
bernd input-filtering

bernd authored 16 years ago

16) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

17) require_once('inc/error.php');
Hanno Böck replace cracklib with zxcvbn

Hanno Böck authored 7 years ago

18) require_once('vendor/autoload.php');
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

19) 
bernd input-filtering

bernd authored 16 years ago

20) 
Hanno Böck replace cracklib with zxcvbn

Hanno Böck authored 7 years ago

21) function strong_password($password, $user = array())
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

22) {
Bernd Wurst support online check for pa...

Bernd Wurst authored 5 years ago

23)     $pwcheck = config('pwcheck');
24)     $result = null;
25)     if ($pwcheck) {
26)         DEBUG($pwcheck);
Bernd Wurst use POST for password checker

Bernd Wurst authored 5 years ago

27)         $req = curl_init($pwcheck);
Bernd Wurst support online check for pa...

Bernd Wurst authored 5 years ago

28)         curl_setopt($req, CURLOPT_RETURNTRANSFER, 1);
29)         curl_setopt($req, CURLOPT_SSL_VERIFYPEER, 1);
30)         curl_setopt($req, CURLOPT_SSL_VERIFYSTATUS, 1);
31)         curl_setopt($req, CURLOPT_CONNECTTIMEOUT, 5);
32)         curl_setopt($req, CURLOPT_TIMEOUT, 5);
33)         curl_setopt($req, CURLOPT_FOLLOWLOCATION, 0);
Bernd Wurst use POST for password checker

Bernd Wurst authored 5 years ago

34)         curl_setopt($req, CURLOPT_POST, 1);
Hanno Böck make sure POST data is sent...

Hanno Böck authored 3 years ago

35)         curl_setopt($req, CURLOPT_POSTFIELDS, "password=".urlencode($password));
Bernd Wurst support online check for pa...

Bernd Wurst authored 5 years ago

36)         $result = chop(curl_exec($req));
37)         DEBUG($result);
38)     }
39)     if ($result === 'good') {
40)         return true;
41)     } elseif ($result === 'bad') {
Bernd Wurst better error message for we...

Bernd Wurst authored 4 years ago

42)         return "Unsere Überprüfung hat ergeben, dass dieses Passwort in bisher veröffentlichten Passwortlisten enthalten ist. Es wird daher nicht akzeptiert.";
Bernd Wurst support online check for pa...

Bernd Wurst authored 5 years ago

43)     }
Bernd Wurst use Zxcvbn as fallback in e...

Bernd Wurst authored 5 years ago

44)     // Kein Online-Check eingerichtet oder der request war nicht erfolgreich
45)     DEBUG('using Zxcvbn for password check!');
46)     $passwordchecker = new ZxcvbnPhp\Zxcvbn();
Bernd Wurst PHP 8.0 compatibility

Bernd Wurst authored 3 years ago

47)     if ($user) {
48)         $strength = $passwordchecker->passwordStrength($password, $user);
49)     } else {
50)         $strength = $passwordchecker->passwordStrength($password);
51)     }
Bernd Wurst use Zxcvbn as fallback in e...

Bernd Wurst authored 5 years ago

52)     DEBUG('password strength: '.$strength['score']);
53)     if ($strength['score'] < 2) {
54)         return "Das Passwort ist zu einfach!";
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

55)     }
56)     return true;
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

57) }
58) 
59) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

60) function filter_input_general($input)
bernd input-filtering

bernd authored 16 years ago

61) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

62)     if ($input === null) {
63)         return null;
64)     }
Bernd Wurst accept integer parameters i...

Bernd Wurst authored 4 years ago

65)     $input = (string) $input;
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

66)     $filtered = preg_replace('/[\x00-\x09\x0b-\x0c\x0e-\x1f]/', '', $input);
67)     if ($filtered !== $input) {
68)         system_failure("Ihre Daten enthielten ungültige Zeichen!");
69)         logger(LOG_WARNING, 'inc/security', 'filter_input_general', 'Ungültige Daten!');
70)     }
71)     return $filtered;
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

72) }
73) 
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

74) function filter_input_oneline($input)
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

75) {
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

76)     if ($input === null) {
77)         return null;
78)     }
Bernd Wurst accept integer parameters i...

Bernd Wurst authored 4 years ago

79)     $input = (string) $input;
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

80)     $filtered = preg_replace('/[\x00-\x1f]/', '', $input);
81)     if ($filtered !== $input) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

82)         system_failure("Ihre Daten enthielten ungültige Zeichen!");
Bernd Wurst accept integer parameters i...

Bernd Wurst authored 4 years ago

83)         logger(LOG_WARNING, 'inc/security', 'filter_input_oneline', 'Ungültige Daten!');
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

84)     }
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

85)     return $filtered;
bernd input-filtering

bernd authored 16 years ago

86) }
87) 
88) 
Bernd Wurst New function filter_output_...

Bernd Wurst authored 4 years ago

89) function filter_output_html($data)
90) {
91)     return htmlspecialchars($data, ENT_QUOTES);
92) }
93) 
94) 
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

95) function verify_input_ascii($data)
96) {
Bernd Wurst accept integer parameters i...

Bernd Wurst authored 4 years ago

97)     $data = (string) $data;
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

98)     $filtered = filter_var($data, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
99)     if ($filtered != $data) {
100)         logger(LOG_WARNING, 'inc/security', 'verify_input_ascii', 'Ungültige Daten: '.$data);
101)         system_failure("Ihre Eingabe enthielt ungültige Zeichen");
102)     }
103)     return $filtered;
104) }
105) 
106) 
107) function verify_input_identifier($data)
108) {
Bernd Wurst accept integer parameters i...

Bernd Wurst authored 4 years ago

109)     $data = (string) $data;
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

110)     if ($data === "") {
111)         system_failure("Leerer Bezeichner");
112)     }
113)     $filtered = preg_replace("/[^[:alnum:]\_\.\-]/", "", $data);
114)     if ($filtered !== $data) {
115)         logger(LOG_WARNING, 'inc/security', 'verify_input_identifier', 'Ungültige Daten: '.$data);
116)         system_failure("Ihre Daten enthielten ungültige Zeichen!");
117)     }
118)     return $filtered;
119) }
120) 
Bernd Wurst New function filter_output_...

Bernd Wurst authored 4 years ago

121) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

122) function filter_input_username($input)
bernd input-filtering

bernd authored 16 years ago

123) {
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

124)     $username = preg_replace("/[^[:alnum:]\_\.\+\-]/", "", $input);
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

125)     if ($username === "") {
126)         system_failure("Leerer Benutzername!");
127)     }
128)     return $username;
hanno Hatte die Kompatibilität ge...

hanno authored 16 years ago

129) }
130) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

131) function verify_input_username($input)
hanno Hatte die Kompatibilität ge...

hanno authored 16 years ago

132) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

133)     if (filter_input_username($input) != $input) {
134)         logger(LOG_WARNING, 'inc/security', 'verify_input_username', 'Ungültige Daten: '.$input);
135)         system_failure("Ihre Daten enthielten ungültige Zeichen!");
136)     }
bernd input-filtering

bernd authored 16 years ago

137) }
138) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

139) 
140) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

141) function filter_input_hostname($input, $wildcard=false)
bernd check auf hostname

bernd authored 16 years ago

142) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

143)     DEBUG('filter_input_hostname("'.$input.'", $wildcard='.$wildcard.')');
Hanno Spezialbehandlung für äöü n...

Hanno authored 5 years ago

144)     $input = strtolower($input);
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

145)     $input = trim($input, "\t\n\r\x00 .");
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

146)     if (preg_replace("/[^.]_/", "", $input) != $input) {
147)         system_failure("Der Unterstrich ist nur als erstes Zeichen eines Hostnames erlaubt.");
148)     }
Hanno Spezialbehandlung für äöü n...

Hanno authored 5 years ago

149)     if (preg_replace("/[^[:alnum:]_*\.\-]/u", "", $input) != $input) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

150)         system_failure("Ihre Daten enthielten ungültige Zeichen!");
151)     }
152)     if (preg_match("/^.+\*/", $input)) {
153)         system_failure("Ihre Daten enthielten ungültige Zeichen (Wildcard-Stern muss ganz vorne stehen)!");
154)     }
155)     if (! $wildcard && preg_replace("/^\*/", "", $input) != $input) {
156)         system_failure("Ihre Daten enthielten ungültige Zeichen (Keine Wildcards erlaubt)!");
157)     }
158)     if (strstr($input, '..')) {
159)         system_failure("Ungültiger Hostname");
160)     }
161)     return $input;
bernd check auf hostname

bernd authored 16 years ago

162) }
163) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

164) function verify_input_hostname($input, $wildcard=false)
bernd Add IP-address patterns

bernd authored 15 years ago

165) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

166)     if (filter_input_hostname($input, $wildcard) != $input) {
167)         logger(LOG_WARNING, 'inc/security', 'verify_input_hostname', 'Ungültige Daten: '.$input);
168)         system_failure("Ihre Daten enthielten ungültige Zeichen!");
169)     }
bernd Add IP-address patterns

bernd authored 15 years ago

170) }
171) 
172) 
Hanno Böck vhost-hostnamen vernünftig...

Hanno Böck authored 5 years ago

173) function verify_input_hostname_utf8($input)
174) {
175)     $puny = idn_to_ascii($input, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
176)     if ($puny === false) {
177)         system_failure("Ungültiger Hostname! idn ".$input);
178)     }
179)     $filter = filter_var($puny, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

180)     if ($filter !== $puny) {
Hanno Böck vhost-hostnamen vernünftig...

Hanno Böck authored 5 years ago

181)         system_failure("Ungültiger Hostname! filter ".$input);
182)     }
183) }
184) 
185) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

186) function verify_input_ipv4($input)
bernd Add IP-address patterns

bernd authored 15 years ago

187) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

188)     if (! preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $input)) {
189)         system_failure('Keine IP-Adresse');
190)     }
bernd Add IP-address patterns

bernd authored 15 years ago

191) }
192) 
193) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

194) function verify_input_ipv6($input)
bernd Add IP-address patterns

bernd authored 15 years ago

195) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

196)     // ripped from Perl module Net-IPv6Addr v0.2
197)     if (! preg_match("/^(([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|[0-9a-f]{0,4}::|:(?::[a-f0-9]{1,4}){1,6}|(?:[a-f0-9]{1,4}:){1,6}:|(?:[a-f0-9]{1,4}:)(?::[a-f0-9]{1,4}){1,6}|(?:[a-f0-9]{1,4}:){2}(?::[a-f0-9]{1,4}){1,5}|(?:[a-f0-9]{1,4}:){3}(?::[a-f0-9]{1,4}){1,4}|(?:[a-f0-9]{1,4}:){4}(?::[a-f0-9]{1,4}){1,3}|(?:[a-f0-9]{1,4}:){5}(?::[a-f0-9]{1,4}){1,2}|(?:[a-f0-9]{1,4}:){6}(?::[a-f0-9]{1,4}))$/i", $input)) {
198)         system_failure("Ungültige IPv6-Adresse");
199)     }
bernd Add IP-address patterns

bernd authored 15 years ago

200) }
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

201) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

202) function verify_input_recorddata($input)
Hanno Böck Prüfe DNS-Records auf probl...

Hanno Böck authored 7 years ago

203) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

204)     if (strstr($input, "\\") || strstr($input, '"')) {
205)         system_failure("Ungültige Zeichen");
206)     }
Hanno Böck Prüfe DNS-Records auf probl...

Hanno Böck authored 7 years ago

207) }
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

208) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

209) function filter_quotes($input)
bernd Im Passwort dürfen auch kei...

bernd authored 16 years ago

210) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

211)     return preg_replace('/["\'`]/', '', $input);
bernd Im Passwort dürfen auch kei...

bernd authored 16 years ago

212) }
213) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

214) 
215) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

216) function filter_shell($input)
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

217) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

218)     return preg_replace('/[^-[:alnum:]\_\.\+ßäöüÄÖÜ/%§=]/', '', $input);
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

219) }
220) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

221) function verify_shell($input)
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

222) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

223)     if (filter_shell($input) != $input) {
224)         system_failure("Ihre Daten enthielten ungültige Zeichen!");
225)     }
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

226) }
bernd input-filtering

bernd authored 16 years ago

227) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

228) 
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

229) function filter_ssh_key($key)
230) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

231)     $keyparts = explode(" ", trim($key));
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

232) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

233)     if ((count($keyparts) > 3) || (count($keyparts) < 2)) {
234)         system_failure("Ungültiger SSH-Key!");
235)     }
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

236) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

237)     if (preg_match("/^[a-z0-9]+-[a-z0-9-]+$/", $keyparts[0]) === 0) {
238)         system_failure("Ungültiger SSH-Key!");
239)     }
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

240) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

241)     if (base64_decode($keyparts[1], 1) == false) {
242)         system_failure("Ungültiger SSH-Key!");
243)     }
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

244) 
Hanno Böck fix regular expression (was...

Hanno Böck authored 3 years ago

245)     if ((count($keyparts) === 3) && (preg_match("/^[a-zA-Z0-9@._-]+$/", $keyparts[2]) === 0)) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

246)         system_failure("Ungültige Zeichen im Kommentar des SSH-Keys!");
247)     }
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

248) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

249)     if (count($keyparts) === 2) {
250)         return $keyparts[0]." ".$keyparts[1];
251)     } else {
252)         return $keyparts[0]." ".$keyparts[1]." ".$keyparts[2];
253)     }
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

254) }
255) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

256) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

257) function check_path($input)
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

258) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

259)     DEBUG("checking {$input} for valid path name");
Bernd Wurst New function filter_output_...

Bernd Wurst authored 4 years ago

260)     if ($input != filter_output_html($input)) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

261)         logger(LOG_WARNING, 'inc/security', 'check_path', 'HTML-Krams im Pfad: '.$input);
262)         DEBUG("HTML-Krams im Pfad");
263)         return false;
264)     }
265)     $components = explode("/", $input);
266)     foreach ($components as $item) {
267)         if ($item == '..') {
268)             logger(LOG_WARNING, 'inc/security', 'check_path', '»..« im Pfad: '.$input);
269)             DEBUG("»..« im Pfad");
270)             return false;
271)         }
Hanno prevent breaking kvhostcrea...

Hanno authored 5 years ago

272)         if (strlen($item) > 255) {
273)             return false;
274)         }
275)     }
276)     if (strlen($input) > 2048) {
277)         return false;
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

278)     }
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

279)     return (preg_match('/^[ A-Za-z0-9.@\/_-]*$/', $input) == 1);
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

280) }
281) 
282) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

283) function in_homedir($path)
284) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

285)     DEBUG("Prüfe »{$path}«");
286)     if (! check_path($path)) {
287)         DEBUG('Kein Pfad');
288)         return false;
289)     }
290)     if (! isset($_SESSION['userinfo']['homedir'])) {
291)         DEBUG("Kann homedir nicht ermitteln");
292)         return false;
293)     }
Bernd Wurst Syntaxfehler

Bernd Wurst authored 5 years ago

294)     return strncmp($_SESSION['userinfo']['homedir'], $path, strlen($_SESSION['userinfo']['homedir'])) == 0;
bernd * alle internen Links sinnv...

bernd authored 15 years ago

295) }
296) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

297) function check_date($input)
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

298) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

299)     return (bool) preg_match("/[0-9]{4}-(0?[1-9]|10|11|12)-([012]?[0-9]|30|31)/", $input);
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

300) }
301) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

302) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

303) function check_emailaddr($input)
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

304) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

305)     return (bool) filter_var($input, FILTER_VALIDATE_EMAIL) == $input;
bernd don't be too complicated. /...

bernd authored 16 years ago

306) }
307) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

308) function check_domain($input)
bernd don't be too complicated. /...

bernd authored 16 years ago

309) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

310)     return (bool) preg_match("/^[a-z0-9\.\-]+\.[a-z\-]{2,63}$/i", $input);
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

311) }