Fixes to EvenHub webview handling

This commit is contained in:
James Babcock committed 2026-10-16 18:22:35 -07:00
1 parent f4751ad480
commit 60595d7321
21 files changed
+1001 -142

No files matched your search

+106
View File
@@ -0,0 +1,106 @@
package com.faceclaw.evenhubprobe
import android.app.Activity
import android.content.MutableContextWrapper
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.webkit.WebView
import com.faceclaw.app.FaceclawEvenHubChromeClient
import com.faceclaw.app.FaceclawEvenHubDocumentStart
import com.faceclaw.app.FaceclawEvenHubJsBridge
import com.faceclaw.app.FaceclawEvenHubListener
import com.faceclaw.app.FaceclawEvenHubProfile
import com.faceclaw.app.FaceclawEvenHubWebView
import com.faceclaw.app.FaceclawEvenHubWebViewClient
import com.faceclaw.app.FaceclawEvenHubWebViewHost
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
/**
* Runs the production EvenHub WebView classes (tests/android-evenhub-native.cjs),
* wired the way app/apps/evenhub/webview.ts wires them, and logs what the host saw.
*
* - A: a packaged app. It calls the bridge and gets a reply, embeds a same-origin
* and a cross-origin iframe, then navigates to a third-party page.
* - B: another packaged app (its own profile), which then visits a cookie check.
* - A2: a second WebView for A's package, which visits the same cookie check.
* - R: a remote app, served by the test server, with the document-start shim.
* - Then the shared renderer is crashed; the process must survive it.
*/
class ProbeActivity : Activity() {
private val handler = Handler(Looper.getMainLooper())
private val seen = LinkedHashSet<String>()
private val webViews = HashMap<String, WebView>()
private val checks = HashMap<String, String>()
private lateinit var config: JSONObject
private fun asset(name: String) = assets.open(name).use { it.readBytes().decodeToString() }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
config = JSONObject(asset("config.json"))
val dist = File(filesDir, "dist").apply { mkdirs() }
for (name in assets.list("dist")!!) File(dist, name).writeBytes(assets.open("dist/$name").use { it.readBytes() })
val third = config.getString("third")
handler.post { create("A", "probe.a", dist.path) }
handler.postDelayed({ evaluate("A", "location.href = '$third/third'") }, 4000)
handler.postDelayed({
// A now shows the third-party page: host scripts must not run there.
evaluate("A", "new Image().src = '$third/report?eval=leak'")
create("B", "probe.b", dist.path)
}, 6000)
handler.postDelayed({ create("A2", "probe.a", dist.path); create("R", "probe.remote", null) }, 8000)
handler.postDelayed({
evaluate("B", "location.href = '$third/cookie?app=B'")
evaluate("A2", "location.href = '$third/cookie?app=A2'")
}, 10000)
handler.postDelayed({ webViews["R"]?.loadUrl("chrome://crash") }, 13000)
handler.postDelayed({
Log.i(TAG, "RESULT " + JSONObject().put("seen", JSONArray(seen.toList())))
}, 17000)
}
private fun create(tag: String, packageId: String, distDir: String?) {
val startUrl = config.getJSONObject("start").getString(tag)
checks[tag] = config.getJSONObject("checks").getString(tag)
val web = FaceclawEvenHubWebView(MutableContextWrapper(this))
seen.add("$tag:profile=${FaceclawEvenHubProfile.apply(web, packageId)}")
web.settings.javaScriptEnabled = true
web.settings.domStorageEnabled = true
web.settings.allowFileAccess = false
val listener = object : FaceclawEvenHubListener {
override fun onEvenAppMessage(handlerName: String?, argsJson: String?, callId: Int) {
seen.add("$tag:" + JSONArray(argsJson ?: "[]").optString(0))
evaluate(tag, "window.__fcResolve($callId, true, true)")
}
override fun onPageFinished(url: String?) {}
override fun onRenderProcessGone(crashed: Boolean) {
seen.add("$tag:gone")
webViews.remove(tag)
}
}
val rule = FaceclawEvenHubJsBridge.originRule(startUrl)!!
FaceclawEvenHubJsBridge.install(web, rule, listener)?.let { seen.add("$tag:bridge-error") }
val inject = asset("inject-$tag.js")
val remote = distDir == null
val documentStart = remote && FaceclawEvenHubDocumentStart.install(web, inject, rule)
val host = if (remote) "" else android.net.Uri.parse(startUrl).host
web.webViewClient = FaceclawEvenHubWebViewClient(distDir, host, inject, remote && !documentStart, listener)
web.webChromeClient = FaceclawEvenHubChromeClient()
FaceclawEvenHubWebViewHost.getInstance().attach(this, web, checks[tag]!!)
webViews[tag] = web
web.loadUrl(startUrl)
}
/** As webview.ts's evaluateJs: guarded to the app's origin. */
private fun evaluate(tag: String, js: String) {
webViews[tag]?.evaluateJavascript("if (${checks[tag]}) {\n$js\n}", null)
}
companion object {
private const val TAG = "EHPROBE"
}
}
+35 -7
View File
@@ -12,6 +12,14 @@
@property(nonatomic) BOOL foregroundPassed;
@end
@implementation Probe
- (void)after:(double)seconds run:(void (^)(void))block {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
}
- (UIButton *)button:(NSString *)identifier in:(UIView *)view {
if ([view isKindOfClass:UIButton.class] && [view.accessibilityIdentifier isEqual:identifier]) return (UIButton *)view;
for (UIView *child in view.subviews) { UIButton *found = [self button:identifier in:child]; if (found) return found; }
return nil;
}
- (void)writeResult:(NSDictionary *)result name:(NSString *)name {
NSData *data = [NSJSONSerialization dataWithJSONObject:result options:0 error:nil];
NSString *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
@@ -30,8 +38,10 @@
self.host.eventHandler = ^(NSString *json) {
NSLog(@"EHPROBE %@", json);
NSDictionary *event = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
if ([event[@"kind"] isEqual:@"error"] && [event[@"message"] containsString:@"outside the app origin"])
if ([event[@"kind"] isEqual:@"notice"] && [event[@"message"] containsString:@"Blocked navigation to a faceclaw-test:"])
[weakSelf.seen addObject:@"blocked-navigation"];
// Blocked and interrupted navigations are not load errors.
if ([event[@"kind"] isEqual:@"error"]) [weakSelf.seen addObject:[@"ERROR:" stringByAppendingString:event[@"message"]]];
if ([event[@"kind"] isEqual:@"call"]) {
[weakSelf.seen addObject:event[@"name"]];
[weakSelf.seen addObject:[NSString stringWithFormat:@"%ld:%@", (long)weakSelf.phase, event[@"name"]]];
@@ -46,6 +56,24 @@
};
NSString *root = NSBundle.mainBundle.resourcePath;
NSString *script = [NSString stringWithContentsOfFile:[root stringByAppendingPathComponent:@"bridge.js"] encoding:NSUTF8StringEncoding error:nil];
NSString *remote = [NSString stringWithContentsOfFile:[root stringByAppendingPathComponent:@"remote.txt"] encoding:NSUTF8StringEncoding error:nil];
NSString *authorize = [NSString stringWithContentsOfFile:[root stringByAppendingPathComponent:@"authorize.txt"] encoding:NSUTF8StringEncoding error:nil];
// Phase 1: an OAuth sign-in through a third-party page comes back to the packaged app.
[self after:2 run:^{ [self.host evaluate:[NSString stringWithFormat:@"location.href = '%@'", authorize]]; }];
// Phase 2: a non-web scheme is blocked without failing the app; a followed link
// leaves the app, which the Return button restores.
[self after:4.5 run:^{ [self.host evaluate:@"location.href = 'faceclaw-test:blocked'"]; }];
[self after:5 run:^{ [self.host evaluate:[NSString stringWithFormat:@"location.href = '%@away'", remote]]; }];
[self after:6 run:^{
UIButton *button = [self button:@"evenhub-return" in:self.window];
if (button && !button.hidden) [self.seen addObject:@"return-visible"];
[self.host evaluate:@"new Image().src = '/away-eval'"];
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
}];
[self after:7 run:^{
if ([self button:@"evenhub-return" in:self.window].hidden) [self.seen addObject:@"return-hidden"];
[self.host evaluate:@"flutter_inappwebview.callHandler('returned')"];
}];
dispatch_async(dispatch_get_main_queue(), ^{
[self.host start:[root stringByAppendingPathComponent:@"dist"] entrypoint:@"index.html" script:script];
});
@@ -57,15 +85,15 @@
self.host.eventHandler = handler;
self.host.packageIdentifier = phase == 2 ? package : [package stringByAppendingString:@".other"];
if (phase <= 3) [self.host start:[root stringByAppendingPathComponent:@"dist"] entrypoint:@"index.html" script:script];
else {
NSString *url = [NSString stringWithContentsOfFile:[root stringByAppendingPathComponent:@"remote.txt"] encoding:NSUTF8StringEncoding error:nil];
[self.host startURL:phase == 5 ? [url stringByAppendingString:@"redirect"] : url script:script];
}
else [self.host startURL:phase == 5 ? [remote stringByAppendingString:@"redirect"] : remote script:script];
});
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 21 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
NSArray *required = @[@"early", @"module", @"fetch", @"image", @"timer", @"raf", @"wasm", @"traversal", @"1:stored", @"2:restored", @"3:stored", @"4:early", @"4:module", @"4:fetch", @"4:timer", @"blocked-navigation"];
BOOL passed = [[NSSet setWithArray:required] isSubsetOfSet:self.seen] && ![self.seen containsObject:@"UNTRUSTED"];
NSArray *required = @[@"early", @"module", @"fetch", @"image", @"timer", @"raf", @"wasm", @"traversal", @"1:stored", @"2:restored", @"3:stored",
@"1:iframe", @"1:oauth:probe:s1:undefined", @"blocked-navigation", @"return-visible", @"return-hidden", @"2:returned",
@"4:early", @"4:module", @"4:fetch", @"4:timer", @"4:iframe", @"5:early", @"5:module"];
BOOL passed = [[NSSet setWithArray:required] isSubsetOfSet:self.seen] && ![self.seen containsObject:@"UNTRUSTED"] &&
![[self.seen.allObjects componentsJoinedByString:@"\n"] containsString:@"ERROR:"];
NSString *signature = [FaceclawCrypto hmacSha256:@"key" message:@"The quick brown fox jumps over the lazy dog"];
passed &= [signature isEqual:@"97yD9DBThCSxMpjmqm+xQ+9NWaFJRhdZl0edvC0aPNg="];
self.foregroundPassed = passed;