Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions frankenphp.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ frankenphp_config frankenphp_get_config() {
};
}

bool should_filter_var = 0;
__thread uintptr_t thread_index;
__thread bool is_worker_thread = false;
__thread zval *os_environment = NULL;
Expand Down Expand Up @@ -613,8 +612,7 @@ void frankenphp_register_trusted_var(zend_string *z_key, char *value,
}
size_t new_val_len = val_len;

if (!should_filter_var ||
sapi_module.input_filter(PARSE_SERVER, ZSTR_VAL(z_key), &value,
if (sapi_module.input_filter(PARSE_SERVER, ZSTR_VAL(z_key), &value,
new_val_len, &new_val_len)) {
zval z_value;
ZVAL_STRINGL_FAST(&z_value, value, new_val_len);
Expand Down Expand Up @@ -743,8 +741,7 @@ void frankenphp_register_variable_safe(char *key, char *val, size_t val_len,
val = "";
}
size_t new_val_len = val_len;
if (!should_filter_var ||
sapi_module.input_filter(PARSE_SERVER, key, &val, new_val_len,
if (sapi_module.input_filter(PARSE_SERVER, key, &val, new_val_len,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would also be fine with just keeping the current logic in worker mode:

Suggested change
if (sapi_module.input_filter(PARSE_SERVER, key, &val, new_val_len,
if ((is_worker_thread && !should_filter_var) || sapi_module.input_filter(PARSE_SERVER, key, &val, new_val_len,

&new_val_len)) {
php_register_variable_safe(key, val, new_val_len, track_vars_array);
}
Expand Down Expand Up @@ -916,12 +913,6 @@ static void *php_main(void *arg) {

frankenphp_sapi_module.startup(&frankenphp_sapi_module);

/* check if a default filter is set in php.ini and only filter if
* it is, this is deprecated and will be removed in PHP 9 */
char *default_filter;
cfg_get_string("filter.default", &default_filter);
should_filter_var = default_filter != NULL;

go_frankenphp_main_thread_is_ready();

/* channel closed, shutdown gracefully */
Expand Down
40 changes: 40 additions & 0 deletions frankenphp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,46 @@ func testInput(t *testing.T, opts *testOptions) {
}, opts)
}

func TestFilterInputDefault_module(t *testing.T) { testFilterInputDefault(t, nil) }
func TestFilterInputDefault_worker(t *testing.T) {
testFilterInputDefault(t, &testOptions{workerScript: "filter.php"})
}
func testFilterInputDefault(t *testing.T, opts *testOptions) {
if opts == nil {
opts = &testOptions{}
}
opts.initOpts = append(opts.initOpts, frankenphp.WithPhpIni(map[string]string{
"filter.default": "string.tolower",
}))
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
req := httptest.NewRequest("GET", "http://example.com/filter.php", nil)
w := httptest.NewRecorder()
handler(w, req)

resp := w.Result()
body, _ := io.ReadAll(resp.Body)

assert.Equal(t, "GET", string(body))
}, opts)
}

func TestFilterInput_module(t *testing.T) { testFilterInput(t, nil) }
func TestFilterInput_worker(t *testing.T) {
testFilterInput(t, &testOptions{workerScript: "filter.php"})
}
func testFilterInput(t *testing.T, opts *testOptions) {
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
req := httptest.NewRequest("GET", "http://example.com/filter.php", nil)
w := httptest.NewRecorder()
handler(w, req)

resp := w.Result()
body, _ := io.ReadAll(resp.Body)

assert.Equal(t, "GET", string(body))
}, opts)
}

func TestPostSuperGlobals_module(t *testing.T) { testPostSuperGlobals(t, nil) }
func TestPostSuperGlobals_worker(t *testing.T) {
testPostSuperGlobals(t, &testOptions{workerScript: "super-globals.php"})
Expand Down
6 changes: 6 additions & 0 deletions testdata/filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
require_once __DIR__.'/_executor.php';

return function () {
echo strtoupper(filter_input(INPUT_SERVER, "REQUEST_METHOD", FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH) ?? "");
};