Bug #361

prevent CSRF and similar things

Added by Anonymous about 2 years ago. Updated over 1 year ago.

Status:Closed Start date:05/30/2011
Priority:High Due date:
Assignee:- % Done:

0%

Category:Programming
Target version:0.1.0
FooCorp's Music Site?:

Description

At the moment you can perform cross-site request forgeries, cross-site scripting and similar things on MediaGoblin pages.


Related issues

related to Bug #324: Handing of bad media types (html!) Closed 05/09/2011

History

Updated by Anonymous about 2 years ago

I'm not an expert on website security, but two things I remember, that should be considered:

  1. I think, it's called "upsniffing". Browsers sometimes try to detect the mime-type from the actual content and use that instead of the supplied mime-type. This is to help broken websites or so. mediagoblin should not be broken. And: If someone uploads html as .jpg the browser is forced to jpeg and wont try to do html on the file (despite it's html). So disable upsniffing, or whatever it was called.
  2. One can mark pages so they're not loaded in a frame/iframe. The problem seems to be, that bad pages embed the victim's page, put some image over it, and let the user click there.

Just some random thoughts. Hopefully the list of security things to consider will be expanded by someone.

Updated by Anonymous about 2 years ago

Okay, found the option:
X-Content-Type-Options: nosniff

Some docs I could quickly find:

IMHO this header should be on all responses by mediagoblin... just my 0.02 unit-of-currency.

Updated by Anonymous about 2 years ago

"Content Security Policy" (CSP) might really be a good add on to have. Noone should rely solely on this, but it might make things a lot safer if other security guards fail.

A simple allow 'self' might already get a lot of things better.

(from today's discussion)

Updated by Anonymous almost 2 years ago

  • Assignee deleted (Anonymous)

Sorry, I just don't have enough time to do something for MediaGoblin at the moment.

Updated by Anonymous almost 2 years ago

Okay, thanks for letting us know, and we'll welcome contributions from you if in the future you do have time!

Updated by Anonymous almost 2 years ago

This bugs turns into some vague "We need better web security" bug.
That said, if someone wants to work on some specific aspect of web security (say CSP), she/he should probably create a dedicated Bug and notify this bug.

Updated by Anonymous almost 2 years ago

I think this bug has kind of turned into that but there's a much more specific goal here, and that's to specifically handle CSRF issues in an easy way. Something like: https://docs.djangoproject.com/en/dev/ref/contrib/csrf/

Updated by Anonymous almost 2 years ago

  • Assignee set to Anonymous
  • Target version changed from 0.0.3 to 0.0.4

cwebber wanted to take care of CSRF for 0.0.4.

I suggest, we turn this bug into the general "security tracking bug" and create a new "Create easy CSRF framework"-bug for the actual work.

Updated by Anonymous almost 2 years ago

  • Category set to Programming
  • Assignee deleted (Anonymous)

Removing myself as the assignee since I haven't really started working on it yet, but I will shortly if nobody else jumps on this one and will reclaim.

Updated by Anonymous almost 2 years ago

  • Target version changed from 0.0.4 to 0.0.5

We release 0.0.4, so I'm bumping this to 0.0.5.

Updated by Anonymous almost 2 years ago

Both look like good FOSS tools for scannign for vulnerabilities?

Caleb has pointed to this article (thanks!): http://sectooladdict.blogspot.com/2011/08/commercial-web-application-scanner.html

We should really only use FOSS tools; that article lists both proprietary and FOSS. It looks like there are quite a few good ones. W3AF appears to be the most feature-rich.

Updated by Anonymous almost 2 years ago

I did some scans with w3af today. It didn't see a CSRF or any other vulnerability after a full_audit. There was one login it claimed to find:

Found authentication credentials to: "http://127.0.0.1:6543/auth/login/". A correct user and password combination is: test1/53cr37. 
This vulnerability was found in the request with id 7679.
however, these credentials didn't work in the webUI.

One warning that gave me pause in interpreting the report was

The remote network has an active filter. 
IMPORTANT: The result of all the other plugins will be unaccurate, web applications could be vulnerable but "protected" by the active filter.
x(

Updated by Anonymous almost 2 years ago

Thanks for the scan, Caleb.

Yeah, we do need to provide some sort of way to rate-limit logins and temporarily ban people who try logging in too much incorrectly.

Weird error. I couldn't possibly guess what that means, especially when you're running on localghost. Are you firewalling against yourself? ;)

Updated by Anonymous almost 2 years ago

You bet.

At this point we either use more scanning tools, benchmark them to make sure we aren't getting false negatives, and/or pony up for some real security code review.

Specifically, if anyone can provide a recipe for exploiting CSRF in MediaGoblin, that would move the ball on this bug for sure.

Updated by Anonymous almost 2 years ago

OWASP 's CSRF mitigation page could be helpful here

Updated by Anonymous almost 2 years ago

  • Assignee set to Anonymous
  • Target version changed from 0.0.5 to 0.1.0

Updated by Anonymous almost 2 years ago

An initial implementation of CSRF protection, largely based on Django's, is in 569-application-middleware in my clone (https://gitorious.org/~nyergler/mediagoblin/nyerglers-mediagoblin/commits/569-application-middleware)

Updated by Anonymous almost 2 years ago

Reading the code: I like it and am quite excited by this.

A few thoughts:
  • Having a secret key requirement is one extra step for administrators, and probably one that in this case does no good? It looks like it's used for generation, but not checking. If that's true, maybe we can skip the whole secret key thing and just use a random number?
  • Could use tests :)
  • I wonder if instead of setting csrf_token in render_template if we should actually just tack it onto the request, like request.csrf_token, in the middleware? Rationale: it keeps it contained to the middleware, and makes csrf token usable outside of render_template? Not sure if this is true though. :) But now that I look at it again this is actually an embedded wtforms form, so maybe tacking it into the template rendering method code is right. Okay, that's some feedback but I leave this one up to you. :)

Updated by Anonymous over 1 year ago

Updated the branch and merge request; I suggest we close this ticket if we're satisfied with this initial bit of CSRF protection, and then split off other parts of this as their own tickets (if needed).

Updated by Anonymous over 1 year ago

  • Status changed from In Progress to Closed

Merged, merged, merged! Woohoo!

Updated by Anonymous over 1 year ago

Okay, some notes on the current CSRF implementation:

  • Great, that we have this now!
  • Why a permanent cookie living for a year? A session cookie should be the right thing. Yes, the server will need to create a new one, when/if the client starts a new session. But really, it's a session cookie thing.
  • I'd call the cookie mediagoblin_csrftoken. Make things clearer IMHO.
  • path='/' is the default. One might use the actual subpath under which GMG runs
  • getrandbits seems nicer than randrange IMHO
  • Calling randrange() two times just doubles the random bits. This looks quite obscure. Aren't 63 bits okay? If double the bits are needed, replace the 63 by 128. But really, 64 bits seem enough.

I have a local branch addressing all of this. If wanted, I can clean it up and push it somewhere.

I leave re-opening / opening a new (Feature) bug to someone else.

Updated by Anonymous over 1 year ago

I'd say merge your branch :).

Updated by Anonymous over 1 year ago

Okay, my branch: idea/csrf_improvement

Updated by Anonymous over 1 year ago

Merged elrond's branch!

Also available in: Atom PDF