pluf2

pluf2 Commit Details


Date:2008-12-05 16:40:59 (16 years 16 days ago)
Author:Loic d'Anterroches
Branch:master
Commit:4f4240c0c67446bcccf605870421dbeaba4f289d
Parents: 3fc7bb73ae987b1a5e050c15ba2c932edc941dbb
Message:Add a middleware to prevent CSRF.

Changes:

File differences

src/Pluf/Middleware/Csrf.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Cross Site Request Forgery Middleware.
*
* This class provides a middleware that implements protection against
* request forgeries from other sites. This middleware must be before
* the Pluf_Middleware_Session middleware.
*
* Based on concepts from the Django CSRF middleware.
*/
class Pluf_Middleware_Csrf
{
public static function makeToken($session_key)
{
return md5(Pluf::f('secret_key').$session_key);
}
/**
* Process the request.
*
* When processing the request, if a POST request with a session,
* we will check that the token is available and valid.
*
* @param Pluf_HTTP_Request The request
* @return bool false
*/
function process_request(&$request)
{
if ($request->method != 'POST') {
return false;
}
$cookie_name = Pluf::f('session_cookie_id', 'sessionid');
if (!isset($request->COOKIE[$cookie_name])) {
// no session, nothing to do
return false;
}
try {
$data = Pluf_Middleware_Session::_decodeData($request->COOKIE[$cookie_name]);
} catch (Exception $e) {
// no valid session
return false;
}
if (!isset($data['Pluf_Session_key'])) {
// no session key
return false;
}
$token = self::makeToken($data['Pluf_Session_key']);
if (!isset($request->POST['csrfmiddlewaretoken'])) {
return new Pluf_HTTP_Response_Forbidden($request);
}
if ($request->POST['csrfmiddlewaretoken'] != $token) {
return new Pluf_HTTP_Response_Forbidden($request);
}
return false;
}
/**
* Process the response of a view.
*
* If we find a POST form, add the token to it.
*
* @param Pluf_HTTP_Request The request
* @param Pluf_HTTP_Response The response
* @return Pluf_HTTP_Response The response
*/
function process_response($request, $response)
{
$cookie_name = Pluf::f('session_cookie_id', 'sessionid');
if (!isset($request->COOKIE[$cookie_name])) {
// no session, nothing to do
return $response;
}
try {
$data = Pluf_Middleware_Session::_decodeData($request->COOKIE[$cookie_name]);
} catch (Exception $e) {
// no valid session
return $response;
}
if (!isset($data['Pluf_Session_key'])) {
// no session key
return $response;
}
$ok = false;
$cts = array('text/html', 'application/xhtml+xml');
foreach ($cts as $ct) {
if (false !== strripos($response->headers['Content-Type'], $ct)) {
$ok = true;
break;
}
}
if (!$ok) {
return $response;
}
$token = self::makeToken($data['Pluf_Session_key']);
$extra = '<div style="display:none;"><input type="hidden" name="csrfmiddlewaretoken" value="'.$token.'" /></div>';
$response->content = preg_replace('/(<form\W[^>]*\bmethod=(\'|"|)POST(\'|"|)\b[^>]*>)/i', '$1'.$extra, $response->content);
return $response;
}
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.05453s using 13 queries.