Skip to content

Commit 20eface

Browse files
Bayheckadil.rakhaliyev
andauthored
Fix: worker postmessage targetUrl fixed (#3066)
* fix: worker postmessage targetUrl fixed * fix: postMessage method updated to have a log and Note * update: test added * update: note added to header script * fix: clean code --------- Co-authored-by: adil.rakhaliyev <adil.rakhaliyev@devexpress.com>
1 parent bd6d3e3 commit 20eface

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/client/sandbox/event/message.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,14 @@ export default class MessageSandbox extends SandboxBase {
215215
postMessage (contentWindow: Window, args) {
216216
const targetUrl = args[1] || destLocation.getOriginHeader();
217217

218+
// NOTE: We do NOT support the postMessage(message, options) overload.
219+
// The second argument is expected to be `targetOrigin` (string).
220+
// If an options object is provided instead, the call is considered invalid and will be aborted.
221+
if (typeof targetUrl !== 'string') {
222+
nativeMethods.consoleMeths.log(`testcafe-hammerhead: postMessage called with invalid targetOrigin; aborting call (type: ${typeof targetUrl})`);
223+
return null;
224+
}
225+
218226
// NOTE: Here, we pass all messages as "no preference" ("*").
219227
// We do an origin check in "_onWindowMessage" to access the target origin.
220228
args[1] = '*';

test/client/fixtures/sandbox/event/message-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ asyncTest('should pass "transfer" argument for "postMessage" (GH-1535)', functio
3535
callMethod(window, 'postMessage', ['test', '*', [channel.port1]]);
3636
});
3737

38+
asyncTest('should not accept an object as "targetOrigin"', function () {
39+
var called = false;
40+
var handler = function () {
41+
called = true;
42+
};
43+
44+
window.addEventListener('message', handler);
45+
callMethod(window, 'postMessage', ['message', { test: 1 }]);
46+
47+
window.setTimeout(function () {
48+
ok(!called, 'message should not be delivered');
49+
window.removeEventListener('message', handler);
50+
start();
51+
}, 100);
52+
});
53+
3854
asyncTest('onmessage event', function () {
3955
var count = 0;
4056

0 commit comments

Comments
 (0)