c42127d3cab5c78430e578a07038f54f8cdff459
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);
35)         curl_setopt($req, CURLOPT_SAFE_UPLOAD, 1);
36)         curl_setopt($req, CURLOPT_POSTFIELDS, array("password" => $password));
Bernd Wurst support online check for pa...

Bernd Wurst authored 5 years ago

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

Bernd Wurst authored 5 years ago

43)         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

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

Bernd Wurst authored 5 years ago

45)     // Kein Online-Check eingerichtet oder der request war nicht erfolgreich
46)     DEBUG('using Zxcvbn for password check!');
47)     $passwordchecker = new ZxcvbnPhp\Zxcvbn();
48)     $strength = $passwordchecker->passwordStrength($password, $user);
49)     DEBUG('password strength: '.$strength['score']);
50)     if ($strength['score'] < 2) {
51)         return "Das Passwort ist zu einfach!";
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

52)     }
53)     return true;
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

54) }
55) 
56) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

59)     if ($input === null) {
60)         return null;
61)     }
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

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

bernd authored 16 years ago

68) }
69) 
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

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

bernd authored 16 years ago

71) {
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

72)     if ($input === null) {
73)         return null;
74)     }
75)     $filtered = preg_replace('/[\x00-\x1f]/', '', $input);
76)     if ($filtered !== $input) {
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

77)         system_failure("Ihre Daten enthielten ungültige Zeichen!");
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

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

Hanno authored 5 years ago

79)     }
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

80)     return $filtered;
bernd input-filtering

bernd authored 16 years ago

81) }
82) 
83) 
Bernd Wurst New function filter_output_...

Bernd Wurst authored 4 years ago

84) function filter_output_html($data)
85) {
86)     return htmlspecialchars($data, ENT_QUOTES);
87) }
88) 
89) 
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

90) function verify_input_ascii($data)
91) {
92)     $filtered = filter_var($data, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
93)     if ($filtered != $data) {
94)         logger(LOG_WARNING, 'inc/security', 'verify_input_ascii', 'Ungültige Daten: '.$data);
95)         system_failure("Ihre Eingabe enthielt ungültige Zeichen");
96)     }
97)     return $filtered;
98) }
99) 
100) 
101) function verify_input_identifier($data)
102) {
103)     if ($data === "") {
104)         system_failure("Leerer Bezeichner");
105)     }
106)     $filtered = preg_replace("/[^[:alnum:]\_\.\-]/", "", $data);
107)     if ($filtered !== $data) {
108)         logger(LOG_WARNING, 'inc/security', 'verify_input_identifier', 'Ungültige Daten: '.$data);
109)         system_failure("Ihre Daten enthielten ungültige Zeichen!");
110) 
111)     }
112)     return $filtered;
113) }
114) 
Bernd Wurst New function filter_output_...

Bernd Wurst authored 4 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

117) {
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

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

Hanno authored 5 years ago

119)     if ($username === "") {
120)         system_failure("Leerer Benutzername!");
121)     }
122)     return $username;
hanno Hatte die Kompatibilität ge...

hanno authored 16 years ago

123) }
124) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

hanno authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

131) }
132) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

133) 
134) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

Hanno authored 5 years ago

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

Bernd Wurst authored 4 years ago

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

Hanno authored 5 years ago

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

Hanno authored 5 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

156) }
157) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

bernd authored 15 years ago

164) }
165) 
166) 
Hanno Böck vhost-hostnamen vernünftig...

Hanno Böck authored 5 years ago

167) function verify_input_hostname_utf8($input)
168) {
169)     $puny = idn_to_ascii($input, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
170)     if ($puny === false) {
171)         system_failure("Ungültiger Hostname! idn ".$input);
172)     }
173)     $filter = filter_var($puny, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 4 years ago

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

Hanno Böck authored 5 years ago

175)         system_failure("Ungültiger Hostname! filter ".$input);
176)     }
177) }
178) 
179) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

Hanno authored 5 years ago

182)     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)) {
183)         system_failure('Keine IP-Adresse');
184)     }
bernd Add IP-address patterns

bernd authored 15 years ago

185) }
186) 
187) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

Hanno authored 5 years ago

190)     // ripped from Perl module Net-IPv6Addr v0.2
191)     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)) {
192)         system_failure("Ungültige IPv6-Adresse");
193)     }
bernd Add IP-address patterns

bernd authored 15 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

213) }
214) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

220) }
bernd input-filtering

bernd authored 16 years ago

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

bernd authored 16 years ago

222) 
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

239)     if ((count($keyparts) === 3) && (preg_match("/^[a-zA-Z0-9@.-_]+$/", $keyparts[2]) === 0)) {
240)         system_failure("Ungültige Zeichen im Kommentar des SSH-Keys!");
241)     }
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

243)     if (count($keyparts) === 2) {
244)         return $keyparts[0]." ".$keyparts[1];
245)     } else {
246)         return $keyparts[0]." ".$keyparts[1]." ".$keyparts[2];
247)     }
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

248) }
249) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

Bernd Wurst authored 4 years ago

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

Hanno authored 5 years ago

255)         logger(LOG_WARNING, 'inc/security', 'check_path', 'HTML-Krams im Pfad: '.$input);
256)         DEBUG("HTML-Krams im Pfad");
257)         return false;
258)     }
259)     $components = explode("/", $input);
260)     foreach ($components as $item) {
261)         if ($item == '..') {
262)             logger(LOG_WARNING, 'inc/security', 'check_path', '»..« im Pfad: '.$input);
263)             DEBUG("»..« im Pfad");
264)             return false;
265)         }
Hanno prevent breaking kvhostcrea...

Hanno authored 5 years ago

266)         if (strlen($item) > 255) {
267)             return false;
268)         }
269)     }
270)     if (strlen($input) > 2048) {
271)         return false;
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

274) }
275) 
276) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

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

Hanno authored 5 years ago

279)     DEBUG("Prüfe »{$path}«");
280)     if (! check_path($path)) {
281)         DEBUG('Kein Pfad');
282)         return false;
283)     }
284)     if (! isset($_SESSION['userinfo']['homedir'])) {
285)         DEBUG("Kann homedir nicht ermitteln");
286)         return false;
287)     }
Bernd Wurst Syntaxfehler

Bernd Wurst authored 5 years ago

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

bernd authored 15 years ago

289) }
290) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

293)     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

294) }
295) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

300) }
301) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

304)     return (bool) preg_match("/^[a-z0-9\.\-]+\.[a-z\-]{2,63}$/i", $input);