Sub topic 1
Why Component Code Needs Isolation
Prerequisites
This chapter assumes that you know basic Lightning web component structure, modern JavaScript modules and classes, the DOM, the same-origin policy, Salesforce namespaces, and static resources.
Why Component Code Needs Isolation
Lightning web components run in a shared browser page. A page can contain components from Salesforce, installed packages, and custom code. Without isolation, one component could inspect another component's private DOM, change its JavaScript objects, read data held in memory, or interfere with event handling.
The browser's same-origin policy limits access between different origins, but components within one Salesforce page often share an origin. Salesforce therefore adds a component-level security boundary. Isolation limits what code can reach and reduces the effect of a vulnerable or badly behaved component.
How LWS Isolates Code by Namespace
Lightning Web Security (LWS) gives each namespace its own JavaScript sandbox. A namespace is an identifier that groups Salesforce metadata and code, such as an installed package namespace or an org's default namespace.
Code inside a sandbox receives controlled views of global objects and browser resources. Components in the same namespace can use supported component APIs, but code in one namespace cannot freely reach another namespace's objects or DOM. Communication crosses the boundary through public component interfaces, properties, and events.
Sandboxes and Distortions
A sandbox controls access. A distortion changes the behavior that code observes when it uses a permitted browser API. For example, LWS can provide a wrapped view of a global object or prevent an operation that could cross a security boundary.
This can make an object appear different from the browser's raw object. Identity checks, prototype checks, direct global mutation, and libraries that depend on undocumented browser behavior can fail. Treat platform APIs as the supported contract. Do not rely on reaching through a component's internal DOM or on sharing mutable globals across namespaces.
Lightning Locker Compared with LWS
Lightning Locker was the earlier security architecture. It commonly used secure wrapper objects and stricter restrictions around JavaScript and the DOM. LWS uses JavaScript sandboxes and membranes while preserving more standard browser behavior. As a result, many modern libraries work with fewer changes under LWS, although unsupported assumptions can still break.
When migrating, test component DOM access, global objects, third-party libraries, and cross-namespace communication. Change code that depends on private internals rather than weakening the security boundary.
Exercise
Why can two components on the same page be unable to share a global object directly?
Show answer
LWS places their namespaces in separate sandboxes, so each component receives a controlled view. Use public properties and events for supported communication.