HTML Security Basics
Before learning the specific protections, you need a picture of what you are protecting and from whom. This lesson builds that picture. It explains the one rule that the browser enforces for you, the same-origin policy, and the many things it does not stop, then shows you how to find the doors into your own pages so you know where to look.
What You Will Learnβ
- What an origin is, and what the same-origin policy protects
- What the policy does not protect you from
- How to think about threats in four simple questions
- The "doors" into a web page: inputs, includes, links, outputs, and storage
- Why
httpscomes first - What the browser can enforce, and what only the server can
- The main families of web vulnerabilities, in plain terms
The Big Ideaβ
A web page is a small building with many doors. Some let visitors in (forms and links). Some let other people's code in (scripts, iframes, fonts). Some send information out (form submissions, requests). Security begins with knowing where all the doors are, and making sure each one has the right lock.
- A Page and Its Doors
- Origin Lab
- Page Survey
<!-- DOOR: other people's code runs with your page's power -->
<script src="https://cdn.example.net/lib.js"></script>
<!-- DOOR: a whole other page inside yours -->
<iframe src="https://widgets.example.net/map" title="Map"></iframe>
<!-- DOOR: a link that opens another site -->
<a href="https://partner.example.com" target="_blank">Our partner</a>
<!-- DOOR: visitors send you data -->
<form action="/login" method="post">
<input type="text" name="user">
<input type="password" name="pw">
</form>
<!-- DOOR: visitor text shown back on the page (the output) -->
<p id="comment"></p>
<!-- DOOR: inline code, which a strict policy cannot easily tell from injected code -->
<button onclick="openMenu()">Menu</button>
| Part | Page A | Page B | Result |
|---|---|---|---|
| Scheme | https | https | Same |
| Host | shop.example.com | shop.example.com | Same |
| Port | 443 | 443 | Same |
Same origin. Scripts on one page can read the other page's content and responses.
An origin is the scheme, the host, and the port. The path never matters. When no port is written, https means 443 and http means 80.
Loading the survey...
In the Origin Lab, press each preset and watch which part changes. In the Page Survey, load the messy page, read what it finds, and then load the fixed page to compare. You will meet every fix in the lessons that follow.
What Is an Origin?β
An origin is three things taken together: the scheme (https), the host (shop.example.com), and the port (443). Two addresses have the same origin only if all three match. The path (/menu, /cart) never matters.
| Address | Same origin as https://shop.example.com/menu? | Why |
|---|---|---|
https://shop.example.com/cart | Yes | Only the path differs |
http://shop.example.com/menu | No | Different scheme |
https://api.example.com/menu | No | Different host (a subdomain counts) |
https://shop.example.com:8443/menu | No | Different port |
https://example.org/menu | No | Different domain |
The Same-Origin Policyβ
The same-origin policy is the browser's most important built-in protection. In simple terms: a script running on one origin cannot read the pages or responses of another origin.
Without it, any site you visited could quietly read your email in another tab. With it, a page on evil.example cannot reach into mail.example just because you have both open. Storage such as localStorage is also kept separate for each origin. (Cookies follow slightly different rules, based on the host and their own settings, so they are covered in the server-side lessons.)
The policy has a deliberate exception for sending: a page may freely include other origins' files and send requests to them. It cannot, by default, read what comes back. A server can choose to relax this using a mechanism called CORS, which is a topic for server-side learning.
What the Policy Does Not Protect You Fromβ
This is the part most beginners miss. The same-origin policy protects origins from each other. It does not protect you from things you invite in yourself.
- A script you include runs as your page. If you add
<script src="https://cdn.example.net/lib.js">, that code gets the full power of your origin, even though it came from elsewhere. If that file is compromised, so is your page. - Code injected into your page runs as your page. If an attacker gets text into your page that the browser treats as markup, it runs with your origin's power. This is cross-site scripting, and it is the subject of XSS Basics.
- Forms and links can cross origins. A page can submit a form to, or link to, another origin. That is how the web works, and it brings its own risks.
- Embeds keep their own powers. An iframe from another origin is separated from your page, but it can still run its own scripts, show its own content, and navigate itself unless you limit it. See iFrame Security.
Think in Four Questionsβ
You do not need a formal method to think about security. For any page, ask:
- What is worth protecting? Visitors' accounts, personal data, payment details, your reputation, your server.
- Who might want it? Criminals after data or money, bots probing for weak sites, someone who dislikes you. Most attacks are automated and aimed at everyone.
- How could they get to it? Through the doors of your page: inputs, includes, links, outputs, and storage.
- What would limit the damage? Escaping, sandboxing, a Content Security Policy,
https, least privilege, and server-side checks.
Writing the answers down for a real page, even in a few lines, turns vague worry into a to-do list.
The Doors of a Pageβ
| Door | What it is | Example | Main risk |
|---|---|---|---|
| Inputs | Data coming in from visitors | Forms, URL parameters, cookies, uploaded files | Hostile or malformed data |
| Outputs | Data shown back on the page | Comments, usernames, search terms | Text treated as code (XSS) |
| Includes | Other people's code and files | Scripts, styles, fonts, iframes, images | A compromised or careless provider |
| Links | Paths to other pages | target="_blank", javascript: addresses | Hijacked tabs, leaked referrers, unsafe schemes |
| Storage | Data kept in the browser | Cookies, localStorage | Theft if a script is injected, exposure on shared computers |
| Transport | How data travels | http against https | Eavesdropping and tampering |
The Page Survey above gives you a quick inventory of the first four. Run it on your own pages and see how many doors there are.
HTTPS Comes Firstβ
https encrypts the connection between the visitor and your server, and lets the browser check it is talking to the real site. Without it, anyone on the network path, such as someone on public Wi-Fi, can read or change what is sent.
- Use
httpsfor every page, not only login pages. - Redirect
httptohttpson the server. - Load every file over
https. Anhttpfile on anhttpspage is called mixed content, and is the topic of Mixed Content. - Most hosts and platforms offer free certificates and make this a setting rather than a project.
https protects the journey. It does not make the page itself safe. A page served over https can still have every other problem in this section.
What the Browser Enforces, and What Only the Server Canβ
| The browser can | Only the server can |
|---|---|
| Keep origins separate | Check who the visitor is |
| Block mixed content | Decide what the visitor may see or do |
| Enforce a Content Security Policy you send | Validate and store data safely |
Apply sandbox, rel, and integrity | Set the security headers and cookie flags |
| Warn about an invalid certificate | Keep secrets, keys, and databases safe |
The browser is on the visitor's device, which means a hostile visitor controls it. Anything that matters must be enforced where the visitor cannot change it: on the server.
The Main Families of Vulnerabilitiesβ
You will hear these names often. The detail comes in later lessons and in server-side study:
- Injection: untrusted data is mixed into code or markup and treated as instructions. Cross-site scripting (XSS) is injection into a web page.
- Broken access control: the server lets someone see or do something they should not, such as opening another person's account by changing a number in the address.
- Insecure configuration: missing headers, open debug pages, default passwords, or files that should not be public.
- Vulnerable components: outdated libraries and plugins with known problems.
- Sensitive data exposure: secrets or personal data sent or stored without protection.
A well-known public list, the OWASP Top 10, ranks the most common web risks. The list is updated now and then, so check the current edition when you read it.
Security Headers at a Glanceβ
Some protections are HTTP headers set by the server. You will learn the important ones in this section. For now, just know their names:
| Header | What it controls | Lesson |
|---|---|---|
Content-Security-Policy | Which sources of scripts, styles, and frames are allowed | Content Security Policy |
Strict-Transport-Security | Tells the browser to use only https for your site | Server setup |
Referrer-Policy | How much of the previous address is sent | noreferrer |
X-Content-Type-Options | Stops the browser guessing file types | Server setup |
Cookie flags (Secure, HttpOnly, SameSite) | How cookies are sent and read | Server setup |
A Safe Starting Routineβ
- List the doors in your page, using the survey tool or by reading the source.
- Move to
https, and fix any mixed content. - Escape output. Show visitor text as text.
- Limit what you include. Fewer scripts, trusted sources, and sandboxed iframes.
- Secure your links and forms.
- Add a Content Security Policy as a second line of defence.
- Check on the server everything you check in the browser.
- Test, and test again after every change.
Best Practicesβ
- Know your doors. A page you have not mapped is a page you cannot protect.
- Use
httpseverywhere, with no exceptions. - Give every include the least power it needs.
- Escape output by default.
- Never rely on the browser to enforce your rules, since the visitor controls it.
- Keep secrets out of the page, because anyone can read the source.
- Keep libraries updated, and remove what you do not use.
- Layer your defences. Assume any one of them can fail.
Quick Checklistβ
- I know which origin my page is on
- I have listed every script, iframe, form, and external link
- Everything loads over
https - Third-party code is trusted, minimal, and limited where possible
- Visitor text is shown as text
- No secrets appear in the source
- Every rule that matters is checked on the server
Security and Privacy Notesβ
- Examine only what you own or may test. Use the tools here on your own pages, or on pages you have permission to test. Probing other people's sites can be illegal.
- The survey tool reads text only. It runs no code from the HTML you paste, but do not paste real secrets or private data into any online tool.
- A clean survey is not a safe site. It cannot see your server, your headers, your database, or what your scripts do.
- Collect only what you need. Data you never store cannot leak, and fewer doors means less to defend.
Global Audience Notesβ
- The same-origin policy and
httpswork the same everywhere, but the laws about data and breach reporting differ between countries. This is not legal advice, so check the rules that apply to the places your visitors live. - Visitors may be on public or shared networks, where unencrypted traffic is easily read.
httpsmatters most for them. - Internationalised web addresses can hide look-alike characters. A link to a site whose name uses similar letters from another script can trick visitors. Show the real destination where it matters.
- Older browsers and slow devices are common in many regions. Do not depend on a single advanced feature for safety. Layer simple, widely supported protections.
Common Mistakes to Avoidβ
- Thinking the same-origin policy protects you from your own scripts.
- Trusting browser-side checks such as
requiredormaxlength. - Mixing
httpandhttps. - Adding third-party scripts without asking what they can do.
- Showing visitor text as HTML.
- Putting secrets, keys, or private notes in the page or comments.
- Using
javascript:addresses in links. - Forgetting storage: treating
localStorageas safe for secrets. - Assuming
httpsmakes the page safe. - Fixing one door and ignoring the rest.
Practice Timeβ
- In the Origin Lab, press each preset. Which part of the address changed each time?
- Make up an address that has the same origin as
https://shop.example.com/menubut a different path. - Does
https://shop.example.com:443/menumatchhttps://shop.example.com/menu? Check in the lab. - In the Page Survey, load the messy page and fix every problem one at a time.
- Paste the source of a page you built into the survey. How many doors does it have?
- Pick one third-party script on one of your pages. What would happen if its host were compromised?
- List the four questions from this lesson, and answer them for your own site.
- Bonus: open DevTools, then the Network panel, and look at the response headers of your home page. Which security headers are present?
Quick Recapβ
Click a question to check your answer.
1. What is an origin?
The scheme, host, and port of an address, taken together. Two addresses share an origin only if all three match. The path does not matter.
2. What does the same-origin policy do?
It stops a script on one origin from reading the pages and responses of another origin, so a site you visit cannot read your data from other sites.
3. What does it not protect you from?
Scripts you include yourself, code injected into your page, and the powers of embeds you add. Those all run with, or alongside, your page's authority.
4. Name five doors into a web page.
Inputs, outputs, includes (scripts, iframes, fonts), links, and storage. Transport (http or https) is another.
5. Why is browser-side checking not security?
The browser runs on the visitor's device, so a hostile visitor controls it. Rules that matter must be enforced on the server.
6. Does https make a page secure?
No. It protects the connection from eavesdropping and tampering. The page can still have injection problems, unsafe embeds, and every other issue.
Key Takeawaysβ
- A page has many doors. Find them all before you lock any.
- An origin is scheme, host, and port. The same-origin policy keeps origins apart.
- It does not protect you from your own includes or injected code.
httpsfirst, and for every file.- The browser cannot enforce your rules against a hostile visitor. The server must.
- Layer defences, and keep secrets out of the page.
- Test only what you own, and keep collecting less data.
Next up: XSS Basics, where you will see how visitor text becomes running code, and how to stop it.