VAITP Dataset

← Back to the dataset

CVE-2021-32862

XSS in nbconvert via user notebooks

  • CVSS 5.4
  • CWE-79 Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')
  • Configuration Issues
  • Remote

The GitHub Security Lab discovered sixteen ways to exploit a cross-site scripting vulnerability in nbconvert. When using nbconvert to generate an HTML version of a user-controllable notebook, it is possible to inject arbitrary HTML which may lead to cross-site scripting (XSS) vulnerabilities if these HTML notebooks are served by a web server (eg: nbviewer).

CVSS base score
5.4
Published
2022-08-18
OWASP
A03 Injection
Orthogonal defect classification
Function
Code defect classification
Incorrect Functionality
Category
Configuration Issues
Subcategory
Cross-Site Scripting (XSS)
Accessibility scope
Remote
Impact
Arbitrary Code Execution
Affected component
nbconvert
Fixed by upgrading
Yes

Solution

Update nbconvert to the latest version.

Vulnerable code sample

import nbformat
from nbconvert import HTMLExporter

# Example notebook with potentially harmful HTML content
notebook_content = """
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>Hello World</div><script>alert('XSS Vulnerability!');</script>"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": "print('Hello World')"
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 4
}
"""

# Load the notebook
nb = nbformat.reads(notebook_content, as_version=4)

# Convert the notebook to HTML without sanitization
html_exporter = HTMLExporter()
(body, resources) = html_exporter.from_notebook_node(nb)

# Output the HTML (this could lead to XSS if served by a web server)
print(body)

Patched code sample

import nbformat
from nbconvert import HTMLExporter
from nbconvert.preprocessors import Preprocessor
from traitlets import Bool

class XSSPreventionPreprocessor(Preprocessor):
    """Preprocessor to sanitize notebook content to prevent XSS."""
    sanitize_output = Bool(True, help="Sanitize output to prevent XSS").tag(config=True)

    def preprocess(self, nb, resources):
        for cell in nb.cells:
            if cell.cell_type == 'code':
                # Sanitize output to remove potentially harmful HTML/JavaScript
                cell.outputs = self.sanitize_outputs(cell.outputs)
        return nb, resources

    def sanitize_outputs(self, outputs):
        sanitized_outputs = []
        for output in outputs:
            if 'data' in output and 'text/html' in output.data:
                # Example sanitization: remove any <script> tags
                html_content = output.data['text/html']
                sanitized_html = self.remove_script_tags(html_content)
                output.data['text/html'] = sanitized_html
            sanitized_outputs.append(output)
        return sanitized_outputs

    @staticmethod
    def remove_script_tags(html):
        # Simple removal of <script> tags (for demonstration purposes)
        return html.replace('<script>', '').replace('</script>', '')

# Example usage
notebook_path = 'example_notebook.ipynb'
with open(notebook_path) as f:
    nb = nbformat.read(f, as_version=4)

html_export

Cite this entry

@misc{vaitp:cve202132862,
  title        = {{XSS in nbconvert via user notebooks}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2022},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2021-32862},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2021-32862/}}
}

Download this entry as JSON Download the whole dataset

Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.

The supreme art of war is to subdue the enemy without fighting.

Sun Tzu – “The Art of War”

:: Shaping the future through research and ingenuity ::

Before you use the VAITP AI models

Please read and accept the following disclaimer:

By choosing "I understand and accept", you acknowledge that you understand and agree to these terms.