d5f29a796a95bd329851539e985f2915436582c3
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);
27)         $req = curl_init($pwcheck.$password);
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);
34)         $result = chop(curl_exec($req));
35)         DEBUG($result);
36)     }
37)     if ($result === 'good') {
38)         return true;
39)     } elseif ($result === 'bad') {
40)         return "Das ist kein gutes Passwort!";
41)     }
Bernd Wurst use Zxcvbn as fallback in e...

Bernd Wurst authored 5 years ago

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

Hanno authored 5 years ago

49)     }
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

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

Hanno authored 5 years ago

51)     return true;
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

52) }
53) 
54) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

57)     if ($input === null) {
58)         return null;
59)     }
60)     return htmlspecialchars(iconv('UTF-8', 'UTF-8', $input), ENT_QUOTES, 'UTF-8');
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

61) }
62) 
63) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

72) }
73) 
74) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

hanno authored 16 years ago

82) }
83) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

hanno authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

90) }
91) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

92) 
93) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

96)     // 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

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

Hanno authored 5 years ago

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

Hanno authored 5 years ago

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

Hanno authored 5 years ago

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

Hanno authored 5 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

118) }
119) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

bernd authored 15 years ago

126) }
127) 
128) 
Hanno Böck vhost-hostnamen vernünftig...

Hanno Böck authored 5 years ago

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

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

bernd authored 15 years ago

147) }
148) 
149) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

168) }
169) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

170) 
171) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

175) }
176) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

182) }
bernd input-filtering

bernd authored 16 years ago

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

bernd authored 16 years ago

184) 
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

210) }
211) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

236) }
237) 
238) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

Bernd Wurst authored 5 years ago

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

bernd authored 15 years ago

251) }
252) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

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

256) }
257) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

262) }
263) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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