Skip to main content

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 https comes 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.

index.html
<!-- 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>

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.

AddressSame origin as https://shop.example.com/menu?Why
https://shop.example.com/cartYesOnly the path differs
http://shop.example.com/menuNoDifferent scheme
https://api.example.com/menuNoDifferent host (a subdomain counts)
https://shop.example.com:8443/menuNoDifferent port
https://example.org/menuNoDifferent 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:

  1. What is worth protecting? Visitors' accounts, personal data, payment details, your reputation, your server.
  2. 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.
  3. How could they get to it? Through the doors of your page: inputs, includes, links, outputs, and storage.
  4. 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​

DoorWhat it isExampleMain risk
InputsData coming in from visitorsForms, URL parameters, cookies, uploaded filesHostile or malformed data
OutputsData shown back on the pageComments, usernames, search termsText treated as code (XSS)
IncludesOther people's code and filesScripts, styles, fonts, iframes, imagesA compromised or careless provider
LinksPaths to other pagestarget="_blank", javascript: addressesHijacked tabs, leaked referrers, unsafe schemes
StorageData kept in the browserCookies, localStorageTheft if a script is injected, exposure on shared computers
TransportHow data travelshttp against httpsEavesdropping 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 https for every page, not only login pages.
  • Redirect http to https on the server.
  • Load every file over https. An http file on an https page 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 canOnly the server can
Keep origins separateCheck who the visitor is
Block mixed contentDecide what the visitor may see or do
Enforce a Content Security Policy you sendValidate and store data safely
Apply sandbox, rel, and integritySet the security headers and cookie flags
Warn about an invalid certificateKeep 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:

HeaderWhat it controlsLesson
Content-Security-PolicyWhich sources of scripts, styles, and frames are allowedContent Security Policy
Strict-Transport-SecurityTells the browser to use only https for your siteServer setup
Referrer-PolicyHow much of the previous address is sentnoreferrer
X-Content-Type-OptionsStops the browser guessing file typesServer setup
Cookie flags (Secure, HttpOnly, SameSite)How cookies are sent and readServer setup

A Safe Starting Routine​

  1. List the doors in your page, using the survey tool or by reading the source.
  2. Move to https, and fix any mixed content.
  3. Escape output. Show visitor text as text.
  4. Limit what you include. Fewer scripts, trusted sources, and sandboxed iframes.
  5. Secure your links and forms.
  6. Add a Content Security Policy as a second line of defence.
  7. Check on the server everything you check in the browser.
  8. Test, and test again after every change.

Best Practices​

  1. Know your doors. A page you have not mapped is a page you cannot protect.
  2. Use https everywhere, with no exceptions.
  3. Give every include the least power it needs.
  4. Escape output by default.
  5. Never rely on the browser to enforce your rules, since the visitor controls it.
  6. Keep secrets out of the page, because anyone can read the source.
  7. Keep libraries updated, and remove what you do not use.
  8. 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 https work 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. https matters 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​

  1. Thinking the same-origin policy protects you from your own scripts.
  2. Trusting browser-side checks such as required or maxlength.
  3. Mixing http and https.
  4. Adding third-party scripts without asking what they can do.
  5. Showing visitor text as HTML.
  6. Putting secrets, keys, or private notes in the page or comments.
  7. Using javascript: addresses in links.
  8. Forgetting storage: treating localStorage as safe for secrets.
  9. Assuming https makes the page safe.
  10. Fixing one door and ignoring the rest.

Practice Time​

  1. In the Origin Lab, press each preset. Which part of the address changed each time?
  2. Make up an address that has the same origin as https://shop.example.com/menu but a different path.
  3. Does https://shop.example.com:443/menu match https://shop.example.com/menu? Check in the lab.
  4. In the Page Survey, load the messy page and fix every problem one at a time.
  5. Paste the source of a page you built into the survey. How many doors does it have?
  6. Pick one third-party script on one of your pages. What would happen if its host were compromised?
  7. List the four questions from this lesson, and answer them for your own site.
  8. 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.
  • https first, 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.