Inter-Script Communication Flow
Relevant source files
The following files were used as context for generating this wiki page:The PPScan extension is composed of several distinct scripts that must communicate to orchestrate prototype pollution scans and report findings. These components include the background script, content scripts injected into web pages, the browser action popup, and a dedicated iframe script for payload delivery. The communication between them relies on a combination of standard Web Extension APIs, DOM events, and the postMessage mechanism.
This document outlines the architecture and data flows between these scripts, detailing how user actions in the popup trigger scans in the page context, how results are collected, and how they are reported back to the user interface. Understanding these pathways is crucial for debugging and extending the scanner's functionality.
Component Architecture
The extension's functionality is distributed across four main script contexts, defined in manifest.json. Each has a specific role and communicates through defined channels.
| Script | File Path | Role |
|---|---|---|
| Background Script | scripts/background.js | Central event handler, manages state (found vulnerabilities), updates the badge count, and communicates with the popup for logging. |
| Content Script | scripts/content_script.js | Injected into all web pages. Listens for triggers from the popup, injects scanning logic, and reports findings to the background script. |
| Popup Script | scripts/popup.js | Manages the UI in the browser action popup. Triggers scans in the content script and fetches log data from the background script. |
| Experiment Script | scripts/exp.js | Runs in a sandboxed, web-accessible iframe. Receives commands from the content script to execute payload tests in isolated frames. |
Sources: manifest.json:11-33, scripts/background.js, scripts/content_script.js, scripts/popup.js, scripts/exp.js
The following diagram illustrates the primary communication channels between these components.
This diagram shows the main scripts and the APIs they use to communicate with each other.
Sources: scripts/popup.js:58-67, scripts/popup.js:1, scripts/content_script.js:3-9, scripts/content_script.js:119-121
Communication Flows
1. Initiating a Scan (Popup → Content Script)
The user initiates a scan by clicking the "Search" or "Hash" buttons in the browser action popup. This interaction is handled by popup.js.
- The click event handler in
popup.jsuseschrome.tabs.executeScriptto run a small piece of code in the context of the active tab. - This code dispatches a
CustomEvent(TriggerBruteorTriggerBruteHash) on the page'sdocumentobject. - The
content_script.js, which is already running on the page, has an event listener for these specific custom events.
This mechanism allows the popup UI to trigger complex logic within the content script without needing a direct message-passing channel.
This sequence shows how a user click in the popup triggers an action in the content script via a DOM event.
Sources: pages/popup.html:86, scripts/popup.js:58-62, scripts/content_script.js:1-12
2. Executing Payloads (Content Script → Iframe)
Once triggered, the content_script.js does not execute the payloads directly. Instead, it delegates this task to exp.js running inside a web-accessible iframe to isolate the scanning logic.
- The content script creates an
<iframe>element. - The
srcof the iframe is set tochrome.runtime.getURL('/pages/iframe.html'). This page's sole purpose is to loadscripts/exp.js. - An event listener for the iframe's
loadevent is set up. - Once the iframe loads, the content script uses
iframe.contentWindow.postMessageto send the target URL and scan type toexp.js.
The message payload is an object with the following structure:
{
"url": "location.href",
"foo": "baz",
"type": "search" or "hash"
}
Sources: scripts/content_script.js:2-9, pages/iframe.html:9, scripts/exp.js:42-44
The exp.js script listens for these messages and, upon receipt, begins creating its own series of iframes to test the various prototype pollution gadgets against the provided URL.
This diagram shows the content script delegating the payload testing to the isolated iframe script.
Sources: scripts/content_script.js:2-9, scripts/exp.js:42-71
3. Reporting a Vulnerability (Content Script → Background)
When the injected scanning logic in content_script.js detects a successful prototype pollution, it needs to report this finding to the central background.js script.
- The injected checker function
check()calls alogger()function with the vulnerable URL. - The
logger()function dispatches aCustomEventnamedPPLogcontaining the URL in itsdetailproperty. - A listener in the main body of
content_script.jscatches thisPPLogevent. - This listener then calls
chrome.runtime.sendMessagewith the event detail (the URL) as the message. - The
background.jsscript has a listener forchrome.runtime.onMessagewhich receives the URL, processes it, adds it to thefoundset, and updates the browser action's badge count.
This sequence details how a finding from the page is bubbled up to the content script and then relayed to the background script for storage.
Sources: scripts/content_script.js:34-37, scripts/content_script.js:119-121, scripts/background.js:7-10
4. Displaying Logs (Popup ↔ Background)
The popup needs to display the list of found vulnerabilities, which are stored in the background.js script. This is achieved using a long-lived port connection.
- When the popup opens,
popup.jsinitiates a connection to the background script usingchrome.extension.connect({ name: "logger" }). - The
background.jsscript listens for these connections withchrome.extension.onConnect. - To fetch the logs,
popup.jssends a'send found urls'message through the port. - The background script's message listener for the port receives this request and replies with a
postMessagecontaining the array of found items. - The
popup.jsscript listens for this reply on the port'sonMessageevent and callslistFound()to render the results in the UI. - A similar flow is used for clearing the logs, where the popup sends a
'clearLog'message.
This diagram illustrates the request/response flow for fetching log data from the background script to display in the popup.
Sources: scripts/popup.js:1-3, scripts/popup.js:72-76, scripts/background.js:12-21
Summary
The inter-script communication in PPScan is a well-defined system that leverages multiple browser extension APIs to ensure separation of concerns. User interactions from the popup.js are delegated to content_script.js using custom DOM events. The content script, in turn, uses postMessage to command an isolated exp.js iframe to perform the actual scanning. Findings are reported back from the content script to the central background.js using chrome.runtime.sendMessage. Finally, a long-lived port connection between the popup and background script facilitates the display and management of scan results. This modular architecture allows each component to perform its task without interfering with the others.