Take the 2-minute tour ×
Information Security Stack Exchange is a question and answer site for Information security professionals. It's 100% free, no registration required.

postMessage is a primitive introduced in HTML5 that web pages can use for cross-origin communication.

What do I need to do to use postMessage securely? What are the primary security pitfalls or mistakes that I need to look out for?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

The W3C security instructions for this are surprisingly accurate and complete:

Authors should check the origin attribute to ensure that messages are only accepted from domains that they expect to receive messages from. Otherwise, bugs in the author's message handling code could be exploited by hostile sites.

Furthermore, even after checking the origin attribute, authors should also check that the data in question is of the expected format. Otherwise, if the source of the event has been attacked using a cross-site scripting flaw, further unchecked processing of information sent using the postMessage() method could result in the attack being propagated into the receiver.

Authors should not use the wildcard keyword (*) in the targetOrigin argument in messages that contain any confidential information, as otherwise there is no way to guarantee that the message is only delivered to the recipient to which it was intended.

Authors who accept messages from any origin are encouraged to consider the risks of a denial-of-service attack. An attacker could send a high volume of messages; if the receiving page performs expensive computation or causes network traffic to be sent for each such message, the attacker's message could be multplied into a denial-of-service attack. Authors are encouraged to employ rate limiting (only accepting a certain number of messages per minute) to make such attacks impractical.

The most important thing is to always validate the origin - you'd be surprised how many sites fail to do so. If the message is confidential then the targetOrigin argument must be specified.

This of course assumes that the browser implementation is correct and without holes. The major browsers have been evaluated and no issues have been found.

See also this Checklist for postMessage Security Review.

share|improve this answer
    
Fantastic answer! Everything I could've hoped for -- and the checklist is great. Thank you. –  D.W. Sep 28 '12 at 17:24
    
You (and many others) say "always validate the origin" even when the w3c source only recommends it: "Authors should check the origin". Do you think there may are any security issues the w3c is not aware of, other than the mentioned "bugs in the author's message handling code [that] could be exploited" or the denial-of-service attack? I think if you have very simple messages and take great care that they are validated and sanitized, it seems acceptable not to validate the origin if you can't for some reason. –  kapep Oct 14 '13 at 17:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.