The vulnerability exists in the ImageViewer widget in JupyterLab's image viewer extension. The analysis of the provided patches (commits be9303f5bcd5308eaeae953c5a3c903046682c2c and f1beab4a2027af4719d6edc07d52d6cf5a39a432) reveals that the core of the issue lies in the improper handling of object URLs created for displaying images, particularly SVG files.
The ImageViewer.onContentChanged method was identified as the primary vulnerable function. When handling non-base64 encoded images, it would create a Blob from the file content and generate an object URL via URL.createObjectURL(). This URL was then set as the src for an <img> element. The critical flaw was the failure to revoke this object URL after it was no longer needed. An attacker could craft a malicious SVG file containing arbitrary JavaScript. When a user opens this file in JupyterLab and then chooses to open the image in a new browser tab, the browser navigates to the un-revoked object URL. This causes the embedded script to execute within the JupyterLab origin, leading to a cross-site scripting (XSS) attack, which could be escalated to remote code execution (RCE).
The patch addresses this by introducing a mechanism to revoke the object URL as soon as the image is loaded or an error occurs, and also when the widget is disposed. This is achieved by adding event listeners (load and error) to the image element, which call URL.revokeObjectURL(). A new private method _revokeObjectUrl and a property _objectUrl were added to reliably manage the lifecycle of the object URL.
The ImageViewer.dispose method was also modified as part of the fix to ensure that any active object URL is revoked when the image viewer widget is closed, preventing dangling object URLs that could be exploited.