Skip to content

Leaf-kit html escaping does not work on characters that are part of extended grapheme cluster

Moderate severity GitHub Reviewed Published Feb 18, 2026 in vapor/leaf-kit • Updated Feb 23, 2026

Package

swift leaf-kit (Swift)

Affected versions

< 1.4.1

Patched versions

1.4.1

Description

Summary

htmlEscaped in leaf-kit will only escape html special characters if the extended grapheme clusters match, which allows bypassing escaping by using an extended grapheme cluster containing both the special html character and some additional characters. In the case of html attributes, this can lead to XSS if there is a leaf variable in the attribute that is user controlled.

Details

Relevant code:
https://github.com/vapor/leaf-kit/blob/main/Sources/LeafKit/String%2BHTMLEscape.swift#L14

Strings in Swift are based on extended grapheme clusters. HTML on the other hand is based on unicode characters.

For example if you have the sequence "́ (U+0022 Quotation mark followed by U+0301 Combining Acute Accent). To HTML this is just a quote mark followed by some other random character. To swift this is one extended grapheme cluster that does not equal a quotation mark by itself which is a different extended grapheme cluster.

Thus "\"́".replacingOccurrences(of: "\"", with: "&quot;") does not replace the quote mark. This allows you to break out of html attributes.

I believe replacingOccurences takes an optional third parameter that allows you to specify options to make it work on UTF-8 characters instead of grapheme clusters, which would be a good fix for this issue.

I see depending on version, leafkit might use replacing instead of replacingOccurences. I don't know swift that well and couldn't find docs on what replacing does, so I don't know if both versions of the function are affected. The version of swift i was testing on I believe was using replacingOccurences

It seems like replacingOccurences will skip past prefix characters of extended grapheme clusters, which is what would be needed in order to meaningfully bypass esaping of <. Thus i think this is mostly limited to attributes and not general text.

PoC

An example vapor application that is vulnerable might look like

routes.swift

import Vapor

struct Hello: Content {
    var msg: String?
}

func routes(_ app: Application) throws {
    app.post { req throws in
	let Hello = try req.content.decode(Hello.self)
        return req.view.render("hello", [
            "msg": Hello.msg ?? "Hello World!"
        ])
    }
}

With a hello.leaf that looks like

<div title="#(msg)">Hover to see message</div>

And then you POST something like msg=%22%cc%81=1%20autofocus%20tabindex=0%20onfocus=alert(1)%20

Impact

If a website uses leaf to escape an attribute value based on user input, the attacker may be able to insert a malicious attribute. If a site is not using a secure CSP policy, then this can be used to execute malicious javascript (XSS). Impact is context dependent if a site is using a secure CSP policy.

References

@ptoffy ptoffy published to vapor/leaf-kit Feb 18, 2026
Published to the GitHub Advisory Database Feb 19, 2026
Reviewed Feb 19, 2026
Published by the National Vulnerability Database Feb 20, 2026
Last updated Feb 23, 2026

Severity

Moderate

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v3 base metrics

Attack vector
Network
Attack complexity
Low
Privileges required
None
User interaction
Required
Scope
Changed
Confidentiality
Low
Integrity
Low
Availability
None

CVSS v3 base metrics

Attack vector: More severe the more the remote (logically and physically) an attacker can be in order to exploit the vulnerability.
Attack complexity: More severe for the least complex attacks.
Privileges required: More severe if no privileges are required.
User interaction: More severe when no user interaction is required.
Scope: More severe when a scope change occurs, e.g. one vulnerable component impacts resources in components beyond its security scope.
Confidentiality: More severe when loss of data confidentiality is highest, measuring the level of data access available to an unauthorized user.
Integrity: More severe when loss of data integrity is the highest, measuring the consequence of data modification possible by an unauthorized user.
Availability: More severe when the loss of impacted component availability is highest.
CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N

EPSS score

Exploit Prediction Scoring System (EPSS)

This score estimates the probability of this vulnerability being exploited within the next 30 days. Data provided by FIRST.
(1st percentile)

Weaknesses

Failure to Sanitize Special Elements into a Different Plane (Special Element Injection)

The product does not adequately filter user-controlled input for special elements with control implications. Learn more on MITRE.

Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')

The product does not neutralize or incorrectly neutralizes user-controllable input before it is placed in output that is used as a web page that is served to other users. Learn more on MITRE.

Improper Neutralization of Alternate XSS Syntax

The product does not neutralize or incorrectly neutralizes user-controlled input for alternate script syntax. Learn more on MITRE.

CVE ID

CVE-2026-27120

GHSA ID

GHSA-4hfh-fch3-5q7p

Source code

Credits

Loading Checking history
See something to contribute? Suggest improvements for this vulnerability.