How we managed to create a safe and rich HTML email renderer

Everything used to be bad, and here is why

In the world of email, the quality with which HTML emails are displayed leaves much to be desired. The main problems are associated with the differences in the ways HTML and CSS are supported in different email clients and web interfaces. Often emails that look perfect in one service are displayed poorly in another service. Users understandably get upset, conversion drops, and the brand’s image suffers as a result.

In large part this is explained by the fact that there are only a handful of monopolistic services in the world that have a web interface for displaying emails, and only a few popular email clients. Gmail and Yahoo haven’t changed much in the way they display emails in years, even though HTML and CSS have improved drastically over time and now provide many new options for content display. For example, adaptive design first emerged with the implementation of media queries around 10-12 years ago, support for the dark theme in CSS emerged about 4-5 years ago, but to this day none of the large email services support these features. They simply have no reason to change anything — instead, it’s webmasters who have to adapt to the whims and undocumented limitations of these services.

For instance, some support animations and others don’t. Gmail doesn’t even support its own (invented and popularized by Google) webp image format, but iCloud does. And there are hundreds of examples like this one. Webmasters are forced to relive the early days of the Internet when the browser wars were raging and you had to jump through all kinds of hoops to make web pages look similar in different browsers.

Yes, at times, some restrictions, such as the lack of script support, are dictated by safety concerns — you wouldn’t want to get an email with an interactable banner that sends your data directly to some ad network. But more often than not the set of limitations looks absolutely inadequate — what harm have simple background images, set with background-image, done to Gmail?

What we came up with

When we made our first steps on the email market with AdGuard Temp Mail, we spent a lot of time studying the email display options. We definitely didn’t want to become another Gmail, inventing our own HTML/CSS subset and cutting down on all the components that we would deem unnecessary. But we also weren’t planning on leaving users one-on-one with web threats.

Chapter 1. Seven strings of JavaScript code that solved the problem

Fortunately, the world of web development doesn’t stay still, and browser developers have long been working on browser sandboxes, which we have taken advantage of.

What are browser sandboxes? They are essentially a set of techniques to restrict the capabilities of the web page, literally a list of dos and don’ts very similar to CSP (we use it too, but more on that later): “don’t use cookies of the parent page,” “don’t execute JavaScript,” “don’t send HTTP Referrer when someone is following a link from the frame” and a number of other directives. Sounds interesting, doesn’t it? Let’s delve deeper into the details of technical implementation.

We receive the email body from the API as a string containing HTML markup, which we then display in the browser using sandbox capabilities:

const html = '<html><body>....</body></html>';

const iframe = document.createElement('iframe');

iframe.credentialless = true;
iframe.sandbox = 'allow-popups allow-popups-to-escape-sandbox';
iframe.referrerpolicy = 'no-referrer';

iframe.srcdoc = html;

document.body.appendChild(iframe);

Add some styles, position the frame on the parent page — and you’re golden, you’ve just created a robust, safe engine to display HTML emails without unnecessary limitations!

In a perfect world that would be the end of it. After all, didn’t we stop the main threat — JavaScript? Unfortunately, modern-day emails often include a mountain of trackers of all kinds, which aim to collect as much data about you as possible. They are zero-pixels — invisible images that transmit your IP address and data about your device to their servers — and special tracking links that record every single action you perform within the email. And we had to find solutions to all of these problems.

Chapter 2. User anonymization

During the development we stumbled upon an excellent instrument to estimate the safety of our engine — emailprivacytester.com.

This service generates an HTML email that contains a number of elements specifically designed to test out certain vulnerabilities. Now we can start the diagnostics process, send ourselves an email to an AdGuard TempMail mailbox and… observe multiple IP address leaks.

Just like we dealt with JavaScript and other similar problems earlier, let’s now focus on these IP address leaks. IP address leak happens when it’s the email sender can figure out your IP address just by sending the email. Of course, if you’re using AdGuard VPN then all the sender will get is the IP address of the VPN server you’re connected to. But do this even once without the protection of a VPN, and the perpetrator will be able to learn your geolocation down to the city. Of course, we couldn’t let that happen to our users. So what to do?

