1+ <?php
2+
3+ /**
4+ * CsrfGuard
5+ *
6+ * This middleware provides protection from CSRF attacks
7+
8+ * USAGE
9+ *
10+ * // Adding middleware
11+ * $app = new Slim();
12+ * $app->add(new CsrfGuard());
13+ *
14+ * // Setting token in view
15+ * <input type="hidden" name="<?=$csrf_key?>" value="<?=$csrf_token?>">
16+ *
17+ * @author Mikhail Osher, https://github.com/miraage
18+ * @version 1.0
19+ */
20+ class CsrfGuard extends Slim_Middleware {
21+ /**
22+ * Request key
23+ *
24+ * @var string
25+ */
26+ protected $ key ;
27+
28+ /**
29+ * Constructor
30+ *
31+ * @param string $key Request key
32+ */
33+ public function __construct ( $ key = 'csrf_token ' ) {
34+ // Validate key (i won't use htmlspecialchars)
35+ if ( !is_string ($ key ) || empty ($ key ) || preg_match ('/[^a-zA-Z0-9\-\_]/ ' , $ key ) ) {
36+ throw new OutOfBoundsException ('Invalid key ' . $ key );
37+ }
38+
39+ $ this ->key = $ key ;
40+ }
41+
42+ /**
43+ * Call middleware
44+ */
45+ public function call () {
46+ // Attach as hook
47+ $ this ->app ->hook ('slim.before ' , array ($ this , 'check ' ));
48+
49+ // Call next middleware
50+ $ this ->next ->call ();
51+ }
52+
53+ /**
54+ * Check token
55+ */
56+ public function check () {
57+ // Create token
58+ $ env = $ this ->app ->environment ();
59+ $ token = sha1 ($ env ['REMOTE_ADDR ' ] . '| ' . $ env ['USER_AGENT ' ]);
60+
61+ // Validate
62+ if ( in_array ($ this ->app ->request ()->getMethod (), array ('POST ' , 'PUT ' , 'DELETE ' )) ) {
63+ $ usertoken = $ this ->app ->request ()->post ($ this ->key );
64+ if ( $ token !== $ usertoken ) {
65+ $ this ->app ->halt (400 , 'Missing token ' );
66+ }
67+ }
68+
69+ // Assign to view
70+ $ this ->app ->view ()->setData (array (
71+ 'csrf_key ' => $ this ->key ,
72+ 'csrf_token ' => $ token ,
73+ ));
74+ }
75+ }
76+
77+ ?>
0 commit comments