Skip to content
This repository has been archived by the owner on Apr 4, 2019. It is now read-only.

Pass dom helper to sanitizer #276

Merged
merged 1 commit into from
Feb 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/morph-attr/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ function AttrMorph(element, attrName, domHelper, namespace) {

AttrMorph.prototype.setContent = function (value) {
if (this.escaped) {
var sanitized = sanitizeAttributeValue(this.element, this.attrName, value);
var sanitized = sanitizeAttributeValue(this.domHelper, this.element, this.attrName, value);
this._update(sanitized, this.namespace);
} else {
this._update(value, this.namespace);
}
};

export default AttrMorph;

export { sanitizeAttributeValue };
12 changes: 3 additions & 9 deletions packages/morph-attr/lib/sanitize-attribute-value.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* jshint scripturl:true */

var parsingNode;
var badProtocols = {
'javascript:': true,
'vbscript:': true
Expand All @@ -20,13 +19,9 @@ export var badAttributes = {
'background': true
};

export function sanitizeAttributeValue(element, attribute, value) {
export function sanitizeAttributeValue(dom, element, attribute, value) {
var tagName;

if (!parsingNode) {
parsingNode = document.createElement('a');
}

if (!element) {
tagName = null;
} else {
Expand All @@ -38,9 +33,8 @@ export function sanitizeAttributeValue(element, attribute, value) {
}

if ((tagName === null || badTags[tagName]) && badAttributes[attribute]) {
parsingNode.href = value;

if (badProtocols[parsingNode.protocol] === true) {
var protocol = dom.protocolForURL(value);
if (badProtocols[protocol] === true) {
return 'unsafe:' + value;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { sanitizeAttributeValue } from "morph-attr/sanitize-attribute-value";
import SafeString from "htmlbars-util/safe-string";

import DOMHelper from "../../dom-helper";

var domHelper = new DOMHelper();

QUnit.module('sanitizeAttributeValue(null, "href")');

var goodProtocols = [ 'https', 'http', 'ftp', 'tel', 'file'];
Expand All @@ -14,7 +18,7 @@ function buildProtocolTest(protocol) {
expect(1);

var expected = protocol + '://foo.com';
var actual = sanitizeAttributeValue(null, 'href', expected);
var actual = sanitizeAttributeValue(domHelper, null, 'href', expected);

equal(actual, expected, 'protocol not escaped');
});
Expand All @@ -26,7 +30,7 @@ test('blocks javascript: protocol', function() {
expect(1);

var expected = 'javascript:alert("foo")';
var actual = sanitizeAttributeValue(null, 'href', expected);
var actual = sanitizeAttributeValue(domHelper, null, 'href', expected);

equal(actual, 'unsafe:' + expected, 'protocol escaped');
});
Expand All @@ -37,7 +41,7 @@ test('blocks blacklisted protocols', function() {
expect(1);

var expected = 'javascript:alert("foo")';
var actual = sanitizeAttributeValue(null, 'href', expected);
var actual = sanitizeAttributeValue(domHelper, null, 'href', expected);

equal(actual, 'unsafe:' + expected, 'protocol escaped');
});
Expand All @@ -48,7 +52,7 @@ test('does not block SafeStrings', function() {
expect(1);

var expected = 'javascript:alert("foo")';
var actual = sanitizeAttributeValue(null, 'href', new SafeString(expected));
var actual = sanitizeAttributeValue(domHelper, null, 'href', new SafeString(expected));

equal(actual, expected, 'protocol unescaped');
});