Quantcast

Forums | MacLife

You are not logged in.

#1 2009-03-12 8:57 am

resedit
Chicken Little
Royal Wombat
From: /dev/null
Registered: 1999-11-01
Posts: 50219
Website

Output filter class I'm working on

http://homepage.mac.com/mpeters/misc/cs … ss.php.txt

constructive criticism very welcome.

The idea is spawned from:
http://people.mozilla.org/~bsterne/cont … ty-policy/

What the class is suppose to do is enforce a content security policy server side before the content is sent to the browser. The reason for doing it server side is that even when the CSP recommendation is finally adopted, it will be some time before it hits significant market penetration in browsers because people will have to be running fresh versions of their browser (or decide to install an add-on) to benefit.

What the class is suppose to do is yank out parts of the web page that would violate the defined CSP and thus not be rendered by the browser anyway.

I do go a little bit beyond that by yanking out anything that is suppose to child of head but isn't and yanking out extra elements that are only suppose to occur once (IE title and head) though that last one is currently broken because the of the way a DOMNodeList works (I know how to fix it).

I also have a disagreement with current CSP recommendation which states that anything in an event attribute won't be executed. I think that's overbearing, if you are going to allow scripting at all then allow the event attributes to trigger a function. So - the class has a whitelist you can set for event attributes you want to allow. However, functions called in those event attributes can not have any arguements [IE onsubmit="return validate('foo');" gets filtered to onsubmit="return validate();" ]

Since the script element is not allowed to have children in a CSP page, any function in an event attribute has to either be a standard js function or properly defined in an external .js file from a whitelisted host, allowing arguments can be dangerous but removing the ability to use event attributes all together I think is taking things too far. But if you really want current CSP compat, simply don't define a whitelist of event attributes you allow.

-=-

Right now the class only deals with the cases where a CSP policy is set to "none" - I haven't yet written the functions to make sure src attribute on allowed elements matches the white list, that's going to be a little bit tricky because CSP accepts wildcards in the whitelist (as it should) - I'll figure it out, but first I want to thoroughly test what I have.

Since html/xhtml is structured data, I figured the best way to write such a filter is with a tool designed to work with structured data, my good friend DOMDocument. Makes it much easier to ensure I'm not mutilating content, easier to catch filter dodging, etc.

So - the class only works on a DOMDocument object.
And yes, that means the page needs to be fully constructed before any of it is sent to browser, but given the stateless nature of http that really is the proper way to do it anyway.

Example usage -

get your data into a DOMDocument - IE

Code:

$mydoc = new DOMDocument("1.0","UTF-8");
$mydoc->preserveWhiteSpace = false;
$mydoc->formatOutput = true;
$mydoc->loadHTML($yourhtml); // you should use loadXML for well formed xhtml

then

Code:

$sanitized = new cspfilter;
$sanitized->csp['allow'] = "none";
$sanitized->csp['img-src'] = "self";
$sanitized->csp['script-src'] = "scripts.example.com evil.haxor.org";
$sanitized->httphost = "www.mydomain.edu";
$sanitized->inputDom($mydoc);
$sanitized->processData();

The class will then alter $mydoc - simply serve it via

Code:

print $mydoc->saveHTML(); // use saveXML() for xhtml

It's not hard to quit smoking. I do it 20 times a day.

Offline

 

#2 2009-03-12 11:34 am

resedit
Chicken Little
Royal Wombat
From: /dev/null
Registered: 1999-11-01
Posts: 50219
Website

Re: Output filter class I'm working on

I just updated the file to fix a major bug (and clean up stuff I noted needed to be cleaned up) - still has a bug related to xml and namespaces, noted at top of file.

I'm going away for weekend but when I get back I'll do more thorough testing, write the src attribute checking (remembering to take base tag into account) and hopefully have it fully functional (except for the xhtml namespace bug - I'll probably fix that last, I suspect it requires some thought to properly fix, I need to read up on php dom attribute namespace handling)


It's not hard to quit smoking. I do it 20 times a day.

Offline

 

#3 2009-03-13 10:31 am

resedit
Chicken Little
Royal Wombat
From: /dev/null
Registered: 1999-11-01
Posts: 50219
Website

Re: Output filter class I'm working on

Fixed some more bugs - implemented the host whitelisting for each resource.
Other than yet undiscovered bugs (which I won't thoroughly test for until after the weekend) - and the known xml namespace bug - I do believe it fully does what I set out to have it do.


It's not hard to quit smoking. I do it 20 times a day.

Offline

 

#4 2009-03-13 10:51 pm

resedit
Chicken Little
Royal Wombat
From: /dev/null
Registered: 1999-11-01
Posts: 50219
Website

Re: Output filter class I'm working on

Here's a test page -

http://www.clfsrpm.net/xss/dom_script_test.php

iframes / objects not working - I won't be able to fix that until monday, but playing with images and scripts does work. Has to be well formed html, it seems tidy yanks script tags - I'll have to look at the tidy option (badly malformed html won't load into the DOM which is why I wanted to pass the input through tidy in the first place)

EDIT - frames now working, but instead of yanking iframes I probably should just turn them into a div so that any content between the opening/closing tag will still be viewable.

Last edited by resedit (2009-03-13 11:01 pm)


It's not hard to quit smoking. I do it 20 times a day.

Offline

 

#5 2009-03-28 8:00 pm

resedit
Chicken Little
Royal Wombat
From: /dev/null
Registered: 1999-11-01
Posts: 50219
Website

Re: Output filter class I'm working on

The class is a hell of a lot better now, though it still needs a little work.

http://www.clfsrpm.net/xss/


It's not hard to quit smoking. I do it 20 times a day.

Offline

 

#6 2009-05-23 3:08 am

resedit
Chicken Little
Royal Wombat
From: /dev/null
Registered: 1999-11-01
Posts: 50219
Website

Re: Output filter class I'm working on

Hey wow - my class was nominated for an innovation award smile

http://www.phpclasses.org/browse/package/5250.html

That was April, I don't think I won, but it's kind of neat.


It's not hard to quit smoking. I do it 20 times a day.

Offline

 

#7 2009-06-02 2:08 pm

b_dubb
loch whatchamacallit
From: chapel hill, nc
Registered: 2002-11-19
Posts: 510
Website

Re: Output filter class I'm working on

gratz


"The Fates lead he who will; he who won't, they drag." - Seneca

Offline

 

#8 2009-06-02 3:11 pm

sturner
Royal High Poobah
Moderator
From: Carrollton, TX USA
Registered: 2000-01-31
Posts: 13705

Re: Output filter class I'm working on

Now that's pretty impressive, res!


I'm not dead yet.
There are 3 types of people, those who can count and those who can't.
"There are few things graven in stone, excepting your date of death."

Offline

 

Board footer

Powered by PunBB 1.2.6
© Copyright 2002–2005 Rickard Andersson