-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow for special attribute filtering. #13
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,7 +130,7 @@ let tornado = { | |
*/ | ||
setAttribute(node, attrName, vals) { | ||
Promise.all(vals).then(values => { | ||
node.setAttribute(attrName, values.join('')); | ||
node.setAttribute(attrName, this.getAttrValue(node.tagName, attrName, values.join(''))); | ||
}); | ||
}, | ||
|
||
|
@@ -330,6 +330,31 @@ let tornado = { | |
return div.innerHTML; | ||
}, | ||
|
||
/** | ||
* Get a secure value for a given attribute on a given node type | ||
* @param {String} nodeType The type of HTML element the attribute will be applied to. | ||
* @param {String} attrType The type of attribute. | ||
* @param {String} val The initial value, to be run through the filters | ||
*/ | ||
getAttrValue(nodeType, attrType, val) { | ||
let filter = this.getAttrFilter(`${nodeType.toLowerCase()}[${attrType.toLowerCase()}]`); | ||
val = Array.isArray(val) ? val.join('') : val; | ||
if (filter) { | ||
return filter(val); | ||
} | ||
return val; | ||
}, | ||
|
||
/** | ||
* Return a filter method for a filter selector. | ||
* TODO: Make these filters registerable | ||
* @param {String} filterSelector A filter selector of type `'el-name[attr-name]'`. For example, | ||
* a filter for `href`s on an `a` tag would be `'a[href]'`. | ||
*/ | ||
getAttrFilter(filterSelector) { | ||
return this.attrFilters[filterSelector]; | ||
}, | ||
|
||
util: { | ||
/** | ||
* Determine if a value is an object | ||
|
@@ -399,6 +424,12 @@ let tornado = { | |
} | ||
return result; | ||
} | ||
}, | ||
|
||
attrFilters: { | ||
'a[href]': function(val) { | ||
return val.replace(/^javascript:.*/, ''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. data: '(white spaces )javascript:alert(1)' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was meant to be an example of what is possible rather than a legitimate filter. I was hoping to get your input on what the real filters should look like. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I've seen have all been whitelists. Restrict urls to be one of : and disallow everything else. there's a regex for this if you need it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that would be great. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you guys discussed the regex? |
||
} | ||
} | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we may have to repeat this key a bunch of times. a[href], img[src]. is there a way to gather a bunch of contexts to filter on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could pretty easily define a single function and then assign it to multiple contexts:
I was also thinking of having some more generic rules. For example,
'a'
would match all attributes in an anchor tag, and'[src]'
would matchsrc
attributes on any tag. That leads to a few questions: