Rob Cook


Home | GitHub | Dev.to | Donate | Contact

Web browser security


2024-08-01

The web browser is the first and most important line of defence in web security. Its job is to take html, css, Javascript, and media resources and turn them into pixels you see on your screen. This processing is known as the rendering pipeline. The code that does this processing is called the rendering engine. The rendering engine will construct an internal representation of the page structure know as the document object model (DOM).

In parallel to the rendering engine is the Javascript engine. This engine executes any Javascript embedded or imported by the page. Modern websites can be very Javascript heavy. They manipulate the DOM directly without the need for generating the intermediary HTML of the page. This process is known as client-side rendering.

To protect users from malicious code, limitations are placed on the operations that Javascript can perform. This is known as sandboxing. Sandboxed Javascript cannot:

Exceptions to these rules can be carved out by invoking certain browser APIs, as long as the browser grants suitable permission.

Before executing any Javascript, the browser must satisfy certain things.

To determine the first point the browser makes use of the concept of an origin. This is a combination of the protocol, port, and domain of a website. This same-origin policy allows Javascript to communicate with other windows and tabs from the same website. Ordinarily, Javascript cannot talk to other windows or tabs open in a browser.

Cross-origin requests are handled differently. Opening a new webpage by clicking a link (a read) is fine. Importing images from other websites (an embed) is allowed only if the website's content security policy permits them. Using the Fetch API to read data from another website is not allowed, unless you explicitly tell the browser beforehand.

Such reads are governed by cross-origin sharing (CORS) rules. To permit such a read, you must correctly configure the web server where the information is being read from to allow it. Do this by setting the Access-Control header in the HTTP response.

It is strongly recommended that you limit cross-origin reads to the bare minimum needed for your application to function.

To determine if the browser is executing the correct Javascript, you can add a integrity check to the relevant <script> tag by including an integrity attribute. The checksum assigned to this attribute will ensure the browser can validate the code has not been tampered with (such as by a man in the middle (MITM) attack).

Javascript has controlled access to disk space on the machine running the web browser. The File API allows for the showing of a file picker dialog, allowing the user to choose a file to share with Javascript. The file content and metadata can be read, but not modified by Javascript. Specifically the File API metadata does not reveal where the file was loaded from, avoiding the leaking of potentially sensitive information.

There are two types of WebStorage object that Javascript can access on the global window object: localStorage and sessionStorage. The former persists indefinitely, whilst the latter lasts only till the page is closed. These objects assign a small amount of writeable space for Javascript to use as a key-value store.

Finally the IndexedDB is another piece of storage that Javascript can access from the global window object. It allows the storage of structured data in a transactional fashion (similar to a traditional database).

Cookies are an important piece of stateful information that can be used by a browser and Javascript. These are the main way of authenticating a user with a website. They are small - around 4KB in size - plain text pieces of information.

A web server can send the Set-Cookie header to a browser to instruct it to save the cookie. This will automatically be added to every subsequent request to the web server via the Cookie header.

A number of attributes make the use of cookies more secure. On the `Set-Cookie` header you can specify:

These attributes can be use in conjunction with one another, and are particularly important in securing your session cookies.

On the subject of cookies, many browsers are limiting or outright banning the use of third-party cookies. These are used by sites to track your browser history amongst sites that have that particular cookie. (Accessing your browser history is something Javascript cannot traditionally do.) To get round this, advertisers and sites that want to track you are turning to other methods that are harder to prevent such as fingerprinting.

The browser is a very important piece of defence for web applications. It is important to understand how it works, and how to work with it to secure your application.

end