Same bug, different decade
Cross-site scripting has been on the OWASP Top 10 for as long as the list has existed. We have output-encoding frameworks, linters, content security policies, code review and a generation of developers who have all worked through the same security training. And yet XSS findings keep turning up in the penetration-test reports of perfectly competent teams.
I have stopped believing this is a discipline problem. When a class of bug survives three decades of education, tooling and awareness campaigns, the bug is not the problem. The platform is.
My thesis is simple, and deliberately uncomfortable: the web took a wrong turn the moment browsers began executing code. XSS is only the most visible symptom. The underlying disease is running code in an environment that nobody controls.
1995: a document becomes a program
The web, in its original form designed by Tim Berners-Lee, was a system for linking documents. A browser fetched a page, rendered it, and that was the end of its job. The worst thing a page could do was be badly written. It could not do anything.
In 1995 Netscape added JavaScript to Navigator 2.0. Overnight, every page became a potential program, executed on the visitor’s machine, on behalf of whoever had written the page. And I must admit, I was excited about all the possibilities that emerged at that moment.
The consequences were visible almost immediately. Netscape needed the same-origin policy to stop one site’s script from reading another’s content. It arrived within a year or so, and it has been the load-bearing wall of web security ever since. Within a few years, researchers were finding holes in it: pages in one frame reading data from another. Microsoft’s security engineers gave this family of problems its name in January 2000: cross-site scripting.
Notice the order of events. First we allowed code to run in an environment we could not control. Then we invented a security model to contain it. Then we spent thirty years patching the model.
What XSS actually is
XSS is usually explained as “a user typed something nasty into a form”. That is the trigger, not the cause.
The cause is that HTML mixes content and instructions in one stream. A page is text, and the text may also contain <script>. Whenever user-supplied data lands in that stream without the right encoding for that exact context (an element body, an attribute, a URL, a JavaScript string or a CSS expression), the browser cannot tell data from code. It does what it was built to do and executes it.
That is why the problem never goes away. There is no single correct way to escape. There are at least five different contexts, each with its own rules, and a modern single-page application switches between them dozens of times per screen. Not to mention that these contexts can also be nested. We ask every developer to get every one of them right, every time, forever.
And the payload is not small. A script that runs in your origin has your user’s session, your user’s DOM and your user’s trust. The same-origin policy cannot help here, because from the browser’s point of view the attacker’s code is your code.
A runtime nobody asked for
If XSS were the only problem, better encoding would eventually fix it. It is not. Once you allow arbitrary code in the browser, a whole family of attacks follows:
- Third-party script compromise. In 2018 attackers added 22 lines of JavaScript to a modified Modernizr library on the British Airways website. Roughly 380,000 customers had their payment data skimmed. The regulator announced an intended fine of £183 million and settled on £20 million. Nobody needed an XSS hole. The page was supposed to run that script.
- Supply-chain attacks. A typical frontend build pulls in hundreds or thousands of transitive npm packages. Every one of them ends up running with your users’ privileges.
- Tracking and fingerprinting. Canvas, fonts, timing: script access is what makes browser fingerprinting possible at all.
- Cryptojacking and malvertising. Ad networks hand your visitors’ CPUs and browsers to whoever wins an auction.
- Side channels. Spectre forced browser vendors to rebuild process isolation and ship site isolation, because untrusted code could read memory it should never see.
All of these share one precondition: the visitor’s machine executes code the visitor never reviewed, written by people the visitor never heard of, loaded from servers the site operator may not control either.
Whose machine is it anyway?
The question I always ask first is: where does the code run, and who controls that place?
On a server, the answer is clear. The operator chooses the operating system, the runtime, the dependencies and the network boundary. The operator can patch, monitor, log, roll back and audit. If something goes wrong, there is one party who is responsible and able to act.
In the browser, the answer is the opposite. The operator does not know the browser vendor, the version, the extensions installed, the operating system, or whether the device is already compromised. The code is delivered into that unknown environment, together with code from other origins, and then it runs with the user’s credentials. It cannot be observed, and it cannot be patched once it has shipped.
We have, in effect, inverted the trust model. Security engineering is built on the principle of keeping untrusted input away from trusted execution. The web did the reverse: it moved execution into the least trusted place out there and sent the sensitive data after it.
If all application logic ran on the server, returning plain documents and accepting plain form posts, a large share of this attack surface would simply not exist. No DOM-based XSS. No malicious third-party scripts reading a payment form. No client-side supply chain. The user’s browser would go back to being a viewer.
Of course, it’s not that simple
The server is not magically safe. Injection is the same class of bug on both sides. SQL injection, server-side template injection, command injection, SSRF and unsafe deserialisation all come from mixing data and code, exactly like XSS. Moving code to the server does not remove that mistake. It moves the consequences somewhere worse: a successful server-side exploit gives the attacker the whole database, not one user’s session.
Not all web attacks need JavaScript. CSRF works with a plain form post. Clickjacking needs only an iframe. HTML injection and CSS-based data exfiltration exist without a single script tag. Even a document-only web would have had a trust problem around cookies and ambient authority.
JavaScript was not the only executable web. Java applets arrived in the same year, and ActiveX and Flash followed within a year or two. Several of them were considerably worse: ActiveX controls ran with the full rights of the user (possibly with admin rights). JavaScript at least came with a sandbox and a same-origin model.
And the benefits are real. Interactive applications, offline use, instant feedback and editors that run in a tab are not nice-to-haves. Millions of people use them daily.
I think the first two points sharpen the thesis rather than refute it. The real defect is not “code” in the abstract. It is the combination of three things: code and data sharing one channel, code running with the user’s authority, and code whose origin and integrity the operator cannot vouch for. JavaScript in the browser is where all three meet by default. A server-side injection flaw is bad, but it happens inside a place the operator owns, monitors and can fix in one deployment.
What a different web looks like
We cannot un-invent JavaScript. But we can see which parts of the web already lean the other way, and they are instructive.
- Hypermedia and server-rendered applications. Approaches like HTMX, Hotwire or classic server-side rendering keep state and logic on the server and ship HTML fragments. The client stays thin, and the amount of code running in the browser drops by an order of magnitude.
- Progressive enhancement. Pages that work as plain documents and form posts, with scripts as an optional layer, degrade gracefully when the script is blocked, broken or hostile.
- Browser-side containment. A strict Content Security Policy, Subresource Integrity, Trusted Types and site isolation are the web platform admitting that the model needs guard rails. They work, but they are opt-in, fiddly and rarely applied completely.
- Sandboxed runtimes. WebAssembly, with capability-based access instead of ambient authority, is a closer match to what a safe client runtime should be. Again, only if the host does not hand it the keys.
Every item on that list does the same thing in a different way: it shrinks the amount of untrusted code that runs with the user’s authority. That is the design goal. The thesis only gives it a direction.
What I would do on Monday
The web did not go wrong because developers are careless. It went wrong because we gave the least controllable place in the whole architecture the most powerful capability, and then asked people to be perfect.
As an architect, I treat client-side code as a liability that has to justify itself, not as the default. In practice that means four rules for the teams I work with:
- Server first. Logic, validation and state live on the server. The browser renders and submits.
- Script as an exception. Every piece of client-side JavaScript needs a reason that a server round trip cannot satisfy.
- No uncontrolled third-party code on pages that handle credentials or payments, and no dependency nobody on the team can name.
- Contain what remains. Strict CSP, Subresource Integrity and Trusted Types, enforced in CI, not left to goodwill.
The interesting question is not whether we could have built a safer web. It is how many of our current security problems we would still have if we had kept the browser a viewer. My honest estimate is: far fewer than we would like to admit.
I hope this article helps to sharpen your view of security a bit, and helps you decide whether we really need to build everything we could build.
Sources
- Cross-site scripting: origin of the term, Microsoft, January 2000
- British Airways data breach: ICO fine reduced to £20m
- It took hackers just 22 lines of code to steal British Airways’ customer data: RiskIQ findings, 380,000 customers