perf: move sandboxed environment to the C side (#2058)

This PR uses `zend_array_dup` to simplify and optimize the environment sandboxing
logic. It also guarantees no environment leakage on FrankenPHP restarts.
This commit is contained in:
Alexander Stecher
2026-02-26 22:34:54 +01:00
committed by GitHub
parent 25ed020036
commit 8f4412cbbf
14 changed files with 158 additions and 213 deletions

View File

@@ -61,6 +61,16 @@ func escapeMetricLabel(s string) string {
return strings.ReplaceAll(s, "\\", "\\\\")
}
func TestMain(m *testing.M) {
// setup custom environment vars for TestOsEnv
if os.Setenv("ENV1", "value1") != nil || os.Setenv("ENV2", "value2") != nil {
fmt.Println("Failed to set environment variables for tests")
os.Exit(1)
}
os.Exit(m.Run())
}
func TestPHP(t *testing.T) {
var wg sync.WaitGroup
tester := caddytest.NewTester(t)
@@ -957,9 +967,6 @@ func testSingleIniConfiguration(tester *caddytest.Tester, key string, value stri
}
func TestOsEnv(t *testing.T) {
require.NoError(t, os.Setenv("ENV1", "value1"))
require.NoError(t, os.Setenv("ENV2", "value2"))
tester := caddytest.NewTester(t)
tester.InitServer(`
{

View File

@@ -5,13 +5,13 @@ package caddy
import (
"encoding/json"
"errors"
"os"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/dunglas/frankenphp"
"github.com/dunglas/mercure"
mercureCaddy "github.com/dunglas/mercure/caddy"
"os"
)
func init() {
@@ -67,5 +67,5 @@ func createMercureRoute() (caddyhttp.Route, error) {
},
}
return mercureRoute, nil;
return mercureRoute, nil
}

View File

@@ -253,8 +253,8 @@ func cmdPHPServer(fs caddycmd.Flags) (int, error) {
if mercure {
mercureRoute, err := createMercureRoute()
if err != nil {
return caddy.ExitCodeFailedStartup, err
}
return caddy.ExitCodeFailedStartup, err
}
subroute.Routes = append(caddyhttp.RouteList{mercureRoute}, subroute.Routes...)
}