|
| 1 | +VulnTotal Browser Extension: Cross-validating vulnerability right within the browser |
| 2 | +==================================================================================== |
| 3 | + |
| 4 | +Organization - `AboutCode <https://www.aboutcode.org>`_ |
| 5 | +----------------------------------------------------------- |
| 6 | +| **Michael Ehab Mikhail** |
| 7 | +| GitHub: `michaelehab <https://github.com/michaelehab>`_ |
| 8 | +| LinkedIn: `@michaelehab16 <https://www.linkedin.com/in/michaelehab16/>`_ |
| 9 | +| Project: `VulnTotal Browser Extension <https://github.com/aboutcode-org/vulntotal-extension>`_ |
| 10 | +| Official GSoC project page: `Project Link <https://summerofcode.withgoogle.com/programs/2024/projects/26FZNTGx>`_ |
| 11 | +| GSoC Proposal: `Proposal Link <https://docs.google.com/document/d/1K7oGBlfHxbrxah3TJW1PcUGgaLMR6q0ctK7_Y10hpVY/edit?usp=sharing>`_ |
| 12 | +
|
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +This project aims to simplify the process of vulnerability analysis by |
| 17 | +developing a browser extension for Chrome-based browsers (Google Chrome, |
| 18 | +Firefox, Arc, etc.). The extension leverages the power of `VulnTotal |
| 19 | +<https://aboutcode.readthedocs.io/en/latest/archive/gsoc/reports/2022/vulnerablecode_vulntotal_keshav.html>`_, |
| 20 | +a Python-based tool that queries multiple vulnerability data sources, such |
| 21 | +as OSV and Snyk, to provide comprehensive security assessments of software |
| 22 | +packages. The key innovation in this project is the use of Pyodide, a port |
| 23 | +of CPython to WebAssembly/Emscripten, which enables Python code to run |
| 24 | +natively in the browser. |
| 25 | + |
| 26 | +By integrating Pyodide with VulnTotal, the extension can perform |
| 27 | +vulnerability analysis directly within the browser environment, |
| 28 | +eliminating the need for server-side components or external applications. |
| 29 | +The extension provides a user-friendly interface that displays the results |
| 30 | +of vulnerability analyses in a clear and interpretable table format. The |
| 31 | +table allows users to quickly assess the security status of the software |
| 32 | +packages they are examining. |
| 33 | + |
| 34 | +This project not only enhances the functionality and reach of VulnTotal but |
| 35 | +also provides a valuable tool for developers and security professionals who |
| 36 | +seek to ensure the safety of their software directly from their browsers. |
| 37 | +By simplifying and streamlining the vulnerability-checking process, this |
| 38 | +extension contributes to safer and more secure software development practices. |
| 39 | + |
| 40 | +Extension Preview |
| 41 | +----------------- |
| 42 | + |
| 43 | +.. figure:: https://github.com/user-attachments/assets/07966663-9f89-4b80-9db3-0840e246d2c5 |
| 44 | + :alt: Extension Preview |
| 45 | + :align: center |
| 46 | + :width: 70% |
| 47 | + |
| 48 | + VulnTotal Extension takes PURL and displays vulnerability data from |
| 49 | + various data sources. By default, vulnerability data is grouped by CVE. |
| 50 | + |
| 51 | +.. note:: |
| 52 | + A PURL is a URL string used to identify and locate a software package |
| 53 | + in a mostly universal and uniform way across programming languages, |
| 54 | + package managers, packaging conventions, tools, APIs, and databases. |
| 55 | + `more on PURL <https://github.com/package-url>`_ |
| 56 | + |
| 57 | +Project Design and Architecture |
| 58 | +------------------------------- |
| 59 | + |
| 60 | +The design of this project revolves around the integration of VulnTotal |
| 61 | +with a browser environment, enabling seamless vulnerability analysis |
| 62 | +without the need for server-side processing. This is achieved by leveraging |
| 63 | +Pyodide, which runs Python code directly in the browser through WebAssembly. |
| 64 | +Below, we discuss the core components of the project and how they interact |
| 65 | +to deliver a functional and user-friendly extension. |
| 66 | + |
| 67 | +Pyodide Integration |
| 68 | +^^^^^^^^^^^^^^^^^^^ |
| 69 | + |
| 70 | +Pyodide is a critical component of this project, allowing the VulnTotal |
| 71 | +Python library to be executed within the browser. Pyodide converts CPython |
| 72 | +to WebAssembly, enabling Python code to run natively in a web environment. |
| 73 | +This setup eliminates the need for external servers or backend |
| 74 | +infrastructure, making the extension self-contained and easy to deploy. |
| 75 | + |
| 76 | +.. figure:: https://github.com/user-attachments/assets/2bf61204-e26b-4322-8403-f126b3d44748 |
| 77 | + :alt: Pyodide integration within the browser |
| 78 | + :align: center |
| 79 | + :width: 70% |
| 80 | + |
| 81 | + In the context of this extension, Pyodide serves as the bridge between |
| 82 | + the browser's JavaScript environment and the Python-based VulnTotal tool. |
| 83 | + The extension uses Pyodide to load the VulnTotal code, perform |
| 84 | + vulnerability checks, and return the results to the user interface, |
| 85 | + where they are displayed in a structured format. |
| 86 | + |
| 87 | +Implementation |
| 88 | +-------------- |
| 89 | + |
| 90 | +The implementation of the browser extension involved several key steps, |
| 91 | +each crucial to ensuring the extension's functionality and integration |
| 92 | +with VulnTotal. The process began with designing the frontend and progressed |
| 93 | +through setting up Pyodide, creating core classes, and integrating all |
| 94 | +components. Below is a detailed account of the implementation process. |
| 95 | + |
| 96 | +Frontend Design |
| 97 | +^^^^^^^^^^^^^^^ |
| 98 | + |
| 99 | +The initial phase of development focused on designing the frontend of the |
| 100 | +extension. To maintain a consistent user experience, I adopted a design |
| 101 | +approach similar to that used in VulnerableCode. This design choice aimed |
| 102 | +to provide users with a familiar and intuitive interface for vulnerability |
| 103 | +analysis. |
| 104 | + |
| 105 | +I created a full demo using JavaScript to simulate how the extension should |
| 106 | +function. This demo utilized a JSON response from the VulnTotal Python |
| 107 | +package, allowing me to test and validate the user interface and interaction |
| 108 | +flow before integrating with Pyodide. |
| 109 | + |
| 110 | +Pyodide Integration |
| 111 | +^^^^^^^^^^^^^^^^^^^ |
| 112 | + |
| 113 | +The most challenging part of the project was integrating the Pyodide Python |
| 114 | +package into the browser environment. Pyodide allows Python code to run |
| 115 | +within the browser using WebAssembly. To achieve this, I performed the |
| 116 | +following steps: |
| 117 | + |
| 118 | +* Setting Up Pyodide: I configured Pyodide, ensuring that the environment |
| 119 | + was properly set up to run Python code. This included loading the |
| 120 | + necessary Python packages into the environment. |
| 121 | +* Using Python Code in JavaScript: I wrote the VulnTotalWorker.js class to |
| 122 | + manage the interaction between the Python environment and the browser. |
| 123 | + This class handles various tasks: |
| 124 | + |
| 125 | + * Environment Initialization: It ensures that the Pyodide environment |
| 126 | + is correctly initialized and ready for executing Python code. |
| 127 | + * Communication with UI: It updates the user interface with loading |
| 128 | + progress and other relevant information. |
| 129 | + * API Key Management: It manages API keys required for VulnTotal |
| 130 | + operations within the Python environment. |
| 131 | + * Request Handling: It processes scan requests, which include the |
| 132 | + Package URL (PURL) and the enabled or disabled data sources, and |
| 133 | + returns the analysis results. |
| 134 | + |
| 135 | +Web Worker Integration |
| 136 | +^^^^^^^^^^^^^^^^^^^^^^ |
| 137 | + |
| 138 | +The VulnTotalWorker class operates within a web worker to handle the actual |
| 139 | +requests for vulnerability analysis. The web worker allows the extension to |
| 140 | +perform background tasks without blocking the main thread, ensuring a |
| 141 | +smooth user experience. It processes requests and communicates with the |
| 142 | +VulnTotalWorker class to fetch and return results. |
| 143 | + |
| 144 | +Background Script |
| 145 | +^^^^^^^^^^^^^^^^^ |
| 146 | + |
| 147 | +The background script is responsible for managing user settings and API keys. |
| 148 | +It stores these preferences and ensures that they are accessible to other |
| 149 | +components of the extension. This worker plays a crucial role in maintaining |
| 150 | +user-specific configurations and providing a seamless experience. |
| 151 | + |
| 152 | +Content Script |
| 153 | +^^^^^^^^^^^^^^ |
| 154 | + |
| 155 | +The content script acts as the glue between different components of the |
| 156 | +extension. It facilitates communication between the background script, web |
| 157 | +worker, and user interface. The content script ensures that all components |
| 158 | +work together effectively, coordinating the flow of data and requests to |
| 159 | +deliver the desired functionality. |
| 160 | + |
| 161 | +Build Script |
| 162 | +^^^^^^^^^^^^ |
| 163 | + |
| 164 | +The final part was adding a script to build the extension. This script uses |
| 165 | +an installed chrome-based browser to bundle all the necessary files, |
| 166 | +including the Pyodide core package and the VulnTotal Python code, into a |
| 167 | +single extension package. This package can then be installed in the browser |
| 168 | +to enable vulnerability analysis directly within the browser environment. |
| 169 | + |
| 170 | +.. figure:: https://github.com/user-attachments/assets/8f13128e-a32c-46f9-af47-4ec18c7e84d4 |
| 171 | + :alt: Extension Architecture |
| 172 | + :align: center |
| 173 | + :width: 70% |
| 174 | + |
| 175 | + The architecture of the extension consists of multiple components that |
| 176 | + work together to provide vulnerability analysis within the browser. The |
| 177 | + frontend interacts with the content script, which coordinates requests |
| 178 | + and data flow between the background script, web worker, and Pyodide |
| 179 | + environment. This setup ensures a seamless and efficient user experience. |
| 180 | + |
| 181 | +Challenges and Solutions |
| 182 | +------------------------ |
| 183 | + |
| 184 | +1. WebAssembly and Project Scale |
| 185 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 186 | + |
| 187 | +The first significant challenge was utilizing WebAssembly (Wasm) for a |
| 188 | +project of this scale. While most tutorials and available content |
| 189 | +demonstrate running simple scripts in the browser, this project involved a |
| 190 | +more complex scenario. Integrating the VulnTotal tool, which has multiple |
| 191 | +dependencies and a larger codebase, required careful optimization and |
| 192 | +adaptation to ensure it ran smoothly within the browser environment. |
| 193 | +Overcoming this challenge involved extensive testing and tweaking of the |
| 194 | +Pyodide setup to ensure compatibility and performance. |
| 195 | + |
| 196 | +2. Browser Context Incompatibilities |
| 197 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 198 | + |
| 199 | +Running Python scripts in a browser context introduces restrictions that do |
| 200 | +not exist when running the same scripts in a standard Python environment. |
| 201 | +One of the key challenges was handling HTTP requests. Certain requests that |
| 202 | +work seamlessly in a Python script faced issues in the browser due to the |
| 203 | +lack of request origin, which is a crucial aspect of web security. |
| 204 | + |
| 205 | +To address this, running requests directly in the content script was not |
| 206 | +feasible. The solution was to leverage Web Workers, which allowed for more |
| 207 | +flexible request handling within the browser. This approach is recommended |
| 208 | +by Google and provides a way to bypass some of the restrictions imposed by |
| 209 | +the browser environment. |
| 210 | + |
| 211 | +3. GitLab Data Source Integration |
| 212 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 213 | + |
| 214 | +Another challenge was integrating the GitLab data source into the extension. |
| 215 | +The original VulnTotal tool had specific configurations and data retrieval |
| 216 | +methods for GitLab that required adaptation for the browser environment. |
| 217 | +Ensuring proper data retrieval and presentation from GitLab required |
| 218 | +modifying the existing VulnTotal code and handling data fetching and |
| 219 | +parsing within the browser constraints. |
| 220 | + |
| 221 | +4. Extension Size Optimization |
| 222 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 223 | + |
| 224 | +The size of the Pyodide release posed a practical challenge. The compressed |
| 225 | +release size for Pyodide version 0.26.1 was approximately 281MB, which is |
| 226 | +impractical to include within the extension files. Additionally, relying on |
| 227 | +a CDN for such a large package would be challenging due to browser |
| 228 | +limitations and potential issues with internet consumption. |
| 229 | + |
| 230 | +To solve this, I opted to use only the Pyodide core package, which has a |
| 231 | +much smaller compressed size of 5.31MB. I then bundled only the necessary |
| 232 | +wheels for the required packages, reducing the overall extension size to |
| 233 | +about 20MB. This made the extension more efficient and practical for users |
| 234 | +to install and use. |
| 235 | + |
| 236 | +5. Patching the Requests Module |
| 237 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 238 | + |
| 239 | +Another challenge involved patching the Python requests module to work |
| 240 | +seamlessly within the browser environment. Pyodide provides a patch that |
| 241 | +allows requests to use XHR for non-streaming requests and the Fetch API for |
| 242 | +streaming requests. However, this patch only worked correctly for the |
| 243 | +initial VulnTotal request. To ensure consistent and correct results, I |
| 244 | +implemented a mechanism to apply this patch every time the Python code was |
| 245 | +executed. This ensured that all requests were handled appropriately, |
| 246 | +maintaining the functionality of the extension. |
| 247 | + |
| 248 | +Linked Pull Requests |
| 249 | +-------------------- |
| 250 | + |
| 251 | +.. list-table:: |
| 252 | + :widths: 10 40 20 30 |
| 253 | + :header-rows: 1 |
| 254 | + |
| 255 | + * - Sr. no |
| 256 | + - Name |
| 257 | + - Link |
| 258 | + - Status |
| 259 | + * - 1 |
| 260 | + - Frontend Demo |
| 261 | + - `aboutcode-org/vulntotal-extension#5 <https://github.com/aboutcode-org/vulntotal-extension/pull/5>`_ |
| 262 | + - Merged |
| 263 | + * - 2 |
| 264 | + - Gitlab Change |
| 265 | + - `aboutcode-org/vulnerablecode#1524 <https://github.com/aboutcode-org/vulnerablecode/pull/1524>`_ |
| 266 | + - Merged |
| 267 | + * - 3 |
| 268 | + - Backend Integration |
| 269 | + - `aboutcode-org/vulntotal-extension#6 <https://github.com/aboutcode-org/vulntotal-extension/pull/6>`_ |
| 270 | + - Merged |
| 271 | + * - 4 |
| 272 | + - Build Scripts and Readme |
| 273 | + - `aboutcode-org/vulntotal-extension#9 <https://github.com/aboutcode-org/vulntotal-extension/pull/9>`_ |
| 274 | + - Merged |
| 275 | + |
| 276 | +Closing Thoughts |
| 277 | +------------------- |
| 278 | + |
| 279 | +I really enjoyed working on such a challenging project. I loved exploring |
| 280 | +the new technologies I used, like WebAssembly, and getting it running in a |
| 281 | +complex project with packages and multiple files like VulnTotal. |
| 282 | + |
| 283 | +I appreciated the weekly status calls and the feedback I received from my |
| 284 | +mentors and the amazing team. They were really helpful and supportive. |
| 285 | +- `Philippe Ombredanne <https://github.com/pombredanne>`_ |
| 286 | +- `Ayan Sinha Mahapatra <https://github.com/AyanSinhaMahapatra>`_ |
| 287 | +- `Ziad Hany <https://github.com/ziadhany>`_ |
| 288 | +- `Keshav Priyadarshi <https://github.com/keshav-space>`_ |
| 289 | +- `Omkar Phansopkar <https://github.com/OmkarPh>`_ |
0 commit comments