Browser Event Listeners
Relevant source files
The following files were used as context for generating this wiki page:The PPScan extension relies on a distributed event-driven architecture to detect client-side prototype pollution vulnerabilities. Event listeners are implemented across its main components—the background script, content scripts, and the popup UI—to manage state, handle user interaction, intercept network requests, and coordinate scanning activities. These listeners enable both passive analysis of scripts and active injection and testing of pollution gadgets.
Communication between these components is primarily achieved through message passing and custom DOM events. The background script acts as a central coordinator, listening for web requests and messages from other parts of the extension. Content scripts listen for custom events triggered by the popup to initiate scans on the page, and they also listen for messages from their own injected code to report findings. This separation of concerns allows each component to react to specific browser events relevant to its role.
Background Script Listeners
Sources: scripts/background.js, scripts/utils.js
The background script is a persistent component that orchestrates the extension's core functionality by listening to various chrome.* API events.
chrome.runtime Listeners
The background script uses chrome.runtime listeners for installation, setup, and communication.
-
onInstalled: On installation, this listener sets the default state for the scanning modes inchrome.storage.sync.toggle(Active Mode):falsebuster(Window Mode):falsepassive(Passive Mode):true
Sources: scripts/background.js:23-31
-
onMessage: This listener handles messages sent from content scripts. When a content script detects a potential prototype pollution via active scanning, it sends a message containing the URL. The background script receives this URL, formats it into a finding object, adds it to thefoundset, and updates the badge count.
Sources: scripts/background.js:7-11
chrome.extension.onConnect
This listener manages long-lived connections, specifically from the popup script. When the popup opens, it establishes a connection named "logger". The background script then listens for messages on this connection's port.
clearLog: Clears all findings from thefoundset and resets the badge count.send found urls: Sends the current list of findings back to the popup for display.
Sources: scripts/background.js:13-21
The following diagram illustrates the communication flow between the popup and the background script to clear logs.
chrome.webRequest Listeners
Web request listeners are crucial for both active and passive scanning modes.
-
onHeadersReceived: If Active Mode (toggleis true), this listener intercepts network responses to modify security headers that might prevent scanning, such asContent-Security-PolicyandX-Frame-Options. This allows the extension to operate in environments with restrictive security policies.
Sources: scripts/background.js:37-54 -
onCompleted: If Passive Mode (passiveis true), this listener is attached after the vulnerability database is downloaded. It triggers thescanfunction for every completed script request ("type": "script"), which then analyzes the script's content for known vulnerable patterns.
Sources: scripts/utils.js:133-136, scripts/background.js:56-58
This flowchart shows the process for passive scanning.
Content Script Listeners
Sources: scripts/content_script.js
The content script injects code into web pages and uses event listeners to communicate with the popup and its own injected logic.
Custom DOM Event Listeners
The content script listens for custom events dispatched from the popup UI to initiate scans.
-
TriggerBrute/TriggerBruteHash: When the "Search" or "Hash" button is clicked in the popup, a script is executed in the active tab that dispatches aTriggerBruteorTriggerBruteHashevent, respectively. The content script's listener for this event creates a hiddeniframe(/pages/iframe.html), waits for it to load, and then posts a message to it containing the page's URL to begin gadget testing.
Sources: scripts/content_script.js:1-24, scripts/popup.js:46-51 -
PPLog: The script logic injected directly into the page's DOM uses aCustomEvent('PPLog', ...)to send findings (the polluted URL) up to the content script. The content script'sPPLoglistener then forwards this information to the background script usingchrome.runtime.sendMessage.
Sources: scripts/content_script.js:108-110, scripts/content_script.js:41-44
The sequence diagram below shows how a brute-force scan is initiated.
Popup Script Listeners
Sources: scripts/popup.js, pages/popup.html
The popup script (popup.js) adds onclick listeners to buttons in popup.html to provide user control over the extension's functionality.
window.onload
The logger function is assigned to window.onload. This function is responsible for initializing the popup's state by retrieving settings from chrome.storage.sync and attaching all the necessary onclick event handlers to the buttons.
Sources: scripts/popup.js:71
onclick Handlers
The popup UI contains several buttons that trigger actions via click listeners.
| Button ID | Action on Click |
|---|---|
toggle | Toggles Active Mode (toggle in chrome.storage). |
buster | Toggles Window Mode (buster in chrome.storage) for frame-busting sites. |
passive | Toggles Passive Mode (passive in chrome.storage). |
brute | Executes a script in the active tab to dispatch a TriggerBrute event, starting a search parameter-based gadget scan. |
brutehash | Executes a script in the active tab to dispatch a TriggerBruteHash event, starting a hash-based gadget scan. |
clear | Posts a clearLog message to the background script to clear all stored findings. |
Sources: scripts/popup.js:14-54, pages/popup.html:81-96
Iframe Message Listener
Sources: scripts/exp.js
The scanner iframe (/pages/iframe.html), which loads exp.js, plays a critical role in active scanning by providing an isolated environment to test gadgets.
window.addEventListener('message', ...)
This is the primary listener in exp.js. It waits for a message from the content script.
- If
event.data.foo == "bar": This message initiates the initial active scan to find a pollution vector. It creates new iframes or windows (depending on "Window Mode") with test payloads in the URL. - If
event.data.foo == "baz": This message indicates a pollution vector was likely found, and the script should now test for exploitable gadgets. It iterates through theGADGETSarray and injects each one into a new iframe, either in the search parameters or the URL hash, to see if it triggers an action (likedebugger).
Sources: scripts/exp.js:34-97
This flowchart describes the gadget testing logic within the exp.js iframe.
Sources: scripts/exp.js:35-64
Conclusion
The PPScan extension's functionality is fundamentally driven by a network of event listeners distributed across its background, content, and UI scripts. chrome.* API listeners in the background script handle persistent tasks like network interception and state management. Custom DOM event listeners in the content scripts provide a bridge between the user-facing popup and the web page context, allowing scans to be initiated on demand. Finally, postMessage and its corresponding listener in the isolated scanner iframe enable the safe and systematic testing of numerous pollution gadgets. This event-driven, component-based architecture allows for clear separation of concerns and effective coordination for vulnerability scanning.