Developer security mini-course

Lesson 1: Secure Input Handling and Output Encoding

Learn secure input handling with practical examples of validation, parameterised queries and context-aware output encoding. Includes a short review exercise.

Secure input handling starts by identifying where data crosses a trust boundary. Browser forms, API requests, uploaded files, message queues and responses from connected services can all carry values your code should treat carefully.

Input validation and output encoding solve different problems. Validation checks whether a value is acceptable for its intended purpose. Encoding helps keep that value as data when it enters a particular output context.

Learning outcome

By the end of this lesson, you should be able to trace an untrusted value from its source to the operation that uses it and select a suitable control at each step.

Consider a customer searching an order history. The search term may pass through a database query, an HTML results page and a log entry. Each use has different requirements.

Validate according to the business rule

Define the expected type, length, range and format, then enforce the rule on the server. Browser checks improve usability but can be bypassed by sending a request directly.

An order quantity might need to be an integer within an allowed range. An identifier might need a defined format and a separate ownership check. Free text needs suitable length limits and safe handling, rather than a rule that rejects every punctuation mark.

Normalise values carefully where needed, applying validation to the representation the application actually uses. Avoid transformations that turn a previously accepted value into an unsafe one later.

Keep data separate from instructions

Use parameterised database queries and supported APIs that preserve the distinction between data and commands. Validation alone is not a substitute for parameter binding.

This pseudocode illustrates the intended separation:

term = validate_search_text(request.search)
rows = database.query(
    "SELECT id, title FROM orders WHERE owner_id = ? AND title = ?",
    [current_user.id, term]
)

The application also scopes results to the current user. Parameterisation protects the query structure; it does not decide which records the user is permitted to access.

Avoid building operating system commands from submitted strings. Prefer a library API for the required task and constrain arguments and executable choices where a command is unavoidable.

Encode for the output context

Use the template or framework's safe output mechanisms. HTML text, HTML attributes, URLs and JavaScript are different contexts; an encoding suitable for one may be wrong for another.

Avoid inserting untrusted values into inline scripts or unsafe DOM operations. If users are allowed to submit formatted HTML, use a maintained sanitisation component with an explicit policy for permitted content.

Treat every source consistently

Hidden fields, read-only controls and the JSON produced by your own frontend are still client input. Data read from a database may also originate from an untrusted source.

Validate third-party responses against the structure and limits you expect. Trust in the supplier does not make its content safe for every downstream operation.

Review exercise

Choose a feature that accepts text and identify:

  1. Every source of the value, including imports and background tasks.
  2. The server-side rule for an acceptable value.
  3. Every database, template, file or system operation that uses it.
  4. The control that keeps it as data at each destination.
  5. Whether an independent permission check is also needed.

Test valid boundary cases as well as invalid values. Confirm that error handling avoids leaking sensitive details and that rejected input does not produce unintended side effects.

Use the same trace during code review when the feature changes. The useful question is how the value is used, rather than whether one filter has been applied somewhere in the request.

Read our Testing methodology to discuss coverage, or Book a demo with the systems and objectives you have in mind.

See your first test report this week.

Book a 30 minute demo. We will verify one of your targets with you and run a test against it, so you can judge the results on your own systems.

Book a demo