# GHSA-5cp4-xmrw-59wf: XSS via JQLite DOM manipulation functions in AngularJS

> Attackers can trigger XSS in AngularJS applications by exploiting JQLite DOM manipulation functions to bypass HTML sanitization.

- **Severity:** Not scored
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Over the internet
- **Victim action needed:** A user has to click or open something
- **Fix available:** Yes
- **Published:** 5 Aug 2020

## What is GHSA-5cp4-xmrw-59wf?

GHSA-5cp4-xmrw-59wf is a cross-site scripting vulnerability in angular, published on 5 Aug 2020. An attacker can exploit it over the internet without logging in, and only if a user opens a file or follows a link.

### Official description

### Summary
XSS may be triggered in AngularJS applications that sanitize user-controlled HTML snippets before passing them to `JQLite` methods like `JQLite.prepend`, `JQLite.after`, `JQLite.append`, `JQLite.replaceWith`, `JQLite.append`, `new JQLite` and `angular.element`.

### Description

JQLite (DOM manipulation library that's part of AngularJS) manipulates input HTML before inserting it to the DOM in `jqLiteBuildFragment`.

One of the modifications performed [expands an XHTML self-closing tag](https://github.com/angular/angular.js/blob/418355f1cf9a9a9827ae81d257966e6acfb5623a/src/jqLite.js#L218).

If `jqLiteBuildFragment` is called (e.g. via `new JQLite(aString)`) with user-controlled HTML string that was sanitized (e.g. with [DOMPurify](https://github.com/cure53/DOMPurify)), the transformation done by JQLite may modify some forms of an inert, sanitized payload into a payload containing JavaScript - and trigger an XSS when the payload is inserted into DOM.

This is similar to a bug in jQuery `htmlPrefilter` function that was [fixed in 3.5.0](https://blog.jquery.com/2020/04/10/jquery-3-5-0-released/).

### Proof of concept

```javascript
const inertPayload = `<div><style><style/><img src=x onerror="alert(1337)"/>` 
```
Note that the style element is not closed and `<img` would be a text node inside the style if inserted into the DOM as-is.
As such, some HTML sanitizers would leave the `<img` as is without processing it and stripping the `onerror` attribute.

```javascript
angular.element(document).append(inertPayload);
```
This will alert, as `<style/>` will be replaced with `<style></style>` before adding it to the DOM, closing the style element early and reactivating `img`.

### Patches
The issue is patched in `JQLite` bundled with angular 1.8.0. AngularJS users using JQuery should upgrade JQuery to 3.5.0, as a similar vulnerability [affects jQuery <3.5.0](https://github.com/jquery/jquery/security/advisories/GHSA-gxr4-xjj5-5px2).

### Workarounds
Changing sanitizer configuration not to allow certain tag grouping (e.g. `<option><style></option>`) or inline style elements may stop certain exploitation vectors, but it's uncertain if all possible exploitation vectors would be covered. Upgrade of AngularJS to 1.8.0 is recommended.

### References
https://github.com/advisories/GHSA-mhp6-pxh8-r675
https://github.com/jquery/jquery/security/advisories/GHSA-gxr4-xjj5-5px2
https://github.com/jquery/jquery/security/advisories/GHSA-jpcq-cgw6-v4j6
https://blog.jquery.com/2020/04/10/jquery-3-5-0-released/
https://snyk.io/vuln/SNYK-JS-ANGULAR-570058

## Which versions are affected by GHSA-5cp4-xmrw-59wf?

2 open source packages ship an affected version, including angular.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `angular` | npm | All versions | 1.8.0 |
| `angular` | npm | All versions | 1.8.0 |

## How do I fix GHSA-5cp4-xmrw-59wf?

Upgrade angular to 1.8.0 or later.

## Is GHSA-5cp4-xmrw-59wf being exploited?

There is no known exploitation of GHSA-5cp4-xmrw-59wf in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-5cp4-xmrw-59wf and fix it if it is affected.

What it is: GHSA-5cp4-xmrw-59wf is a cross-site scripting vulnerability in angular, published on 5 Aug 2020. An attacker can exploit it over the internet without logging in, and only if a user opens a file or follows a link.
Exploitation: There is no known exploitation of GHSA-5cp4-xmrw-59wf in the wild and no public exploit code.

Affected packages and fixed versions:
- angular (npm): affected all versions; fixed in 1.8.0
- angular (npm): affected all versions; fixed in 1.8.0

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/GHSA-5cp4-xmrw-59wf/ (Markdown: https://insights.sbomify.com/GHSA-5cp4-xmrw-59wf/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-5cp4-xmrw-59wf

---
From sbomify Insights: https://insights.sbomify.com/GHSA-5cp4-xmrw-59wf/
Summaries are generated and may contain mistakes; the official description above is the record.
