KodingKaro
HTML · Advanced · Security and Privacy

Safe links, forms, and untrusted input

HTML is the surface where user data is displayed and submitted, so safe handling must include the server as well as the browser.

✓ What you will learn in this topic

  • Explain the purpose of Safe links, forms, and untrusted input in a real HTML document.
  • Choose and apply the elements shown in both examples.
  • Recognize the specific mistake described in this lesson.

Concept explanation

Use descriptive external links and protect intentional new-tab contexts with rel=\"noopener\". Form constraints can improve experience but never establish trust. The server must validate, authorize, and safely encode values. When displaying user text, use text-oriented DOM APIs rather than treating input as HTML. If rich text is required, use a well-maintained sanitizer with an explicit allowlist.

Syntax

Keep untrusted values as text; validate and encode server-side and only allow intentional safe markup.

Example 1: a small page

The destination is clear and the opener relationship is protected.

<a href=\"https://partner.example\" target=\"_blank\" rel=\"noopener\">Visit our partner (new tab)</a>

Expected result: The partner page opens in a new tab and cannot access the opener window through its normal reference.

Example 2: a practical page

This markup makes a clear submission interface; the server still must validate and safely store or display the value.

<form action=\"/feedback\" method=\"post\">
  <label for=\"feedback\">Your feedback</label>
  <textarea id=\"feedback\" name=\"feedback\" required></textarea>
  <button type=\"submit\">Send feedback</button>
</form>

Expected result: A labeled form submits feedback to the server, where trust and storage decisions must be enforced.

Common mistakes

Do not trust browser validation, user-generated HTML, or an iframe merely because it is visually separated.

Best practices

Validate and encode on the server, constrain embedded content, minimize external requests, and use safe DOM text APIs.

Practice exercise

Review a page with user comments, an external link, a third-party video, and a signup form. List the server checks, link protections, and embed restrictions it needs.

Lesson summary

  • Client markup can guide input but cannot replace server validation and safe output handling.
  • Validate and encode on the server, constrain embedded content, minimize external requests, and use safe DOM text APIs.

Knowledge check

Why should user comments not be inserted directly with innerHTML?

Show the explanation

The string may contain markup that creates script execution or other unsafe behavior; render as text or sanitize intentionally.

Practical example

html
<a href=\"https://partner.example\" target=\"_blank\" rel=\"noopener\">Visit our partner (new tab)</a>

Expected result:

The partner page opens in a new tab and cannot access the opener window through its normal reference.

Hands-on practice task

Review a page with user comments, an external link, a third-party video, and a signup form. List the server checks, link protections, and embed restrictions it needs.

In this section

Other lessons in Security and Privacy

Broaden your knowledge

Related learning topics