Skip to article content
FrontendXLesson 18 / 20

Security review of a change

Turn a vague failure report into a reproduction, isolate the cause, form and falsify hypotheses, instrument the code, fix async and state bugs, lock the fix behind a regression test and review changes for real trade-offs.

advanced · about 40 minutes · 2 exercises

  • Explain
  • Debug

What you will be able to do

#
  • Trace untrusted input to the place it is used and check the trust boundary.
  • Separate a blocking security issue from a hardening nit.

Overview

#

A security review asks one question: what can an attacker control, and where does that value reach? A change is risky when untrusted input crosses a trust boundary without a check.

The first class is injection. Any value that is inserted into HTML, a URL, a query or a shell command can change the meaning of that context. Rendering user content as HTML is the classic case.

snippet.js
// blocking: user content is executed as markup
el.innerHTML = comment.body;

The fix is to render as text, or to sanitise with a maintained library when markup is genuinely required. Escaping at the point of output is the reliable defence, not escaping on the way in.

The second class is a leaked secret. A token, key or password written to a log, an error message or a client bundle is exposed to anyone who can read that output. Log an identifier instead of the secret.

snippet.js
// blocking: the token is written to the log
logger.info({ token }, 'auth attempt');

// fixed: log a non-secret identifier
logger.info({ userId }, 'auth attempt');

The third class is a missing origin or authorisation check. A message handler that trusts event.data without checking event.origin, or a client that trusts an id it was given without a server-side authorisation check, can be driven by another page or another user. Client-side checks are usability; the server is the boundary that enforces access.

The fourth class is an open redirect. Redirecting to a URL taken from a query parameter lets an attacker send users to another site under your domain. Allow only relative paths or an allow-list of hosts.

Review the dependencies in the same change and check that a new package is necessary. When you report an issue, name the input, the boundary it crosses and the concrete consequence, and label it blocking or a nit. A blocking issue is an exploitable path or a data leak; a nit is hardening that does not change the current risk.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Debug

    This message handler trusts whatever another window sends, so any page that can post to it can drive the action. Fix it to check the origin and validate the message shape before using the data.

    starter.javascript
    window.addEventListener('message', (event) => {
      applyTheme(event.data.theme);
    });

    Reveal the reference answer
    reference.javascript
    const ALLOWED_ORIGIN = 'https://app.example.com';
    window.addEventListener('message', (event) => {
      if (event.origin !== ALLOWED_ORIGIN) return;
      const theme = event.data && event.data.theme;
      if (theme !== 'light' && theme !== 'dark') return;
      applyTheme(theme);
    });

    Authored expected output: Messages from any origin other than the allow-listed origin are ignored, and a message whose theme is not light or dark is ignored.

    Without an origin check any page that can reach this window can send a message. Comparing event.origin to an allow-list, then validating the payload against the expected values, closes the untrusted path before the value is used.

  2. Exercise 2Explain

    Give a security review rubric for a change that renders user content and calls a backend, and state which findings are blocking.

    Reveal the reference answer
    reference.text
    Trace every value the user can control to the place it is used. Check output encoding: user content rendered as HTML must be rendered as text or sanitised at the point of output. Check for secrets: no token, key or password in a log, an error message or a client bundle. Check the trust boundary: authorisation is enforced on the server, and client checks are only usability. Check message handlers for an event.origin allow-list and payload validation. Check for open redirects by allowing only relative paths or an allow-list of hosts. Check that any new dependency is necessary. Blocking findings are an exploitable path, a leaked secret or a missing server-side authorisation check; hardening that does not change the current risk is a nit.

    Authored expected output: A rubric that traces untrusted input, checks output encoding, secrets, authorisation, message origins and redirects, and labels exploitable paths as blocking.

    Security review is a trace from attacker-controlled input to its use, so a rubric that follows the value finds the real issues. Labelling by consequence keeps the author focused on what must change before merge.

Common misconceptions

#
  • Client-side validation is a security boundary.
  • An issue is only blocking if it is easy to exploit.

Follow-up practice

#
  • debugging-review-19-test-flake-triage

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.