Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Prevent CSRF in tip.json (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Jun 29, 2012
1 parent bc3f9bb commit 98258d2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion www/%participant_id/tip.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ if not user.ANON:
# =====================
# Insert instead of update. The analytics may be interesting some day.

if POST and 'amount' in body:
if POST and 'amount' in body and body.get('csrf') == user.session_token:

try:
amount = decimal.Decimal(body['amount'])
if amount not in AMOUNTS:
Expand Down
15 changes: 14 additions & 1 deletion www/assets/%version/gittip.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ Gittip.submitForm = function(url, data, success, error)
console.log("failed", xhr, foo, bar);
}

data.csrf = Gittip.getCookie('session');
jQuery.ajax({ url: url
, type: "GET"
, data: data
Expand Down Expand Up @@ -308,6 +309,18 @@ Gittip.initPayment = function(stripe_publishable_key, participantId)
});
};

Gittip.getCookie = function(name)
{ // http://www.quirksmode.org/js/cookies.html
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

Gittip.initTipButtons = function()
{
$('BUTTON.tip').click(function()
Expand All @@ -333,7 +346,7 @@ Gittip.initTipButtons = function()
select(this, amount);
jQuery.ajax(
{ url: '/' + tippee + '/tip.json'
, data: {amount: amount}
, data: {amount: amount, csrf: Gittip.getCookie('session')}
, type: "POST"
, error: function(x,y,z) {
select(cur); console.log(x,y,z);
Expand Down

0 comments on commit 98258d2

Please sign in to comment.