-
Notifications
You must be signed in to change notification settings - Fork 16
inject request inspector JS at document start to fix race condition #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maxeoneo
wants to merge
1
commit into
acsbendi:master
Choose a base branch
from
maxeoneo:fix/early-webapp-requests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <script> | ||
| // Fires immediately during HTML parsing — before onPageStarted() can inject the | ||
| // interception hooks via evaluateJavascript() on older devices. | ||
| fetch('https://example.com/api/early-fetch', { | ||
| method: 'POST', | ||
| body: '{"early":true}', | ||
| headers: { | ||
| 'Content-Type': 'application/json' | ||
| } | ||
| }); | ||
| </script> | ||
| </head> | ||
| <body></body> | ||
| </html> |
90 changes: 90 additions & 0 deletions
90
app/src/androidTest/java/com/acsbendi/requestinspectorwebview/EarlyRequestTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package com.acsbendi.requestinspectorwebview | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.graphics.Bitmap | ||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import android.webkit.WebView | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import androidx.test.platform.app.InstrumentationRegistry | ||
| import androidx.webkit.WebViewFeature | ||
| import org.junit.After | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Assume.assumeTrue | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import java.util.concurrent.CountDownLatch | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class EarlyRequestTest { | ||
|
|
||
| private lateinit var webView: WebView | ||
| private lateinit var matcher: CapturingRequestMatcher | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| InstrumentationRegistry.getInstrumentation().runOnMainSync { webView.destroy() } | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that a fetch() fired by an inline <script> — which executes before | ||
| * onPageStarted() can inject the interceptor via evaluateJavascript() — is still | ||
| * recorded with full JS details when DOCUMENT_START_SCRIPT is supported. | ||
| * | ||
| * The 500 ms delay in onPageStarted() forces the race deterministically: inline | ||
| * scripts always fire before the fallback evaluateJavascript() injection runs. | ||
| * The test therefore only passes when the fix registers the hooks at document | ||
| * creation time via addDocumentStartJavaScript() in the constructor, before any | ||
| * page script executes. | ||
| */ | ||
| @SuppressLint("SetJavaScriptEnabled") | ||
| @Test | ||
| fun earlyFetch_withDelayedFallbackInjection_isCapturedByDocumentStartScript() { | ||
| assumeTrue( | ||
| "Skipping: DOCUMENT_START_SCRIPT not supported on this device", | ||
| WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT) | ||
| ) | ||
|
|
||
| matcher = CapturingRequestMatcher() | ||
| val earlyRequestLatch = matcher.expectRequest() | ||
| val pageFinishedLatch = CountDownLatch(1) | ||
|
|
||
| InstrumentationRegistry.getInstrumentation().runOnMainSync { | ||
| val context = InstrumentationRegistry.getInstrumentation().targetContext | ||
| webView = WebView(context) | ||
|
|
||
| webView.webViewClient = object : RequestInspectorWebViewClient(webView, matcher) { | ||
| override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) { | ||
| // Delay the evaluateJavascript fallback injection so that inline | ||
| // page scripts always fire before the hooks could be installed that | ||
| // way. On devices with DOCUMENT_START_SCRIPT the hooks were already | ||
| // registered in the constructor, so this delay is irrelevant there. | ||
| Handler(Looper.getMainLooper()).postDelayed({ | ||
| super.onPageStarted(view, url, favicon) | ||
| }, 500) | ||
| } | ||
|
|
||
| override fun onPageFinished(view: WebView, url: String) { | ||
| super.onPageFinished(view, url) | ||
| pageFinishedLatch.countDown() | ||
| } | ||
| } | ||
| webView.loadUrl("file:///android_asset/test_page_early_request.html") | ||
| } | ||
|
|
||
| assertTrue("Page failed to load", pageFinishedLatch.await(15, TimeUnit.SECONDS)) | ||
| assertTrue( | ||
| "Early fetch JS details were not recorded — hooks were not installed before inline scripts ran", | ||
| earlyRequestLatch.await(5, TimeUnit.SECONDS) | ||
| ) | ||
|
|
||
| val req = matcher.lastRequest!! | ||
| assertEquals(WebViewRequestType.FETCH, req.type) | ||
| assertEquals("https://example.com/api/early-fetch", req.url.toString()) | ||
| assertEquals("POST", req.method) | ||
| assertEquals("{\"early\":true}", req.body) | ||
| assertEquals("application/json", req.headers["content-type"]) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.