Image proxy

Fortunately, there was no reason to reinvent the wheel. All of this has been done before, and all the “big” email service providers have been doing hust that for a long time: they wrap all images in their own proxy.

How? For example, if your email had this image:

<img src="http://my.website/image/cat.jpg" />

then every time you open the email, the owner of the my.website server receives an HTTP request. The metadata of this request will contain your IP address, along with some additional data about your device, including your operating system, browser version, and other details that could potentially deanonymize you.

How does the proxy work? It replaces the src attribute in all image tags with its own URL, as shown below:

<img src="https://email.provider/image?url=http://my.website/image/cat.jpg" />

The email.provider web server is designed to download every source image only once. After that, it saves the image and the subsequent requests are not seen by the owner of the my.website server. Even for the initial request, my.website only sees that it’s made by email.provider and records only its IP address, not any personal data. It’s good and all, except that in this case such data is collected by email.provider itself (and we remember that it is often a large company like Google or Yahoo who plays this role).

We developed a similar solution. However, unlike others, our image proxy does not collect or store user data and is completely anonymous.

Having set up the proxy, we now need to somehow “wrap” all images inside the email in it, and for that we need to look at our source HTML as a DOM. We can’t work with DOM inside the sandbox since it doesn’t have JavaScript. We can’t execute JavaScript there even for our own code, as it would open the door for executing it for all the other code in the incoming emails. And if we’re to work with DOM outside of the sandbox, we’d like to have some insurance that the HTML is clean.

Luckily, we have found an amazing library: DOMPurify. It can “clean” HTML content and make it safe and structured. It has been extensively tested and has been deservedly praised by security experts. Let’s start using it:

const html = '<html><body>....</body></html>';
const clean = DOMPurify.sanitize(html, {
    // restore partial HTML to a full-fledged document, if needed
    WHOLE_DOCUMENT: true,
    // these tags make no sense in the context of an HTML email, so we’re deleting them
    FORBID_TAGS: ['audio', 'video', 'button', 'input', 'form'],
});

const iframe = document.createElement('iframe');

// the set of actions with the sandbox from step one

iframe.srcdoc = clean;

document.body.appendChild(iframe);

Great, we’ve successfully removed the clutter, now we can return to image processing. DOMPurify will help us here, too! It has an excellent hooks mechanism — we can solve the next problem without changing the context:

DOMPurify.addHook('afterSanitizeAttributes', (node) => {
    if (node.tagName === 'IMG' && node.hasAttribute('src')) {
        // changing the original src to a link to ImageProxy
        const src = wrapWithImageProxy(node.getAttribute('src'));
        node.setAttribute('src', src);
    }
});

Now it’s time to run the diagnostics again, and we see that the number of IP leaks has dropped… but not to zero. How come? It’s time we talked about CSS.

IP address leaks through CSS

CSS carries truly impressive content stylization capabilities. No other system even begins to approach CSS — its simplicity, ease of use, and rich functionality make CSS one of the best inventions of software engineering. However, like many powerful tools, some of its features can be misused. In the context of our task, the “harmful” part is the url() function, which downloads content via a link to style the page. The most commonly seen example is background images. The code to download them looks like this:

