From fe8a7c607b2f5207cf367692e38c119aa309e484 Mon Sep 17 00:00:00 2001 From: Christoph Pakulski Date: Mon, 24 Nov 2025 20:42:57 +0000 Subject: [PATCH] Use effective context when calling foreign functions. Signed-off-by: Christoph Pakulski --- changelogs/current.yaml | 6 ++++++ source/common/runtime/runtime_features.cc | 1 + source/extensions/common/wasm/foreign.cc | 12 ++++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/changelogs/current.yaml b/changelogs/current.yaml index e6196e82ae37e..b8558bb4053cc 100644 --- a/changelogs/current.yaml +++ b/changelogs/current.yaml @@ -81,6 +81,12 @@ minor_behavior_changes: change: | Use mobile specific network observer registries to propagate network change signals. This behavior can be reverted by setting the runtime guard ``envoy.reloadable_features.mobile_use_network_observer_registry``. +- area: wasm + change: | + Foreign functions are executed on effective context, if such is defined. Effective context is set by wasm SDKs, but was not used + when foreign function was called. This fixes an issue where a foreign function is called from HTTP or GRPC callback and + that foreign function needs a stream context, not root context. This behavior can be reverted by + setting the runtime guard ``envoy.reloadable_features.wasm_use_effective_ctx_for_foreign_functions`` to false. bug_fixes: # *Changes expected to improve the state of the world and are unlikely to have negative effects* diff --git a/source/common/runtime/runtime_features.cc b/source/common/runtime/runtime_features.cc index 388178bb10f53..8b053afec6a73 100644 --- a/source/common/runtime/runtime_features.cc +++ b/source/common/runtime/runtime_features.cc @@ -86,6 +86,7 @@ RUNTIME_GUARD(envoy_reloadable_features_use_migration_in_quiche); RUNTIME_GUARD(envoy_reloadable_features_use_response_decoder_handle); RUNTIME_GUARD(envoy_reloadable_features_validate_connect); RUNTIME_GUARD(envoy_reloadable_features_validate_upstream_headers); +RUNTIME_GUARD(envoy_reloadable_features_wasm_use_effective_ctx_for_foreign_functions); RUNTIME_GUARD(envoy_reloadable_features_websocket_allow_4xx_5xx_through_filter_chain); RUNTIME_GUARD(envoy_reloadable_features_websocket_enable_timeout_on_upgrade_response); RUNTIME_GUARD(envoy_reloadable_features_xds_failover_to_primary_enabled); diff --git a/source/extensions/common/wasm/foreign.cc b/source/extensions/common/wasm/foreign.cc index 64cdd64e985fa..c428ac1c7dcc0 100644 --- a/source/extensions/common/wasm/foreign.cc +++ b/source/extensions/common/wasm/foreign.cc @@ -210,7 +210,11 @@ RegisterForeignFunction registerSetEnvoyFilterStateForeignFunction( const std::function&) -> WasmResult { envoy::source::extensions::common::wasm::SetEnvoyFilterStateArguments args; if (args.ParseFromString(arguments)) { - auto context = static_cast(proxy_wasm::current_context_); + auto context = static_cast( + Runtime::runtimeFeatureEnabled( + "envoy.reloadable_features.wasm_use_effective_ctx_for_foreign_functions") + ? proxy_wasm::contextOrEffectiveContext() + : proxy_wasm::current_context_); return context->setEnvoyFilterState(args.path(), args.value(), toFilterStateLifeSpan(args.span())); } @@ -220,7 +224,11 @@ RegisterForeignFunction registerSetEnvoyFilterStateForeignFunction( RegisterForeignFunction registerClearRouteCacheForeignFunction( "clear_route_cache", [](WasmBase&, std::string_view, const std::function&) -> WasmResult { - auto context = static_cast(proxy_wasm::current_context_); + auto context = static_cast( + Runtime::runtimeFeatureEnabled( + "envoy.reloadable_features.wasm_use_effective_ctx_for_foreign_functions") + ? proxy_wasm::contextOrEffectiveContext() + : proxy_wasm::current_context_); context->clearRouteCache(); return WasmResult::Ok; });