Skip to main content
All Terms Glossary

Polyglot Payloads

Definition

Polyglot payloads are attack strings that are valid in multiple contexts, potentially bypassing security filters. For example, a string might be both valid JavaScript and valid HTML. These sophisticated payloads can exploit parser differences and escape sanitization. Prevention requires context-aware input validation and output encoding.

What polyglot payloads are

A polyglot payload is a single string crafted to be valid, and to do something useful for the attacker, in more than one parsing context at once. A classic web example is a sequence that behaves as ordinary text in HTML, as an attribute value, and as executable JavaScript, so that whichever way a page happens to interpret the input, part of it still triggers script execution. Attackers build them by combining fragments that terminate one context and open another, using comment tricks and characters that different parsers treat differently. The same idea appears in file formats, where one file is simultaneously a valid image and a valid script or archive.

Why polyglots are dangerous

Polyglots exist to defeat context-specific defences. Many input filters and sanitizers assume they know where user data will end up, an HTML body, a URL, a JavaScript string, and encode only for that case. A payload that stays valid across several contexts slips past filters tuned for just one, and exploits differences between how a server-side sanitizer and the browser's parser read the same bytes. This makes polyglots a favourite for XSS that survives web application firewall rules and for uploads that pass an image check but execute as script. A single reusable string can probe many injection points, lowering the effort to find one that fires.

Defending against polyglot payloads

Defence starts with context-aware output encoding: encode data for the exact sink it lands in (HTML, attribute, URL, JavaScript, CSS) rather than applying one generic filter, and prefer safe DOM APIs such as textContent over innerHTML. Validate uploads by content type and re-encode files server-side so a polyglot cannot keep its second identity. A strict Content Security Policy limits what an injected script can do even if a payload lands. cside complements these controls by analysing the behaviour of scripts that actually execute in the browser and blocking malicious activity in real time, rather than relying on filters that a cleverly encoded string can evade.

Definition

How is a polyglot payload different from a normal XSS payload?

A standard XSS payload targets one known context. A polyglot is engineered to remain valid in several contexts simultaneously, so the attacker does not need to know exactly how the application will parse the input; the same string works whether it lands in HTML, an attribute, or JavaScript.

Definition

Can polyglots appear in files, not just text input?

Yes. File polyglots are crafted so one file satisfies two format specifications at once, for example a valid GIF that is also runnable JavaScript. They are used to bypass upload filters or content-type checks that only validate against a single expected format.

Got more questions

Talk to a security expert

We answer client-side security questions every day. Bring yours.

Book a demo

Want to walk through this with an engineer?

Thirty minutes, on your own site. Not a slide deck.

We'll show you:

Which third-party scripts are running on your site right now
Where you stand on PCI DSS 6.4.3 and 11.6.1
How much of your traffic is bots and AI agents

Rather just send a question?

Finding open slots…

Real humans only. We'd know.

Having trouble booking? Open scheduler in a new tab

What are you trying to solve?

Tell us in a line and we'll come back with something useful, not a generic pitch.

We usually help with:

Seeing which third-party scripts run on your site
PCI DSS 6.4.3 and 11.6.1 evidence
Bots, AI agents and account takeover

Prefer to just book a time? Pick a slot instead