← Glossary

What Is Script Injection?

Script injection is the introduction of executable code into a web page or application context that did not originally contain it. It ranges from authorised mechanisms such as browser extension content scripts and testing tools to cross-site scripting, where an attacker gets code to run through a vulnerability.

Updated:

Script injection means introducing additional executable code into a web page or application context, whether deliberately through an integration or unexpectedly through an unsafe input path. The code might be a content script that a browser extension places on every page, a snippet a developer evaluates through a testing tool, a tag manager that loads third-party scripts, or a payload that an attacker smuggles in through a form field the application failed to sanitise. The mechanism is similar in each case: new code enters a page's script execution context and runs with some set of privileges there. What differs, and what matters, is who put it there, with what permission, and in which context it executes. Treating every injection as an attack overlooks legitimate uses; treating all of it as benign ignores the class of vulnerability that makes the term alarming.

Where Scripts Can Be Introduced

A page's code arrives through several doors, and injected script can use any of them.

  • Server response. If the server composes a page from untrusted input without encoding it, the input can contain markup that the browser executes. This is the classic cross-site scripting path.
  • Client-side DOM manipulation. Code already on the page can create script elements, set event handler attributes or evaluate strings. A page that writes user-supplied data into the DOM with an unsafe API can be made to execute it, which is DOM-based cross-site scripting when the data is attacker-controlled.
  • Browser extensions. Extensions declare content scripts in their manifest or inject them programmatically. Chrome's documentation describes content scripts as running in the context of web pages with access to the DOM, injected statically, dynamically or on demand.
  • Developer and automation tooling. Debugging protocols and test frameworks can evaluate arbitrary code in a page, which is how automated tests read values and how developers experiment in the console.
  • Third-party tags. Analytics, advertising and consent scripts are loaded by the page's own code and run with full page privileges; a compromised tag provider is an injection vector the site chose to trust.
  • Network intermediaries. On unencrypted connections, a device on the path can modify responses to include script. HTTPS protects against ordinary network modification unless an intermediary can terminate the trusted TLS connection.

Each door has a corresponding control: output encoding and safe APIs for the first two, the extension permission model for the third, access control on debugging endpoints for the fourth, subresource integrity and vendor review for the fifth, and transport encryption for the sixth.

Authorised Injection and Its Security Boundaries

Browser extensions are the clearest example of injection by design, and their model shows how boundaries are drawn. A content script shares the page's DOM, so it can read and change what the user sees, but by default it runs in what Chrome calls an isolated world: a private execution environment whose JavaScript variables are invisible to the page and to other extensions. Variables and functions are not directly shared between those worlds, even though both see the same document. Explicit message passing can connect them and must validate untrusted messages. An extension that needs to run in the page's own context must ask for the main world explicitly, giving up that separation. Programmatic injection through the scripting API requires the scripting permission plus host permissions for the site, or the temporary permission granted when the user invokes the extension on the current tab. The user, through install-time and runtime consent, is the authority that makes extension injection legitimate.

Testing and automation follow a different logic: the operator may control the test environment and have authorisation to instrument it. Running a tool does not itself establish that authority or remove the need to protect test accounts and data. The boundary here is operational. A debugging endpoint that can evaluate script in any open page must not be reachable by anyone else, because to them it is an injection vector with no permission prompt at all. Tag managers sit in between: the site owner authorises them, but they extend trust to whatever the vendor serves, and a Content Security Policy that restricts script sources is the usual way to bound that trust.

Script Injection Compared With XSS and DOM Mutation

TermWhat it namesRelationship
Script injectionAny introduction of executable code into an application contextThe general act, authorised or not
Cross-site scripting (XSS)A vulnerability where untrusted data is placed into a page so the browser treats it as codeScript injection achieved through a flaw, by someone without permission
DOM mutationAny change to the document treeA possible side effect of injection, but most mutations involve no new code

The OWASP cheat sheet frames XSS as what happens when an application puts untrusted data into a page in a way that lets the browser treat it as code. The defining elements are untrusted data and the absence of authorisation. An extension's content script is injected but is not XSS; a comment field that renders a stored script tag is XSS and is also injection. Script injection vs XSS is therefore a question of scope: XSS is the subset that constitutes a vulnerability.

DOM mutation is a different axis. Injecting a script element mutates the DOM, but changing an attribute, inserting text or re-rendering a component also mutates it without executing anything new. A mutation observer can report that a script element appeared; it cannot say whether the insertion was an extension doing its job, a tag manager loading analytics, or an attack. Conversely, injection can occur without a visible mutation, as when a string is evaluated directly.

Defending Against Unauthorised Injection

Defences target the unauthorised doors. For server and DOM paths, OWASP recommends output encoding for data displayed as text, a maintained sanitiser when user-authored HTML must be accepted, framework features that encode by default, and safe APIs that treat data as text rather than markup. Content Security Policy restricts which script sources the browser will execute and is positioned by OWASP as an additional layer of defence rather than a sole control. Subresource integrity checks that eligible fetched scripts match expected hashes; dynamic dependencies and vendor changes require separate handling. HTTPS protects the network path against ordinary response tampering. Reviewing which extensions are installed, and with what host permissions, addresses the authorised door when the authorisation was careless.

Limits of the Concept

Script injection describes a capability, not an outcome. Code injected into an isolated world cannot read page variables; code injected into the main world can do anything the page can, including reading cookies not marked HttpOnly and sending data elsewhere. Whether an injection matters depends on the context it lands in and the privileges that context holds. Detection is likewise bounded: a page can observe DOM changes and enforce a policy on script sources, but it cannot reliably tell a legitimate extension from a hostile one, and users should not expect any site to protect them from code they themselves installed. A useful review therefore identifies the source of the code, the world in which it executes and the data it can reach. The same visible DOM change can come from very different trust relationships.

Frequently asked questions

Is every script injection an XSS vulnerability?

No. XSS is the subset where untrusted data is placed into a page so that the browser executes it, without authorisation. Extension content scripts, testing tools and tag managers inject code with permission, and none of those is a vulnerability in itself.

Can an extension inject scripts with permission?

Yes. Extensions declare content scripts or inject them programmatically, which requires the scripting permission and host permissions or the temporary permission the user grants on the active tab. By default the code runs in an isolated world that shares the DOM but not JavaScript variables with the page.

Does changing the DOM always execute code?

No. A mutation can change text, structure or attributes without executing code. Whether an inserted script runs depends on the insertion method, script type and applicable policy. An observer reporting a script element does not prove execution or establish who authorised it.

What is an isolated world?

It is the private execution environment in which extension content scripts run by default. The script and the page see the same document, but variables and functions defined by one are invisible to the other, which keeps the extension and the page from interfering with each other's code.

Does Content Security Policy prevent script injection?

A well-designed CSP can block many unsafe execution paths, but it supplements encoding, safe APIs and sanitisation. Extension behaviour depends on context: isolated content scripts have their own restrictions, while Chrome documents that scripts injected into the main world follow the page's CSP.