Skip to main content

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, never GET
  • Why the form's action must be https
  • How to mark up password and one-time code fields
  • What hidden fields, formaction and target can do to you
  • How the CSP form-action directive 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.

index.html
<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>
Response headers
Content-Security-Policy: form-action 'self'; frame-ancestors 'self'

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 Referer header 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" and new-password tell 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.

info

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​

  • formaction on a button or input overrides the form's action. A button with formaction="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. Add rel="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:

AttributeWhy it is not protection
required, pattern, min, max, maxlength, type="email"Can be removed in DevTools, or skipped by sending a request directly
readonly, disabledEditable in DevTools. Disabled fields are simply not sent, and a sender can add them back
type="hidden"Readable and editable
accept on a file inputA hint for the file picker only
autocomplete="off"Does not stop browsers or managers reliably
novalidate, or its absenceOnly 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​

  1. Open DevTools, then Network, submit a test form, and check the request method and whether the address contains your test values.
  2. Check that the form's Request URL starts with https://.
  3. Look at the Request Payload or Form Data. Are any values you did not expect being sent?
  4. In Elements, search for formaction, type="hidden", autocomplete="off", and method="get" on forms with sensitive fields.
  5. Try your sign-in form with a password manager. Does it fill the right fields?
  6. Run the Form Auditor tab on your own form markup.
  7. Check Response Headers for form-action and frame-ancestors.

Best Practices​

  1. Use method="post" for anything sensitive or state-changing.
  2. Use https:// or relative actions.
  3. Set autocomplete correctly on password, username, and code fields.
  4. Never block pasting in password fields.
  5. Treat hidden fields as editable. Check values on the server.
  6. Include a CSRF token in every state-changing form.
  7. Audit formaction and target.
  8. Send form-action and frame-ancestors in your CSP.
  9. Validate everything on the server, whatever the HTML says.
  10. Collect only what you need.

Quick Checklist​

  • Sensitive forms use method="post"
  • Every form action is https or relative
  • Password fields use type="password" and the right autocomplete
  • 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, and target="_blank" forms use rel="noopener"
  • form-action and frame-ancestors are 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​

  1. Leaving out method, so the form uses GET.
  2. Sending credentials in the address.
  3. An http action on an https page.
  4. type="text" for a password field.
  5. autocomplete="off" as a security measure.
  6. Blocking paste on password fields.
  7. Trusting hidden fields, or readonly and disabled fields.
  8. Relying on required and pattern as validation.
  9. Forgetting the CSRF token, or adding it but never checking it.
  10. Unreviewed formaction attributes.

Practice Time​

  1. In the Submit Lab, keep get and https off. List every place the password is visible.
  2. Switch to post and tick https. What can a person on the network still see?
  3. Switch to post with https off. What is still exposed?
  4. Load the messy forms into the Form Auditor, and fix every problem until it reports a clean result.
  5. Complete the Autocomplete Matcher.
  6. Open a sign-in page you own. Does a password manager fill it correctly?
  7. In DevTools, change a hidden field's value and submit. What does your server do?
  8. 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 POST over https.
  • 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 formaction and target, and set form-action in 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.