Categories
Security Web

The T in HTML: why a text format became an application platform

The A and the T

The A in SPA stands for application. The T in HTML stands for text.

We rarely say the two together, which is a pity, because they describe a mismatch that the whole industry has been working around for thirty years. We take a language designed to mark up text, and we ask it to describe the user interface of an application: toolbars, drag and drop, real-time collaboration, an entire word processor.

In my previous two articles I looked at the web’s security problems from two sides. The first argued that executing code in the browser was the original sin. The second argued that many of the decisions that followed were driven by corporate competition, not by technical reasoning. This article asks a third question, and I think it is the most basic of the three: is HTML a suitable foundation for applications at all?

My answer is no, not by design. It became an application platform by accident, and a surprising number of our problems, including XSS, are what that accident looks like from the inside.

In the late 2010s, one of my products was a website for an electronic health record system. The desktop version of the application was built on .NET using a UI framework from Infragistics and talked to the database via NHibernate. Although Infragistics also had a UI framework for the web, we decided to separate the development of web and desktop. The original application was never meant to be run on the web. So just “replacing the UI framework” would have introduced more complexity than needed. Rebuilding the web version from scratch in a simplified version was the way we went. Even with a mature desktop application in hand, the web UI had to be built from scratch.

What HTML was for

HTML was built to describe documents. A heading is a heading, a paragraph is a paragraph, a link points somewhere else. Everything about the format assumes that the content is the point and that different programs may present it differently: a browser, a search indexer, a screen reader.

Tim Berners-Lee was explicit that this was a deliberate choice. He wrote that he chose not to make HTML a programming language, so that different programs could do different things with the same document. The idea later became the W3C’s Rule of Least Power, which recommends choosing the least powerful language that suits the purpose. The same W3C text names HTML as an example of a technology designed with least power in mind.

Least power has a price and a benefit, and they are two sides of one coin. You cannot do everything with a weak language. But you can analyse, index, transform and reuse what is written in it, because it describes what something is, not what to do.

An application is the opposite. It is a description of behaviour: when this happens, do that, and keep this state. The more powerful the language, the less you can know about what it will do without running it. Keep that in mind, because the next sections are about what happens when you build behaviour on top of a format that was designed to avoid it.

What an application needs

HTML covers almost none of what a user interface toolkit provides. The comparison below uses a native toolkit such as WPF/XAML as the reference. It is a simplification, but the pattern is clear.

NeedNative toolkit (e.g. WPF/XAML)HTML and the DOM
Describing the interfaceA typed tree of UI objectsText markup, parsed into a tree of generic nodes
Standard widgetsButtons, lists, grids, menus, dialogs, built inForm controls only; everything else is assembled from generic elements
LayoutLayout panels designed for interfacesDocument flow, later extended with flexbox and grid
Data bindingPart of the frameworkNot part of the platform; provided by JavaScript frameworks
Reusable componentsControls with a defined interfaceWeb Components, arrived late and still debated
Treatment of user inputText stays textText can become markup, and markup can become script

The last row is the one that matters for security, and I will come back to it. The other rows explain why the web needed an entire industry of frameworks to be usable for applications.

How the gap was filled

Nobody decided that HTML should become an application format. It happened in layers, each a workaround for the one before.

Forms came first. They were the web’s earliest hook for interaction: a page could collect input and send it to a server. That is still the one place where HTML has a real widget set.

DHTML turned the document into a scriptable surface. In 1997, both Netscape and Microsoft promised Dynamic HTML in their version 4 browsers, and their object models were incompatible: Netscape used document.layers, Microsoft used document.all. The W3C’s standard DOM followed only afterwards, with DOM Level 1 finalised in October 1998. This is the moment where the document model became a user interface toolkit. It happened inside the browser war I described in my previous article, which means the design was driven by what each vendor could demonstrate first.

Layout was improvised. CSS was designed to style documents. Building application layouts with it meant years of workarounds, until layout systems built for the purpose arrived. That is the pattern again: the platform lacks something, developers build a workaround, and years later the platform standardises a version of the workaround.

Frameworks hid the mismatch. React, Angular, Vue and their predecessors exist largely to give developers the thing the platform does not: a component model, state management, and a way to update the interface efficiently. They sit on top of the DOM and make it tolerable.

Accessibility had to be added back. When an interface is built from generic elements, the browser no longer knows that something is a button or a menu. ARIA attributes exist to tell it afterwards, which is a repair job for information that a purpose-built toolkit would have carried from the start.

Components came last. The idea of Web Components was first presented in 2011 by Alex Russell. Chrome shipped the current Shadow DOM and Custom Elements versions in versions 53 and 54, and Firefox enabled them by default in version 63 in 2018. Even then, early critics pointed out that introducing Shadow DOM meant losing built-in accessibility features.

Taken together, this is a platform that grew a UI toolkit one patch at a time, with every patch decided by a different party for a different reason.

Why XSS follows from the choice

In a native toolkit, you create a TextBlock object and assign it a string. The string is data. No part of the system will ever look inside it and wonder whether it is actually a button. The structure of the interface and the content shown in it travel through different channels.

