9 HTML5
9 HTML5
9.4. WebSockets
The browser successfully performs the HTTP request; however, it cannot access
the response.
Two different origins cannot interact one with each other even if
they have mutual trust.
Example:
https://www.w3.org/TR/cors/
• WebGL textures
https://caniuse.com/
function crossOriginXHRGet(){
var xmlhttpr = new XMLHttpRequest();
var url = 'http://originb.ori/page.html';
xmlhttpr.open('GET', url, true);
xmlhttpr.onreadystatechange = handler;
xmlhttpr.send(body);
}
We will deal with these headers in-depth in the next chapter. They
are called control access headers.
This way, the browser can avoid sending lots of extra HTTP
requests for the subsequent requests.
Example:
If the web server had forbidden the request, the real HTTP
DELETE request would not have even been sent by the browser.
Access-Control-Allow-Origin: <allowedOrigin>
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: X-PINGOTHER
Access-Control-Max-Age: <deltaSeconds>
Access-Control-Expose-Headers: 7200
Access-Control-Expose-Headers: [<MyHeader1>][<MyHeader2>][*]
Access-Control-Request-Method: <field-name>
Access-Control-Request-Method: PUT
When using this feature, two windows that have some kind of
relationship can exchange messages.
function sendCustomMessage() {
popup.postMessage("Hi there!", "http://victim.site/to.php");
}
</script>
<script>
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
if (event.origin !== "http://trusted.site") {
console.log("Message from a unauthorized origin!");
return;
}
console.log("Message received")
}
</script>
<script>
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
console.log("Message received")
// Do some actions
// . . .
}
</script>
localStorage.setItem('username','jsmith');
localStorage.removeItem('username');
console.log(localStorage.getItem('username'));
// prints null
localStorage.clear()
Supported by browsers
• ws:// for WebSockets
• wss:// for Secure WebSockets
Full-duplex
• The protocol allows communication in both directions simultaneously
• No polling overhead
• A client sends data only when it has something to send
ws.onmessage = function(e) {
alert("Received message");
// read the message
var msg= e.data;
alert(msg);
}
Example:
An iframe could redirect visitors to an arbitrary website
by simply setting the location property of its parent
document.
<iframe src="http://domain2.com/home.html"/>
domain1.com domain2.com
<script>
window.parent.location='http://attacker.site/';
</script>
By using this technique, the main document can only inform the
visitor that the page is being changed.
The iframe document could change the contents of the main document,
using the same technique used by the defacement-via-XSS attack family.
Example:
<script>
window.parent.document.body.innerHTML = 'defaced';
</script>
index.html vulnerable.html
For example:
❑ ALLOW-SCRIPT
• This flag allows script execution
❑ ALLOW-FORMS
• This flag allows form submission
❑ ALLOW-TOP-NAVIGATION
• This flag allows the iframe content to navigate its top-level browsing
context.