How I Bypassed Facebook CSRF in 2016
Facebook's Lead Ads Continued Flow feature automatically added the user's anti-CSRF token (fb_dtsg) to POST requests sent to a destination URL, and the whitelist restricting the feature could be bypassed by adding extra fields to the form-creation JSON. An attacker could create a malicious Lead Ad form that sent authenticated POST requests with the victim's token to Facebook endpoints, bypassing CSRF protection.
Published on blog.darabi.me
I discovered a vulnerability in Facebook Lead Ads that allowed me to create an arbitrary form capable of sending a POST request containing Facebook's anti-CSRF token to Facebook endpoints or external hosts.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Facebook's Lead Ads product lets advertisers build forms that collect information from users inside Facebook. The Continued Flow feature extends that form onto the advertiser's own site: the final step is completed there, and Facebook takes the data the user submitted and delivers it to a destination URL, either through a hash or through a POST request. The bug I found lived in that delivery step. When Continued Flow sent its POST, Facebook automatically attached the user's fb_dtsg anti-CSRF token to the request. If the destination could be influenced, the feature turned into a machine for producing authenticated POST requests carrying a valid token.
| Product | Facebook Lead Ads | | Feature | Continued Flow | | Issue | CSRF protection bypass / insufficient server-side validation | | Impact | Send authenticated POST requests with the victim's Facebook CSRF token | | Bounty | $7,500 |
Context
Facebook used the fb_dtsg parameter as an anti-CSRF token for many state-changing requests. For protected actions the token had to be present in the request and was validated server-side; without a valid token the request should be rejected. The question that drove this research was therefore simple: could any Facebook functionality be abused to generate and submit requests that already contain a valid fb_dtsg on behalf of the user?
Continued Flow was a good candidate. A Continued Flow Lead Ad allowed the final step of the form to be completed on the advertiser's website. Facebook collected the user's submitted data and sent it to a destination URL using either a hash or a POST request. The interesting part was the POST implementation: when Continued Flow submitted the request, Facebook added the user's fb_dtsg token automatically.
Setup
The research was done against Facebook's Lead Ads Continued Flow feature with an account that was not whitelisted for Continued Flow. According to Facebook's documentation, Continued Flow was only available to whitelisted users, so the whitelist was the obstacle to get past.. The testing of the resulting Lead Ad was done with Facebook's own Lead Ads testing tool. No other tooling is described in the report.
Finding it
The starting observation was the automatic token injection. If the destination of the Continued Flow request could be controlled, the feature effectively became a way to generate authenticated POST requests containing a valid Facebook CSRF token. An attacker-controlled Lead Ad form could then potentially submit requests directly to Facebook endpoints while carrying the victim's own fb_dtsg.
The obstacle was the whitelist. While reviewing how Lead Ad forms were created, I noticed that Facebook sent the form configuration as a JSON object to the form-creation endpoint. I then found another endpoint that returned previously created Lead Ad forms as JSON. Comparing the returned structure revealed additional fields related to Continued Flow.
Exploitation
Step 1: Add the Continued Flow fields to the form JSON
I manually added the Continued Flow fields to the normal form-builder JSON and changed their values. The server accepted the modified structure and created a Continued Flow form even though my account was not whitelisted for the feature. There was no effective server-side validation enforcing the whitelist restriction.
Step 2: Choose a state-changing target
As a proof of concept I used the issue against Facebook's Timeline Review setting.
Target endpoint:
https://facebook.com/ajax/settings/timeline/review.php
Request body:
tag_approval_enabled=0
The body sets tag approval to off. Because the request is a POST to a state-changing endpoint, it is exactly the kind of action fb_dtsg is meant to protect.
Step 3: Build the destination URL
The resulting destination could be constructed as:
https://facebook.com/ajax/settings/timeline/review.php?tag_approval_enabled=0&__a=1
This is the URL handed to Continued Flow as the destination. The parameters carry the same setting change, and __a=1 is appended to the URL.
Step 4: Run the form
I tested the resulting Lead Ad using Facebook's own Lead Ads testing tool, and the request was successfully executed.
Root cause
Two design decisions combined. First, Continued Flow's POST delivery attached the user's fb_dtsg to a request whose destination was derived from advertiser-controlled form configuration. Facebook generated the request and added the user's token to it.
Second, the Continued Flow whitelist was not enforced server-side. The form-creation endpoint accepted a JSON structure containing Continued Flow fields from an account that was not whitelisted, so the restriction that was supposed to limit who could use the feature had no effect on the server. The report does not describe any further validation of the destination beyond what is stated above.
Impact
An attacker who can get a victim to submit a Continued Flow Lead Ad form can cause Facebook to send a POST request to a destination of the attacker's choosing, with the victim's valid fb_dtsg attached. In the demonstrated case the request targeted the Timeline Review setting. More generally, the primitive is an authenticated POST with a valid anti-CSRF token, aimed at Facebook endpoints or external hosts, which is why it is a CSRF bypass rather than a simple form submission.
Resolution
Facebook acknowledged and fixed the primary issue, and later fixed the additional whitelist-related issue as well.
Disclosure timeline
| Date | Event |
| March 29, 2016 | Initial vulnerability reported. |
| April 6, 2016 | Facebook requested additional information. |
| April 6, 2016 | Additional technical details provided. |
| April 7, 2016 | Issue acknowledged by Facebook's security team. |
| April 7, 2016 | Additional fb_dtsg field behaviour reported. |
| April 12, 2016 | Primary vulnerability fixed. |
| April 13, 2016 | $7,500 bounty awarded. |
| April 18, 2016 | Additional details about the whitelist bypass provided. |
| May 6, 2016 | Second issue fixed. |
Variants to test
- Server-side delivery features that sign requests for you. Look for any product that takes a user-supplied destination and POSTs to it on the user's behalf, such as webhook testers, form handlers, link previews or ad delivery flows. A positive result is a request arriving at your listener with a session cookie, CSRF token or other credential attached that the platform added itself.
- Feature flags exposed in object JSON. Compare the JSON returned by a read endpoint with the JSON accepted by the corresponding write endpoint, as was done here with Lead Ad forms. A positive result is a write endpoint that accepts fields for a feature your account is not entitled to, and the feature then working.
- Whitelist checks that only exist in the UI. Try creating the privileged object directly with the fields present in the read representation. A positive result is a successful creation response for a feature that the UI does not offer to your account.
- Token-named input fields. The report notes that a custom Lead Ads field was also tested with its name set to
fb_dtsg. On other targets, try naming a user-controlled field after the anti-CSRF parameter and observe whether it is reflected into a signed request or merged into the token parameter. A positive result is your value appearing where the platform's own token is expected. - Destination allowlists. Where a destination URL is validated, test whether the check applies to the host, the path or the full URL, and whether query parameters survive the check. A positive result is a request reaching a state-changing endpoint on the target's own domain through a destination that passed validation.
What happened, in plain words
Facebook has a product called Lead Ads. Advertisers use it to build little forms that people fill in without leaving Facebook, for example to sign up for a newsletter or ask a salesperson to call them. A feature called Continued Flow lets the advertiser finish the form on their own website instead: the last step happens on the advertiser's site, and Facebook takes whatever the person typed and delivers it to a web address the advertiser chose.
That delivery step is where the bug was. When Continued Flow delivered the data as a POST request, Facebook quietly attached the user's fb_dtsg value to the request. fb_dtsg is the parameter Facebook used as its anti-CSRF token for many state-changing requests. If an attacker could choose where that request went, Facebook would end up building a request to any address the attacker liked, stamped with the victim's own valid token. That is a way around CSRF protection.
I reported it, Facebook fixed it, and the bounty was $7,500.
| Product | Facebook Lead Ads | | Feature | Continued Flow | | Issue | CSRF protection bypass / insufficient server-side validation | | Impact | Send authenticated POST requests with the victim's Facebook CSRF token | | Bounty | $7,500 |
Background: what CSRF and tokens are
Before the details, two ideas need explaining, because the whole bug is about them.
CSRF stands for Cross-Site Request Forgery. Imagine you are logged in to your bank in one browser tab. In another tab you open a page someone sent you. That page contains a hidden form that automatically submits a request to the bank, something like "transfer money to this account". Your browser happily sends it, and because you are logged in, the bank sees a request that looks like it came from you. The attacker never learned your password; they just made your browser send a request for them. That is CSRF: forging a request using the victim's existing login.
Anti-CSRF tokens are the usual defence. When you load a page on the bank's site, the server puts a random secret value into the page, and the page sends it back with every action that changes something. A page on another website cannot read that secret, so it cannot include it in a forged request. The server checks the token and rejects requests that do not carry the right one. A token is like a cloakroom ticket: the cloakroom only hands your coat to someone holding the matching ticket, so a stranger who walks in without one gets nothing.
Facebook used a parameter called fb_dtsg as this token for many requests that change something on your account. For protected actions the token had to be in the request and was checked on Facebook's servers; without a valid token the request should be rejected. So the question that started this research was simple: could any Facebook feature be tricked into building and sending a request that already contains a valid fb_dtsg on the user's behalf?
The feature that made it possible
Continued Flow was a good candidate. A Continued Flow Lead Ad let the final step of the form be completed on the advertiser's website. Facebook collected the data the user submitted and sent it to a destination URL, either as a hash or as a POST request. The interesting part was the POST version: when Continued Flow submitted the request, Facebook added the user's fb_dtsg token automatically.
Think of it as a courier service. You tell Facebook where to deliver a parcel, and Facebook delivers it, but it also stamps the parcel with your personal seal so the receiver knows it came from you. If you can choose the address, you can have your sealed parcel delivered anywhere, and the receiver will trust it because the seal is genuine.
Setting up
I did the research against Facebook's Lead Ads Continued Flow feature using a Facebook account. That account was not whitelisted for Continued Flow. A whitelist is simply a list of accounts that are allowed to use something; if your name is not on the list, the feature is not supposed to be available to you. According to Facebook's documentation, Continued Flow was only for whitelisted users, so the whitelist was the obstacle to get past..
To test the resulting Lead Ad I used Facebook's own Lead Ads testing tool. The report describes no other tooling.
Finding it
The starting observation was the automatic token injection. If the destination of the Continued Flow request could be controlled, the feature effectively became a way to generate authenticated POST requests containing a valid Facebook CSRF token. An attacker-controlled Lead Ad form could then potentially submit requests directly to Facebook endpoints while carrying the victim's own fb_dtsg.
An endpoint is just a specific address on a server that does one job, like a particular counter in a large office: one counter for passports, another for driving licences. A POST request is the way a browser sends data to the server to create or change something, as opposed to simply asking for a page.
The obstacle was the whitelist. While reviewing how Lead Ad forms were created, I noticed that Facebook sent the form configuration as a JSON object to the form-creation endpoint. JSON is a plain text format for structured data, a bit like a very strict shopping list with labels: {"name": "my form", "fields": [...]}. I then found another endpoint that returned previously created Lead Ad forms as JSON. Comparing the returned structure revealed additional fields related to Continued Flow.
In plain terms: the fields existed in the data model and were simply not part of the normal form-builder JSON. The form builder is the web page an advertiser uses to design a form; it only offers certain options, but the server behind it may accept more than the page shows.
Exploitation
Step 1: Add the Continued Flow fields to the form JSON
I manually added the Continued Flow fields to the normal form-builder JSON and changed their values. The server accepted the modified structure and created a Continued Flow form even though my account was not whitelisted for the feature. There was no effective server-side validation enforcing the whitelist restriction.
Server-side validation means the server itself checks what it receives. If a restriction only exists in the web page, it is like a nightclub with a sign saying "members only" but no doorman: anyone who walks past the sign gets in. Here the sign was the form builder, and the server had no doorman.
Step 2: Choose a state-changing target
As a proof of concept I used the issue against Facebook's Timeline Review setting. Timeline Review is a Facebook setting. Turning it off changes something on the account, which is exactly the kind of action the token is meant to protect.
Target endpoint:
https://facebook.com/ajax/settings/timeline/review.php
This line is the address of the Facebook endpoint that changes the Timeline Review setting. It is the "counter" the forged request needs to reach.
Request body:
tag_approval_enabled=0
This is the data sent to that endpoint. tag_approval_enabled is the setting's name, and 0 means off. So the body says: turn tag approval off. Because this is a POST to an endpoint that changes account state, it is exactly the kind of action fb_dtsg exists to protect.
Step 3: Build the destination URL
The resulting destination could be constructed as:
https://facebook.com/ajax/settings/timeline/review.php?tag_approval_enabled=0&__a=1
This is the URL handed to Continued Flow as the destination. The part before the question mark is the same endpoint as above. Everything after the question mark is data carried in the URL itself: tag_approval_enabled=0 carries the same setting change, and __a=1 is appended to the URL. The & simply separates the two values.
Step 4: Run the form
I tested the resulting Lead Ad using Facebook's own Lead Ads testing tool, and the request was successfully executed. That last point matters: the attacker never needs to steal the token, because Facebook adds it for them.
Why it worked
Two design decisions combined.
First, Continued Flow's POST delivery attached the user's fb_dtsg to a request whose destination came from advertiser-controlled form configuration. Facebook generated the request and added the user's token to it. It is like a company that stamps every outgoing letter with the director's signature: the signature is real, but it no longer means the director wrote the letter.
Second, the Continued Flow whitelist was not enforced server-side. The form-creation endpoint accepted a JSON structure containing Continued Flow fields from an account that was not whitelisted, so the restriction meant to limit who could use the feature had no effect on the server. The report does not describe any further validation of the destination beyond what is stated above.
Impact
An attacker who can get a victim to submit a Continued Flow Lead Ad form can cause Facebook to send a POST request to a destination of the attacker's choosing, with the victim's valid fb_dtsg attached. In the demonstrated case the request targeted the Timeline Review setting. More generally, the primitive is an authenticated POST with a valid anti-CSRF token, aimed at Facebook endpoints or external hosts, which is why it is a CSRF bypass rather than a simple form submission.
Resolution
Facebook acknowledged and fixed the primary issue, and later fixed the additional whitelist-related issue as well.
Disclosure timeline
| Date | Event |
| March 29, 2016 | Initial vulnerability reported. |
| April 6, 2016 | Facebook requested additional information. |
| April 6, 2016 | Additional technical details provided. |
| April 7, 2016 | Issue acknowledged by Facebook's security team. |
| April 7, 2016 | Additional fb_dtsg field behaviour reported. |
| April 12, 2016 | Primary vulnerability fixed. |
| April 13, 2016 | $7,500 bounty awarded. |
| April 18, 2016 | Additional details about the whitelist bypass provided. |
| May 6, 2016 | Second issue fixed. |
Things worth testing elsewhere
- Server-side delivery features that sign requests for you. Look for any product that takes a user-supplied destination and POSTs to it on the user's behalf, such as webhook testers, form handlers, link previews or ad delivery flows. A positive result is a request arriving at your listener with a session cookie, CSRF token or other credential attached that the platform added itself.
- Feature flags exposed in object JSON. Compare the JSON returned by a read endpoint with the JSON accepted by the corresponding write endpoint, as was done here with Lead Ad forms. A positive result is a write endpoint that accepts fields for a feature your account is not entitled to, and the feature then working.
- Whitelist checks that only exist in the UI. Try creating the privileged object directly with the fields present in the read representation. A positive result is a successful creation response for a feature that the UI does not offer to your account.
- Token-named input fields. The report notes that a custom Lead Ads field was also tested with its name set to
fb_dtsg. On other targets, try naming a user-controlled field after the anti-CSRF parameter and observe whether it is reflected into a signed request or merged into the token parameter. A positive result is your value appearing where the platform's own token is expected. - Destination allowlists. Where a destination URL is validated, test whether the check applies to the host, the path or the full URL, and whether query parameters survive the check. A positive result is a request reaching a state-changing endpoint on the target's own domain through a destination that passed validation.
Glossary
- API: a set of addresses and rules a program uses to talk to a service, like a menu of things you can ask a server to do.
- Anti-CSRF token: a secret value a site puts into its pages and expects back with actions that change something, so that other websites cannot forge those actions.
- Authenticated request: a request that carries proof of who is making it, such as a logged-in session or a valid token.
- Bounty: money a company pays a researcher for reporting a security bug responsibly.
- Client-side: happening in the user's browser, on the user's machine.
- Continued Flow: a Facebook Lead Ads feature that lets the last step of a lead form be completed on the advertiser's website.
- CSRF (Cross-Site Request Forgery): tricking a logged-in user's browser into sending a request the user did not intend, using their existing login.
- CSRF bypass: a way around the protection that is supposed to stop CSRF.
- Endpoint: a specific address on a server that performs one particular job.
fb_dtsg: the name of the parameter Facebook used as its anti-CSRF token for many state-changing requests.- Feature flag: a setting that switches a feature on or off for an account or group.
- Hash: here, a way of delivering data to a destination using the fragment part of a URL, the part after
#. - JSON: a plain text format for structured data, made of labels and values.
- Lead Ads: a Facebook advertising product for building forms that collect information from users inside Facebook.
- POST request: a request that sends data to a server to create or change something.
- Primitive: a basic building block an attacker can reuse; here, the ability to make Facebook send a signed request anywhere.
- Proof of concept: a small, safe demonstration that a bug is real.
- Server-side validation: checks the server itself performs on incoming data, which a user cannot skip by editing a web page.
- Session: the state of being logged in, kept alive by the browser and the server.
- State-changing request: a request that alters something, such as a setting or an account, rather than just reading a page.
- Timeline Review: a Facebook setting that asks you before posts you are tagged in appear on your timeline.
- UI (user interface): the pages and buttons a person sees and uses.
- URL: the address of something on the web.
- Webhook: a web address one service calls to deliver data to another service automatically.
- Whitelist: a list of accounts or items that are allowed to use something.