Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<html> | ||
<head> | ||
<title> | ||
|
||
Connect - csrf | ||
|
||
</title> | ||
<link rel='stylesheet' href='main.css' /> | ||
</head> | ||
<body> | ||
<div id="content"> | ||
|
||
<h1>csrf</h1> | ||
<span class="filename">lib/middleware/csrf.js</span> | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div class="comment"> | ||
|
||
<p class="description"><p>CRSF protection middleware.</p></p> | ||
|
||
|
||
<div class="body"> | ||
<p>By default this middleware generates a token named "_csrf" | ||
which should be added to requests which mutate | ||
state, within a hidden form field, query-string etc. This | ||
token is validated against the visitor's <code>req.session._csrf</code> | ||
property which is re-generated per request.</p> | ||
|
||
<p>The default <code>value</code> function checks <code>req.body</code> generated | ||
by the <code>bodyParser()</code> middleware, and <code>req.query</code> generated | ||
by <code>query()</code>.</p> | ||
|
||
<p>This middleware requires session support, thus should be added | ||
somewhere <em>below</em> <code>session()</code> and <code>cookieParser()</code>.</p> | ||
|
||
<h2>Examples</h2> | ||
|
||
<pre><code> var form = '\n\ | ||
<form action="/" method="post">\n\ | ||
<input type="hidden" name="_csrf" value="{token}" />\n\ | ||
<input type="text" name="user[name]" value="{user}" />\n\ | ||
<input type="password" name="user[pass]" />\n\ | ||
<input type="submit" value="Login" />\n\ | ||
</form>\n\ | ||
'; | ||
|
||
connect( | ||
connect.cookieParser() | ||
, connect.session({ secret: 'keyboard cat' }) | ||
, connect.bodyParser() | ||
, connect.csrf() | ||
|
||
, function(req, res, next){ | ||
if ('POST' != req.method) return next(); | ||
req.session.user = req.body.user; | ||
next(); | ||
} | ||
|
||
, function(req, res){ | ||
res.setHeader('Content-Type', 'text/html'); | ||
var body = form | ||
.replace('{token}', req.session._csrf) | ||
.replace('{user}', req.session.user && req.session.user.name || ''); | ||
res.end(body); | ||
} | ||
).listen(3000);</code></pre> | ||
|
||
<h2>Options</h2> | ||
|
||
<ul><li><code>value</code> a function accepting the request, returning the token </li></ul> | ||
</div> | ||
|
||
|
||
|
||
<ul class="tags"> | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="param"><em>param</em> <span class="types">Object</span> <span class="name">options</span> <span class="description"></span></li> | ||
|
||
|
||
|
||
|
||
|
||
<li class="api"><em>api</em> <span class="visibility">public</span></li> | ||
|
||
|
||
|
||
|
||
|
||
|
||
</ul> | ||
|
||
<!-- | ||
<pre class="code"> | ||
<code> | ||
module.exports = function csrf(options) { | ||
var options = options || {} | ||
, value = options.value || defaultValue; | ||
return function(req, res, next){ | ||
// generate CSRF token | ||
var token = req.session._csrf; | ||
req.session._csrf = utils.uid(24); | ||
// ignore GET (for now) | ||
if ('GET' == req.method) return next(); | ||
// determine value | ||
var val = value(req); | ||
// check | ||
if (val != token) return utils.forbidden(res); | ||
next(); | ||
} | ||
}; | ||
</code> | ||
</pre> | ||
--> | ||
|
||
</div> | ||
|
||
|
||
|
||
|
||
</div> | ||
</body> | ||
</html> |