All lessons Leer en español

Security in depth · Unit 20 · Lesson 3 of 27

Understanding client-side JavaScript

Follow data through browser code and distinguish public configuration from privileged secrets.

9 minready

Helpful before thisHTTP and proxies

After this lesson you can

  • explain what bundles and source maps expose
  • distinguish display logic from server authorization
  • trace a value without executing unknown code

JavaScript decides much of what a modern browser displays and requests. Reading it can explain why a form sends a particular field or why a page changes after a response. The central skill is following data, not assuming every unusual function is a vulnerability.

Follow one valueSource: Where the browser receives a value. Transformation: Parsing, validation or formatting. Use: Text display, URL, request or parserFollow one value1SourceWhere the browser receives a value2TransformationParsing, validation or formatting3UseText display, URL, request or parser
Follow a value from its source through transformations to its use.

The delivered code is not secret

Minification reduces file size; obfuscation can make reading harder. Neither reliably keeps an embedded shared secret from the person running the client. A source map links generated positions to original sources. Some include source content; others require separate files. Publishing a source map is not inherently a vulnerability: impact depends on what it reveals and the intended disclosure policy.

Read transformations, not just names

A variable called token might be a harmless identifier. A call to an HTML-parsing API might receive only trusted static content. Ask where the value came from, which transformations occur, and what eventually interprets it.

Static reading does not require executing unfamiliar code. Replacing one evaluation function with a logging call does not make the rest of a script safe to run. Treat unknown code as potentially active; do not use live credentials merely to understand a file.

Scenario: three things in one bundle

A weather app contains a publishable map key, a hidden administrator menu, and a credential for a private billing service. The map key may be intended for distribution, subject to its provider’s restrictions. Hiding the menu affects presentation; the server must still enforce permissions. The billing credential should be revoked, replaced, and moved out of the public client, with exposure reviewed.

Short-lived user tokens can legitimately reach a browser. Their lifetime, audience, scope, storage, and exposure differ from a shared administrative application secret.

Browser contexts still matter

Workers and service workers are not automatically new origins. Their security behavior follows worker type, origin, scope, and browser policy. A service worker can influence requests within its scope, which makes deployment and update integrity important.

CORS controls a browser’s access to certain cross-origin responses. It does not supply server authorization. Likewise, a restrictive content security policy can limit script behavior without making every data flow safe.

Describe a finding precisely

Record the source, transformation, sensitive use, expected control, and evidence. A client-side role check alone is a design clue; confirming that the server omits authorization is a separate conclusion. Continue with XSS to understand why the final parsing context matters.

Trace one value to two destinations

An invented club app receives a member’s display name as JSON. In the profile view, a normal text renderer displays it. In an export preview, the same name is combined with a markup string before an HTML-parsing operation. These are supplied design records, not code to execute.

Stage Profile Export preview
Source Member-controlled name Same stored name
Transformation Whitespace trimmed Whitespace trimmed
Final use Text rendering HTML parsing

Trimming changes spacing; it does not define an HTML policy. JSON transport and database storage also do not grant the name authority to become markup. The profile’s text treatment preserves its intended meaning. For the export, choose a text slot or a rendering mechanism that preserves text in the final context.

PredictThe team fixes the profile renderer. Is the export preview covered by that fix?

The records show a separate final use. Review and correct the export path too. A safe treatment at one destination does not automatically protect another consumer of the same stored value.

This design review identifies a missing rendering boundary. It does not by itself establish script execution, affected sessions, or the number of people exposed. Keep the source-to-use explanation separate from claims about observed impact.

Repeat the tracing method for credentials with a different question: what authority does the value grant, and who is expected to possess it? An intentionally public identifier and a shared privileged secret can both look like long strings. Classify them from the provider’s contract, intended distribution, permissions, and lifetime, rather than variable names.

A good review note can be short: name the value’s source, transformations, final interpreter, intended meaning, proposed control, and the result that would verify that meaning.

Terms you met

bundleminificationsource mapsinkpublic client

Check yourself

No timer. No penalties. Read the explanation and try again whenever you like.

  1. The club trims a stored name before parsing it as HTML. Which boundary remains unresolved?

    Show the answer

    Correct answer: Whether member-controlled text can be interpreted as markup at the final use. Whitespace trimming does not provide an HTML rendering policy. Use a text-preserving destination for a plain name.

  2. The profile renderer is fixed, but the export has its own HTML-parsing path. What should the review conclude?

    Show the answer

    Correct answer: The export needs its own correction and verification of text treatment. Follow the same value to each destination rather than assuming one fix covers every use.

  3. A public bundle contains a long string named token. What best determines whether it is a secret exposure?

    Show the answer

    Correct answer: The issuing service's contract, intended distribution, and authority granted by that value. A public identifier and a privileged credential require different handling even when their shapes look similar.

  4. A private billing credential was shipped to every browser. What is the appropriate corrective direction?

    Show the answer

    Correct answer: Retire the credential, review its exposure, and keep replacement shared authority out of public clients. Removal alone does not revoke existing copies. Correct both the credential lifecycle and the distribution design.

  5. A static review finds only a client-side role check. Which conclusion is supportable?

    Show the answer

    Correct answer: The interface restricts presentation; server enforcement needs separate evidence. Locate the authoritative operation and its policy evidence before asserting a missing server control.

Try it

  • WriteWrite two source-to-use notes for the club’s display name, one for the profile and one for the export. Explain why trimming is insufficient for the export. Add a verification criterion that checks the name remains text without executing unknown code.
References