Secure Forms
A form is the front door of your site. It is where visitors hand you passwords, card numbers, messages, and personal details. HTML gives you several attributes that make that handover safer, and a few tempting ones that do nothing at all for security.
The server-side half of form safety, such as validating input, stopping SQL injection, and building CSRF protection, is covered in Secure Form Design. Validation messages and flags are in Form Validation. This lesson is about what you write in the HTML itself, and where HTML's protection ends.
What You Will Learnβ
- Why sensitive forms use
POST, neverGET - Why the form's action must be
https - How to mark up password and one-time code fields
- What hidden fields,
formactionandtargetcan do to you - How the CSP
form-actiondirective limits where forms can send data - Which HTML attributes look like security but are not
- How to audit a form
The Big Ideaβ
HTML decides how the data travels and how browsers help the visitor. It cannot decide whether the data is allowed. Anything in a form can be changed by the visitor, or sent without using your form at all, so the server must check everything. Use HTML to avoid leaks and mistakes. Use the server to enforce rules.
- The Code
- Submit Lab
- Form Auditor
- Autocomplete Matcher
<form action="/login" method="post">
<input type="hidden" name="csrf_token" value="SERVER_GENERATED_VALUE">
<label for="email">Email</label>
<input id="email" name="email" type="email" autocomplete="username" required>
<label for="password">Password</label>
<input id="password" name="password" type="password"
autocomplete="current-password" required>
<button type="submit">Sign in</button>
</form>
Content-Security-Policy: form-action 'self'; frame-ancestors 'self'
Address the browser requests:
http://www.example.com/login?username=ada&password=correct+horseRequest body:
(none)| Who can see the password? | Answer |
|---|---|
| Address bar and browser history | Yes, including the password |
| Server and proxy logs | Often yes, because addresses are commonly logged |
| Other sites, through the Referer header | Possibly, from the results page, unless a referrer policy hides it |
| Someone on the network | Everything, including the password |
This only builds text. No request is made. Use made-up values, never a real password.
Loading the checker...
Choose the autocomplete value that fits each field.
1. Use POST for Anything Sensitiveβ
If you do not write a method, a form uses GET, which puts every field into the address. Open the Submit Lab, keep the method on get, and read the address aloud: the password is part of it.
An address ends up in many places:
- the browser history and the address bar, which anyone at the screen can read,
- server and proxy logs, which are often kept for a long time,
- the
Refererheader sent to other sites that the results page loads from (see noreferrer), - bookmarks, and links the visitor shares without thinking.
So for passwords, tokens, personal data, and anything that changes something on the server, use method="post". Use GET only for harmless, repeatable requests, such as search.
POST is not encryption. It only keeps the data out of the address. Without https, the body can still be read on the network.
2. The Action Must Be httpsβ
On a page served over https, an action that points to http sends everything the visitor typed unprotected. Browsers warn about it, and may block it. Use https:// or a relative address such as /login, which follows the page's scheme. See Mixed Content.
Turn the https checkbox on and off in the Submit Lab, and see what a person on the network can learn in each case.
3. Mark Up Password and Code Fields Properlyβ
<!-- Sign in -->
<input type="password" name="password" autocomplete="current-password">
<!-- Sign up or change password -->
<input type="password" name="new_password" autocomplete="new-password">
<!-- A texted or app-generated code -->
<input type="text" name="otp" inputmode="numeric" autocomplete="one-time-code">
What each choice does:
type="password"hides the characters on screen. It does not encrypt anything.autocomplete="current-password"andnew-passwordtell password managers which is which. A manager can then fill the right password, or suggest a strong new one.autocomplete="one-time-code"lets browsers offer a code that arrived by text message.autocomplete="username"on the account name field helps managers pair it with the password.autocomplete="off"on a password field is usually ignored by password managers and it makes things worse for visitors. Do not use it as a security feature.- Do not block pasting into password fields. It stops password managers, pushes people toward shorter passwords, and does not protect anyone.
Practise in the Autocomplete Matcher tab.
Some browsers can send text from fields to a spelling service. For secrets such as recovery keys, add spellcheck="false" as well. Behaviour varies, so treat it as a small extra, not a protection.
4. Hidden Fields Are Not Secretβ
An input type="hidden" is hidden only from the screen. The visitor can read and edit it in DevTools, and anyone can send a request with any value they like.
<!-- Do not trust this on the server -->
<input type="hidden" name="price" value="19.99">
<input type="hidden" name="role" value="member">
Use hidden fields for harmless state and for tokens the server generates and checks. Never use them to carry prices, discounts, roles, or user identifiers that the server accepts without checking. Look the real value up on the server instead.
5. CSRF Tokens in the Formβ
Cross-site request forgery tricks a visitor's browser into submitting a form to your site, using the visitor's logged-in session. The standard defence is a random token that the server puts into each form, usually as a hidden field, and checks when the form comes back. Cookies with a SameSite setting add another layer. Building and checking the token is server work, covered in Secure Form Design. In HTML, your job is just to include it in every form that changes something.
6. Watch for formaction, target, and Rogue Formsβ
formactionon a button or input overrides the form'saction. A button withformaction="https://tracker.example.net/collect"sends the form to a different site. Check every use of it.target="_blank"on a form opens the result in a new tab. Addrel="noopener"as you would on a link. See noopener.- Injected forms. If an attacker can inject HTML into your page (see XSS Basics), they can add a form that looks like yours and posts to their site. This is one reason escaping output matters so much.
- Framed forms. A form inside someone else's frame can be used for clickjacking. Send
frame-ancestors, as covered in iFrame Security.
Limit where forms can submit: form-actionβ
The CSP directive form-action lists the places a form on your page may submit to:
Content-Security-Policy: form-action 'self' https://pay.example.net
With this in place, an injected or altered form that posts to attacker.example is blocked by the browser. It is a useful second layer, and it is not covered by default-src, so set it explicitly. See Content Security Policy.
7. What HTML Cannot Doβ
These attributes help usability. They are not security, because the visitor controls the browser, and requests do not have to come from your page:
| Attribute | Why it is not protection |
|---|---|
required, pattern, min, max, maxlength, type="email" | Can be removed in DevTools, or skipped by sending a request directly |
readonly, disabled | Editable in DevTools. Disabled fields are simply not sent, and a sender can add them back |
type="hidden" | Readable and editable |
accept on a file input | A hint for the file picker only |
autocomplete="off" | Does not stop browsers or managers reliably |
novalidate, or its absence | Only affects the browser's messages |
Validate every value on the server, as stressed in Form Validation and Secure Form Design.
8. A Few More Habitsβ
- Stop double submissions. Disable the submit button after the first click, and let the server ignore a repeated request. This protects payments and orders.
- Redirect after a successful POST, so a page refresh does not send the form again.
- Keep error messages neutral. "Incorrect email or password" is safer than saying which was wrong.
- Use real labels, and clear errors. Accessible forms are also less error-prone. See Accessible Forms.
- Ask for as little as you need. Data you never collect cannot be leaked.
Checking Your Setupβ
- Open DevTools, then Network, submit a test form, and check the request method and whether the address contains your test values.
- Check that the form's Request URL starts with
https://. - Look at the Request Payload or Form Data. Are any values you did not expect being sent?
- In Elements, search for
formaction,type="hidden",autocomplete="off", andmethod="get"on forms with sensitive fields. - Try your sign-in form with a password manager. Does it fill the right fields?
- Run the Form Auditor tab on your own form markup.
- Check Response Headers for
form-actionandframe-ancestors.
Best Practicesβ
- Use
method="post"for anything sensitive or state-changing. - Use
https://or relative actions. - Set
autocompletecorrectly on password, username, and code fields. - Never block pasting in password fields.
- Treat hidden fields as editable. Check values on the server.
- Include a CSRF token in every state-changing form.
- Audit
formactionandtarget. - Send
form-actionandframe-ancestorsin your CSP. - Validate everything on the server, whatever the HTML says.
- Collect only what you need.
Quick Checklistβ
- Sensitive forms use
method="post" - Every form action is
httpsor relative - Password fields use
type="password"and the rightautocomplete - Code fields use
autocomplete="one-time-code" - No sensitive data in query strings
- Hidden fields hold nothing the server trusts blindly
- State-changing forms carry a CSRF token
- No unexpected
formaction, andtarget="_blank"forms userel="noopener" -
form-actionandframe-ancestorsare set - The server validates everything again
Security and Privacy Notesβ
- Collect less, keep less. Each field is a responsibility. Say why you need it, and delete it when you do not.
- Never log passwords or full card numbers, even in debugging.
- Card details are best handled by a payment provider's own hosted fields or page, which keeps them off your server. Follow that provider's current documentation.
- Autofill can fill hidden fields, and some tools fill every field they find. Do not put fields in a form that the visitor does not need to see, and ask only for what is needed.
- Privacy law can apply to data you collect, depending on where you and your visitors are. This is not legal advice, so check the rules that apply to you.
Global Audience Notesβ
- Names and addresses vary widely. Do not reject names for length, accents, spaces, or hyphens in the name of "security". See Autocomplete.
- Not everyone has a phone for codes, or a stable connection to receive them. Offer an alternative where you can.
- Password managers are less common in some places, so clear instructions and sensible rules matter, and blocking paste hurts the most.
- Show and hide controls for passwords help people on small keyboards. If you add one, make it a labelled button.
- Shared devices are common. Offer a clear "sign out", and do not leave sensitive data visible after submission.
Common Mistakes to Avoidβ
- Leaving out
method, so the form usesGET. - Sending credentials in the address.
- An
httpaction on anhttpspage. type="text"for a password field.autocomplete="off"as a security measure.- Blocking paste on password fields.
- Trusting hidden fields, or
readonlyanddisabledfields. - Relying on
requiredandpatternas validation. - Forgetting the CSRF token, or adding it but never checking it.
- Unreviewed
formactionattributes.
Practice Timeβ
- In the Submit Lab, keep
getand https off. List every place the password is visible. - Switch to
postand tick https. What can a person on the network still see? - Switch to
postwith https off. What is still exposed? - Load the messy forms into the Form Auditor, and fix every problem until it reports a clean result.
- Complete the Autocomplete Matcher.
- Open a sign-in page you own. Does a password manager fill it correctly?
- In DevTools, change a hidden field's value and submit. What does your server do?
- Bonus: write the CSP line that lets forms submit only to your own site and one payment provider.
Quick Recapβ
Click a question to check your answer.
1. Why must sensitive forms use POST?
GET puts the fields in the address, which ends up in history, logs, and referrers. POST keeps them in the request body.
2. Does POST make the data private?
No. It only keeps the data out of the address. Use https so the body cannot be read on the network.
3. Which autocomplete values suit a password field?
current-password for sign-in, and new-password for sign-up or change-password. Avoid off.
4. Are hidden fields secret?
No. Visitors can read and edit them, so the server must check any value they carry, except for tokens it generates and verifies.
5. What does formaction do, and why check it?
It overrides the form's action for that button, so it can send the form to a different site.
6. Is required or pattern a security measure?
No. They are conveniences. A visitor can bypass them, so the server must validate everything.
Key Takeawaysβ
- Sensitive forms use
POSToverhttps. - Mark password and code fields correctly and do not block pasting.
- Hidden, readonly, and disabled fields are not secure.
- Include a CSRF token, and check it on the server.
- Audit
formactionandtarget, and setform-actionin your CSP. - HTML prevents leaks and mistakes. The server enforces the rules.
Next up: HTML Security Checklist, where you will bring the whole section together into one practical review you can run on any page.