04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 17: Ajax and JSON<br />

Even though this is possible, you should avoid using this feature. Storing usernames<br />

and passwords in JavaScript is highly insecure, because anyone with a JavaScript<br />

debugger can view what is stored in the variables, exposing your username and<br />

password in plain text.<br />

Cross - Domain Requests<br />

One of the major limitations of Ajax communication via XHR is the cross - domain security policy. By<br />

default, XHR objects can access resources only on the domain from which the containing web page<br />

originates. This security feature prevents some malicious behavior. However, the need for legitimate<br />

cross - domain access was great enough for solutions to begin appearing in browsers.<br />

Though the cross - domain solutions are different, they have similar goals. The first is to ensure that<br />

cookies are not sent with the request or with the response, because cookie stealing is a major security<br />

risk. The second is to ensure that resources cannot be requested without permission. In short, JavaScript<br />

should never be able to request a resource on a domain without that resource specifically saying it can be<br />

accessed. To that end, IE and Firefox have implemented cross - domain solutions.<br />

The XD omainRequest Object<br />

Microsoft introduced the XDomainRequest (XDR) type in IE 8. This object works in a manner similar to<br />

XHR but in a way that is safe and secure for cross - domain communication. The XDR object implements<br />

part of the W3C Access Control for Cross - Site Requests specification as part of its security approach (this<br />

specification is still under development at the time of this writing, and so may undergo changes in the<br />

future). Here are some of the ways that XDR differs from XHR:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

Cookies are neither sent with requests nor received with responses.<br />

There is no access to set request headers other than Content - Type .<br />

There is no access to response headers.<br />

Only GET and POST requests are supported.<br />

XDR can access only resources with a header of Access - Control - Allow - Origin set to *.<br />

These changes mitigate issues related to CSRF and cross - site scripting (XSS) attacks. The resource being<br />

requested can dynamically decide whether to set the Access - Control - Allow - Origin header based on<br />

any data it deems appropriate: user - agent, referrer, and so on. As part of the request, an Origin header<br />

is sent with a value indicating the origin domain of the request, allowing the remote resource to<br />

recognize an XDR request explicitly.<br />

XDR object usage looks very similar to XHR object use. You start by creating a new instance of<br />

XDomainRequest , call the open() method, and then call the send() method. Unlike the open() method<br />

on XHR objects, the one on XDR objects accepts only two arguments: the request type and the URL.<br />

578

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!