8a4acb54b452ad801825c59cc7aa542eae0f9f3f
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) 
11) You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see 
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) {
Hanno Böck replace cracklib with zxcvbn

Hanno Böck authored 7 years ago

23)   $passwordchecker = new ZxcvbnPhp\Zxcvbn();
24)   $strength = $passwordchecker->passwordStrength($password, $user);
Bernd Wurst Subusers-Modul in einer ers...

Bernd Wurst authored 12 years ago

25) 
Hanno Böck replace cracklib with zxcvbn

Hanno Böck authored 7 years ago

26)   if ($strength['score'] < 2) {
27)     return "Das Passwort ist zu einfach!";
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

28)   }
29) 
Hanno Böck replace cracklib with zxcvbn

Hanno Böck authored 7 years ago

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

bernd authored 16 years ago

31) }
32) 
33) 
bernd input-filtering

bernd authored 16 years ago

34) function filter_input_general( $input )
35) {
Bernd Wurst Weitere Prepared-Statements

Bernd Wurst authored 10 years ago

36)   if ($input === NULL) {
37)     return NULL;
38)   }
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

39)   return htmlspecialchars(iconv('UTF-8', 'UTF-8', $input), ENT_QUOTES, 'UTF-8');
40) }
41) 
42) 
43) function verify_input_general( $input )
44) {
Bernd Wurst Security

Bernd Wurst authored 6 years ago

45)   if (filter_input_general($input) !== $input) {
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

46)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Logger mit Logleveln

bernd authored 14 years ago

47)     logger(LOG_WARNING, 'inc/security', 'verify_input_general', 'Ungültige Daten: '.$input);
Bernd Wurst Security

Bernd Wurst authored 6 years ago

48)   } else {
49)       return $input;
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

50)   }
bernd input-filtering

bernd authored 16 years ago

51) }
52) 
53) 
54) function filter_input_username( $input )
55) {
Hanno Böck Fehler bei leeren Benutzern...

Hanno Böck authored 7 years ago

56)   $username=preg_replace("/[^[:alnum:]\_\.\+\-]/", "", $input);
57)   if ($username === "") {
58)     system_failure("Leerer Benutzername!");
59)   }
60)   return $username;
hanno Hatte die Kompatibilität ge...

hanno authored 16 years ago

61) }
62) 
63) function verify_input_username( $input )
64) {
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

65)   if (filter_input_username( $input ) != $input) {
bernd Logger mit Logleveln

bernd authored 14 years ago

66)     logger(LOG_WARNING, 'inc/security', 'verify_input_username', 'Ungültige Daten: '.$input);
bernd Add IP-address patterns

bernd authored 15 years ago

67)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

68)   }
bernd input-filtering

bernd authored 16 years ago

69) }
70) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

71) 
72) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

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

bernd authored 16 years ago

74) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

75)   // FIXME: Eine "filter"-Funktion sollte keinen system_failure verursachen sondern einfach einen bereinigten String liefern.
76)   
Bernd Wurst Wildcard-CSRs können jetzt...

Bernd Wurst authored 11 years ago

77)   DEBUG('filter_input_hostname("'.$input.'", $wildcard='.$wildcard.')');
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

78)   $input = str_replace(array('Ä', 'Ö', 'Ü'), array('ä', 'ö', 'ü'), strtolower($input));
bernd Hostnames sollten nicht mit...

bernd authored 16 years ago

79)   $input = rtrim($input, "\t\n\r\x00 .");
80)   $input = ltrim($input, "\t\n\r\x00 .");
Bernd Wurst Hostnames dürfen keine Unte...

Bernd Wurst authored 7 years ago

81)   if (preg_replace("/[^.]_/", "", $input) != $input) {
82)     system_failure("Der Unterstrich ist nur als erstes Zeichen eines Hostnames erlaubt.");
83)   }
Bernd Wurst Erlaube underscore in hostn...

Bernd Wurst authored 9 years ago

84)   if (preg_replace("/[^[:alnum:]äöü_*\.\-]/", "", $input ) != $input)
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

85)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Erlaube wildcards bei vhosts

bernd authored 13 years ago

86)   if (preg_match("/^.+\*/", $input ))
87)     system_failure("Ihre Daten enthielten ungültige Zeichen (Wildcard-Stern muss ganz vorne stehen)!");
88)   if (! $wildcard && preg_replace("/^\*/", "", $input ) != $input)
bernd Erlaube * im Hostname

bernd authored 14 years ago

89)     system_failure("Ihre Daten enthielten ungültige Zeichen (Keine Wildcards erlaubt)!");
bernd Hostname darf kein »..« ent...

bernd authored 16 years ago

90)   if (strstr($input, '..'))
91)     system_failure("Ungültiger Hostname");
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

