State Management
Relevant source files
The following files were used as context for generating this wiki page:State management in PPScan is divided into two primary categories: persistent configuration state and transient session state. Persistent state, which includes user-configurable settings like scan modes, is managed using the chrome.storage.sync API, ensuring settings are preserved across browser sessions. Transient state, which primarily involves the in-memory log of discovered vulnerabilities, is managed within the background script and exists only for the duration of a browser session or until manually cleared.
This system allows the user to configure the extension's behavior via the popup UI, with these settings being read by various components like the content script and background script to enable or disable specific functionalities. The session state provides a real-time log of findings, which is communicated to the popup for display.
Core Architecture
The state management architecture relies on the interplay between the popup UI, the background script, content scripts, and the chrome.storage API. The background script acts as the central hub for managing the list of found vulnerabilities and setting initial configuration, while the popup serves as the user's interface for modifying the persistent configuration.
The following diagram illustrates the high-level interaction between these components.
This diagram shows the main components and their interactions for state management. The popup modifies persistent storage, which is read by background and content scripts. The background script manages the session state (the found set).
Sources: scripts/background.js, scripts/popup.js, scripts/content_script.js, manifest.json
Persistent Configuration State
Configuration state is persisted using chrome.storage.sync, making it available across the extension's components and between browser sessions. This state controls the activation of the "Active", "Passive", and "Window" scanning modes.
Sources: scripts/background.js:23-32, scripts/popup.js:6-57
Persisted Settings
The following settings are stored in chrome.storage.sync:
| Key | Type | Description | Default Value |
|---|---|---|---|
toggle | Boolean | Enables or disables "Active Mode", which injects scripts to test for prototype pollution vulnerabilities. | false |
buster | Boolean | Enables or disables "Window Mode" for active scanning, which is useful for bypassing frame-busting code. | false |
passive | Boolean | Enables or disables "Passive Mode", which scans loaded scripts for known vulnerable patterns. | true |
Sources: scripts/background.js:23-32, scripts/popup.js:8-10, 20-22, 40-42
State Initialization
Upon installation, the background script sets the default values for all configuration keys. This ensures the extension starts in a known, predictable state.
// scripts/background.js:22-32
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.set({ toggle: false }, () => {
console.log('[*] Toggle: disabled');
});
chrome.storage.sync.set({ buster: false }, () => {
console.log('[*] Buster: disabled');
});
chrome.storage.sync.set({ passive: true }, () => {
console.log('[*] Passive: enabled');
});
});
Sources: scripts/background.js:22-32
State Modification and Consumption
The user modifies the state via buttons in the popup UI (pages/popup.html). The scripts/popup.js script contains onclick handlers that use chrome.storage.sync.set to update the configuration. Other scripts, like content_script.js and background.js, use chrome.storage.sync.get to read the current state and adjust their behavior accordingly. For example, the active scanning logic in content_script.js is entirely conditional on the toggle key being true.
The following sequence diagram shows how a user enables "Active Mode".
This diagram illustrates the flow of updating the toggle state from the popup and how the content script consumes this state change.
Sources: scripts/popup.js:11-25, scripts/content_script.js:29-31
Transient Session State
Transient state consists of data that is relevant only to the current browsing session. The primary piece of session state is the found set, which logs all discovered vulnerabilities.
Vulnerability Tracking (found set)
The found set is an in-memory Set object declared in background.js. It stores unique vulnerability reports as stringified JSON objects. Using a Set prevents duplicate entries.
Sources: scripts/background.js:4
Data Structure
Each entry added to the found set is a JSON string with the following structure:
{
"domain": "https://example.com",
"type": "Active Mode",
"file": "https://example.com/vulnerable.js",
"lineCol": "10:5"
}
- domain: The origin of the page where the vulnerability was found.
- type: The method of discovery (e.g., 'Active Mode' or a specific library name from passive scanning).
- file: The full URL of the file or page containing the vulnerability.
- lineCol: The line and column number, if available.
Sources: scripts/background.js:8-9, scripts/utils.js:111-114
State Updates and Communication
The found set is populated from two sources:
- Active Scanning: The
content_script.jssends a message viachrome.runtime.sendMessagewhen a vulnerability is confirmed. The listener inbackground.jsreceives this message and adds an entry to thefoundset. - Passive Scanning: The
checkfunction inutils.js(called by thescanlistener inbackground.js) adds an entry when a pattern is matched in a downloaded script.
The popup UI communicates with the background script via a long-lived connection (chrome.extension.connect) to retrieve and clear the found list.
The diagram below shows the flow for displaying and clearing found vulnerabilities.
This diagram shows how the popup requests the vulnerability list from the background script and how it initiates the clearing of that list.
Sources: scripts/background.js:12-20, scripts/popup.js:1-4, 61-69
Badge State
The extension's browser action badge provides a real-time count of discovered vulnerabilities. The setBadgeCount function, defined in utils.js, is called whenever the size of the found set changes. It updates the badge text with the number of items and sets the background color to red if any vulnerabilities have been found.
Sources: scripts/utils.js:132-137, scripts/background.js:6, 10, 17