Skip to content

Commit 17246b4

Browse files
authored
Merge pull request #2377 from OneSignal/add-webview-security-hardening
add: security hardening around webview javaScriptEnabled
2 parents 51a1b8b + 2398782 commit 17246b4

File tree

1 file changed

+29
-2
lines changed
  • OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/display/impl

1 file changed

+29
-2
lines changed

OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/display/impl/WebViewManager.kt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.app.Activity
55
import android.os.Build
66
import android.view.View
77
import android.webkit.JavascriptInterface
8+
import android.webkit.WebSettings
89
import android.webkit.WebView
910
import com.onesignal.common.AndroidUtils
1011
import com.onesignal.common.ViewUtils
@@ -299,7 +300,6 @@ internal class WebViewManager(
299300
}
300301
}
301302

302-
@SuppressLint("SetJavaScriptEnabled", "AddJavascriptInterface")
303303
suspend fun setupWebView(
304304
currentActivity: Activity,
305305
base64Message: String,
@@ -310,7 +310,7 @@ internal class WebViewManager(
310310
webView!!.overScrollMode = View.OVER_SCROLL_NEVER
311311
webView!!.isVerticalScrollBarEnabled = false
312312
webView!!.isHorizontalScrollBarEnabled = false
313-
webView!!.settings.javaScriptEnabled = true
313+
secureSetup(webView!!)
314314

315315
// Setup receiver for page events / data from JS
316316
webView!!.addJavascriptInterface(OSJavaScriptInterface(), JS_OBJ_NAME)
@@ -329,6 +329,33 @@ internal class WebViewManager(
329329
webView!!.loadData(base64Message, "text/html; charset=utf-8", "base64")
330330
}
331331

332+
/**
333+
* Applies security hardening to the WebView to prevent common vulnerabilities.
334+
*
335+
* Security measures:
336+
* - JavaScript is enabled for IAM functionality but file access is completely blocked
337+
* - Prevents file:// URL access to mitigate local file inclusion attacks
338+
* - Blocks cross-origin access from file URLs to prevent data exfiltration
339+
* - Disables mixed content (HTTP resources on HTTPS pages) to prevent MITM attacks
340+
*
341+
* This configuration protects against:
342+
* 1. Malicious JavaScript accessing local device files
343+
* 2. Cross-site scripting (XSS) attacks via file:// protocol
344+
* 3. Man-in-the-middle attacks via downgraded HTTP content
345+
*
346+
* @SuppressLint is used because JavaScript is required for IAM functionality,
347+
* but we mitigate the risk through strict file access controls.
348+
*/
349+
@SuppressLint("SetJavaScriptEnabled")
350+
fun secureSetup(webView: WebView) =
351+
with(webView.settings) {
352+
javaScriptEnabled = true
353+
allowFileAccess = false
354+
allowFileAccessFromFileURLs = false
355+
allowUniversalAccessFromFileURLs = false
356+
mixedContentMode = WebSettings.MIXED_CONTENT_NEVER_ALLOW
357+
}
358+
332359
// This sets the WebView view port sizes to the max screen sizes so the initialize
333360
// max content height can be calculated.
334361
// A render complete or resize event will fire from JS to tell Java it's height and will then display

0 commit comments

Comments
 (0)