Background Script (background.js)
Relevant source files
The following files were used as context for generating this wiki page:The background.js script is the central, persistent component of the PPScan browser extension. It acts as the core controller, managing the extension's state, handling communication between different parts of the extension (like the popup and content scripts), and orchestrating both passive and active security scanning operations. It is responsible for initializing default settings, managing a list of found vulnerabilities, updating the browser action badge, and intercepting network requests to perform its analysis.
The script is loaded via pages/background.html, which is defined as the persistent background page in the extension's manifest. This ensures that the script runs for the entire duration of the browser session, allowing it to monitor activity across all tabs.
Sources: manifest.json:14-17, pages/background.html:8, scripts/background.js
Architecture and Core Components
The background script orchestrates the extension's functionality by listening to various events and managing data. Its primary responsibilities include initialization, communication handling, and managing the two main scanning modes.
Initialization
Upon installation, the background script sets up the initial configuration in chrome.storage.sync. It also initializes an in-memory Set to store vulnerability findings and sets the browser action badge count to zero.
Sources: scripts/background.js:27-35
The following diagram illustrates the extension's installation and initialization flow.
Diagram showing the initialization process upon extension installation.
Sources: scripts/background.js:4, scripts/background.js:6, scripts/background.js:27-35
The default settings are as follows:
| Key | Default Value | Description |
|---|---|---|
toggle | false | Disables "Active Mode" by default. |
buster | false | Disables "Window Mode" (frame buster) by default. |
passive | true | Enables "Passive Mode" by default. |
Sources: scripts/background.js:27-35
Communication Hub
background.js serves as a message broker between the content scripts and the popup UI. It uses two distinct listeners for this purpose.
chrome.runtime.onMessage: Listens for messages from content scripts. When a content script detects a potential prototype pollution vulnerability through active checks, it sends a message containing the URL. The background script adds this finding to thefoundset and updates the badge count.chrome.extension.onConnect: Manages a persistent connection with the popup script (popup.js). This is used to handle requests from the popup to retrieve the list of found vulnerabilities or to clear the log.
The following sequence diagram shows the communication flow between the extension's components.
Diagram illustrating message passing between the content script, background script, and popup.
Sources: scripts/background.js:9-25, scripts/content_script.js:100-102, scripts/popup.js:1-4, scripts/popup.js:52-56
Passive Scanning Mode
When "Passive Mode" is enabled (passive: true in storage), the background script initiates a passive scan of web resources.
Sources: scripts/background.js:66-68
Workflow
- Check Storage: The script checks if the
passiveflag is true. - Update Database: If so, it calls
updateDB()fromutils.js. This function downloads a list of vulnerability patterns from/database.csvif it hasn't been loaded already. - Add Listener: Once the database is loaded, a listener is attached to the
chrome.webRequest.onCompletedevent for all script resources. - Scan Scripts: For each completed script request, the
scanfunction is triggered. It downloads the script's content and usespatternMatch()to check it against the regular expression and text patterns from the database. - Log Findings: If a match is found, the vulnerability details (domain, type, file, line number) are added to the
foundset, and the browser action badge is updated.
Flowchart of the passive scanning process.
Sources: scripts/background.js:66-68, scripts/utils.js:122-129, scripts/utils.js:108-111, scripts/utils.js:90-106
The script avoids scanning certain Google ad-related scripts by maintaining a hardcoded blacklist.
Sources: scripts/utils.js:3-8
Active Scanning Mode
When "Active Mode" is enabled (toggle: true in storage), the background script actively modifies HTTP response headers to bypass certain security restrictions like Content Security Policy (CSP) and X-Frame-Options. This facilitates the active testing performed by the content script.
Sources: scripts/background.js:37-39
Header Modification
An onHeadersReceived listener is registered to intercept responses. It performs the following modifications:
- Content-Security-Policy: Replaces the header's value with a permissive policy (
default-src * 'unsafe-inline' 'unsafe-eval' data: blob:;) to allow dynamic script execution and resource loading. - X-Frame-Options: Neutralizes this header by renaming it to
foo, allowing the page to be iframed. - Caching Headers: Renames caching-related headers like
If-None-Matchtofooto help ensure fresh responses are received.
Flowchart of the HTTP header modification logic in Active Mode.
Sources: scripts/background.js:39-59, scripts/utils.js:142-144, scripts/utils.js:146-148, scripts/utils.js:150-152
These modifications are crucial for the injection and execution of test payloads from scripts/content_script.js and scripts/exp.js, which probe for prototype pollution vulnerabilities.