Utility Functions (utils.js)
Relevant source files
The following files were used as context for generating this wiki page:The scripts/utils.js file provides a collection of core utility functions that form the backbone of the PPScan browser extension's passive scanning capabilities. It is loaded into the extension's background context via pages/background.html and is responsible for fetching a vulnerability database, passively scanning network script requests for known patterns, and providing helper functions for string comparison and browser action UI updates.
These utilities are primarily invoked by the main background script, scripts/background.js, to handle tasks such as initializing the pattern database, processing completed web requests, and modifying HTTP response headers when active mode is enabled.
Passive Scanning Engine
The core feature driven by utils.js is the passive scanner, which analyzes JavaScript files requested by web pages for potential prototype pollution vulnerabilities. This process is initiated when "Passive Mode" is enabled.
Sources: scripts/utils.js:103-111, scripts/background.js:68-70
Data Flow
The passive scanning process is triggered by a webRequest.onCompleted event listener. The following diagram illustrates the flow from the event trigger to a potential finding being recorded.
This diagram shows the passive scanning workflow initiated by a completed script request.
Sources: scripts/utils.js:80-101, scripts/utils.js:103-107
Key Functions
| Function | Description |
|---|---|
scan() | The entry point for the scanner, called by the webRequest.onCompleted listener. It receives request details and passes the URL to the check function. |
check() | Validates the request URL, ensuring it's not blacklisted and is either HTTP or HTTPS. It then initiates the download of the script content. |
patternMatch() | Compares the downloaded script content against a database of known vulnerability patterns (both regex and plain text). |
setBadgeCount() | Updates the browser action icon's badge with the number of found vulnerabilities. |
Sources: scripts/utils.js:10-41, scripts/utils.js:80-101, scripts/utils.js:103-107, scripts/utils.js:113-118
Vulnerability Detection
The check function orchestrates the detection process. After a script is downloaded, patternMatch is used to find vulnerabilities. If a match is found, a finding object is created and added to the found set in background.js.
// scripts/utils.js:80-101
const check = ({ requestUri, initiator }) => {
// ... URL validation ...
if (url.protocol == "http:" || url.protocol == "https:") {
download(url).then(res => {
const [result, match] = patternMatch(res, database);
result.forEach((name, i) => {
// ... calculate line and column ...
const lineCol = `${line}:${column}`;
if (blacklist.indexOf(requestUri + ':' + lineCol) != -1) {
return;
}
found.add(JSON.stringify({ domain: initiator, type: name, file: requestUri, lineCol }))
setBadgeCount(found.size);
});
})
}
};
The found set is managed in background.js and its size is used to update the extension's badge count.
Sources: scripts/utils.js:80-101, scripts/background.js:4-5, scripts/background.js:9-12
Database Management
The scanner relies on a database of vulnerability patterns. utils.js contains functions to download and parse this database.
Database Initialization
The updateDB function is responsible for fetching and preparing the vulnerability database. It is called from background.js if passive mode is enabled. Once the database is successfully downloaded and parsed, it sets up the webRequest.onCompleted listener to start scanning.
This diagram shows the sequence for initializing the vulnerability database and activating the passive scanner.
Sources: scripts/utils.js:109-111, scripts/background.js:68-70
Database Structure
The database is a CSV file fetched from databaseUrl. The downloadDB function parses each line into an object with the following structure.
| Field | Type | Description |
|---|---|---|
name | String | The name of the vulnerability/library. |
type | String | The type of pattern (regex or text). |
chunk | String | The actual pattern string to match. |
Sources: scripts/utils.js:49-62
// scripts/utils.js:53-60
let data = line.split('|');
let name, type, chunk;
name = data[0].trim();
type = data[1].trim();
chunk = data.slice(2).join('|').trim();
return { name, type, chunk };
This snippet shows how each line of the database file is parsed into a structured object.
Helper Functions
utils.js also exports several helper functions used throughout the background scripts for tasks like string comparison and header identification.
Header Checkers
These functions are used in background.js to identify specific HTTP headers for modification when "Active Mode" is enabled. They perform a case-insensitive comparison.
isCSPHeader({ name }): Checks if a header isContent-Security-Policy.isXFrameEnabled({ name }): Checks if a header isX-Frame-Options.isCached({ name }): Checks if a header isIf-None-Match.
Sources: scripts/utils.js:124-135, scripts/background.js:54-60
Blacklist
A hardcoded blacklist array prevents scanning of certain scripts, likely to avoid false positives or performance issues with common analytics and ad scripts. The check function consults this list before processing a script.
Sources: scripts/utils.js:3-8, scripts/utils.js:85-87, scripts/utils.js:95-97
Conclusion
utils.js is a critical component of the PPScan extension, providing the complete logic for the passive scanning feature. It handles fetching and parsing vulnerability definitions, matching them against network resources, and reporting findings. Its modular helper functions are also leveraged by other parts of the extension, such as the active-mode header modification logic in background.js, making it a central utility script for the project.