Skip to content
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

Pass markupSanitizer option to Renderer. Use glimmer getProtocolForURL #39

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions addon/components/render-mobiledoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { RENDER_TYPE } from 'ember-mobiledoc-dom-renderer';
import layout from '../templates/components/render-mobiledoc';
import { getDocument } from '../utils/document';
import assign from '../utils/polyfilled-assign';
import createMarkupSanitizer from '../utils/create-markup-sanitizer';

const {
assert,
Expand Down Expand Up @@ -122,6 +123,7 @@ export default Ember.Component.extend({
let passedOptions = this.get('cardOptions');
let cardOptions = this.get('_cardOptions');
options.cardOptions = passedOptions ? assign(passedOptions, cardOptions) : cardOptions;
options.markupSanitizer = createMarkupSanitizer(this);

let renderer = new Renderer(options);
let { result, teardown } = renderer.render(mobiledoc);
Expand Down
24 changes: 24 additions & 0 deletions addon/utils/create-markup-sanitizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import getProtocolForURLfn from './get-protocol-for-url';

export default function createMarkupSanitizer(component) {
let protocolForURL = getProtocolForURLfn(component);
let badProtocols = [
'vbscript:', // jshint ignore:line
'javascript:' // jshint ignore:line
];

let sanitizeHref = (href) => {
let protocol = protocolForURL(href);
if (protocol && badProtocols.indexOf(protocol) !== -1) {
return `unsafe:${href}`;
} else {
return href;
}
};

return ({tagName, attributeName, attributeValue}) => {
if (tagName === 'a' && attributeName === 'href') {
return sanitizeHref(attributeValue);
}
};
}
16 changes: 16 additions & 0 deletions addon/utils/get-protocol-for-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Ember from 'ember';

export default function getProtocolForURLfn(component) {
let glimmerEnv = Ember.getOwner(component).lookup('service:-glimmer-environment');
if (glimmerEnv && glimmerEnv.protocolForURL) {
return glimmerEnv.protocolForURL;
} else {
return (str) => {
let colonIdx = str.indexOf(':');
if (colonIdx === -1) { return; }

return str.replace(/^\s+/,'').split(':')[0] + ':';
};
}
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"ember-cli-htmlbars": "^1.0.1",
"ember-getowner-polyfill": "^1.2.2",
"ember-wormhole": "^0.5.1",
"mobiledoc-dom-renderer": "^0.6.3"
"mobiledoc-dom-renderer": "^0.6.4-0"
},
"ember-addon": {
"configPath": "tests/dummy/config"
Expand Down
6 changes: 2 additions & 4 deletions tests/helpers/mobiledoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ export function createSimpleMobiledoc(text) {
};
}

export function createMobiledocWithStrongMarkup(text) {
export function createMobiledocWithMarkup({markup, text}) {
return {
version: MOBILEDOC_VERSION,
markups: [
['STRONG']
],
markups: [ markup ],
atoms: [],
cards: [],
sections: [
Expand Down
14 changes: 12 additions & 2 deletions tests/integration/components/render-mobiledoc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CARD_ELEMENT_CLASS, ATOM_ELEMENT_CLASS } from 'ember-mobiledoc-dom-rend
import Ember from 'ember';
import {
createSimpleMobiledoc,
createMobiledocWithStrongMarkup,
createMobiledocWithMarkup,
createMobiledocWithCard,
createMobiledocWithAtom
} from '../../helpers/mobiledoc';
Expand Down Expand Up @@ -341,7 +341,7 @@ test('Can pass markupElementRenderer', function(assert) {
this.set('markupElementRenderer', {
strong(_, doc) { return doc.createElement('span'); }
});
this.set('mobiledoc', createMobiledocWithStrongMarkup('Hi'));
this.set('mobiledoc', createMobiledocWithMarkup({text: 'Hi', markup: ['strong']}));

this.render(hbs`{{render-mobiledoc mobiledoc=mobiledoc markupElementRenderer=markupElementRenderer}}`);

Expand Down Expand Up @@ -383,3 +383,13 @@ test('Can pass cardOptions and they appear for atoms', function(assert) {

this.render(hbs`{{render-mobiledoc mobiledoc=mobiledoc atomNames=atomNames cardOptions=cardOptions}}`);
});

test('A HREF values are sanitized', function(assert) {
let badHref = 'javascript:evil'; // jshint ignore:line
this.set('mobiledoc', createMobiledocWithMarkup({text: 'hi', markup: ['a', ['href', badHref]]}));
this.render(hbs`{{render-mobiledoc mobiledoc=mobiledoc}}`);

let a = this.$('a');
assert.ok(a.length, 'has a');
assert.equal(a.attr('href'), 'unsafe:javascript:evil');
});