-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(url sanitization): Pass an A markupElementRenderer when necessary…
… (in Fastboot) (#42) * Pass an A markupElementRenderer when necessary (in Fastboot) Fixes #38 * run fastboot tests on travis
- Loading branch information
Showing
12 changed files
with
298 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* global module */ | ||
import Ember from 'ember'; | ||
|
||
const isNode = typeof window === 'undefined' && (typeof module === 'object' && typeof module.require === 'function'); | ||
const hasDOM = typeof document === 'object'; | ||
const needsMarkupElementRenderer = !isNode && !hasDOM; | ||
|
||
function fallbackProtocolParser(str) { | ||
let colonIdx = str.indexOf(':'); | ||
if (colonIdx === -1) { return; } | ||
|
||
return str.replace(/^\s+/,'').split(':')[0] + ':'; | ||
} | ||
|
||
function getProtocolForURLfn(component) { | ||
let glimmerEnv = Ember.getOwner(component).lookup('service:-glimmer-environment'); | ||
if (glimmerEnv && glimmerEnv.protocolForURL) { | ||
return glimmerEnv.protocolForURL; | ||
} else { | ||
return fallbackProtocolParser; | ||
} | ||
} | ||
|
||
function createHrefSanitizer(component) { | ||
let protocolForUrl = getProtocolForURLfn(component); | ||
const badProtocols = [ | ||
'vbscript:', // jshint ignore:line | ||
'javascript:' // jshint ignore:line | ||
]; | ||
|
||
return (href) => { | ||
let protocol = protocolForUrl(href); | ||
if (protocol && badProtocols.indexOf(protocol) !== -1) { | ||
return `unsafe:${href}`; | ||
} else { | ||
return href; | ||
} | ||
}; | ||
} | ||
|
||
export default function createMarkupElementRenderer(component) { | ||
if (!needsMarkupElementRenderer) { | ||
return {}; | ||
} else { | ||
let sanitizeHref = createHrefSanitizer(component); | ||
|
||
return { | ||
A(tagName, dom, attrs) { | ||
let el = dom.createElement(tagName); | ||
Object.keys(attrs).forEach(attrName => { | ||
let attrValue = attrs[attrName]; | ||
|
||
if (attrName === 'href') { | ||
attrValue = sanitizeHref(attrValue); | ||
} | ||
|
||
el.setAttribute(attrName, attrValue); | ||
}); | ||
|
||
return el; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,3 @@ const { merge, assign } = Ember; | |
let polyfilledAssign = assign || merge; | ||
|
||
export default polyfilledAssign; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Ember from 'ember'; | ||
import { | ||
createSimpleMobiledoc, | ||
createMobiledocWithMarkup | ||
} from '../utils/mobiledoc'; | ||
|
||
const mobiledocs = [ | ||
{ | ||
name: 'simple', | ||
mobiledoc: createSimpleMobiledoc('hello world') | ||
}, | ||
{ | ||
name: 'with-markup', | ||
mobiledoc: createMobiledocWithMarkup('markup text', ['em']) | ||
}, | ||
{ | ||
name: 'with-link', | ||
mobiledoc: createMobiledocWithMarkup('linked', ['a', ['href', 'http://example.com/with-link']]) | ||
}, | ||
{ | ||
name: 'with-unsafe-link', | ||
mobiledoc: createMobiledocWithMarkup('linked unsafe', ['a', ['href', 'javascript:evil']]) | ||
} | ||
]; | ||
|
||
export default Ember.Controller.extend({ | ||
mobiledocs | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Ember from 'ember'; | ||
import config from './config/environment'; | ||
|
||
const Router = Ember.Router.extend({ | ||
location: config.locationType, | ||
rootURL: config.rootURL | ||
}); | ||
|
||
Router.map(function() { | ||
}); | ||
|
||
export default Router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{outlet}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h1>ember-fastboot-addon-tests</h1> | ||
|
||
<div id="mobiledocs"> | ||
{{#each mobiledocs as |mobiledocInfo|}} | ||
<div class="render-mobiledoc-wrapper {{mobiledocInfo.name}}"> | ||
{{render-mobiledoc mobiledoc=mobiledocInfo.mobiledoc}} | ||
</div> | ||
{{/each}} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const MOBILEDOC_VERSION = '0.3.1'; | ||
|
||
export function createSimpleMobiledoc(text) { | ||
return { | ||
version: MOBILEDOC_VERSION, | ||
markups: [], | ||
atoms: [], | ||
cards: [], | ||
sections: [ | ||
[1, 'P', [ | ||
[0, [], 0, text] | ||
]] | ||
] | ||
}; | ||
} | ||
|
||
export function createMobiledocWithStrongMarkup(text) { | ||
return { | ||
version: MOBILEDOC_VERSION, | ||
markups: [ | ||
['STRONG'] | ||
], | ||
atoms: [], | ||
cards: [], | ||
sections: [ | ||
[1, 'P', [ | ||
[0, [0], 1, text] | ||
]] | ||
] | ||
}; | ||
} | ||
|
||
export function createMobiledocWithMarkup(text, markup) { | ||
return { | ||
version: MOBILEDOC_VERSION, | ||
markups: [ | ||
markup | ||
], | ||
atoms: [], | ||
cards: [], | ||
sections: [ | ||
[1, 'P', [ | ||
[0, [0], 1, text] | ||
]] | ||
] | ||
}; | ||
} | ||
|
||
export function createMobiledocWithAtom(atomName) { | ||
return { | ||
version: MOBILEDOC_VERSION, | ||
markups: [], | ||
atoms: [ | ||
[atomName, 'value', {foo: 'bar'}] | ||
], | ||
cards: [], | ||
sections: [ | ||
[1, 'P', [ | ||
[1, [], 0, 0] | ||
]] | ||
] | ||
}; | ||
} | ||
|
||
export function createMobiledocWithCard(cardName) { | ||
return { | ||
version: MOBILEDOC_VERSION, | ||
markups: [], | ||
atoms: [], | ||
cards: [ | ||
[cardName, {foo: 'bar'}] | ||
], | ||
sections: [ | ||
[10, 0] | ||
] | ||
}; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use strict'; | ||
|
||
const expect = require('chai').expect; | ||
const setupTest = require('ember-fastboot-addon-tests').setupTest; | ||
|
||
describe('index', function() { | ||
setupTest('fastboot'/*, options */); | ||
|
||
it('renders', function() { | ||
return this.visit('/') | ||
.then(function(res) { | ||
let $ = res.jQuery; | ||
let response = res.response; | ||
|
||
expect(response.statusCode).to.equal(200); | ||
expect($('body').length).to.equal(1); | ||
expect($('h1').text().trim()).to.equal('ember-fastboot-addon-tests'); | ||
}); | ||
}); | ||
|
||
it('renders simple mobiledoc', function() { | ||
let name = 'simple'; | ||
|
||
return this.visit('/') | ||
.then(function(res) { | ||
let $ = res.jQuery; | ||
let response = res.response; | ||
|
||
let wrapper = $(`.render-mobiledoc-wrapper.${name}`); | ||
expect(wrapper.length).to.equal(1); | ||
|
||
let rendered = $(`.render-mobiledoc-wrapper.${name} p`).text().trim(); | ||
expect(rendered).to.equal('hello world'); | ||
}); | ||
}); | ||
|
||
it('renders mobiledoc with markup', function() { | ||
let name = 'with-markup'; | ||
return this.visit('/') | ||
.then(function(res) { | ||
let $ = res.jQuery; | ||
let response = res.response; | ||
|
||
let wrapper = $(`.render-mobiledoc-wrapper.${name}`); | ||
expect(wrapper.length).to.equal(1); | ||
|
||
let markup = $(`.render-mobiledoc-wrapper.${name} em`); | ||
expect(markup.length).to.equal(1); | ||
expect(markup.text().trim()).to.equal('markup text'); | ||
}); | ||
}); | ||
|
||
it('renders mobiledoc with link', function() { | ||
let name = 'with-link'; | ||
return this.visit('/') | ||
.then(function(res) { | ||
let $ = res.jQuery; | ||
let response = res.response; | ||
|
||
let wrapper = $(`.render-mobiledoc-wrapper.${name}`); | ||
expect(wrapper.length).to.equal(1); | ||
|
||
let link = $(`.render-mobiledoc-wrapper.${name} a`); | ||
expect(link.length).to.equal(1); | ||
expect(link.attr('href')).to.equal('http://example.com/with-link'); | ||
expect(link.text().trim()).to.equal('linked'); | ||
}); | ||
}); | ||
|
||
it('renders mobiledoc with unsafe link', function() { | ||
let name = 'with-unsafe-link'; | ||
return this.visit('/') | ||
.then(function(res) { | ||
let $ = res.jQuery; | ||
let response = res.response; | ||
|
||
let wrapper = $(`.render-mobiledoc-wrapper.${name}`); | ||
expect(wrapper.length).to.equal(1); | ||
|
||
let link = $(`.render-mobiledoc-wrapper.${name} a`); | ||
expect(link.length).to.equal(1); | ||
expect(link.attr('href')).to.equal('unsafe:javascript:evil'); | ||
expect(link.text().trim()).to.equal('linked unsafe'); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters