HTML Security
HTML and Security
Most of the security of a website is handled on the server.
But HTML also has a few simple tools that make your pages safer.
This chapter shows the most important ones.
Use HTTPS
A page that starts with https:// is sent in a secret code between the server and the browser.
Nobody on the way can read or change it.
A page that starts with http:// is sent as plain text.
Always use HTTPS, and use https:// in your links, images, and scripts.
If an HTTPS page loads a file over HTTP, the browser may block the file or show a warning.
Show User Text Safely
Never trust text that a user has typed.
If you put the text on the page with innerHTML, the browser reads it as HTML.
A user could then add their own elements, or even their own scripts, to your web page.
Use textContent instead, so the text is shown as plain text:
Example
<input type="text" id="name" value="<b>Maria</b>">
<button onclick="show()">Show</button>
<p>With innerHTML:</p>
<p id="html"></p>
<p>With textContent:</p>
<p id="text"></p>
<script>
function show() {
const name = document.getElementById("name").value;
document.getElementById("html").innerHTML = name;
document.getElementById("text").textContent = name;
}
</script>
Try it Yourself »
When you click Show, the page shows:
With innerHTML:
Maria
With textContent:
<b>Maria</b>
With innerHTML, the browser made the name bold, because it read <b> as a tag.
With textContent, the tags are shown as plain text, and nothing can run.
Links to Other Sites
A link with target="_blank" opens a new tab.
Add rel="noopener", so the new page cannot control your page:
<a href="https://example.com" target="_blank" rel="noopener">Visit example.com</a>
New browsers do this by default, but it is still good practice for older browsers.
Read more about the rel attribute.
Sandbox Iframes
An iframe shows another page inside your web page.
The sandbox attribute stops the page in the iframe from running scripts, sending forms, and opening popups:
Example
<p>A normal iframe:</p>
<iframe srcdoc="<p id='msg'>No script</p>
<script>document.getElementById('msg').innerHTML = 'The script ran';</script>">
</iframe>
<p>An iframe with sandbox:</p>
<iframe sandbox srcdoc="<p id='msg'>No script</p>
<script>document.getElementById('msg').innerHTML = 'The script ran';</script>">
</iframe>
Try it Yourself »
The srcdoc attribute holds the page that the iframe shows.
Both iframes hold the same small page with a script.
The script changes the text from "No script" to "The script ran".
The page shows:
A normal iframe:
The script ran
An iframe with sandbox:
No script
Use sandbox when you show a page that you do not control.
Read more about the sandbox attribute.
Content Security Policy
A Content Security Policy tells the browser which scripts are allowed to run on your page.
You can add it with a <meta> tag in the <head> section:
Example
<head>
<meta http-equiv="Content-Security-Policy" content="script-src 'self'">
</head>
<body>
<p id="msg">The script did not run.</p>
<script>
document.getElementById("msg").innerHTML = "The script ran.";
</script>
</body>
Try it Yourself »
The page shows:
The script did not run.
script-src 'self' means that only script files from your own site can run.
Scripts written directly in the page are blocked.
So even if someone adds a script to your page, the browser will not run it.
Forms and Passwords
- Always use
type="password"for password fields, so the password is hidden while the user types - Use
method="post", so the password is not shown in the address bar - Only send forms to pages that start with
https://
<form action="/login" method="post">
<label for="pwd">Password:</label>
<input type="password" id="pwd" name="pwd">
<input type="submit" value="Log in">
</form>
Note: Checks in HTML, like required, help the user, but they do not protect your site.
The server must always check the data again!
Summary
- Always use HTTPS
- Use
textContent, notinnerHTML, for text that users type - Add
rel="noopener"to links that open a new tab - Use
sandboxon iframes that show pages you do not control - A Content Security Policy decides which scripts can run
- The server must always check form data again