background-image: url(http://my.website/image/cat.jpg);

The problems here are exactly the same as the ones described in the previous chapter — IP leaks. Besides the background images, the url() function, as detailed in Mozilla’s CSS documentation, can be used in various CSS rules. There are a lot of them, and the syntax of the CSS selector allows to set relatively complex rules, such as the selector from one of the examples by the link above:

background-image: cross-fade(20% url(http://my.website/image/first.png), url(http://my.website/image/second.png));

It’s not easy to safely parse a rule like that, you’d need proper tools. And we got lucky again and found exactly the tool for the job! csstree does precisely what we need — allows to analyze CSS and change some of its selector types. In code, it looks like this:

csstree.walk(ast, (node) => {
    if (node.type === 'Url') {
        node.value = wrapWithImageProxy(node.value);
    }
});

which gives us this output:

background-image: cross-fade(20% url(https://img.agrd.eu/image?url=http://my.website/image/first.png), url(https://img.agrd.eu/image?url=http://my.website/image/second.png));

Voila, no more IP address leaks!

CSP

Data privacy is a field where there can’t be any exceptions. Not a single one. That’s why it is imperative to come up with something for cases when a vulnerability would be discovered in one of the libraries or in our code that would incorrectly interpret data. The last line of defence, when everything else fails. In our case, we use the one and only CSP in this role. CSP is a set of directives allowing granular control of page access to certain resources. Webmasters often struggle with CSP. It’s easy to forget that some new resources needed on the web page are not allowed in CSP settings — to be completely honest, we have made this mistake in the past, too. But we managed to tame CSP with a solution like this:

// restrict image sources to only those wrapped inside our proxy

img-src data: https://img.agrd.eu/image;

// disallow connections to any sources not explicitly permitted above

script-src 'none';
style-src 'none';
font-src 'none';
connect-src 'none';
media-src 'none';
object-src 'none';
prefetch-src 'none';
child-src 'none';
frame-src 'none';
worker-src 'none';
frame-ancestors 'none';
...

// which can be simplified as

default-src 'none';

Now we are safe: even if one of the libraries goes rogue or our image address transformation function code breaks, CSP will not let the browser send a request to the perpetrator’s server and leak your IP address.

Results of our work with data privacy

Thanks to the capabilities of the modern browsers and a few excellent libraries, we’ve achieved a safe, high-quality display of HTML emails without any IP leaks, JavaScript, or any other potentially dangerous content — everything is completely under our control.

What is so great about it

We’ve covered many security-related topics, possibly shifting the focus away from a crucial achievement: we managed to create 100% HTML5/CSS3-compatible HTML emails, something literally nobody else in the world has done! And that means that you have at your disposal all the capabilities that modern-day HTML pages can offer: animation, adaptivity, modern image formats, dark themes as they were intended (don’t you also despise the ugly color shifts from primitive algorithms?). You don’t have to suffer through complex layouts using tables like in the 90s, but can instead design beautiful adaptive emails for mobile devices with ease.

It is wild that in the mid-2020s, major email providers still display emails with numerous flaws and vulnerabilities. We are extremely happy that we managed to create a safe alternative that meets all the requirements of a modern email client.

If something goes wrong

We are proud of everything we have achieved with AdGuard Temp Mail so far, but we also realize that we’re still in the beginning of our journey. When you create a new product, it’s impossible to avoid all mistakes and shortcomings. If you identify a vulnerability in email display, please report it to security@adguard.com. We will deal with it in the shortest time possible, and you will become eligible for a reward as part of our Bug Bounty program.

Our developer team takes user feedback very seriously. So if you notice that your HTML email is displayed incorrectly, please send us a message to support@adguard.com, and we will address the problem promptly.

Thích bài viết này?
AdGuard VPN AdGuard DNS AdGuard Mail AdGuard Wallet
AdGuard VPN AdGuard DNS AdGuard Mail AdGuard Wallet
Màn hình chính của AdGuard cho Windows
Màn hình sự bảo vệ của AdGuard cho Windows, hiển thị các tính năng và cài đặt sự bảo vệ
Màn hình Thống kê của AdGuard cho Windows, hiển thị thống kê về quảng cáo và trình theo dõi Đã bị chặn
Màn hình Quản lý ứng dụng của AdGuard cho Windows, hiển thị các tùy chọn quản lý sự bảo vệ cho các ứng dụng được cài đặt trên thiết bị
20.687 20687 đánh giá của người dùng
Tuyệt vời!

AdGuard: trình chặn quảng cáo cho Windows

AdGuard cho Windows không chỉ là một trình chặn quảng cáo. Đây là công cụ đa năng giúp chặn quảng cáo, kiểm soát truy cập vào các trang web nguy hiểm, tăng tốc độ tải trang và bảo vệ trẻ em khỏi nội dung không phù hợp
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Cửa hàng ứng dụng Microsoft
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
AdGuard cho Windows v8.0, thời gian dùng thử 14 ngày
Màn hình chính của AdGuard cho Mac
Màn hình Chế độ ẩn của AdGuard cho Mac
20.687 20687 đánh giá của người dùng
Tuyệt vời!

AdGuard: trình chặn quảng cáo cho Mac

AdGuard cho Mac là một trình chặn quảng cáo độc đáo được thiết kế dành riêng cho macOS. Ngoài việc bảo vệ bạn khỏi các quảng cáo gây phiền nhiễu trong trình duyệt và ứng dụng, nó còn bảo vệ bạn khỏi theo dõi, lừa đảo và gian lận
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Đọc thêm
AdGuard cho Mac v2.19, thời gian dùng thử 14 ngày
Màn hình chính của AdGuard cho Android
Màn hình Bảo vệ theo dõi của AdGuard cho Android
Màn hình Quản lý ứng dụng của AdGuard cho Android, hiển thị các tùy chọn quản lý sự bảo vệ cho các ứng dụng được cài đặt trên thiết bị
Màn hình Thống kê của AdGuard cho Android, hiển thị thống kê về quảng cáo và trình theo dõi Đã bị chặn
Màn hình chính của trình duyệt riêng tư của AdGuard cho Android
Mã QR để tải xuống AdGuard cho Android
20.687 20687 đánh giá của người dùng
Tuyệt vời!

AdGuard: trình chặn quảng cáo cho Android

Chặn quảng cáo và trình theo dõi trên mọi trình duyệt, trò chơi và các ứng dụng khác. Bảo vệ quyền riêng tư của bạn và cho phép bạn kiểm soát cách các ứng dụng sử dụng Internet. Cài đặt qua APK
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Đọc thêm
Quét để tải xuống
Sử dụng bất kỳ trình đọc mã QR nào có sẵn trên thiết bị của bạn.
AdGuard cho Android v4.14, thời gian dùng thử 14 ngày
Màn hình chính của AdGuard cho iOS
Màn hình sự bảo vệ của AdGuard cho iOS, hiển thị các tính năng và cài đặt sự bảo vệ
Màn hình Thống kê của AdGuard cho iOS, hiển thị thống kê về quảng cáo và trình theo dõi Đã bị chặn
Mã QR để tải xuống AdGuard cho iOS
20.687 20687 đánh giá của người dùng
Tuyệt vời!

AdGuard: trình chặn quảng cáo cho iOS

Trình chặn quảng cáo iOS tốt nhất cho iPhone và iPad. AdGuard loại bỏ mọi loại quảng cáo và trình theo dõi trong Safari và bảo vệ quyền riêng tư của bạn trên tất cả các ứng dụng ở cấp độ DNS
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Đọc thêm
Quét để tải xuống
Sử dụng bất kỳ trình đọc mã QR nào có sẵn trên thiết bị của bạn.
AdGuard cho iOS phiên bản 4.5
Màn hình chính của AdGuard Content Blocker
Màn hình Bộ lọc của AdGuard Content Blocker
Màn hình cài đặt của AdGuard Content Blocker
20.687 20687 đánh giá của người dùng
Tuyệt vời!

Trình Chặn Nội Dung AdGuard

AdGuard Content Blocker loại bỏ tất cả các loại quảng cáo trên các trình duyệt di động hỗ trợ công nghệ chặn nội dung — cụ thể là Samsung Internet và Yandex Browser. Tính năng của nó bị hạn chế so với AdGuard cho Android, nhưng miễn phí, dễ cài đặt và hiệu quả
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Đọc thêm
Trình Chặn Nội Dung AdGuard phiên bản 2.8
Màn hình chính của AdGuard Browser Extension
Màn hình Bảo vệ theo dõi của AdGuard Browser Extension
20.687 20687 đánh giá của người dùng
Tuyệt vời!

Tiện ích mở rộng Trình duyệt AdGuard

AdGuard là tiện ích mở rộng chặn quảng cáo nhanh nhất và nhẹ nhất chặn hiệu quả tất cả các loại quảng cáo trên tất cả các trang web! Chọn AdGuard cho trình duyệt bạn sử dụng và nhận duyệt web không có quảng cáo, nhanh chóng và an toàn.
Cài đặt
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Cài đặt
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Cài đặt
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Cài đặt
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Cài đặt
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Đọc thêm
Cài đặt
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Đọc thêm
Tiện ích mở rộng Trình duyệt AdGuard phiên bản 5.5
Màn hình chính của AdGuard Assistant
20.687 20687 đánh giá của người dùng
Tuyệt vời!

Trợ lý AdGuard

Một tiện ích mở rộng trình duyệt đồng hành cho ứng dụng AdGuard trên máy tính. Nó cho phép bạn chặn các mục tùy chỉnh trên websites, thêm websites vào danh sách cho phép và gửi báo cáo trực tiếp từ trình duyệt của bạn
Trợ lý AdGuard phiên bản 1.4
20.687 20687 đánh giá của người dùng
Tuyệt vời!

Trang chủ AdGuard

AdGuard Home là một giải pháp dựa trên mạng để chặn quảng cáo và trình theo dõi. Cài đặt nó một lần trên bộ định tuyến của bạn để bao phủ tất cả các thiết bị trên mạng gia đình của bạn — không cần phần mềm máy khách bổ sung. Điều này đặc biệt quan trọng đối với các thiết bị IoT khác nhau thường gây ra mối đe dọa đối với quyền riêng tư của bạn
Trang chủ AdGuard phiên bản 0.107
Màn hình chính của AdGuard Pro cho iOS
Màn hình Bảo vệ của AdGuard Pro cho iOS, hiển thị các tính năng và cài đặt sự bảo vệ
Màn hình Thống kê của AdGuard Pro cho iOS, hiển thị số liệu về quảng cáo và trình theo dõi đã bị chặn
20.687 20687 đánh giá của người dùng
Tuyệt vời!

AdGuard Pro cho iOS

AdGuard Pro cho iOS được trang bị tất cả các tính năng bảo vệ chặn quảng cáo nâng cao. Nó cung cấp các công cụ tương tự như phiên bản trả phí của AdGuard cho iOS. Nó nổi trội trong việc chặn quảng cáo trên Safari và cho phép bạn tùy chỉnh cài đặt DNS để bảo vệ tối ưu. Nó chặn quảng cáo trong trình duyệt và ứng dụng, bảo vệ con bạn khỏi nội dung không phù hợp và giữ an toàn cho dữ liệu cá nhân của bạn.
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Đọc thêm
AdGuard Pro cho iOS phiên bản 4.5
Màn hình chính của AdGuard Mini cho Mac
Màn hình sự bảo vệ Safari của AdGuard Mini cho Mac
Màn hình tạo quy tắc của AdGuard Mini cho Mac
20.687 20687 đánh giá của người dùng
Tuyệt vời!

AdGuard Mini cho Mac — trình chặn quảng cáo Safari

AdGuard Mini cho Mac là một trình chặn quảng cáo Safari mạnh mẽ. Ứng dụng nhẹ này loại bỏ quảng cáo, chặn các trình theo dõi, và tăng tốc tải trang. Nó giúp bạn trình duyệt trang web trong Safari mà không bị làm phiền và giữ dữ liệu của bạn được riêng tư
Cài đặt
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Đọc thêm
AdGuard Mini cho Mac phiên bản 2.3
Màn hình chính của AdGuard cho Android TV với sự bảo vệ được bật
Màn hình chặn quảng cáo của AdGuard cho Android TV, hiển thị các tính năng và cài đặt của nó
Màn hình cài đặt của AdGuard cho Android TV
Màn hình Quản lý ứng dụng của AdGuard cho Android TV, hiển thị các ứng dụng nơi quảng cáo và trình theo dõi Đã bị chặn
20.687 20687 đánh giá của người dùng
Tuyệt vời!

AdGuard cho Android TV

AdGuard cho Android TV là ứng dụng duy nhất chặn quảng cáo, bảo vệ quyền riêng tư của bạn và hoạt động như một tường lửa cho Smart TV của bạn. Nhận cảnh báo về các mối đe dọa trên web, sử dụng DNS an toàn và hưởng lợi từ lưu lượng truy cập được mã hóa. Thư giãn và đi sâu vào các chương trình yêu thích của bạn với bảo mật hàng đầu và không có quảng cáo!
AdGuard cho Android TV v4.14, thời gian dùng thử 14 ngày
Linh vật AdGuard, Agnar, đang cầm chú chim cánh cụt Linux
20.687 20687 đánh giá của người dùng
Tuyệt vời!

AdGuard cho Linux

AdGuard cho Linux là trình chặn quảng cáo Linux trên toàn hệ thống đầu tiên trên thế giới. Chặn quảng cáo và trình theo dõi ở cấp độ thiết bị, chọn từ các bộ lọc được cài đặt sẵn hoặc thêm bộ lọc của riêng bạn — tất cả thông qua giao diện dòng lệnh
AdGuard cho Linux phiên bản 1.4
20.687 20687 đánh giá của người dùng
Tuyệt vời!

AdGuard Temp Mail

Trình tạo địa chỉ email tạm thời miễn phí giúp bạn ẩn danh và bảo vệ quyền riêng tư của bạn. Không có thư rác trong hộp thư đến chính của bạn!
20.687 20687 đánh giá của người dùng
Tuyệt vời!

AdGuard VPN

75 Vị Trí Trên Thế Giới

Truy cập mọi thông tin

Mã hoá mạnh

Không nhật ký

Đường truyền nhanh nhất

Hỗ trợ 24/7

Dùng thử miễn phí
Bằng cách tải xuống chương trình, bạn chấp nhận các điều khoản của Thỏa thuận cấp phép
Đọc thêm
20.687 20687 đánh giá của người dùng
Tuyệt vời!

AdGuard DNS

AdGuard DNS là một cách tuyệt vời để chặn quảng cáo Internet không yêu cầu cài đặt bất kỳ ứng dụng nào. Nó rất dễ sử dụng, hoàn toàn miễn phí, dễ dàng thiết lập trên bất kỳ thiết bị nào và cung cấp cho bạn các chức năng cần thiết tối thiểu để chặn quảng cáo, quầy, trang web độc hại và nội dung người lớn.
20.687 20687 đánh giá của người dùng
Tuyệt vời!

AdGuard Mail

Bảo vệ danh tính của bạn, tránh thư rác và giữ an toàn cho hộp thư đến thông qua các bí danh và Địa chỉ email tạm thời của chúng tôi. Trải nghiệm dịch vụ chuyển tiếp Email miễn phí và ứng dụng cho mọi hệ điều hành.
20.687 20687 đánh giá của người dùng
Tuyệt vời!

AdGuard Wallet

Ví tiền điện tử an toàn và riêng tư giúp bạn kiểm soát hoàn toàn tài sản của mình. Quản lý nhiều ví và khám phá hàng nghìn loại tiền điện tử để lưu trữ, gửi và trao đổi
Việc tải xuống AdGuard đã bắt đầu Nhấp vào nút có hình mũi tên để bắt đầu cài đặt. Chọn "Mở" và nhấp vào "OK", sau đó đợi tệp được tải xuống. Trong cửa sổ đã mở, kéo biểu tượng AdGuard vào thư mục "Ứng dụng". Cảm ơn bạn đã chọn AdGuard! Chọn "Mở" và nhấp vào "OK", sau đó đợi tệp được tải xuống. Trong cửa sổ mở, nhấp vào "Cài đặt". Cảm ơn bạn đã chọn AdGuard!
Cài đặt AdGuard cho cả thiết bị di động