3a072b88368ae6495a3fa9a8a4ff8e8ae87d6101
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) {
Hanno Fix coding style with php-c...

Hanno authored 5 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 Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

31) }
32) 
33) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

36)     if ($input === null) {
37)         return null;
38)     }
39)     return htmlspecialchars(iconv('UTF-8', 'UTF-8', $input), ENT_QUOTES, 'UTF-8');
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

40) }
41) 
42) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

45)     if (filter_input_general($input) !== $input) {
46)         system_failure("Ihre Daten enthielten ungültige Zeichen!");
47)         logger(LOG_WARNING, 'inc/security', 'verify_input_general', 'Ungültige Daten: '.$input);
48)     } else {
49)         return $input;
50)     }
bernd input-filtering

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 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) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

hanno authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

71) 
72) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

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

Hanno authored 5 years ago

77)     DEBUG('filter_input_hostname("'.$input.'", $wildcard='.$wildcard.')');
78)     $input = str_replace(array('Ä', 'Ö', 'Ü'), array('ä', 'ö', 'ü'), strtolower($input));
79)     $input = rtrim($input, "\t\n\r\x00 .");
80)     $input = ltrim($input, "\t\n\r\x00 .");
81)     if (preg_replace("/[^.]_/", "", $input) != $input) {
82)         system_failure("Der Unterstrich ist nur als erstes Zeichen eines Hostnames erlaubt.");
83)     }
84)     if (preg_replace("/[^[:alnum:]äöü_*\.\-]/", "", $input) != $input) {
85)         system_failure("Ihre Daten enthielten ungültige Zeichen!");
86)     }
87)     if (preg_match("/^.+\*/", $input)) {
88)         system_failure("Ihre Daten enthielten ungültige Zeichen (Wildcard-Stern muss ganz vorne stehen)!");
89)     }
90)     if (! $wildcard && preg_replace("/^\*/", "", $input) != $input) {
91)         system_failure("Ihre Daten enthielten ungültige Zeichen (Keine Wildcards erlaubt)!");
92)     }
93)     if (strstr($input, '..')) {
94)         system_failure("Ungültiger Hostname");
95)     }
96)     return $input;
bernd check auf hostname

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

bernd authored 15 years ago

105) }
106) 
107) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

Hanno authored 5 years ago

110)     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)) {
111)         system_failure('Keine IP-Adresse');
112)     }
bernd Add IP-address patterns

bernd authored 15 years ago

113) }
114) 
115) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

bernd authored 15 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

134) }
135) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

141) }
142) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

148) }
bernd input-filtering

bernd authored 16 years ago

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

bernd authored 16 years ago

150) 
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

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

Hanno Böck authored 7 years ago

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

Hanno authored 5 years ago

171)     if (count($keyparts) === 2) {
172)         return $keyparts[0]." ".$keyparts[1];
173)     } else {
174)         return $keyparts[0]." ".$keyparts[1]." ".$keyparts[2];
175)     }
Hanno Böck validiere SSH-Keys korrekt

Hanno Böck authored 7 years ago

176) }
177) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

181)     DEBUG("checking {$input} for valid path name");
182)     if ($input != filter_input_general($input)) {
183)         logger(LOG_WARNING, 'inc/security', 'check_path', 'HTML-Krams im Pfad: '.$input);
184)         DEBUG("HTML-Krams im Pfad");
185)         return false;
186)     }
187)     $components = explode("/", $input);
188)     foreach ($components as $item) {
189)         if ($item == '..') {
190)             logger(LOG_WARNING, 'inc/security', 'check_path', '»..« im Pfad: '.$input);
191)             DEBUG("»..« im Pfad");
192)             return false;
193)         }
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

196) }
197) 
198) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

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

Hanno authored 5 years ago

201)     DEBUG("Prüfe »{$path}«");
202)     if (! check_path($path)) {
203)         DEBUG('Kein Pfad');
204)         return false;
205)     }
206)     if (! isset($_SESSION['userinfo']['homedir'])) {
207)         DEBUG("Kann homedir nicht ermitteln");
208)         return false;
209)     }
Bernd Wurst Syntaxfehler

Bernd Wurst authored 5 years ago

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

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

Bernd Wurst authored 10 years ago

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

Hanno authored 5 years ago

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

216) }
217) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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

bernd authored 16 years ago

222) }
223) 
Hanno Fix coding style with php-c...

Hanno authored 5 years ago

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

bernd authored 16 years ago

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

Hanno authored 5 years ago

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