Secure HTML habits: HTTPS, safe links, escaping and CSP
Learn the security basics every site owner needs: HTTPS, safe links, escaping input, form protection and content security policy.
In this lesson you will
- Use HTTPS and avoid mixed content
- Write safe external links
- Explain XSS and escaping
- Follow a simple security checklist
About 14 minutes
Why a beginner needs to think about security
A bakery site seems like an unlikely target. But attackers rarely choose targets by name. They run automated tools that probe thousands of sites for the same few weaknesses, and a small unprotected site is just easier to hit. Many of the weaknesses are caused by small HTML choices. You do not need to become a security expert, but a handful of habits will protect you and your visitors from most common problems.
Habit 1: use HTTPS everywhere
HTTPS encrypts everything between the visitor and your site, so passwords, form entries and cookies cannot be read on the way. Browsers mark plain HTTP sites as “Not secure”, and many features, such as location and service workers, refuse to work without HTTPS. Free certificates are available through most hosts, so there is no excuse. Link to resources with https:// too, because a secure page that loads an insecure image triggers a warning called mixed content.
Habit 2: links that open new tabs
When you open a link in a new tab with target="_blank", older browsers let the new page reach back and control the original page through window.opener. Modern browsers now protect against this by default, but adding the attribute is still good practice and is harmless.
<a href="https://other-site.example" target="_blank" rel="noopener noreferrer">Visit partner</a>
noopenerstops the new page from reaching the old one.noreferreralso hides which page the visitor came from.nofolloworsponsoredtell search engines you do not vouch for a link, such as a paid link or an affiliate link. This is an SEO matter, not a security one.
Habit 3: never trust what people type
Anything that a visitor can type or upload is untrusted. If your page prints someone’s comment straight into the HTML without checking it, an attacker can write a comment that contains a script, and every visitor who views it runs that script. This attack is called cross-site scripting, or XSS, and it is among the most common problems on the web.
<!-- A comment someone posts -->
<img src=x onerror="alert('hacked')">
The defence is called escaping: turning special characters into harmless text before putting them in a page. The less-than sign becomes < and the quote becomes ". Your language or platform has built-in helpers for it. In WordPress they are functions such as esc_html() and esc_attr(). In JavaScript, prefer textContent to innerHTML when showing text.
Press Run, then compare the two lines. The first uses textContent and shows the text safely. The second uses innerHTML, which would treat the text as HTML. Change the second one to textContent too.
<p id="a"></p><p id="b"></p>
<script>
var bad = '<b>bold</b> <i>text</i>';
document.getElementById('a').textContent = bad;
document.getElementById('b').innerHTML = bad;
</script>
Change innerHTML on the second line to textContent.
<p id="a"></p><p id="b"></p>
<script>
var bad = '<b>bold</b> <i>text</i>';
document.getElementById('a').textContent = bad;
document.getElementById('b').textContent = bad;
</script>
Habit 4: protect your forms
- Always validate on the server as well as in the browser.
- Use POST for anything that changes data, and add an anti-forgery token so other sites cannot submit your form for a visitor. Frameworks and WordPress call this a nonce or CSRF token.
- Never put secrets such as API keys in HTML or in front-end JavaScript. Anyone can read them with View Source.
- Add
autocomplete="new-password"orcurrent-passwordto password fields so password managers work and people can use strong passwords. - Limit file uploads by type and size on the server, and store them where they cannot run as code.
Habit 5: a content security policy
A Content Security Policy (CSP) is a rule set that tells the browser which sources of scripts, styles and images are allowed. If an attacker manages to inject a script from elsewhere, the browser refuses to run it. It is usually set as an HTTP header by the server, but a basic version can be written in the head.
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; img-src 'self' https:; script-src 'self'">
A strict policy can break a site that uses third-party tools such as analytics or chat widgets, so introduce it step by step and test carefully. Many hosts offer a report-only mode that logs violations without blocking anything.
Habit 6: be careful with iframes and third parties
- Every script or widget you add from another company runs with the powers of your page. Add only what you need and trust.
- Use the
sandboxattribute on iframes you do not fully control. - Keep WordPress, themes and plugins updated. Most real-world hacks use known holes in outdated software.
- Use strong, unique passwords and two-factor authentication on your hosting and admin accounts.
Do not copy random code from the internet into your page without understanding what it does, especially anything that loads a script from another address. It is a common way for malicious code to arrive.
A tiny security checklist
- The whole site uses HTTPS and no insecure resources.
- External links use rel=”noopener noreferrer” when they open in a new tab.
- All user input is escaped when printed.
- Forms have server-side validation and anti-forgery tokens.
- No secrets sit in the HTML or JavaScript.
- Software is up to date, and admin accounts use two-factor login.
Make this external link safe: it should open in a new tab with rel=”noopener noreferrer”.
<a href="https://example.com">Partner site</a>
Add target=”_blank” and rel=”noopener noreferrer”.
<a href="https://example.com" target="_blank" rel="noopener noreferrer">Partner site</a>
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What does HTTPS provide?
- Faster images
- Encryption between visitor and site
- Better colours
- Free hosting
It protects data in transit from being read.
What does rel="noopener" prevent?
- Opening links
- The new page reaching back to the original
- Downloading files
- Printing
It cuts off access through window.opener.
What is XSS?
- A font style
- Injecting scripts through untrusted input
- A CSS framework
- An image format
Cross-site scripting runs attacker code in visitors’ browsers.
What is the main defence against XSS?
- Adding more images
- Escaping untrusted input before printing it
- Using bigger fonts
- Disabling CSS
Escaping turns special characters into harmless text.
Where should API secrets be kept?
- In the HTML
- In front-end JavaScript
- On the server, not in public code
- In a comment
Anything in the page can be read by visitors.
Summary
- Use HTTPS for everything.
- Escape anything people type before printing it.
- Keep secrets on the server and software up to date.
- Add a content security policy gradually.
Questions
Is my small site really a target?
Yes. Attacks are automated and look for known weaknesses, not for famous names.
Do I need a CSP?
It is a valuable extra layer, but start with HTTPS, escaping and updates first.
Is rel=noreferrer bad for analytics?
It hides the referring page from the target site, so it may hide your traffic source from partners. Use noopener alone if you want to keep the referrer.
