23c50b6858f94d5abb7af26209056f8f97b8dd7b
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)     }
62)     return htmlspecialchars(iconv('UTF-8', 'UTF-8', $input), ENT_QUOTES, 'UTF-8');
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

63) }
64) 
65) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

66) function verify_input_general($input)
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

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

Hanno authored 5 years ago

68)     if (filter_input_general($input) !== $input) {
69)         system_failure("Ihre Daten enthielten ungültige Zeichen!");
70)         logger(LOG_WARNING, 'inc/security', 'verify_input_general', 'Ungültige Daten: '.$input);
71)     } else {
72)         return $input;
73)     }
bernd input-filtering

bernd authored 16 years ago

74) }
75) 
76) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

79)     $username=preg_replace("/[^[:alnum:]\_\.\+\-]/", "", $input);
80)     if ($username === "") {
81)         system_failure("Leerer Benutzername!");
82)     }
83)     return $username;
hanno Hatte die Kompatibilität ge...

hanno authored 16 years ago

84) }
85) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

hanno authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

92) }
93) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

94) 
95) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

98)     // FIXME: Eine "filter"-Funktion sollte keinen system_failure verursachen sondern einfach einen bereinigten String liefern.
Hanno remove whitespace in empty...

Hanno authored 5 years ago

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

Hanno authored 5 years ago

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

Hanno authored 5 years ago

101)     $input = strtolower($input);
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

102)     $input = rtrim($input, "\t\n\r\x00 .");
103)     $input = ltrim($input, "\t\n\r\x00 .");
104)     if (preg_replace("/[^.]_/", "", $input) != $input) {
105)         system_failure("Der Unterstrich ist nur als erstes Zeichen eines Hostnames erlaubt.");
106)     }
Hanno Spezialbehandlung für äöü n...

Hanno authored 5 years ago

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

Hanno authored 5 years ago

108)         system_failure("Ihre Daten enthielten ungültige Zeichen!");
109)     }
110)     if (preg_match("/^.+\*/", $input)) {
111)         system_failure("Ihre Daten enthielten ungültige Zeichen (Wildcard-Stern muss ganz vorne stehen)!");
112)     }
113)     if (! $wildcard && preg_replace("/^\*/", "", $input) != $input) {
114)         system_failure("Ihre Daten enthielten ungültige Zeichen (Keine Wildcards erlaubt)!");
115)     }
116)     if (strstr($input, '..')) {
117)         system_failure("Ungültiger Hostname");
118)     }
119)     return $input;
bernd check auf hostname

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

bernd authored 15 years ago

128) }
129) 
130) 
Hanno Böck vhost-hostnamen vernünftig...

Hanno Böck authored 5 years ago

131) function verify_input_hostname_utf8($input)
132) {
133)     $puny = idn_to_ascii($input, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
134)     if ($puny === false) {
135)         system_failure("Ungültiger Hostname! idn ".$input);
136)     }
137)     $filter = filter_var($puny, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
138)     if ($filter === false) {
139)         system_failure("Ungültiger Hostname! filter ".$input);
140)     }
141) }
142) 
143) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

Hanno authored 5 years ago

146)     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)) {
147)         system_failure('Keine IP-Adresse');
148)     }
bernd Add IP-address patterns

bernd authored 15 years ago

149) }
150) 
151) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

170) }
171) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

172) 
173) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

177) }
178) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

184) }
bernd input-filtering

bernd authored 16 years ago

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

bernd authored 16 years ago

186) 
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

207)     if (count($keyparts) === 2) {
208)         return $keyparts[0]." ".$keyparts[1];
209)     } else {
210)         return $keyparts[0]." ".$keyparts[1]." ".$keyparts[2];
211)     }
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

217)     DEBUG("checking {$input} for valid path name");
218)     if ($input != filter_input_general($input)) {
219)         logger(LOG_WARNING, 'inc/security', 'check_path', 'HTML-Krams im Pfad: '.$input);
220)         DEBUG("HTML-Krams im Pfad");
221)         return false;
222)     }
223)     $components = explode("/", $input);
224)     foreach ($components as $item) {
225)         if ($item == '..') {
226)             logger(LOG_WARNING, 'inc/security', 'check_path', '»..« im Pfad: '.$input);
227)             DEBUG("»..« im Pfad");
228)             return false;
229)         }
Hanno prevent breaking kvhostcrea...

Hanno authored 5 years ago

230)         if (strlen($item) > 255) {
231)             return false;
232)         }
233)     }
234)     if (strlen($input) > 2048) {
235)         return false;
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

238) }
239) 
240) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

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

Hanno authored 5 years ago

243)     DEBUG("Prüfe »{$path}«");
244)     if (! check_path($path)) {
245)         DEBUG('Kein Pfad');
246)         return false;
247)     }
248)     if (! isset($_SESSION['userinfo']['homedir'])) {
249)         DEBUG("Kann homedir nicht ermitteln");
250)         return false;
251)     }
Bernd Wurst Syntaxfehler

Bernd Wurst authored 5 years ago

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

bernd authored 15 years ago

253) }
254) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

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

258) }
259) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

264) }
265) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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