CS Fundamentals Trainer · JavaScript · Security
A page listens for message events from an embedded widget at https://widget.example. Which checks make the handler safe?
A page listens for `message` events from an embedded widget at `https://widget.example`. Which checks make the handler safe?
- Comparing `event.origin` exactly against `'https://widget.example'`
- Validating the type and shape of `event.data` before using it
- Accepting the message if `event.origin.includes('widget.example')`
- Trusting an `origin` field the sender puts inside `event.data`
- Checking that `event.source === iframe.contentWindow`
Difficulty: medium