From a5fc0970d21ccdbb1c71ee33358a6154d3b7cc9e Mon Sep 17 00:00:00 2001 From: Juan <15802531+jtsnova@users.noreply.github.com> Date: Mon, 21 Sep 2026 07:12:05 +0200 Subject: [PATCH 1/6] feat: add support for disabling plugins in configuration --- .../main/java/com/getcapacitor/Bridge.java | 10 ++++-- .../main/java/com/getcapacitor/Plugin.java | 2 ++ .../com/getcapacitor/ConfigBuildingTest.java | 7 +++++ cli/src/declarations.ts | 31 ++++++++++++++----- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index 9a3c6e0739..b7ed76d5a0 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -706,7 +706,10 @@ private String getLegacyPluginName(Class pluginClass) { public void registerPlugin(Class pluginClass) { String pluginId = pluginId(pluginClass); if (pluginId == null) return; - + if (config.getPluginConfiguration(pluginId).getBoolean("disabled", false)) { + Logger.debug("Not registering disabled plugin: " + pluginId); + return; + } try { this.plugins.put(pluginId, new PluginHandle(this, pluginClass)); } catch (InvalidPluginException ex) { @@ -720,7 +723,10 @@ public void registerPluginInstance(Plugin plugin) { Class clazz = plugin.getClass(); String pluginId = pluginId(clazz); if (pluginId == null) return; - + if (config.getPluginConfiguration(pluginId).getBoolean("disabled", false)) { + Logger.debug("Not registering disabled plugin: " + pluginId); + return; + } try { this.plugins.put(pluginId, new PluginHandle(this, plugin)); } catch (InvalidPluginException ex) { diff --git a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java index f70cc4101d..8901b985d2 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java @@ -1052,4 +1052,6 @@ protected String getLogTag(String... subTags) { protected String getLogTag() { return Logger.tags(this.getClass().getSimpleName()); } + + protected Boolean } diff --git a/android/capacitor/src/test/java/com/getcapacitor/ConfigBuildingTest.java b/android/capacitor/src/test/java/com/getcapacitor/ConfigBuildingTest.java index 1af51039da..cea330dcdf 100644 --- a/android/capacitor/src/test/java/com/getcapacitor/ConfigBuildingTest.java +++ b/android/capacitor/src/test/java/com/getcapacitor/ConfigBuildingTest.java @@ -37,6 +37,7 @@ public void setup() { testPluginObject.put("var3", testPluginNestedObject); testPluginObject.put("var4", 2); testPluginObject.put("var5", testPluginArray); + testPluginObject.put("disabled", true); pluginConfig.put(TEST_PLUGIN_NAME, testPluginObject); @@ -89,6 +90,12 @@ public void getPluginBoolean() { assertTrue(testBool); } + @Test + public void getPluginDisabled() { + boolean disabled = config.getPluginConfiguration(TEST_PLUGIN_NAME).getBoolean("disabled", false); + assertTrue(disabled); + } + @Test public void getPluginInt() { int testInt = config.getPluginConfiguration(TEST_PLUGIN_NAME).getInt("var4", -1); diff --git a/cli/src/declarations.ts b/cli/src/declarations.ts index 2cee84996b..448983c9d3 100644 --- a/cli/src/declarations.ts +++ b/cli/src/declarations.ts @@ -725,20 +725,25 @@ export interface PluginsConfig { /** * Plugin configuration by class name. * + * Set `disabled` to `true` in a plugin's configuration to prevent its native + * implementation from being registered and loaded. For example: + * `{ plugins: { MyPlugin: { disabled: true } } }`. + * + * This does not affect a plugin-specific `enabled` setting, which may have a + * different meaning. + * * @since 1.0.0 */ - [key: string]: - | { - [key: string]: any; - } - | undefined; + [key: string]: PluginConfig & { + [key: string]: any; +} | undefined; /** * Capacitor Cookies plugin configuration * * @since 4.3.0 */ - CapacitorCookies?: { + CapacitorCookies?: PluginConfig & { /** * Enable CapacitorCookies to override the global `document.cookie` on native. * @@ -752,7 +757,7 @@ export interface PluginsConfig { * * @since 4.3.0 */ - CapacitorHttp?: { + CapacitorHttp?: PluginConfig & { /** * Enable CapacitorHttp to override the global `fetch` and `XMLHttpRequest` on native. * @@ -766,7 +771,7 @@ export interface PluginsConfig { * * @since 8.0.0 */ - SystemBars?: { + SystemBars?: PluginConfig & { /** * Specifies how to handle problematic insets on Android. * @@ -827,6 +832,16 @@ export interface PluginsConfig { }; } +export interface PluginConfig { + /** + * Prevent this plugin's native implementation from being registered. + * + * @since 8.5.3 + */ + disabled?: boolean; + +} + export interface PackageOptions { /** * Create a symlink to the plugin folder instead of pointing to the plugin path. From 6dab82ba8cb3caeed839728e7ed210309bc12df3 Mon Sep 17 00:00:00 2001 From: Juan <15802531+jtsnova@users.noreply.github.com> Date: Mon, 21 Sep 2026 07:27:43 +0200 Subject: [PATCH 2/6] chore: remove noise --- android/capacitor/src/main/java/com/getcapacitor/Plugin.java | 1 - 1 file changed, 1 deletion(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java index 8901b985d2..383306705c 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java @@ -1053,5 +1053,4 @@ protected String getLogTag() { return Logger.tags(this.getClass().getSimpleName()); } - protected Boolean } From 9f9f39c8b63f8377944bc3eee57b5fb834b8e4b2 Mon Sep 17 00:00:00 2001 From: Juan <15802531+jtsnova@users.noreply.github.com> Date: Mon, 21 Sep 2026 07:28:15 +0200 Subject: [PATCH 3/6] chore: remove noise --- android/capacitor/src/main/java/com/getcapacitor/Plugin.java | 1 - 1 file changed, 1 deletion(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java index 383306705c..f70cc4101d 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java @@ -1052,5 +1052,4 @@ protected String getLogTag(String... subTags) { protected String getLogTag() { return Logger.tags(this.getClass().getSimpleName()); } - } From 2abb33725cb0d6165a354dfb01e07c1602e28840 Mon Sep 17 00:00:00 2001 From: Juan <15802531+jtsnova@users.noreply.github.com> Date: Mon, 21 Sep 2026 07:34:15 +0200 Subject: [PATCH 4/6] feat: ios - prevent registration of disabled plugins --- ios/Capacitor/Capacitor/CapacitorBridge.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ios/Capacitor/Capacitor/CapacitorBridge.swift b/ios/Capacitor/Capacitor/CapacitorBridge.swift index b240d5252a..22e2c9449b 100644 --- a/ios/Capacitor/Capacitor/CapacitorBridge.swift +++ b/ios/Capacitor/Capacitor/CapacitorBridge.swift @@ -378,6 +378,10 @@ open class CapacitorBridge: NSObject, CAPBridgeProtocol { if plugins[pluginInstance.jsName] != nil { CAPLog.print("⚡️ Overriding existing registered plugin \(pluginInstance.classForCoder)") } + guard !config.getPluginConfig(plugin.jsName).getBoolean("disabled", false) else { + CAPLog.print("⚡️ Not registering disabled plugin \(plugin.jsName)") + return + } plugins[pluginInstance.jsName] = pluginInstance pluginInstance.load(on: self) @@ -398,6 +402,10 @@ open class CapacitorBridge: NSObject, CAPBridgeProtocol { CAPLog.print("⚡️ Unable to load plugin \(type.classForCoder()). No such module found.") return nil } + guard !config.getPluginConfig(plugin.jsName).getBoolean("disabled", false) else { + CAPLog.print("⚡️ Not registering disabled plugin \(plugin.jsName)") + return nil + } plugin.load(on: self) plugins[plugin.jsName] = plugin return plugin From 17a0080713c13dfd52546a128acb7055f1ef9728 Mon Sep 17 00:00:00 2001 From: Andre Destro Date: Wed, 25 Nov 2026 10:07:53 +0000 Subject: [PATCH 5/6] feat: let disabled take precedence over enabled, add tests and docs --- .../android/DisabledPluginTest.java | 31 +++++++++++++ .../android/TestHostActivity.java | 3 +- .../com/getcapacitor/ConfigBuildingTest.java | 7 --- cli/src/declarations.ts | 41 ++++++++++++----- core/cookies.md | 13 ++++-- core/http.md | 10 +++-- ios/Sources/Capacitor/CapacitorBridge.swift | 15 ++++--- .../CapacitorTests/DisabledPluginTests.swift | 45 +++++++++++++++++++ 8 files changed, 134 insertions(+), 31 deletions(-) create mode 100644 android/capacitor/src/androidTest/java/com/getcapacitor/android/DisabledPluginTest.java create mode 100644 ios/Tests/CapacitorTests/DisabledPluginTests.swift diff --git a/android/capacitor/src/androidTest/java/com/getcapacitor/android/DisabledPluginTest.java b/android/capacitor/src/androidTest/java/com/getcapacitor/android/DisabledPluginTest.java new file mode 100644 index 0000000000..232829f7ba --- /dev/null +++ b/android/capacitor/src/androidTest/java/com/getcapacitor/android/DisabledPluginTest.java @@ -0,0 +1,31 @@ +package com.getcapacitor.android; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import androidx.test.core.app.ActivityScenario; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.getcapacitor.Bridge; +import com.getcapacitor.plugin.CapacitorCookies; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** The test host sets plugins.CapacitorCookies.disabled, so the bridge must never register it. */ +@RunWith(AndroidJUnit4.class) +public class DisabledPluginTest { + + @Test + public void doesNotRegisterDisabledPlugins() { + try (ActivityScenario scenario = ActivityScenario.launch(TestHostActivity.class)) { + scenario.onActivity((activity) -> { + Bridge bridge = activity.getBridge(); + assertNotNull(bridge); + assertNull("disabled core plugin must not be registered", bridge.getPlugin("CapacitorCookies")); + assertNotNull("plugins that are not disabled must still be registered", bridge.getPlugin("CapacitorHttp")); + + bridge.registerPluginInstance(new CapacitorCookies()); + assertNull("disabled plugin instance must not be registered", bridge.getPlugin("CapacitorCookies")); + }); + } + } +} diff --git a/android/capacitor/src/androidTest/java/com/getcapacitor/android/TestHostActivity.java b/android/capacitor/src/androidTest/java/com/getcapacitor/android/TestHostActivity.java index 05d7a5d7c5..f461b90aeb 100644 --- a/android/capacitor/src/androidTest/java/com/getcapacitor/android/TestHostActivity.java +++ b/android/capacitor/src/androidTest/java/com/getcapacitor/android/TestHostActivity.java @@ -8,6 +8,7 @@ /** * Host for instrumented tests. CapacitorHttp is on so the proxy is reachable, and a plugin that * tries to allow the proxy path is registered so tests can prove the guard still wins. + * CapacitorCookies is disabled so tests can prove a disabled plugin is never registered. */ public class TestHostActivity extends BridgeActivity { @@ -16,7 +17,7 @@ protected void load() { registerPlugin(InterceptorAllowingPlugin.class); try { - JSONObject plugins = new JSONObject("{\"CapacitorHttp\":{\"enabled\":true}}"); + JSONObject plugins = new JSONObject("{\"CapacitorHttp\":{\"enabled\":true},\"CapacitorCookies\":{\"disabled\":true}}"); config = new CapConfig.Builder(this).setPluginsConfiguration(plugins).create(); } catch (JSONException e) { throw new IllegalStateException("bad test plugin config", e); diff --git a/android/capacitor/src/test/java/com/getcapacitor/ConfigBuildingTest.java b/android/capacitor/src/test/java/com/getcapacitor/ConfigBuildingTest.java index cea330dcdf..1af51039da 100644 --- a/android/capacitor/src/test/java/com/getcapacitor/ConfigBuildingTest.java +++ b/android/capacitor/src/test/java/com/getcapacitor/ConfigBuildingTest.java @@ -37,7 +37,6 @@ public void setup() { testPluginObject.put("var3", testPluginNestedObject); testPluginObject.put("var4", 2); testPluginObject.put("var5", testPluginArray); - testPluginObject.put("disabled", true); pluginConfig.put(TEST_PLUGIN_NAME, testPluginObject); @@ -90,12 +89,6 @@ public void getPluginBoolean() { assertTrue(testBool); } - @Test - public void getPluginDisabled() { - boolean disabled = config.getPluginConfiguration(TEST_PLUGIN_NAME).getBoolean("disabled", false); - assertTrue(disabled); - } - @Test public void getPluginInt() { int testInt = config.getPluginConfiguration(TEST_PLUGIN_NAME).getInt("var4", -1); diff --git a/cli/src/declarations.ts b/cli/src/declarations.ts index 470e419106..11add0d7d5 100644 --- a/cli/src/declarations.ts +++ b/cli/src/declarations.ts @@ -741,18 +741,16 @@ export interface PluginsConfig { /** * Plugin configuration by class name. * - * Set `disabled` to `true` in a plugin's configuration to prevent its native - * implementation from being registered and loaded. For example: + * Every plugin accepts `disabled` (see `PluginConfig`), for example * `{ plugins: { MyPlugin: { disabled: true } } }`. * - * This does not affect a plugin-specific `enabled` setting, which may have a - * different meaning. - * * @since 1.0.0 */ - [key: string]: PluginConfig & { - [key: string]: any; -} | undefined; + [key: string]: + | (PluginConfig & { + [key: string]: any; + }) + | undefined; /** * Capacitor Cookies plugin configuration @@ -763,6 +761,9 @@ export interface PluginsConfig { /** * Enable CapacitorCookies to override the global `document.cookie` on native. * + * This only controls the patching. The plugin's own methods work either + * way; use `disabled` to stop the plugin from loading. + * * @default false */ enabled?: boolean; @@ -777,6 +778,9 @@ export interface PluginsConfig { /** * Enable CapacitorHttp to override the global `fetch` and `XMLHttpRequest` on native. * + * This only controls the patching. The plugin's own methods work either + * way; use `disabled` to stop the plugin from loading. + * * @default false */ enabled?: boolean; @@ -850,12 +854,27 @@ export interface PluginsConfig { export interface PluginConfig { /** - * Prevent this plugin's native implementation from being registered. + * Prevent the plugin's native implementation from being registered and + * loaded on iOS and Android. + * + * A disabled plugin is not available to the app: + * `Capacitor.isPluginAvailable()` returns `false` and calls to its methods + * fail as unimplemented. * - * @since 8.5.3 + * This is different from the `enabled` option of `CapacitorHttp` and + * `CapacitorCookies`, which only controls the patching of `fetch`, + * `XMLHttpRequest` and `document.cookie`. `disabled: true` takes precedence + * over `enabled: true`. + * + * Plugins that declare their own configuration type don't list `disabled`, + * so TypeScript rejects it in `capacitor.config.ts`. Set it in + * `capacitor.config.json` instead, or cast the plugin's configuration, for + * example `SplashScreen: { disabled: true } as any`. + * + * @since 9.1.0 + * @default false */ disabled?: boolean; - } export interface PackageOptions { diff --git a/core/cookies.md b/core/cookies.md index a1d237ee4e..ad8eb518c6 100644 --- a/core/cookies.md +++ b/core/cookies.md @@ -7,9 +7,16 @@ The Capacitor Cookies API provides native cookie support via patching `document. By default, the patching of `document.cookie` to use native libraries is disabled. If you would like to enable this feature, modify the configuration below in the `capacitor.config` file. -| Prop | Type | Description | Default | -| ------------- | -------------------- | ------------------------------------------------------------------------- | ------------------ | -| **`enabled`** | boolean | Enable the patching of `document.cookie` to use native libraries instead. | false | +| Prop | Type | Description | Default | Since | +| -------------- | -------------------- | ------------------------------------------------------------------------------------ | ------------------ | ----- | +| **`enabled`** | boolean | Enable the patching of `document.cookie` to use native libraries instead. | false | 4.3.0 | +| **`disabled`** | boolean | Prevent the plugin from loading on iOS and Android. Takes precedence over `enabled`. | false | 9.1.0 | + +`enabled` only controls the patching. With `enabled` unset or `false`, `CapacitorCookies.setCookie()` and the other methods below still work. +Set `disabled` to `true` to stop the plugin from loading at all: its methods then fail as unimplemented, and `document.cookie` is not patched even if `enabled` is `true`. + +On Android, this plugin is also what shares cookies between the web view and native requests made with `HttpURLConnection` (including `CapacitorHttp`), and it clears session cookies when it loads. +With `disabled: true`, neither happens. ### Example Configuration diff --git a/core/http.md b/core/http.md index f459597c1f..f85b8dad15 100644 --- a/core/http.md +++ b/core/http.md @@ -7,9 +7,13 @@ The Capacitor Http API provides native http support via patching `fetch` and `XM By default, the patching of `window.fetch` and `XMLHttpRequest` to use native libraries is disabled. If you would like to enable this feature, modify the configuration below in the `capacitor.config` file. -| Prop | Type | Description | Default | -| ------------- | -------------------- | ------------------------------------------------------------------------------------ | ------------------ | -| **`enabled`** | boolean | Enable the patching of `fetch` and `XMLHttpRequest` to use native libraries instead. | false | +| Prop | Type | Description | Default | Since | +| -------------- | -------------------- | -------------------------------------------------------------------------------------------------------- | ------------------ | ----- | +| **`enabled`** | boolean | Enable the patching of `fetch` and `XMLHttpRequest` to use native libraries instead. | false | 4.3.0 | +| **`disabled`** | boolean | Prevent the plugin from loading on iOS and Android. Takes precedence over `enabled`. | false | 9.1.0 | + +`enabled` only controls the patching. With `enabled` unset or `false`, `CapacitorHttp.request()` and the other methods below still work. +Set `disabled` to `true` to stop the plugin from loading at all: its methods then fail as unimplemented, and `fetch` and `XMLHttpRequest` are not patched even if `enabled` is `true`. ### Example Configuration diff --git a/ios/Sources/Capacitor/CapacitorBridge.swift b/ios/Sources/Capacitor/CapacitorBridge.swift index e7f8ad4bdc..1bbe6cfc7e 100644 --- a/ios/Sources/Capacitor/CapacitorBridge.swift +++ b/ios/Sources/Capacitor/CapacitorBridge.swift @@ -188,8 +188,11 @@ open class CapacitorBridge: NSObject, CAPBridgeProtocol { */ func exportCoreJS(localUrl: String) { do { - let cookiesEnabled = config.getPluginConfig("CapacitorCookies").getBoolean("enabled", false) - let httpEnabled = config.getPluginConfig("CapacitorHttp").getBoolean("enabled", false) + // A disabled plugin is not registered, so patching fetch/cookies to call it would leave those calls hanging. + let cookiesConfig = config.getPluginConfig("CapacitorCookies") + let cookiesEnabled = cookiesConfig.getBoolean("enabled", false) && !cookiesConfig.getBoolean("disabled", false) + let httpConfig = config.getPluginConfig("CapacitorHttp") + let httpEnabled = httpConfig.getBoolean("enabled", false) && !httpConfig.getBoolean("disabled", false) try JSExport.exportCapacitorGlobalJS(userContentController: webViewDelegationHandler.contentController, isDebug: isDevEnvironment, loggingEnabled: config.loggingEnabled, @@ -324,13 +327,13 @@ open class CapacitorBridge: NSObject, CAPBridgeProtocol { return } + guard !config.getPluginConfig(pluginInstance.jsName).getBoolean("disabled", false) else { + CAPLog.print("⚡️ Not registering disabled plugin \(pluginInstance.jsName)") + return + } if plugins[pluginInstance.jsName] != nil { CAPLog.print("⚡️ Overriding existing registered plugin \(pluginInstance.classForCoder)") } - guard !config.getPluginConfig(plugin.jsName).getBoolean("disabled", false) else { - CAPLog.print("⚡️ Not registering disabled plugin \(plugin.jsName)") - return - } plugins[pluginInstance.jsName] = pluginInstance pluginInstance.load(on: self) diff --git a/ios/Tests/CapacitorTests/DisabledPluginTests.swift b/ios/Tests/CapacitorTests/DisabledPluginTests.swift new file mode 100644 index 0000000000..3791e0892d --- /dev/null +++ b/ios/Tests/CapacitorTests/DisabledPluginTests.swift @@ -0,0 +1,45 @@ +import XCTest +@testable import Capacitor + +class DisabledPluginTests: XCTestCase { + private var bridge: MockBridge! + private var handler: WebViewDelegationHandler! + + override func setUp() { + super.setUp() + let descriptor = InstanceDescriptor() + descriptor.pluginConfigurations = [ + "CapacitorCookies": ["disabled": true], + "CapacitorHttp": ["enabled": true, "disabled": true] + ] + handler = WebViewDelegationHandler() + bridge = MockBridge( + with: InstanceConfiguration(with: descriptor, isDebug: true), + delegate: MockBridgeViewController(), + assetHandler: MockAssetHandler(router: CapacitorRouter()), + delegationHandler: handler + ) + } + + func testDisabledPluginTypeIsNotRegistered() { + bridge.registerPlugin(CAPCookiesPlugin.self) + bridge.registerPlugin(CAPWebViewPlugin.self) + + XCTAssertNil(bridge.plugin(withName: "CapacitorCookies")) + XCTAssertNotNil(bridge.plugin(withName: "WebView")) + } + + func testDisabledPluginInstanceIsNotRegistered() { + bridge.registerPluginInstance(CAPCookiesPlugin()) + + XCTAssertNil(bridge.plugin(withName: "CapacitorCookies")) + } + + // An enabled but disabled CapacitorHttp must not be patched in, or fetch would call a plugin that isn't there. + func testDisabledWinsOverEnabledForPatching() { + let source = handler.contentController.userScripts.map(\.source).joined() + + XCTAssertTrue(source.contains("httpEnabled: false")) + XCTAssertTrue(source.contains("cookiesEnabled: false")) + } +} From 6f0d52a5472559451dd2def7b7b94579f840f5e7 Mon Sep 17 00:00:00 2001 From: Andre Destro Date: Fri, 27 Nov 2026 18:12:23 +0000 Subject: [PATCH 6/6] fix: keep the CapacitorHttp proxy off when the plugin is disabled disabled already wins over enabled for registration and for patching fetch, XHR and document.cookie, but the HTTP interceptor still read only enabled, so with both set a page script could call the proxy directly. Also adds an iOS test for loading a disabled plugin by class name. --- .../com/getcapacitor/WebViewLocalServer.java | 3 +- .../WebViewLocalServerProxyGuardTest.java | 11 +++++- .../Capacitor/WebViewAssetHandler.swift | 3 +- .../CapacitorTests/DisabledPluginTests.swift | 37 +++++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java index 74a463fef2..71135abc28 100755 --- a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +++ b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java @@ -179,7 +179,8 @@ public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) { } if (null != loadingUrl.getPath() && loadingUrl.getPath().startsWith(Bridge.CAPACITOR_HTTP_INTERCEPTOR_START)) { - boolean httpEnabled = bridge.getConfig().getPluginConfiguration("CapacitorHttp").getBoolean("enabled", false); + PluginConfig httpConfig = bridge.getConfig().getPluginConfiguration("CapacitorHttp"); + boolean httpEnabled = httpConfig.getBoolean("enabled", false) && !httpConfig.getBoolean("disabled", false); if (!httpEnabled) { return null; } diff --git a/android/capacitor/src/test/java/com/getcapacitor/WebViewLocalServerProxyGuardTest.java b/android/capacitor/src/test/java/com/getcapacitor/WebViewLocalServerProxyGuardTest.java index c7883b1842..e04b41c2e8 100644 --- a/android/capacitor/src/test/java/com/getcapacitor/WebViewLocalServerProxyGuardTest.java +++ b/android/capacitor/src/test/java/com/getcapacitor/WebViewLocalServerProxyGuardTest.java @@ -42,6 +42,7 @@ public class WebViewLocalServerProxyGuardTest { private MockedStatic loggerStatic; private WebViewLocalServer server; private Bridge bridge; + private PluginConfig httpConfig; @Before public void setUp() { @@ -50,7 +51,7 @@ public void setUp() { uriStatic.when(() -> Uri.parse("https://localhost")).thenReturn(appOrigin); loggerStatic = mockStatic(Logger.class); - PluginConfig httpConfig = mock(PluginConfig.class); + httpConfig = mock(PluginConfig.class); when(httpConfig.getBoolean("enabled", false)).thenReturn(true); CapConfig config = mock(CapConfig.class); when(config.getPluginConfiguration("CapacitorHttp")).thenReturn(httpConfig); @@ -166,6 +167,14 @@ public void blocksSubframeDocumentLoadOnEveryProxyRoute() { } } + @Test + public void httpInterceptorIsOffWhenCapacitorHttpIsDisabled() { + // disabled wins over enabled: the plugin isn't registered, so its proxy must not be served either + when(httpConfig.getBoolean("disabled", false)).thenReturn(true); + assertNull(server.shouldInterceptRequest(subResource("localhost", HTTP_PATH))); + loggerStatic.verify(() -> Logger.debug(anyString()), never()); + } + @Test public void blocksRequestWithoutHost() { assertBlocked(request(uri("https", null, -1, FILE_PATH), false, Collections.emptyMap())); diff --git a/ios/Sources/Capacitor/WebViewAssetHandler.swift b/ios/Sources/Capacitor/WebViewAssetHandler.swift index e71a702522..27c9cfaf9f 100644 --- a/ios/Sources/Capacitor/WebViewAssetHandler.swift +++ b/ios/Sources/Capacitor/WebViewAssetHandler.swift @@ -37,7 +37,8 @@ open class WebViewAssetHandler: NSObject, WKURLSchemeHandler { if url.path.starts(with: CapacitorBridge.httpInterceptorStartIdentifier) { // Only serve the proxy when CapacitorHttp is on. A scheme task can't tell a document from // a subresource, so keeping documents out relies on the check in decidePolicyFor. - if configuration?.getPluginConfig("CapacitorHttp").getBoolean("enabled", false) == true { + let httpConfig = configuration?.getPluginConfig("CapacitorHttp") + if httpConfig?.getBoolean("enabled", false) == true && httpConfig?.getBoolean("disabled", false) != true { handleCapacitorHttpRequest(urlSchemeTask, localUrl, false) } else { urlSchemeTask.didFailWithError(URLError(.unsupportedURL)) diff --git a/ios/Tests/CapacitorTests/DisabledPluginTests.swift b/ios/Tests/CapacitorTests/DisabledPluginTests.swift index 3791e0892d..3de77d3ff8 100644 --- a/ios/Tests/CapacitorTests/DisabledPluginTests.swift +++ b/ios/Tests/CapacitorTests/DisabledPluginTests.swift @@ -1,6 +1,22 @@ +import WebKit import XCTest @testable import Capacitor +private class RecordingSchemeTask: NSObject, WKURLSchemeTask { + let request: URLRequest + private(set) var failure: Error? + + init(url: URL) { + self.request = URLRequest(url: url) + super.init() + } + + func didReceive(_ response: URLResponse) {} + func didReceive(_ data: Data) {} + func didFinish() {} + func didFailWithError(_ error: Error) { failure = error } +} + class DisabledPluginTests: XCTestCase { private var bridge: MockBridge! private var handler: WebViewDelegationHandler! @@ -35,6 +51,27 @@ class DisabledPluginTests: XCTestCase { XCTAssertNil(bridge.plugin(withName: "CapacitorCookies")) } + // Page scripts can also reach a plugin by its class name, which loads it on demand. + func testDisabledPluginIsNotLoadedByClassName() { + bridge.handleJSCall(call: JSCall(options: [:], pluginId: "CAPCookiesPlugin", method: "getCookies", callbackId: "1")) + bridge.handleJSCall(call: JSCall(options: [:], pluginId: "CAPWebViewPlugin", method: "noSuchMethod", callbackId: "2")) + + XCTAssertNil(bridge.plugin(withName: "CapacitorCookies")) + XCTAssertNotNil(bridge.plugin(withName: "WebView")) + } + + // disabled wins over enabled for the HTTP interceptor too, so page scripts can't reach it directly. + func testDisabledHttpPluginIsNotServedByTheInterceptor() throws { + let assetHandler = WebViewAssetHandler(router: CapacitorRouter()) + assetHandler.setConfiguration(bridge.config) + let url = try XCTUnwrap(URL(string: "capacitor://localhost\(CapacitorBridge.httpInterceptorStartIdentifier)?u=https%3A%2F%2Fexample.com")) + let task = RecordingSchemeTask(url: url) + + assetHandler.webView(WKWebView(), start: task) + + XCTAssertEqual((task.failure as? URLError)?.code, .unsupportedURL) + } + // An enabled but disabled CapacitorHttp must not be patched in, or fetch would call a plugin that isn't there. func testDisabledWinsOverEnabledForPatching() { let source = handler.contentController.userScripts.map(\.source).joined()