In HTML, they travel through the same channel. An interface is written as text, and a parser decides which parts of that text are structure and which are content. To show a user’s name on the page, you put it into a string of markup, and from then on the parser’s rules apply to the name. If the name contains the characters that mean “this is structure now”, the parser believes it. This is the entire mechanism of XSS.

It is a fair objection that the DOM itself offers a safe route. You can create elements and assign text through textContent, and the browser will never interpret that text as markup. That is true, and it is exactly what the better frameworks do for you. But look at what the safe route is: it avoids HTML as a format and uses the object tree directly. The moment a developer builds a fragment as a string, or inserts it with innerHTML, the unsafe route is open again. The platform makes the dangerous option the shortest one.

The industry’s response confirms the diagnosis. Frameworks escape by default and give their raw-HTML escape hatches deliberately alarming names, such as React’s dangerouslySetInnerHTML. Trusted Types wraps strings in a type so that the browser can refuse the untyped ones. In other words, we are retrofitting a type system onto a text format, after the fact, to get back something that a typed object tree has by construction.

That is the point of this article. XSS is not best understood as a recurring human error that we need better training to stop. It is what you get when the description of an application’s interface is text, and untrusted data has to pass through it.

The way out, and what it costs

Some of the most demanding web applications have stopped using the DOM for their interface. In May 2021 Google announced that it would move Google Docs from HTML-based rendering to canvas-based rendering, citing performance and consistency across platforms. Visual Studio Code had already moved its integrated terminal to canvas rendering in 2017. A word processor in particular has layout needs, such as handling mixed-direction text selections, that the DOM was never designed to provide.

This is the logical consequence of my argument. If the document model is the wrong foundation for an application, you draw your own interface and use the browser only as a window.

The price is steep. A canvas has no semantic layer: with the DOM, every node is an object that the browser and assistive technology understand, and with canvas that understanding has to be rebuilt separately. Accessibility, text selection, browser extensions, find-in-page and translation all depended on the document structure that was just thrown away. Google said it would continue to support assistive technology, and the accessibility community watched closely.

There is an interesting side effect for security. An interface that is drawn rather than described does not have a text channel where user data can turn into structure. The class of bug I described above largely disappears, though an application still needs to handle its other inputs with care.

So the escape route exists, and it shows the trade-off clearly. You can have a platform whose interface is accessible, searchable and extensible because it is text, or you can have an interface that is robust and fast because it is not. The web currently cannot give you both.

Of course, the web did not win because of HTML

A fair reader will now say that this argument ignores why the web was such a success. They would be right, and it is worth being precise about it.

The web won as a delivery channel. A URL, no installation, no update process, every device. Anyone who has run software across several operating systems and release cycles knows how valuable that is. Nobody chose HTML as an application format. People chose the web as a way to reach users, and HTML came with it.

The browser sandbox is a real achievement. Running unknown code from a page you opened a minute ago is safer than installing an unknown executable, even with all the weaknesses I listed in the first article. A native toolkit gives you a typed interface tree, and it also gives the application the full rights of the user. I would not trade that away lightly.

Many applications are documents and forms. Look at what a lot of business software does: lists, detail pages, forms, workflows. For that, HTML, links and form posts are a good fit, and the hypermedia approach I mentioned in the first article works with the format instead of against it. The argument in this article is about rich applications: editors, design tools, dashboards with live data. How large that share is depends on what you count. By number of websites it is a minority, but by user time and by new projects it is large and growing, not least because AI tools now generate such applications by default.

So the honest position is a narrower one. HTML is a sound basis for documents and for applications that are really documents with forms. It is a poor basis for applications that need the full range of a user interface toolkit, and the cost of forcing it shows up in frameworks, in accessibility repairs, and in security.

What I would take from this

The question “is HTML suitable for applications?” has no single answer, but it leads to a useful habit. Before you choose an architecture, ask whether what you are building is a document with some interaction, or an application with some text.

  1. Decide deliberately. A single-page application is not the default. For documents, forms and workflows, server-rendered pages are simpler, easier to secure and easier to make accessible.
  2. If you need a rich interface, use the object tree, not strings. Build the interface through the framework’s safe mechanisms, treat every raw-HTML escape hatch as a security decision that needs review, and enforce it with tooling such as Trusted Types.
  3. Count the whole cost. The interface framework, the accessibility repairs and the security controls are all part of the price of using a text format as a UI platform. They belong in the estimate.
  4. Know the exits. For the most demanding cases, drawing your own interface is a legitimate option, as long as you plan for what you lose.

The web gave us a way to reach everyone with a single link. It did not give us a good way to describe an application, and for thirty years we have been making up the difference ourselves. We should at least be honest that this is what we are doing.

Designing an application, whether for the web, the desktop or for a server without a UI, has never been an easy task. But it has never been easier than today to generate a full-fledged web UI with all the toolkits we have at hand. Is there any web app you would have built differently today? I hope this article will help you make better decisions on upcoming projects.

Part 1: What went wrong with the Web

Part 2: Shipped to win: how the browser wars broke web security

Sources

By marcus

Experienced technology leader and software architect with more than 30 years of experience in software delivery, architecture and leadership. Focus on technical strategy, sustainable architectures and team leadership.

One reply on “The T in HTML: why a text format became an application platform”