Flickr API Explorer – Force users to execute any API request.
The Flickr API Explorer did not validate the CSRF token (magic_cookie) on requests that execute API calls as the logged-in user with full permissions. An attacker could force a logged-in user to perform state-changing API actions such as posting comments, adding photos to sets, or joining groups.
Published on buer.haus
Flickr has a developer application section called The App Garden. Developers are able to create apps that make API calls to Flickr as an authenticated user via OAuth.
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.
Context
Flickr runs a developer section called The App Garden. Developers register applications there and make API calls to Flickr on behalf of an authenticated user through OAuth. Part of the App Garden is the API Explorer, a feature that lets a developer fill in arguments for an API call in a form and test it. When submitting a call, the developer can choose to sign it as the currently logged-in account with full permissions.
The API Explorer covers a large surface. The report points to a search for site:flickr.com/services/api/explore/ returning about 445 results, and lists examples of state-changing calls available through it:
- Add, modifying, and deleting comments.
- Add and removing photosets, including photos inside of photosets.
- Add and removing galleries, including photos inside of galleries.
- Adding people and tags to photos.
- Posting photos to blogs.
- Joining/leaving groups.
- Setting a photo as a "favorite."
- etc.
The researcher found a Cross-Site Request Forgery (CSRF) attack vector that allowed attacking any user on Flickr.
Setup
The precondition is a victim who is logged into Flickr and who loads attacker-controlled content in the same browser. No special tooling is described. The researcher worked with the API Explorer forms and inspected the requests the Explorer itself makes, as opposed to the requests a normal application would make.
Finding it
When an API call is made through the API Explorer, the Explorer sends the API request as a POST inside an iframe, with everything needed to execute it. The endpoint for that POST differs from the one an application would normally use. The report stresses that the following are the requests sent inside the iframe, not the request sent while using the Explorer.
Normal usage:
https://api.flickr.com/services/rest/?method=flickr.photos.comments.addComment&[redacted]&photo_id=14369938517&comment_text=test&format=php_serial&auth_token=[redacted]&api_sig=[redacted]
API Explorer:
https://www.flickr.com/services/api/render?method=flickr.photos.comments.addComment&[redacted]&photo_id=14369938517&comment_text=test&format=json&nojsoncallback=1&auth_token=[redacted]&api_sig=[redacted]
The render endpoint uses syntax highlighting to "beautify" the return to help the developer understand it.
An API call needs API_Key, Auth_Token and Api_Sig. Removing them produces the appropriate errors saying the token, key or signature is invalid or missing. That is how the developer API works and it is working as intended.
The researcher then looked at the request the API Explorer itself makes:
Two request variables matter. sign_call selects what level of permission the API call should have, and the value full means the request is sent as the current logged-in user with full permission. magic_cookie (referred to in the report as the csrftoken) carries a random hash value. That is Flickr's CSRF protection, intended to prevent API Explorer requests from being forced on unsuspecting users: the idea is that these requests should not be sendable without a valid csrftoken.
The researcher's reasoning was that sites typically use a csrftoken blacklist rather than a whitelist. With a blacklist, requests do not require csrftoken validation unless they are specifically listed, so requests execute without validation wherever the list is incomplete; with a whitelist they would not execute at all. The conclusion was that the API Explorer requests did not actually validate or require the magic_cookie.
Exploitation
Step 1: Build a self-submitting form
Without CSRF protection, a POST form can be created that submits automatically. Because the API call executes inside an iframe, the attacker cannot retrieve information from the API call, so the exploit is limited to state-changing API requests.
The following code, hidden on a website the victim loads while logged into Flickr, forces the victim to comment on the param_photo_id specified:
<form method="POST" action="https://www.flickr.com/services/api/explore/flickr.photos.comments.addComment" id="csrf">
<input type="hidden" name="method" value="flickr.photos.comments.addComment" />
<input type="hidden" name="magic_cookie" value="" />
<input type="hidden" name="enable_photo_id" value="on" />
<input type="hidden" name="param_photo_id" value="14369938517" />
<input type="hidden" name="enable_comment_text" value="on" />
<input type="hidden" name="param_comment_text" value="test csrf 123" />
<input type="hidden" name="format" value="rest" />
<input type="hidden" name="sign_call" value="full" />
<input type="submit" value="Submit" />
</form>
<script>
document.getElementById("csrf").submit();
</script>
The form posts to the API Explorer endpoint for flickr.photos.comments.addComment. magic_cookie is present but empty, which is enough because the value is not validated. enable_photo_id and enable_comment_text switch on the corresponding parameters, whose values are carried in param_photo_id and param_comment_text. sign_call set to full makes the call run as the logged-in victim with full permission. The script submits the form without any interaction.
Step 2: The victim loads the page
When the victim loads the page containing the form while authenticated to Flickr, the browser posts the form and the comment is added under the victim's account. The same construction works for any other state-changing API call exposed in the Explorer, such as the photoset, gallery, tag, blog, group and favourite operations listed above.
Root cause
The API Explorer accepted requests without validating or requiring the magic_cookie csrftoken. The likely cause is that CSRF validation was applied as a blacklist rather than a whitelist: requests were only checked when specifically listed, so the Explorer's POST requests fell outside the check. The existing defence, the random magic_cookie hash, therefore did not stop a forged request that simply omitted a valid value or sent an empty one. The report notes that letting developers test API calls, with so much filled in for them, circumvented some of Flickr's API security measures.
Impact
An attacker who gets a logged-in Flickr user to load a page under the attacker's control can force that user to execute any state-changing API request available in the API Explorer, with full permissions: commenting, editing photosets and galleries, adding people and tags, posting to blogs, joining and leaving groups, favouriting photos, and more. The attack works against any user on Flickr. Because the call runs inside an iframe, the attacker cannot read the response, so the exploit is limited to state-changing requests and cannot be used to exfiltrate information through the API call itself.
Fix
The report gives no detail on the change made; it records that the fix was validated on 7/14/2014, after the report on 7/1/2014. The lessons it draws are:
- Move CSRF protection to a whitelist so that all state-changing/POST requests are covered by default. (The report words this as moving to a blacklist, but the safer default is that every request is checked unless explicitly exempted.)
- Add logging for all failed csrftoken requests and monitor for spikes to discover broken requests before users report them.
- If developers are allowed to test API calls, be careful about how much information is filled in for them.
Variants to test
When testing a site, locate the CSRF protection as soon as possible. All state-changing requests should be POST and covered by CSRF protection. Validate the protection by trying each of the following:
- Remove the csrftoken request variable completely. A positive result is that the request still executes; the site may only validate the token when it is present.
- Put an invalid csrftoken value, for example
&csrftoken=asdf. This is the fastest way to see what happens with a valid versus an invalid token. A positive result is that the action succeeds with the invalid value. - Keep the csrftoken variable present but do not set a value, for example
&csrftoken=. A positive result is that the action succeeds with an empty value. - Try old csrftoken values that should have expired. A positive result is that a stale token is still accepted.
- See if a valid csrftoken for one user is valid for other users. A positive result is that a token issued to one account authorises actions on another.
Even on a site with thousands of requests, always test the csrftoken: protection on the first 1000 requests you discovered does not mean the 1001st is protected.
Timeline and bounty
- Reported on: 7/1/2014
- Validated the fix: 7/14/2014
- Bounty reward: $100
What happened, in plain words
Flickr is a website where people store and share photos. Besides the normal website that you and I click around in, Flickr offers a separate area for programmers, called The App Garden. There, people who write software can register small programs (called apps) that talk to Flickr on a user's behalf. To do that, those programs use something called an API.
An API (Application Programming Interface) is a way for one piece of software to ask another piece of software to do something, without a human clicking buttons. A useful everyday comparison: think of a restaurant. You do not walk into the kitchen and cook; you tell the waiter what you want, the waiter takes your order to the kitchen, and the kitchen sends back a dish. The API is the waiter: a fixed set of requests you are allowed to make, and fixed kinds of answers you get back. Flickr's API lets a program say things like "add a comment to this photo" or "add this photo to a gallery".
Part of the App Garden is a feature called the API Explorer. It is a testing playground: a programmer fills in a form with the arguments for an API call, presses a button, and sees what Flickr answers. When submitting a call, the programmer can choose to sign it as the currently logged-in account with full permissions. In other words, the Explorer will act as if the logged-in person had asked for it, with all their rights.
The API Explorer covers a large surface. The report points to a web search for site:flickr.com/services/api/explore/ that returned about 445 results, and lists examples of calls available through it that change something on the account (these are called state-changing calls, because they alter data rather than just read it):
- Add, modifying, and deleting comments.
- Add and removing photosets, including photos inside of photosets.
- Add and removing galleries, including photos inside of galleries.
- Adding people and tags to photos.
- Posting photos to blogs.
- Joining/leaving groups.
- Setting a photo as a "favorite."
- etc.
The researcher found a Cross-Site Request Forgery (CSRF) attack vector that allowed attacking any user on Flickr. CSRF is a trick where an attacker makes your own browser send a request to a website you are already logged into, without you meaning to. Your browser automatically attaches your login (usually a cookie), so the website thinks the request came from you. A comparison: imagine a bank that accepts written instructions, and anyone who slips a filled-in form under your door and gets you to post it for them can move your money, because the bank only checks the signature at the bottom, which is yours.
Setup
The precondition is a victim who is logged into Flickr and who loads attacker-controlled content in the same browser. No special tooling is described. The researcher worked with the API Explorer forms and inspected the requests the Explorer itself makes, as opposed to the requests a normal application would make.
A cookie, in this context, is a small piece of data your browser stores for a website and sends back with every request to that site. It is how a website remembers that you are logged in. A token is similar: a value that proves something, here that a request is legitimate and comes from the right place.
Finding it
When an API call is made through the API Explorer, the Explorer sends the API request as a POST inside an iframe, with everything needed to execute it. POST is one of the ways a browser can send data to a server (the other common one is GET, which puts data in the address bar). An iframe is a small window embedded inside a web page, showing another page. The endpoint (the address the request is sent to) for that POST differs from the one an application would normally use. The report stresses that the following are the requests sent inside the iframe, not the request sent while using the Explorer.
Normal usage:
https://api.flickr.com/services/rest/?method=flickr.photos.comments.addComment&[redacted]&photo_id=14369938517&comment_text=test&format=php_serial&auth_token=[redacted]&api_sig=[redacted]
API Explorer:
https://www.flickr.com/services/api/render?method=flickr.photos.comments.addComment&[redacted]&photo_id=14369938517&comment_text=test&format=json&nojsoncallback=1&auth_token=[redacted]&api_sig=[redacted]
Line by line, in plain words:
- The first address is what a normal app would call. It goes to
api.flickr.com/services/rest/and carries a set of name=value pairs after the?, separated by&.method=flickr.photos.comments.addCommentsays which action is wanted: add a comment.api_keyidentifies the application.photo_idsays which photo.comment_text=testis the comment.format=php_serialasks for the answer in a particular data format.auth_tokenproves which user the app is acting for, andapi_sigis a signature that proves the request was not tampered with. - The second address is what the Explorer actually sends. It goes to a different place,
www.flickr.com/services/api/render, and asks for the answer asformat=jsonwithnojsoncallback=1. The samemethod,api_key,photo_id,comment_text,auth_tokenandapi_sigare present.
The render endpoint uses syntax highlighting to "beautify" the return to help the developer understand it. Syntax highlighting means colouring and formatting text so it is easier to read, the way a code editor colours keywords.
An API call needs API_Key, Auth_Token and Api_Sig. Removing them produces the appropriate errors saying the token, key or signature is invalid or missing. That is how the developer API works and it is working as intended. In other words, the normal API is well guarded: without the right key, the right user token and a valid signature, nothing happens.
The researcher then looked at the request the API Explorer itself makes:
Two request variables matter. sign_call selects what level of permission the API call should have, and the value full means the request is sent as the current logged-in user with full permission. magic_cookie (referred to in the report as the csrftoken) carries a random hash value. That is Flickr's CSRF protection, intended to prevent API Explorer requests from being forced on unsuspecting users: the idea is that these requests should not be sendable without a valid csrftoken. A hash here simply means a long random-looking string that is hard to guess.
The researcher's reasoning was that sites typically use a csrftoken blacklist rather than a whitelist. With a blacklist, requests do not require csrftoken validation unless they are specifically listed, so requests execute without validation wherever the list is incomplete; with a whitelist they would not execute at all. A comparison: a bouncer with a blacklist only stops the people on a list of troublemakers, so anyone not on the list walks in; a bouncer with a whitelist only lets in people on the guest list, so everyone else is stopped. The conclusion was that the API Explorer requests did not actually validate or require the magic_cookie.
Exploitation
Step 1: Build a self-submitting form
Without CSRF protection, a POST form can be created that submits automatically. A form is the part of a web page with fields and a submit button; normally a person fills it in and clicks. Here the fields are hidden and a tiny script clicks submit for the visitor. Because the API call executes inside an iframe, the attacker cannot retrieve information from the API call, so the exploit is limited to state-changing API requests. In other words, the attacker can make things happen but cannot read the answers.
The following code, hidden on a website the victim loads while logged into Flickr, forces the victim to comment on the param_photo_id specified:
<form method="POST" action="https://www.flickr.com/services/api/explore/flickr.photos.comments.addComment" id="csrf">
<input type="hidden" name="method" value="flickr.photos.comments.addComment" />
<input type="hidden" name="magic_cookie" value="" />
<input type="hidden" name="enable_photo_id" value="on" />
<input type="hidden" name="param_photo_id" value="14369938517" />
<input type="hidden" name="enable_comment_text" value="on" />
<input type="hidden" name="param_comment_text" value="test csrf 123" />
<input type="hidden" name="format" value="rest" />
<input type="hidden" name="sign_call" value="full" />
<input type="submit" value="Submit" />
</form>
<script>
document.getElementById("csrf").submit();
</script>
Line by line, in plain words:
<form method="POST" action="https://www.flickr.com/services/api/explore/flickr.photos.comments.addComment" id="csrf">starts a form.method="POST"says the data is sent in the body of the request rather than in the address bar.actionis the address it is sent to: the API Explorer page for adding a comment.id="csrf"gives the form a name so the script can find it.<input type="hidden" name="method" value="flickr.photos.comments.addComment" />is a field the visitor never sees. It tells the Explorer which API call to run: add a comment.<input type="hidden" name="magic_cookie" value="" />sends the CSRF token field, but with an empty value. This is the heart of the bug: the field is present, so nothing looks missing, but there is no valid token in it.<input type="hidden" name="enable_photo_id" value="on" />switches on the photo identifier parameter, and<input type="hidden" name="param_photo_id" value="14369938517" />supplies its value: the photo to comment on.<input type="hidden" name="enable_comment_text" value="on" />switches on the comment text parameter, and<input type="hidden" name="param_comment_text" value="test csrf 123" />supplies the comment itself.<input type="hidden" name="format" value="rest" />asks for the answer in the plain REST format.<input type="hidden" name="sign_call" value="full" />is the important permission switch: it makes the call run as the logged-in victim with full permission.<input type="submit" value="Submit" />is the submit button, which the visitor never sees or presses.<script> document.getElementById("csrf").submit(); </script>is a short piece of JavaScript, the language browsers run. It finds the form by its name and submits it immediately, so the visitor does not have to do anything.
The form posts to the API Explorer endpoint for flickr.photos.comments.addComment. magic_cookie is present but empty, which is enough because the value is not validated. enable_photo_id and enable_comment_text switch on the corresponding parameters, whose values are carried in param_photo_id and param_comment_text. sign_call set to full makes the call run as the logged-in victim with full permission. The script submits the form without any interaction.
Step 2: The victim loads the page
When the victim loads the page containing the form while authenticated to Flickr, the browser posts the form and the comment is added under the victim's account. The same construction works for any other state-changing API call exposed in the Explorer, such as the photoset, gallery, tag, blog, group and favourite operations listed above.
Root cause
The API Explorer accepted requests without validating or requiring the magic_cookie csrftoken. The likely cause is that CSRF validation was applied as a blacklist rather than a whitelist: requests were only checked when specifically listed, so the Explorer's POST requests fell outside the check. The existing defence, the random magic_cookie hash, therefore did not stop a forged request that simply omitted a valid value or sent an empty one. The report notes that letting developers test API calls, with so much filled in for them, circumvented some of Flickr's API security measures.
Impact
An attacker who gets a logged-in Flickr user to load a page under the attacker's control can force that user to execute any state-changing API request available in the API Explorer, with full permissions: commenting, editing photosets and galleries, adding people and tags, posting to blogs, joining and leaving groups, favouriting photos, and more. The attack works against any user on Flickr. Because the call runs inside an iframe, the attacker cannot read the response, so the exploit is limited to state-changing requests and cannot be used to exfiltrate information through the API call itself.
Fix
The report gives no detail on the change made; it records that the fix was validated on 7/14/2014, after the report on 7/1/2014. The lessons it draws are:
- Move CSRF protection to a whitelist so that all state-changing/POST requests are covered by default. (The report words this as moving to a blacklist, but the safer default is that every request is checked unless explicitly exempted.)
- Add logging for all failed csrftoken requests and monitor for spikes to discover broken requests before users report them. Logging means keeping a record of events; a spike is a sudden jump in how many happen.
- If developers are allowed to test API calls, be careful about how much information is filled in for them.
Variants to test
When testing a site, locate the CSRF protection as soon as possible. All state-changing requests should be POST and covered by CSRF protection. Validate the protection by trying each of the following:
- Remove the csrftoken request variable completely. A positive result is that the request still executes; the site may only validate the token when it is present.
- Put an invalid csrftoken value, for example
&csrftoken=asdf. This is the fastest way to see what happens with a valid versus an invalid token. A positive result is that the action succeeds with the invalid value. - Keep the csrftoken variable present but do not set a value, for example
&csrftoken=. A positive result is that the action succeeds with an empty value. - Try old csrftoken values that should have expired. A positive result is that a stale token is still accepted.
- See if a valid csrftoken for one user is valid for other users. A positive result is that a token issued to one account authorises actions on another.
Even on a site with thousands of requests, always test the csrftoken: protection on the first 1000 requests you discovered does not mean the 1001st is protected.
Timeline and bounty
- Reported on: 7/1/2014
- Validated the fix: 7/14/2014
- Bounty reward: $100
Glossary
- API: a fixed set of requests one piece of software can make to another, and the answers it gets back.
- App Garden: Flickr's section for developers, where apps that use its API are registered.
- API Explorer: a form-based testing tool in the App Garden that lets a developer run API calls and see the answers.
- API key: a value that identifies which application is making an API call.
- Auth token: a value that proves which user an application is acting for.
- API signature (
api_sig): a value computed from the request that proves it was not altered. - Endpoint: the address a request is sent to.
- POST: a way for a browser to send data to a server in the body of the request, rather than in the address bar.
- GET: a way for a browser to request data with the details in the address bar.
- Iframe: a small window embedded inside a web page that shows another page.
- Cookie: a small piece of data a browser stores for a site and sends back with each request, commonly used to remember that you are logged in.
- Token: a value that proves something, such as that a request is legitimate.
- CSRF (Cross-Site Request Forgery): an attack where a page you visit makes your browser send a request to another site you are logged into, so the request appears to come from you.
- CSRF token (
magic_cookiehere): a random value a site expects in state-changing requests to prove they were intended by the user. - Blacklist: a list of things that are blocked or checked; anything not on the list is allowed.
- Whitelist: a list of things that are allowed; anything not on the list is blocked.
- Hash: a long random-looking string, here used as a token value.
- State-changing request: a request that alters data (adding, editing, deleting) rather than only reading it.
- Syntax highlighting: colouring and formatting text so it is easier to read.
- JavaScript: the language browsers run to make web pages interactive.
- Logging: keeping a record of events, such as failed requests.
- Spike: a sudden increase in how often something happens.
- Exfiltrate: to get data out of a system without permission.
- OAuth: a standard way for an application to act on a user's behalf without seeing the user's password.