The modern JavaScript security model is based upon Java. In theory, downloaded scripts are run by default in a restricted sandbox environment that isolates them from the rest of the operating system. Scripts are permitted access only to data in the current document or closely
The fundamental
The primary JavaScript security policy is the same-origin policy. The
same-origin policy
prevents scripts loaded from one Web site from getting or setting properties of a document loaded from a different site. This policy
When a script attempts to access properties or
|
URL of Target Window |
Result of Same Origin Check with www.example.com |
Reason |
|---|---|---|
|
http://www.example.com/index.html |
Passes |
Same domain and protocol |
|
http://www.example.com/other1/other2/index.html |
Passes |
Same domain and protocol |
|
http://www.example.com:8080/dir/page.html |
Does not pass |
Different port |
|
http://www2.example.com/dir/page.html |
Does not pass |
Different server |
|
http://otherdomain.com/ |
Does not pass |
Different domain |
|
ftp://www.example.com/ |
Does not pass |
Different protocol |
Consider the following example:
var w = window.open("http://www.google.com");
// Now wait a while, hoping they'll start using the newly opened window.
// After 10 seconds, let's try to see what URL they're looking at!
var snoopedURL;
setTimeout("snoopedURL = w.location.href)", 10 * 1000);
Because of the same-origin policy, the only way this script will work is if it s loaded from www.google.com . If you load it from your own server, the attempt to access the Location object will fail because your domain doesn t match www.google.com (or whatever domain the user happens to be visiting). The attempt to access the Location object will similarly fail if you save the script to your local disk and open it from there, but this time because the protocol doesn t match (file:// versus http://). Internet Explorer 6 silently fails for this example, but the output in the JavaScript Console for Mozilla-based browsers is
Sometimes browsers don t fail at all but instead
Embedded Documents
The same-origin check is performed when trying to access the properties or methods of another
Window
object. Since each frame in a framed page has its own
Window
object, the same-origin policy applies to scripts attempting to access the content of frames. If two
The policy additionally applies to << iframe >>s, as well as << layer >>s and << ilayer >>s in Netscape 4, and documents included with the << object >> tag.
External Scripts
Externally linked scripts are
<<script type="text/javascript" src="http://www.example.com/scripts/somescript.js">><</script>>
This script will load and work as expected.
Be careful, since linked scripts are considered part of the page they re linked into, if JavaScript in the file http://www.example.com/scripts/somescript.js
Modern browsers enforce the same-origin policy on nearly all the properties and methods available to JavaScript. The few useful unprotected methods and properties are listed in Table 22-2. The fact that these are unprotected means that you can access them in another window even if the page in that window was loaded from a different domain. As you can see, none of the
|
Method/Property |
Exception |
|---|---|
|
window.focus(), window.blur(), window.close() |
Not subject to same origin policy in most browsers. |
|
window.location |
Setting this property is not subject to same origin policy in most browsers. |
|
window.open() |
Not subject to same origin policy in Internet Explorer. |
|
history.forward(), history.back(), history.go() |
Not subject to same origin policy in Mozilla and Netscape browsers. |
| Note |
Old browsers often have significantly more exceptions to the same-origin policy than do modern browsers. This is sometimes by design, but more often by mistake. You can find information about same-origin policy enforcement in older Netscape 4.x browsers at http://developer.netscape.com/docs/manuals/communicator/jssec/contents.htm . |
You have a bit of leeway with the same-origin policy if you re working with documents loaded from different servers within the same domain. Setting the domain property of the Document in which a script resides to a more general domain allows scripts to access that domain without violating the same-origin policy. For example, a script in a document loaded from www.myhost.example.com could set the domain property to myhost.example.com or example.com . Doing so enables the script to pass origin checks when accessing windows loaded from myhost.example.com or example.com , respectively. The script from www.myhost.example.com could not, however, set the domain to a totally different domain such as google.com or moveon.org.
The same-origin policy is very important from a user-privacy perspective. Without it, scripts in active documents from arbitrary domains could snoop not only the URLs you visit, but the cookies for these sites and any form entries you make. Most modern browsers do a good job of enforcing this policy, but older browsers did not.
Aside from poor enforcement by early browsers, the same-origin policy has another problem. Consider that one Web server often
http://www.example.com/account/
But by the rules of the same-origin policy, a script loaded from
http://www.example.com/otheraccount/
would be granted full access to the http://www.domain.com/account pages if they are present in accessible windows. This occurrence might be rare, but it is a serious shortcoming. There s really not much one can do to protect against this problem.
Another issue with the same-origin policy is that you can t, in general,
| Note |
Internet Explorer 5 allowed sites in the Trusted security zone to ignore the same-origin policy. However, Internet Explorer 6 does not provide this feature, so you shouldn t rely on it. |
Object signing technology was introduced in Netscape 4, and continues to be supported by modern-day Mozilla-based browsers (and, to some extent, by Internet Explorer). Object signing provides a digital guarantee of the origin of active content, such as Java applets and JavaScripts. While Java and JavaScript are normally confined to the Java sandbox, signed objects are permitted to request specific extended capabilities, such as access to the local file system and full control over the browser. The idea is that because the origins of the code can be
As with all things Web-related, the major browser
The creation of signed scripts for Netscape and Mozilla browsers involves acquiring a digital certification of your identity as a developer or an organization. You can get such a certificate from the same sources from which you might acquire an SSL certificate
The certificate of identity is used in conjunction with a
signing tool
to create a digital signature on your script. The signing tool packages your pages and the scripts they contain into a .jar file and then signs this file. The signature on the file guarantees to
When a Netscape or Mozilla browser encounters a .jar file (i.e., a page containing signed script), it checks the signature and allows the scripts the file contains to request extended privileges. Such privileges range from access to local files to the ability to set users browser preferences. The exact mechanics of this process are beyond the scope of this book, but there is plenty of information available online. For information about signed scripts in Netscape 4 browsers, good places to start are
http://developer.netscape.com/docs/manuals/communicator/jssec/contents.htm
http://developer.netscape.com/viewsource/goodman_sscripts.html
For modern Mozilla-based browsers, good starting points are
http://www.mozilla.org/projects/security/
http://www.mozilla.org/projects/security/components/jssec.html
Signed scripts are primarily useful in an intranet environment; they re not so useful on the Web in general. To see why this is, consider that even though you can authenticate the origin of a signed script on the Web, there s still no reason to trust the creator. If you encounter a script signed by your company s IT department, you can probably trust it without much risk. However, you d have no reason to think that a party you don t know ”for example, a random company on the Web ”is at all trustworthy. So they signed their JavaScript ”that doesn t mean it doesn t try to do something malicious! And if it did, most users would have no way of knowing.
Another problem with signed scripts is that what it takes to acquire a certificate of identity can vary wildly from provider to provider. Personal certificates sometimes require only the submission of a valid e-mail address. Other types of certificates require the submission of proof of incorporation, domain name ownership, or official state and country identification cards. But the user has no easy way of knowing how the identity of the certificate holder was verified. It could be that the author just submitted his/her
Developers should realize that for these reasons some users may be unwilling to grant privileges to signed code, no matter whose signature it bears. Defensive programming tactics should be employed to accommodate this possibility.
In general, it s best to use signed scripts only when users have enough information about the signer to be able to make informed decisions about