92)   return $input;
bernd check auf hostname

bernd authored 16 years ago

93) }
94) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

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

bernd authored 15 years ago

96) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

97)   if (filter_input_hostname( $input, $wildcard ) != $input) {
bernd Logger mit Logleveln

bernd authored 14 years ago

98)     logger(LOG_WARNING, 'inc/security', 'verify_input_hostname', 'Ungültige Daten: '.$input);
bernd Add IP-address patterns

bernd authored 15 years ago

99)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
100)   }
101) }
102) 
103) 
104) function verify_input_ipv4( $input )
105) {
106)   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))
107)     system_failure('Keine IP-Adresse');
108) }
109) 
110) 
111) function verify_input_ipv6( $input )
112) {
113)   // ripped from Perl module Net-IPv6Addr v0.2
114)   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))
115)     system_failure("Ungültige IPv6-Adresse");
116) }
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

117) 
Hanno Böck Prüfe DNS-Records auf probl...

Hanno Böck authored 7 years ago

118) function verify_input_recorddata( $input )
119) {
120)   if (strstr($input, "\\") || strstr($input, '"'))
121)     system_failure("Ungültige Zeichen");
122) }
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

123) 
bernd Im Passwort dürfen auch kei...

bernd authored 16 years ago

124) function filter_quotes( $input )
125) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

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

bernd authored 16 years ago

127) }
128) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

129) 
130) 
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

131) function filter_shell( $input )
132) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

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

bernd authored 16 years ago

134) }
135) 
136) function verify_shell( $input )
137) {
138)   if (filter_shell($input) != $input)
139)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

140) }
bernd input-filtering

bernd authored 16 years ago

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

bernd authored 16 years ago

142) 
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

143) function filter_ssh_key($key)
144) {
145)   $keyparts = explode(" ", trim($key));
146) 
147)   if ((count($keyparts) > 3) || (count($keyparts) < 2)) {
148)     system_failure("Ungültiger SSH-Key!");
149)   }
150) 
151)   if (preg_match("/^[a-z0-9]+-[a-z0-9-]+$/", $keyparts[0]) === 0) {
152)     system_failure("Ungültiger SSH-Key!");
153)   }
154) 
155)   if (base64_decode($keyparts[1], 1) == false) {
156)     system_failure("Ungültiger SSH-Key!");
157)   }
158) 
Hanno Böck bessere fehlermeldung bei s...

Hanno Böck authored 7 years ago

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

Hanno Böck authored 7 years ago

161)   }
162) 
163)   if (count($keyparts) === 2) {
164)     return $keyparts[0]." ".$keyparts[1];
165)   } else {
166)     return $keyparts[0]." ".$keyparts[1]." ".$keyparts[2];
167)   }
168) }
169) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

170) 
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

171) function check_path( $input )
172) {
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

173)   DEBUG("checking {$input} for valid path name");
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

174)   if ($input != filter_input_general($input))
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

175)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

176)     logger(LOG_WARNING, 'inc/security', 'check_path', 'HTML-Krams im Pfad: '.$input);
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

177)     DEBUG("HTML-Krams im Pfad");
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

178)     return False;
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

179)   }
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

180)   $components = explode("/", $input);
181)   foreach ($components AS $item)
182)   {
183)     if ($item == '..')
184)     {
bernd Logger mit Logleveln

bernd authored 14 years ago

185)       logger(LOG_WARNING, 'inc/security', 'check_path', '»..« im Pfad: '.$input);
bernd check auf hostname

bernd authored 16 years ago

186)       DEBUG("»..« im Pfad");
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

187)       return False;
188)     }
189)   }
bernd Erlaube Leerzeichen im Pfad

bernd authored 13 years ago

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

bernd authored 16 years ago

191) }
192) 
193) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

194) function in_homedir($path)
195) {
196)   DEBUG("Prüfe »{$path}«");
197)   if (! check_path($path))
198)   {
199)     DEBUG('Kein Pfad');
200)     return False;
201)   }
202)   if (! isset($_SESSION['userinfo']['homedir']))
203)   {
204)     DEBUG("Kann homedir nicht ermitteln");
205)     return False;
206)   }
207)   return strncmp($_SESSION['userinfo']['homedir'], $path, count($_SESSION['userinfo']['homedir'])) == 0;
208) }
209) 
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

210) function check_date( $input )
211) {
Bernd Wurst Erlaube beim SEPA-Mandat au...

Bernd Wurst authored 9 years ago

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

213) }
214) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

215) 
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

216) function check_emailaddr( $input )
217) {
bernd Syntax-fehler

bernd authored 16 years ago

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

bernd authored 16 years ago

219) }
220) 
221) function check_domain( $input )
222) {
Bernd Wurst hardening the domain input

Bernd Wurst authored 6 years ago

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