Malicious Handlebars Code Snippets causing Discord to Crash
Discord's syntax highlighter could be forced into recursive context switching between Handlebars and HTML by alternating raw block expressions and script tags. An attacker could send crafted code snippets that cause significant performance lag or a full application crash on Desktop and Web clients.
- Accepted in 14 days
- Fixed in 6 months
- Disclosed after 11 months
Summary by poeck
A vulnerability in Discord's syntax highlighting allows attackers to trigger a Denial-of-Service attack by exploiting recursive context switching between Handlebars and HTML. By alternating between raw block expressions and script tags, the highlighter is forced into a heavy computational loop that causes significant performance lag or a full application crash on Desktop and Web clients.
What the report shows
The target is Discord's syntax highlighting, in the Web and Desktop clients. The researcher, poeck, reported the issue through Bugcrowd against the "Web & Desktop App" asset. The report was rated high severity and paid a 2,000.00 USD bounty.
The finding is a client-side denial of service. Discord highlights code blocks in messages, and the highlighter understands more than one language grammar. By mixing Handlebars constructs with HTML, the researcher forced the highlighter to switch back and forth between the two grammars recursively. The report describes the mechanism as "recursive context switching between Handlebars and HTML": raw block expressions and script tags alternate, and each switch pushes the highlighter into another round of work.
The result, as the summary states, is that the highlighter is forced into a heavy computational loop. On the affected clients this shows up as significant performance lag, up to a full application crash. The report gives no payload text, no message content, no version numbers and no timeline, so the exact input that triggers the loop is not reproduced here.
Root cause
The report describes the mechanism as recursion between the two grammars in the highlighter. A highlighter that supports embedded languages has to decide, at each point in the input, which grammar is active. Handlebars raw blocks can contain arbitrary text, including HTML, and HTML can contain script tags whose contents may again be treated as template or script code. If the highlighter re-enters the embedded grammar on every such transition without a depth limit or a guard against re-entering a context it is already inside, a short input can produce a very large number of nested or repeated parse operations.
The report frames this as recursion between the two grammars rather than as a memory-safety issue. The effect is described on the Desktop and Web clients.
Impact
An attacker who can post a message containing a crafted code block can make other users' clients lag or crash when the message is rendered. The trigger is message content rendered by the client. The report does not describe persistence, data exposure or code execution, and the impact should be read as availability only: degraded performance and application crashes on the affected clients.
How this kind of bug works
Syntax highlighters are usually built from grammar definitions that can embed one language inside another: HTML containing JavaScript, Markdown containing fenced code, templates containing markup. The parser tracks a stack of active contexts and switches grammar when it sees a delimiter such as an opening tag, a closing tag or a raw block marker.
The danger appears when two grammars can each embed the other. A construct in language A opens a region parsed as language B, and a construct in B opens a region parsed as A again. If the implementation follows these transitions without bounding depth or without memoising work already done, the amount of parsing grows far faster than the input length. Even without true unbounded recursion, repeated re-scanning of the same region on each switch produces quadratic or worse behaviour. The input stays small, so message size limits do not help, and the cost lands on every client that renders the message.
Defences are structural: a maximum nesting depth for embedded contexts, a rule that a grammar cannot re-enter a context already on the stack, and linear-time parsing strategies such as those used by highlighter engines designed to avoid backtracking. Running highlighting off the main thread and imposing a time budget per block limits the visible damage but does not remove the underlying cost.
How to test for it
- Map which grammars are supported and which can embed others. Template languages (Handlebars, Mustache, Jinja, ERB), markup (HTML, XML) and script languages that contain markup strings are the interesting pairs.
- Build inputs that alternate between two grammars: open a raw or template block, place markup inside it, place a script or template region inside that markup, and repeat the pattern. Increase the nesting one level at a time.
- Send the input to a test account or channel you control, then observe rendering time in the browser's performance profiler and watch for main-thread stalls, long tasks, memory growth or a tab crash.
- Compare against a control input of similar length with no grammar switching, so you can attribute the cost to the switching rather than to input size.
A positive result is a superlinear increase in render time as nesting grows, or a reproducible crash of the client with a small payload. Common false positives are large but flat inputs that are simply expensive to highlight, slow networks, and one-off stalls caused by unrelated page load. Confirm the effect is repeatable, tied to the grammar alternation, and observable on a second client or browser profile before reporting.
How it is fixed
General mitigations for this class of bug include capping nesting depth and refusing re-entry into an active grammar. Where the engine cannot be changed quickly, highlighting can be moved off the main thread with a time or operation budget per block, and pathological blocks can be rendered as plain text. Regression tests should include alternating-grammar inputs at increasing depth.
Hunting notes
This class of bug hides wherever a product renders rich text with language-aware parsing: chat and comment systems, code review tools, note apps, dashboards that display logs or configuration, and email clients that render HTML. Recon signals include a visible language selector on code blocks, documentation listing supported highlighting languages, and client bundles that reference a highlighter library with embedded-grammar support.
State clearly that the impact is availability only, and demonstrate that a normal user viewing the content is affected, not just the sender.
What happened, in plain words
Discord is a chat application that runs in the browser (Web) and as an installed program (Desktop). One of its features is syntax highlighting: when someone sends a message containing a code block, Discord colours the code so it is easier to read, much like a word processor colouring grammar in a document. To do this, Discord uses a piece of software called a syntax highlighter, which reads the text and tries to work out which programming language it is looking at.
A researcher named poeck reported that this highlighter can be pushed into doing far too much work. The trick is to mix two languages that the highlighter understands, Handlebars and HTML, in a way that makes it jump back and forth between them again and again. The report calls this "recursive context switching between Handlebars and HTML". In everyday terms, imagine a form that asks you to fill in a box, and inside that box is another form asking you to fill in a box, and so on: each answer sends you to a new form instead of finishing. The highlighter keeps switching grammar and never settles, so it burns through processing power.
The consequence is a denial of service, usually shortened to DoS. That is not about stealing data or breaking in; it is about making a service or an app stop working properly for the people using it. Here the effect is that the Discord client lags badly or crashes completely. The report was submitted through Bugcrowd against the "Web & Desktop App" asset, rated high severity, and paid a 2,000.00 USD bounty.
A few things the report does not include: the exact message text that triggers the problem, any version numbers, and a timeline. So this article explains the mechanism rather than reproducing the payload.
The technical side, explained from zero
The pieces involved
- Syntax highlighting: the colouring of code so keywords, strings and tags stand out. It is done by a program that parses the text.
- Grammar: the set of rules that describes a language, for example which characters open a tag or start a comment. A highlighter holds one grammar per supported language.
- Handlebars: a templating language. Templates are text with placeholders that get filled in later, a bit like a letter with blanks to complete. Handlebars has a construct called a raw block, which marks a region whose contents should be passed through untouched.
- HTML: the language web pages are written in, built from tags such as script tags, which wrap code the browser runs.
- Client-side: work done on the user's own computer or browser, rather than on Discord's servers.
Why the two languages together are a problem
A highlighter that supports several languages has to decide, at every point in the text, which grammar is currently active. It keeps a kind of stack, like a pile of cards, where each card says "right now you are inside HTML" or "right now you are inside Handlebars". When it sees a marker, such as an opening tag or a raw block marker, it puts a new card on the pile and switches grammar.
The danger is when two grammars can each contain the other. A Handlebars raw block can hold arbitrary text, including HTML. HTML can hold script tags, and the contents of a script tag may again be treated as template or script code. So a construct in one language opens a region parsed as the other, and a construct in the other opens a region parsed as the first again. If the highlighter follows these transitions without a limit on how deep it can go, a short message can produce a very large number of parsing operations. The report describes exactly this: raw block expressions and script tags alternate, and each switch pushes the highlighter into another round of work, forcing it into a heavy computational loop.
The report describes the effect on the Desktop and Web clients. The report frames the issue as recursion between the two grammars.
What the impact is, and what it is not
The impact is availability only: degraded performance and application crashes on the affected clients. The report does not describe persistence, data exposure or code execution. A crafted code block can make the client lag or crash when rendered.
In general, how this kind of bug works
Syntax highlighters are usually built from grammar definitions that can embed one language inside another: HTML containing JavaScript, Markdown containing fenced code, templates containing markup. The parser tracks a stack of active contexts and switches grammar when it sees a delimiter such as an opening tag, a closing tag or a raw block marker.
The danger appears when two grammars can each embed the other. If the implementation follows these transitions without bounding depth, or without remembering work already done, the amount of parsing grows far faster than the input length. Even without true unbounded recursion, re-scanning the same region on each switch produces quadratic or worse behaviour: double the input and the work can grow fourfold or more.
Defences are structural: a maximum nesting depth for embedded contexts, a rule that a grammar cannot re-enter a context already on the stack, and linear-time parsing strategies such as those used by highlighter engines designed to avoid backtracking. Running highlighting off the main thread and imposing a time budget per block limits the visible damage but does not remove the underlying cost.
How to test for it
- Map which grammars are supported and which can embed others. Template languages (Handlebars, Mustache, Jinja, ERB), markup (HTML, XML) and script languages that contain markup strings are the interesting pairs.
- Build inputs that alternate between two grammars: open a raw or template block, place markup inside it, place a script or template region inside that markup, and repeat the pattern. Increase the nesting one level at a time.
- Send the input to a test account or channel you control, then observe rendering time in the browser's performance profiler and watch for main-thread stalls, long tasks, memory growth or a tab crash.
- Compare against a control input of similar length with no grammar switching, so you can attribute the cost to the switching rather than to input size.
A positive result is a superlinear increase in render time as nesting grows, or a reproducible crash of the client with a small payload. Common false positives are large but flat inputs that are simply expensive to highlight, slow networks, and one-off stalls caused by unrelated page load. Confirm the effect is repeatable, tied to the grammar alternation, and observable on a second client or browser profile before reporting.
How it is fixed
General mitigations for this class of bug include capping nesting depth and refusing re-entry into an active grammar. Where the engine cannot be changed quickly, highlighting can be moved off the main thread with a time or operation budget per block, and pathological blocks can be rendered as plain text. Regression tests should include alternating-grammar inputs at increasing depth.
Where to look for it
This class of bug hides wherever a product renders rich text with language-aware parsing: chat and comment systems, code review tools, note apps, dashboards that display logs or configuration, and email clients that render HTML. Recon signals include a visible language selector on code blocks, documentation listing supported highlighting languages, and client bundles that reference a highlighter library with embedded-grammar support.
State clearly that the impact is availability only, and demonstrate that a normal user viewing the content is affected, not just the sender.
Glossary
- Syntax highlighting: colouring code in a message so its structure is easier to read, done by a parser that recognises language rules.
- Grammar: the rules describing a language, used by a highlighter to decide what each part of the text is.
- Handlebars: a templating language where placeholders in text are filled in later; it supports raw blocks that pass their contents through untouched.
- Raw block: a Handlebars construct marking a region whose contents are not processed as Handlebars.
- HTML: the markup language of web pages, built from tags such as script tags.
- Script tag: an HTML tag that wraps code for the browser to run.
- Recursive context switching: repeatedly entering one grammar from another and back again, without settling.
- Denial of service (DoS): making an app or service unusable for its users, for example by exhausting its processing power.
- Client-side: work performed on the user's own device or browser rather than on the server.
- Main thread: the part of a browser or app that handles the interface; if it is busy, the app appears frozen.
- Stack: an ordered pile of items where the most recent is handled first, used here to track which grammar is active.
- Quadratic behaviour: work that grows with the square of the input size, so doubling the input roughly quadruples the cost.
- Linear-time parsing: parsing whose cost grows in proportion to input size, the property that avoids this bug.
- Availability: whether a system is usable when people need it; an availability bug stops it working rather than exposing data.
- Bugcrowd: a bug bounty platform where researchers report vulnerabilities to companies.
- Bug bounty: a reward paid by a company for a valid security report.
- Severity: a rating of how serious a vulnerability is.
- Asset: the specific product or component a report targets, here the Web & Desktop App.