Skip to content

Commit

Permalink
Merge pull request #79 from Goff-Davis/issue/78-post-message-check
Browse files Browse the repository at this point in the history
#78 Materia.Engine does not check if received POST messages are valid
  • Loading branch information
clpetersonucf authored Aug 1, 2019
2 parents 507bbd9 + f9c9463 commit 87a2f98
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/js/materia/materia.creatorcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Namespace('Materia').CreatorCore = (() => {
let _resizeInterval = null

const _onPostMessage = e => {
if (typeof e.data !== 'string') return
const msg = JSON.parse(e.data)
switch (msg.type) {
case 'initNewWidget':
Expand Down
6 changes: 6 additions & 0 deletions src/js/materia/materia.creatorcore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ describe('creatorcore', () => {
expectOnlyCreatorMethodCalledToBe('onQuestionImportComplete')
})

it('reacts properly to post messages with non-string data', () => {
mockPostMessageFromWidget('', '', undefined)
console.log(window.addEventListener)
expect(window.addEventListener).toHaveLastReturnedWith(undefined)
})

it('reacts properly to unknown post messages', () => {
jest.spyOn(console, 'warn')
mockPostMessageFromWidget('undefinedMessageType', 'unknown-source', ['payload'])
Expand Down
1 change: 1 addition & 0 deletions src/js/materia/materia.enginecore.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Namespace('Materia').Engine = (() => {
let _widgetClass = null

const _onPostMessage = e => {
if (typeof e.data !== 'string') return
const msg = JSON.parse(e.data)
switch (msg.type) {
case 'initWidget':
Expand Down
9 changes: 9 additions & 0 deletions src/js/materia/materia.enginecore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ describe('enginecore', () => {
expect(Engine.getMediaUrl('fR93X')).toBe('mediaUrl/fR93X')
})

it('reacts properly to post messages with non-string data', () => {
Engine.start({ start: jest.fn() })
let _onPostMessage = window.addEventListener.mock.calls[0][1]
_onPostMessage({
data: undefined
})
expect(parent.postMessage).toHaveLastReturnedWith(undefined)
})

it('end sends post message and defaults to show score screen', () => {
Engine.end()
let ex = JSON.stringify({
Expand Down

0 comments on commit 87a2f98

Please sign in to comment.