Popup Logic (popup.js)
Relevant source files
The following files were used as context for generating this wiki page:The popup.js script provides the core logic for the PPScan browser extension's user interface, which is defined in popup.html. This UI serves as the central control panel for the user, allowing them to manage scanning modes, manually trigger prototype pollution scans, and view discovered vulnerabilities. The popup acts as an orchestrator, interacting with the background script (background.js) for data persistence and state management, and with the content script (content_script.js) to inject payloads into the active web page.
The popup's functionality is self-contained but relies on communication with other extension components to perform its tasks. It uses chrome.storage.sync to manage user settings and chrome.extension.connect to maintain a persistent communication channel with the background script for logging and retrieving scan results.
UI Components and Initialization
The user interface is defined in popup.html and is controlled by popup.js. Upon loading, the window.onload event triggers the main logger function, which initializes the entire popup state.
Sources: scripts/popup.js:90, pages/popup.html
Initialization Flow
The logger function performs the following actions on startup:
- Establishes a long-lived connection to the background script, identifying itself with the name
"logger". - Retrieves the current state for "Active Mode", "Window Mode", and "Passive Mode" from
chrome.storage.sync. - Updates the text and visibility of the corresponding control buttons based on the retrieved state.
- Assigns
onclickevent handlers to all interactive elements (mode toggles, scan buttons, clear link). - Sends an initial message (
'send found urls') to the background script to request the list of currently discovered vulnerabilities. - Sets up a listener (
port.onMessage) to receive scan results from the background script and render them.
Sources: scripts/popup.js:1-4, scripts/popup.js:7-14, scripts/popup.js:75-79
The following diagram shows the initialization sequence when the popup is opened.
Sources: scripts/popup.js:1-14, scripts/popup.js:73-79
UI Controls
The main interactive elements are defined in popup.html and managed by popup.js.
| Element ID | Description |
|---|---|
toggle | Button to enable or disable "Active Mode" scanning. |
buster | Button to enable or disable "Window Mode". Only visible when Active Mode is on. |
passive | Button to enable or disable "Passive Mode" scanning. |
brute | Button to trigger a gadget brute-force scan using URL search parameters. |
brutehash | Button to trigger a gadget brute-force scan using the URL hash. |
clear | Link to clear all stored scan results. |
found-list | Table that displays the list of found vulnerabilities. |
Sources: pages/popup.html:101-122, scripts/popup.js:15-73
Mode Toggling
The popup allows the user to toggle three distinct modes. Each mode's state is stored in chrome.storage.sync and controls a different aspect of the extension's behavior.
Sources: scripts/popup.js:15-58, scripts/background.js:25-36
Mode Toggling Logic
The flow for toggling any mode is consistent:
- The user clicks a mode button.
- The
onclickhandler reads the current boolean value fromchrome.storage.sync. - The value is inverted.
- The new value is written back to
chrome.storage.sync. - The button's text is updated to reflect the new state (e.g., "Enable..." vs. "Disable...").
Sources: scripts/popup.js:15-58
Storage Keys and Modes
| Button ID | Storage Key | Description | Default |
|---|---|---|---|
toggle | toggle | Controls "Active Mode", which injects test payloads on page loads and enables header modification. | false |
buster | buster | Controls "Window Mode" for active scans, used to bypass frame-busting techniques. | false |
passive | passive | Controls "Passive Mode", which scans loaded scripts for vulnerable patterns. | true |
Sources: scripts/popup.js:15-58, scripts/background.js:25-36
When "Active Mode" is disabled, the "Window Mode" button is hidden from the UI.
// scripts/popup.js:21-23
document.getElementById("toggle").value = "Enable Active Mode";
document.getElementById("buster").style.display = "none";
Sources: scripts/popup.js:21-23
Manual Scan Triggers
The "Brute Gadgets" feature allows the user to manually initiate a scan on the current page to find exploitable gadgets. This is done by dispatching custom events that are captured by the content script.
Sources: scripts/popup.js:60-69, scripts/content_script.js:1-23
Scan Trigger Flow
- The user clicks either the "Search" (
brute) or "Hash" (brutehash) button in the popup. - The corresponding
onclickhandler executeschrome.tabs.executeScripton the active tab. - The executed script creates and dispatches a
CustomEvent:TriggerBrutefor the "Search" button.TriggerBruteHashfor the "Hash" button.
- The
content_script.js, already running on the page, listens for these events. - Upon catching an event, the content script creates a hidden
iframepointing topages/iframe.html. - It then sends a
postMessageto this iframe, containing the page's URL and the scan type (searchorhash). - The script within the iframe (
exp.js) receives this message and begins injecting a series of gadget payloads into the target page via more iframes.
The following diagram illustrates this process.
The code used to trigger the scan is a simple one-liner injected into the active tab:
// scripts/popup.js:62-64
chrome.tabs.executeScript(null, {
code: `document.dispatchEvent(new CustomEvent('TriggerBrute'));`
});
Sources: scripts/popup.js:62-64
Displaying and Clearing Scan Results
The popup is responsible for displaying all vulnerabilities found by both active and passive scans. This data is stored in the background script and retrieved upon request.
Fetching and Displaying Results
The listFound function handles the rendering of results. It is called when the popup opens and receives data from the background script.
- The function first checks if the
foundarray has any items. If not, it hides the results table and label. - It iterates through the
foundarray. Each element is a JSON string. - For each element, it parses the JSON into an object with keys:
domain,type,file, andlineCol. - A new table row (
<tr>) is created with the parsed data and appended to thefound-listtable body.
// scripts/popup.js:82-88
function listFound(found) {
foundLabel.style.display = foundList.style.display = found.length > 0 ? 'block' : 'none';
found.forEach((str) => {
const line = JSON.parse(str)
const tr = document.createElement("tr");
tr.innerHTML = `<td><a target="_blank" href="${line['domain']}">${new URL(line['domain']).hostname}</a></td><td>${line['type']}</td><td><a target="_blank" href="${line['file']}">${line['file']}:${line['lineCol']}</a></td>`;
foundList.appendChild(tr);
});
}
Sources: scripts/popup.js:82-88
Clearing Results
The user can clear all stored results by clicking the "Clear" link.
- The
onclickhandler for the#clearelement sends a'clearLog'message to the background script via the established port. - The background script's listener for this port receives the message and calls
found.clear()to empty theSetof stored vulnerabilities. - The background script then sends back the now-empty list, which causes the popup UI to update and hide the results table.
Sources: scripts/popup.js:71-73, scripts/background.js:18-21
The communication flow for clearing logs is shown below.