osv.dev icon indicating copy to clipboard operation
osv.dev copied to clipboard

Stored Cross-Site Scripting (XSS) via Unsanitized Upstream Hierarchy Rendering

Open Alearner12 opened this issue 7 months ago • 0 comments

Stored Cross-Site Scripting (XSS) via Unsanitized Upstream Hierarchy Rendering

Description

A stored Cross-Site Scripting (XSS) vulnerability exists in the osv.dev website rendering logic. Vulnerability IDs belonging to the upstream and downstream hierarchies are concatenated into raw HTML strings without being properly escaped. These HTML strings are then rendered using Jinja2's | safe filter, which disables autoescaping and allows for script execution.

Root Cause Analysis

The vulnerability originates in gcp/website/frontend_handlers.py within the construct_hierarchy_string() function:

# frontend_handlers.py (vulnerable code)
def construct_hierarchy_string(target_bug_id: str, hierarchy: ComputedHierarchy,
                               known_ids: set[str]) -> str:
    # ...
    def print_subtree(vuln_id: str) -> None:
        if vuln_id != target_bug_id:
            if vuln_id in known_ids:
                output_lines.append("<li><a href=\"/vulnerability/" + vuln_id + "\">" +
                                    vuln_id + " </a></li>")
            else:
                output_lines.append("<li>" + vuln_id + "</li>")

The vuln_id is appended directly to the HTML string output_lines without any sanitization (e.g., markupsafe.escape()).

This string is then passed to the frontend and rendered in gcp/website/frontend3/src/templates/vulnerability.html using the | safe filter:

<!-- vulnerability.html -->
<dd class="upstream expandible-hierarchy-list">{{ vulnerability.upstream_hierarchy | safe }}</dd>
<dd class="downstream expandible-hierarchy-list">{{ vulnerability.downstream_hierarchy | safe }}</dd>

Because osv.dev automatically ingests records from various external sources (e.g., GitHub Advisory Database), an attacker can submit a malicious payload as an upstream/alias ID in a source repository. When OSV parses this, the payload is persisted to Datastore (upstream_raw / UpstreamGroup), bypassing validation, and is eventually rendered by the frontend, triggering the XSS.

Proof of Concept

To demonstrate the vulnerability locally without needing to ingest a malicious record into Datastore, the underlying logic can be simulated:

  1. Create poc.py to simulate the exact rendering flow used by osv.dev:
from flask import Flask, render_template_string
from collections import namedtuple

ComputedHierarchy = namedtuple('ComputedHierarchy', ['root_nodes', 'graph'])

def construct_hierarchy_string(target_bug_id, hierarchy, known_ids):
    output_lines = []
    root_nodes = hierarchy.root_nodes
    graph = hierarchy.graph

    def print_subtree(vuln_id):
        if vuln_id != target_bug_id:
            if vuln_id in known_ids:
                output_lines.append("<li><a href=\"/vulnerability/" + vuln_id + "\">" +
                                    vuln_id + " </a></li>")
            else:
                output_lines.append("<li>" + vuln_id + "</li>")
        if vuln_id in graph:
            for child in sorted(graph[vuln_id]):
                if child != target_bug_id:
                    output_lines.append("<ul class=\"substream\">")
                    print_subtree(child)
                    output_lines.append("</ul>")

    for root in sorted(root_nodes):
        output_lines.append("<ul class=\"aliases\">")
        print_subtree(root)
        output_lines.append("</ul>")
    return "".join(output_lines)

app = Flask(__name__)

TEMPLATE = """<!DOCTYPE html>
<html><body>
<h2>Upstream (vulnerable - | safe):</h2>
<dd class="upstream expandible-hierarchy-list">{{ html | safe }}</dd>
</body></html>"""

@app.route('/')
def index():
    payload = '"><img src=x onerror=alert(document.domain)>'
    hierarchy = ComputedHierarchy(
        root_nodes={'CVE-2024-1234', payload},
        graph={'CVE-2024-1234': set(), payload: set()})
    html = construct_hierarchy_string('VICTIM-001', hierarchy, {'CVE-2024-1234'})
    return render_template_string(TEMPLATE, html=html)

if __name__ == '__main__':
    app.run(port=5000)
  1. Run the script: python3 poc.py
  2. Access http://127.0.0.1:5000/. The JavaScript payload will execute immediately.

Recommended Fix

Since the template explicitly passes the hierarchy variable to | safe, ensuring that all user-controlled data is properly escaped before concatenation is mandatory. markupsafe.escape() should be applied to vuln_id inside construct_hierarchy_string().

I will be opening a Pull Request shortly with the necessary changes to patch this vulnerability.

Alearner12 avatar Apr 17 '26 04:04 Alearner12