Skip to content

Commit 40b578e

Browse files
committed
Add request parameter filtering to avoid XSS attacks
1 parent a3cf57b commit 40b578e

File tree

5 files changed

+95
-8
lines changed

5 files changed

+95
-8
lines changed

src/TgUtils/DummyStringFilter.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
package TgUtils;
4+
5+
/**
6+
* An interface for not filtering string at all.
7+
*/
8+
public class DummyStringFilter implements StringFilter {
9+
10+
public static $INSTANCE = new DummyStringFilter();
11+
12+
public __construct() {
13+
}
14+
15+
/**
16+
* Filters the given string and returns sanitized value.
17+
* @param string $s - string to sanitize (can be null)
18+
* @return the sanitized string.
19+
*/
20+
public filter($s) {
21+
return $s;
22+
}
23+
24+
}
25+

src/TgUtils/NoHtmlStringFilter.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
package TgUtils;
4+
5+
/**
6+
* An interface for filter strings from any HTML tags.
7+
*/
8+
public class NoHtmlStringFilter implements StringFilter {
9+
10+
public static $INSTANCE = new NoHtmlStringFilter();
11+
12+
public __construct() {
13+
}
14+
15+
/**
16+
* Filters the given string and returns sanitized value.
17+
* @param string $s - string to sanitize (can be null)
18+
* @return the sanitized string.
19+
*/
20+
public filter($s) {
21+
if ($s == NULL) return $s;
22+
return strip_tags($s);
23+
}
24+
25+
}
26+

src/TgUtils/Request.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,15 @@ public function hasGetParam($key) {
186186

187187
/**
188188
* Returns the GET parameter value from the request.
189-
* @param string $key - the parameter name
190-
* @param mixed $default - the default value to return when parameter does not exist (optional, default is NULL).
189+
* @param string $key - the parameter name
190+
* @param mixed $default - the default value to return when parameter does not exist (optional, default is NULL).
191+
* @param object $filter - a filter to sanitize the value.
191192
* @return mixed the parameter value or its default.
192193
*/
193-
public function getGetParam($key, $default = NULL) {
194+
public function getGetParam($key, $default = NULL, $filter = NULL) {
194195
$params = $this->getParams;
195-
return isset($params[$key]) ? $params[$key] : $default;
196+
if ($filter == NULL) $filter = StringFilters::$NO_HTML;
197+
return isset($params[$key]) ? $filter->filter($params[$key]) : $default;
196198
}
197199

198200
/**
@@ -214,13 +216,15 @@ public function hasPostParam($key) {
214216

215217
/**
216218
* Returns the POST parameter value from the request.
217-
* @param string $key - the parameter name
218-
* @param mixed $default - the default value to return when parameter does not exist (optional, default is NULL).
219+
* @param string $key - the parameter name
220+
* @param mixed $default - the default value to return when parameter does not exist (optional, default is NULL).
221+
* @param object $filter - a filter to sanitize the value.
219222
* @return mixed the parameter value or its default.
220223
*/
221-
public function getPostParam($key, $default = NULL) {
224+
public function getPostParam($key, $default = NULL, $filter = NULL) {
222225
$params = $this->getPostParams();
223-
return isset($params[$key]) ? $params[$key] : $default;
226+
if ($filter == NULL) $filter = StringFilters::$NO_HTML;
227+
return isset($params[$key]) ? $filter->filter($params[$key]) : $default;
224228
}
225229

226230
/**

src/TgUtils/StringFilter.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
package TgUtils;
4+
5+
/**
6+
* An interface for filter strings from evil input.
7+
*/
8+
public interface StringFilter {
9+
10+
/**
11+
* Filters the given string and returns sanitized value.
12+
* @param string $s - string to sanitize (can be null)
13+
* @return the sanitized string.
14+
*/
15+
public filter($s);
16+
17+
}
18+

src/TgUtils/StringFilters.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
package TgUtils;
4+
5+
/**
6+
* Provides default string filters.
7+
*/
8+
public class StringFilters {
9+
10+
public static $DUMMY = DummyStringFilter::$INSTANCE;
11+
public static $NO_HTML = NoHtmlStringFilter::$INSTANCE;
12+
13+
}
14+

0 commit comments

Comments
 (0)