Study guides · Intermediate

Memory Safety and Web Security

Distinguish memory-boundary violations from injection across interpretation contexts, then choose the relevant defense.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Keep memory access within valid boundaries
  2. Identify where data becomes an instruction
  3. HTML and JavaScript have different contexts
  4. Test several distinct layers
  5. Trace three boundaries in one search request
  6. Read Cookie attributes by responsibility
  7. Separate request submission from accepted mutation
  8. Do not extend a memory guarantee into every Web decision
  9. Ask Logeia about this articleCheck your understanding

Keep memory access within valid boundaries

Memory safety concerns accesses staying within valid storage bounds and lifetimes. Reading beyond an array or accessing released storage can expose data or corrupt execution. Bounds checks and ownership rules can prevent classes of these faults. They do not, by themselves, ensure that an application returns a document only to an authorized user. That is a different boundary enforced through application policy. Describe which fault class a language or mechanism addresses instead of treating “safe” as an unlimited property covering memory behavior, authorization, cryptography and business logic simultaneously.

Identify where data becomes an instruction

Injection occurs when a value supplied as data changes the structure of an interpreted command or document. Concatenating search text into SQL can turn value characters into query syntax. Parameterized queries separate the statement structure from bound values. However, table names and column names are not generally substitutable through ordinary value parameters. Those positions need a different design, such as selecting from an explicit set of allowed identifiers. A universal escaping function is therefore not a complete strategy. First identify the interpretation context and determine whether the input belongs there at all.

HTML and JavaScript have different contexts

Displaying a user name as HTML text is different from embedding it inside a JavaScript string or a URL attribute. Each context has its own syntax and safe handling rules. Prefer templates that render values as ordinary text, and avoid raw HTML insertion unless a suitable trusted sanitizer is required and correctly applied. URL contexts also require a policy for allowed schemes. Content Security Policy can reduce consequences as an additional layer, but it does not replace correct output handling. A transformation performed once when input is received cannot automatically make that value safe in every later context.

Test several distinct layers

Test each boundary with representative failures: input lengths near a limit, unexpected syntax, another owner’s identifier and lifetime violations where relevant. Least-privilege database credentials can reduce damage if query handling fails, while browser response headers do not perform server-side authorization. Even an attack string recorded for investigation must remain inert when a log viewer displays it. Explain what each layer still protects when another layer fails. This produces a clearer design than labeling every protection “input validation” and assuming that one check at the front door covers all later interpreters and resources.

Trace three boundaries in one search request

Consider a document-title search. The server first derives the requester from a session and determines the documents that principal may read. It then fixes SQL structure and binds the search text as a value. Finally it displays returned titles as HTML text. These are different boundaries: identity and permission, SQL interpretation, and HTML interpretation.

Safely bound search text can still return another person's titles if owner scope is wrong. Correct authorization can still be followed by unsafe HTML insertion. Success at one boundary cannot substitute for the others. The parameterized-query experiment compares concatenated and bound values using disposable SQLite. Its verified results do not establish authorization for a real service or execution behavior for a different database. Follow the full request path when deciding which additional checks are needed.

BoundaryDecisionSeparate remaining responsibility
MemoryUse valid bounds and lifetimeDocument permission
AuthorizationPrincipal may read resourceSQL structure/value separation
SQLBind search text as a valueHTML rendering of results
HTMLRender title as textRequest intent and CSRF
MutationCheck Origin, token, and other policyApplication-specific invariants

Read Cookie attributes by responsibility

Cookie attributes describe scope and transmission conditions. Secure concerns HTTPS, HttpOnly restricts script access, and SameSite concerns site relationships. HttpOnly does not mean the browser stops attaching that cookie to requests. Path is also not a service-level authorization rule.

The Cookie experiment uses local HTTPS with a test CA trusted only for the disposable profile and observes actual browser requests. It distinguishes a different origin in the same site, cross-site fetch, and a top-level cross-site GET. Record sent values, request type, and source rather than inferring everything from an attribute name. Additional browser policies, including third-party-cookie restrictions, can matter. Preserve the browser version and environment, and confirm that site and origin are not interchangeable concepts before interpreting the results.

Separate request submission from accepted mutation

A logged-in browser attaching a cookie does not prove that the user intended an update. The CSRF experiment submits real forms from a different origin in the same site. It compares an operation checking only the cookie with one also checking Origin and a token. The first mutates; the protected route must leave state unchanged while its conditions are unsatisfied.

A CORS restriction on response reading does not establish that a form or side effect never occurred. A 403 returned after mutation would also fail the requirement. Pair response status with server-side counters. An adversary who can learn the token or execute arbitrary same-origin script has different capabilities. CSRF defenses alone do not prevent XSS. Combine this experiment with session design and state precisely whose authority and intent each check addresses.

Do not extend a memory guarantee into every Web decision

A memory-safe implementation can still concatenate SQL or trust another person's identifier. Conversely, a correctly authorized operation can pass invalid storage to a native extension. The memory-safety guide compares bounds, lifetime, and release conditions with the service responsibilities left over.

During review, follow one value from input through authentication, authorization, interpretation, output, and external libraries. Check the contract of each context rather than applying the same escaping function or annotation everywhere. Tests should distinguish successful searches, rejected requests, text rendered inertly, and state preserved on failure. Return to the protected asset in the threat model and explain which path and condition each control covers. That connection makes the collection of defenses understandable as a design rather than a list of unrelated settings.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 22

Choose a learning path