Skip to content
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
8 changes: 8 additions & 0 deletions src/client/sandbox/event/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ export default class MessageSandbox extends SandboxBase {
postMessage (contentWindow: Window, args) {
const targetUrl = args[1] || destLocation.getOriginHeader();

// NOTE: We do NOT support the postMessage(message, options) overload.
// The second argument is expected to be `targetOrigin` (string).
// If an options object is provided instead, the call is considered invalid and will be aborted.
if (typeof targetUrl !== 'string') {
nativeMethods.consoleMeths.log(`testcafe-hammerhead: postMessage called with invalid targetOrigin; aborting call (type: ${typeof targetUrl})`);
return null;
}

// NOTE: Here, we pass all messages as "no preference" ("*").
// We do an origin check in "_onWindowMessage" to access the target origin.
args[1] = '*';
Expand Down
16 changes: 16 additions & 0 deletions test/client/fixtures/sandbox/event/message-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ asyncTest('should pass "transfer" argument for "postMessage" (GH-1535)', functio
callMethod(window, 'postMessage', ['test', '*', [channel.port1]]);
});

asyncTest('should not accept an object as "targetOrigin"', function () {
var called = false;
var handler = function () {
called = true;
};

window.addEventListener('message', handler);
callMethod(window, 'postMessage', ['message', { test: 1 }]);

window.setTimeout(function () {
ok(!called, 'message should not be delivered');
window.removeEventListener('message', handler);
start();
}, 100);
});

asyncTest('onmessage event', function () {
var count = 0;

Expand Down