SEO and Security: MitM Attacks via Canonical Tag

In its own way this is kind of cool, but it's definitely a warning for all SEOs to check that some enterprising developer hasn't potentially caused a real issue while trying to complete a requirement you may have raised.

A hypothetical: Imagine you're building a new site, or migrating one, dealing with a new CMS, whatever.

You're a smart SEO, so you tell your dev that you want all pages to have a canonical tag that points to itself; ie site.com/page should canonicalise to site.com/page.

Cool, fine.

Except!

What happens to site.com/page?q=query?

Well, if this url canonicalises to the parameterised version, you're in a sticky situation from an SEO point of view, but do you ever think about the nefarious things that can be done now you've given users the ability to control what is rendered by the browser?

Let me explain: the canonical tag for the above url would read like this:

<link rel="canonical" href="https://site.com/page?q=query">

So, here's the question - what happens if the url that was passed was site.com/page?"><script>document.write("SEND ME SOME MONEY PLEASE")</script>?

And here's the thing: Usually, nothing. Cloudflare will often put a very quick stop to this, as will most other security suites, but it is always worth checking what happens before it bites you in the arse, because here's what could happen:

What could happen is the above url is used to populate the canonical tag with absolutely zero sanitisation, and now the canonical tag looks like this:

<link rel="canonical" href="https://site.com/page?"><script>document.write("SEND ME SOME MONEY PLEASE")</script>">

The Problem

The problem here may not be immediately apparent, but what we now have is a terminated canonical tag and a script tag that is running JS in the user's browser - JS that you didn't write.

This means that, an unscrupulous individual could use JS to rewrite the page that the user sees, or change the link in your leadgen form to point to a competitor, or a phishing site, or a number of other horrible things that can result in you losing business, having a PR crisis, or maybe getting sued? I dunno I'm not a lawyer.

This is especially problematic because that JS is essentially shareable via a link, meaning anyone who clicks on a link to the above url is going to have that JS run and almost definitely not realise - and yes I know what you're thinking: you're thinking "yeah but who's going to fall for that" - to which I say imagine that url was hidden behind a link shortener, and also included a load of legitimate-looking tracking parameters and the like, and now the url was long enough that the offending parameter wasn't visible in your address bar.

Or, imagine you're my mum, who does not so much surf the information superhighway as bounce along the barriers, each collision resulting in another badly screenshotted FB photo that she then prints and hangs on the wall.

The point is: This can be easy to spot, but if you're not looking or some attempt has been made to hide it, then it can also be really easy to miss. But hey, it's only one user, right? Or maybe a few?

Well, imagine that injected JS wasn't used to steal credit cards or link to competitors - what if it was used to redirect to a competitor site, and then the badguys flooded the internet with links to those compromised urls?

We know that Google will follow JS redirects if you're persistent enough, and while I'm fairly certain this wouldn't totally remove your site from the SERPs, a prolonged enough campaign would be enough to cause some damage for little-to-no cost, something that will hurt your bottom line month after month until you realise and do something about it.

The End

I'm going to be honest here, the likelihood of all this is probably pretty low - it's longshot after longshot, but so are most security vulnerabilities and it only needs to work once in order to potentially cause real issues for a business - and given that testing this is so quick (Just try appending q=<script>hello</script> to the end of any url on your site and assuming the page returns a 200, check out what the canonical tag is), you might as well eh? Save yourself a little disaster and also feel really smart in the process.

Oh, and if this DOES affect a site you work on: Don't tell me on Twitter. Tell your dev team.