Vulnerability: XSS in mdBook > This is a cross-post of [the official security advisory][ml]. The official post contains a signed version with our PGP key, as well.

[ml]: https://groups.google.com/g/rustlang-security-announcements/c/3-sO6of29O0

The Rust Security Response Working Group was recently notified of a security issue affecting the search feature of mdBook, which could allow an attacker to execute arbitrary JavaScript code on the page.

The CVE for this vulnerability is [CVE-2020-26297](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-26297).

## Overview

The search feature of mdBook (introduced in version 0.1.4) was affected by a cross site scripting vulnerability that allowed an attacker to execute arbitrary JavaScript code on an user's browser by tricking the user into typing a malicious search query, or tricking the user into clicking a link to the search page with the malicious search query prefilled.

mdBook 0.4.5 fixes the vulnerability by properly escaping the search query.

## Mitigations

Owners of websites built with mdBook have to upgrade to mdBook 0.4.5 or greater and rebuild their website contents with it. It's possible to install mdBook 0.4.5 on the local system with:

```
cargo install mdbook --version 0.4.5 --force
```

## Acknowledgements

Thanks to Kamil Vavra for responsibly disclosing the vulnerability to us according to [our security policy](https://www.rust-lang.org/policies/security).

## Timeline of events

All times are listed in UTC.

- 2020-12-30 20:14 - The issue is reported to the Rust Security Response WG
- 2020-12-30 20:32 - The issue is acknowledged and the investigation began
- 2020-12-30 21:21 - Found the cause of the vulnerability and prepared the patch
- 2021-01-04 15:00 - Patched version released and vulnerability disclosed
Vulnerability ID: GHSA-gx5w-rrhp-f436
CWE: CWE-79
Severity score: 8.2
Project: https://github.com/rust-lang/mdBook
Published: 2021-08-25
File: src/theme/searcher/searcher.js
Commit message: fix xss in the search page

Thanks to Kamil Vavra for responsibly disclosing the vulnerability
according to Rust's Security Policy.
Commit author: Pietro Albini

Vulnerable version (before fix):
 window.search = window.search || {};             url.push("");         }          return '<a href="' + path_to_root + url[0] + '?' + URL_MARK_PARAM + '=' + searchterms + '#' + url[1]             + '" aria-details="teaser_' + teaser_count + '">' + result.doc.breadcrumbs + '</a>'             + '<span class="teaser" id="teaser_' + teaser_count + '" aria-label="Search Result Teaser">' 

Fixed version (after fix):
 window.search = window.search || {};             url.push("");         }          // encodeURIComponent escapes all chars that could allow an XSS except         // for '. Due to that we also manually replace ' with its url-encoded         // representation (%27).         var searchterms = encodeURIComponent(searchterms.join(" ")).replace(/\'/g, "%27");          return '<a href="' + path_to_root + url[0] + '?' + URL_MARK_PARAM + '=' + searchterms + '#' + url[1]             + '" aria-details="teaser_' + teaser_count + '">' + result.doc.breadcrumbs + '</a>'             + '<span class="teaser" id="teaser_' + teaser_count + '" aria-label="Search Result Teaser">' 

Patch:
patch: @@ -145,6 +145,11 @@ window.search = window.search || {};
             url.push("");
         }
 
+        // encodeURIComponent escapes all chars that could allow an XSS except
+        // for '. Due to that we also manually replace ' with its url-encoded
+        // representation (%27).
+        var searchterms = encodeURIComponent(searchterms.join(" ")).replace(/\'/g, "%27");
+
         return '<a href="' + path_to_root + url[0] + '?' + URL_MARK_PARAM + '=' + searchterms + '#' + url[1]
             + '" aria-details="teaser_' + teaser_count + '">' + result.doc.breadcrumbs + '</a>'
             + '<span class="teaser" id="teaser_' + teaser_count + '" aria-label="Search Result Teaser">'
path: src/theme/searcher/searcher.js