banner



How To Set Referrer Policy

Equally web developers, we should care about the privacy of our users. This article explains what the Referer header is and what information information technology exposes. We besides learn to employ the Referrer-Policy to control how many details the referer header should include. We can increase privacy and deal with some potential security issues past doing all of the higher up.

The Referer header

When we visit a website, it tin contain a clickable link that redirects us to a dissimilar folio. When we click on it, the browser might transport a Referer request header to the server of the page we request. It contains the website'south accost that includes the link we've used.

The word referer is a misspeling of the wordreferrer.

To discover this, let'south visit the wanago_io page on Twitter. It contains a bunch of links that lead to wanago.io.

When nosotros click on the above, we get redirected to https://t.co/QtA0ttrRn3. The https//t.co address is a domain Twitter uses for shortening links. Allow's investigate its response:

To a higher place, we can see that when nosotros open https://t.co/QtA0ttrRn3, it immediately redirects us to wanago.io.

Thanks to the browser sending the header when requesting wanago.io, I know that the person that visited the blog came from Twitter.

Security concerns around referer

Unfortunately, the Referer header carries some privacy and security risks. Information technology is fine every bit long as the Referer information is used for logging or analytics. Withal, the Referer mechanism tin can be driveling to track or steal sensitive information.

Imagine having the functionality of resetting a password. When the users request a new countersign, they get an electronic mail with a link such every bit this i:

https://your-website/reset-countersign?token=secret_token

When the users visit the to a higher place URL, they might click on a link earlier finalizing setting the new password. When that happens, some other site might get the total URL along with the secret token that we use to authorize changing the password.

Dealing with the security bug using Referrer-Policy

Thankfully, there are ways to control what information the browser includes in the referer header. I of them is using the Referrer-Policy header.

Fifty-fifty though the Referer header contains the misspelled give-and-takereferrer, the Referrer-Policy is spelled correctly.

There are quite a few values to choose from when setting up the Referrer-Policy.

strict-origin-when-cross-origin

The value is a default referrer policy. Information technology ways that a browser is free to transport the origin, path, and the query cord in the Referer header when making a same-origin asking:

  • the user visits https://wanago.io/courses/api-with-nestjs/
  • clicks on a link leading to https://wanago.io/2020/05/11/nestjs-api-controllers-routing-module/
  • the request contains the .

An important affair to realize is that the browser omits some parts of the URL when sending the Referer header, even in the higher up case. A good instance is the fragment, which is the role of the URL later on the sign. The to a higher place is yet another consequence of using hash-based routing. Check out Comparison the HashRouter and the BrowserRouter in React applications for more.

If yous want to know more well-nigh the parts of the URL that the browser leaves out when sending the Referer header, check out the documentation.

For the cross-origin requests, the browser sends only the page's origin. The above means that the asking would contain only the value.

An of import caveat is that the above would merely happen if the security protocol stays the same. For instance, if the user navigates from an HTTPS page to an HTTP website, the browser does not send the Referer header.

Nosotros should avoid including sensitive information in the URL. However, if nosotros have to exercise that, the setting ensures that the user won't leak it outside of our website through the Referer header.

no-referrer

If nosotros prepare the Referrer-Policy to , the browser does non send the Referer header.

The helmet library sets the value for the Referrer-Policy by default. If y'all want to know more than about helmet, cheque out Increasing security of Express applications with the Helmet middleware.

unsafe-url

When we set the Referrer-Policy to , the browser sends the origin, path, and the query string regardless of the origin or the security protocol. Unfortunately, using this arroyo might cause a leak of private information, such as tokens.

no-referrer-when-downgrade

With the value, we send the origin, path, and the query string in the Referer header if the security protocol stays the same or improves. For example, if an HTTPS website redirects the user to an HTTP page, the browser doesn't send the Referer header.

origin

If we set the Referrer-Policy to , the browser sends simply the origin in the Referer header.

same-origin

When using the pick, the browser sends the origin, path, and query string but for the aforementioned-origin requests.

strict-origin

With the setting, the browser sends the origin through the Referer header when the security level stays the same.

origin-when-cross-origin

By setting the value, the browser sends the origin, path, and the query cord through the Referer header for same-origin requests. On the other hand, the browser sends only the origin for cross-origin requests and when downgrading the security protocol.

Which option to choose?

After reading all of the above, we can conclude that the default setting deals with most of the security concerns. However, if we want to increase the privacy of our users, we can consider setting the Referrer-Policy to .

Setting the Referrer-Policy

If we serve our content using Node.js and Express, setting the Referrer-Policy is very straightforward.

As well doing it manually, like in the above snippet, we can also apply the helmet library. It tin can ready the Referrer-Policy header for united states of america, besides other headers. If you want to know more, cheque out the Increasing security of Express applications with the Helmet middleware.

Nosotros can also modify the Referrer-Policy through HTML. One way to do that is using the element.

Setting the policy directly on elements

Besides using , we can likewise utilize the attribute with , , , , and tags.

As an alternative, we tin also use on , , and elements.

Summary

In this article, nosotros've gone through the idea behind the Referer header. We've also learned what issues it might cause and how to deal with them. To do that, we've used the Referrer-Policy header and learned how to set it with Node.js and Express. The default referrer policy value would prevent the nearly significant problems, such as leaking sensitive data. Fifty-fifty though that'due south the case, nosotros can still increase our users' privacy by turning off the Referer header. We also need to watch out and not set the Referrer-Policy to a value that might hurt our users.

Previous article
The danger of iframe clickjacking and how to deal with it

Adjacent article
JavaScript testing #nine. Replacing Enzyme with React Testing Library

Source: https://wanago.io/2022/03/28/referer-header-referrer-policy/

0 Response to "How To Set Referrer Policy"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel