forked from jvoisin/snuffleupagus
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ported server.strip and server.encode features from suhosin
- Loading branch information
Showing
10 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#include "php_snuffleupagus.h" | ||
|
||
static void (*orig_register_server_variables)(zval *track_vars_array) = NULL; | ||
|
||
static const unsigned char sp_hexchars[] = "0123456789ABCDEF"; | ||
|
||
static const char sp_is_dangerous_char[256] = { | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | ||
}; | ||
|
||
static void sp_server_strip(HashTable *svars, char *key, int keylen) { | ||
zval *value = zend_hash_str_find(svars, key, keylen); | ||
if (!value || Z_TYPE_P(value) != IS_STRING) { return; } | ||
|
||
zend_string *tmp_zstr = Z_STR_P(value); | ||
char *tmp = ZSTR_VAL(tmp_zstr); | ||
char *tmpend = tmp + ZSTR_LEN(tmp_zstr); | ||
|
||
for (char *p = tmp; p < tmpend; p++) { | ||
if (sp_is_dangerous_char[(int)*p]) { | ||
*p = '_'; | ||
} | ||
} | ||
} | ||
|
||
static void sp_server_encode(HashTable *svars, char *key, int keylen) { | ||
zval *value = zend_hash_str_find(svars, key, keylen); | ||
if (!value || Z_TYPE_P(value) != IS_STRING) { return; } | ||
|
||
zend_string *tmp_zstr = Z_STR_P(value); | ||
char *tmp = ZSTR_VAL(tmp_zstr); | ||
char *tmpend = tmp + ZSTR_LEN(tmp_zstr); | ||
int extra = 0; | ||
|
||
for (char *p = tmp; p < tmpend; p++) { | ||
extra += sp_is_dangerous_char[(int)*p] * 2; | ||
} | ||
if (!extra) { return; } | ||
|
||
zend_string *new_zstr = zend_string_alloc(ZSTR_LEN(tmp_zstr) + extra, 0); | ||
char *n = ZSTR_VAL(new_zstr); | ||
for (char *p = tmp; p < tmpend; p++, n++) { | ||
if (sp_is_dangerous_char[(int)*p]) { | ||
*n++ = '%'; | ||
*n++ = sp_hexchars[*p >> 4]; | ||
*n = sp_hexchars[*p & 15]; | ||
} else { | ||
*n = *p; | ||
} | ||
} | ||
ZSTR_VAL(new_zstr)[ZSTR_LEN(new_zstr)] = 0; | ||
Z_STR_P(value) = new_zstr; | ||
|
||
zend_string_release_ex(tmp_zstr, 0); | ||
} | ||
|
||
static void sp_register_server_variables(zval *track_vars_array) { | ||
orig_register_server_variables(track_vars_array); | ||
|
||
HashTable *svars; | ||
svars = Z_ARRVAL_P(track_vars_array); | ||
|
||
|
||
if (SNUFFLEUPAGUS_G(config).server_encode) { | ||
sp_server_encode(svars, ZEND_STRL("REQUEST_URI")); | ||
sp_server_encode(svars, ZEND_STRL("QUERY_STRING")); | ||
} | ||
|
||
if (SNUFFLEUPAGUS_G(config).server_strip) { | ||
sp_server_strip(svars, ZEND_STRL("PHP_SELF")); | ||
sp_server_strip(svars, ZEND_STRL("HTTP_HOST")); | ||
sp_server_strip(svars, ZEND_STRL("HTTP_USER_AGENT")); | ||
|
||
// for cgi + fpm | ||
sp_server_strip(svars, ZEND_STRL("PATH_INFO")); | ||
sp_server_strip(svars, ZEND_STRL("PATH_TRANSLATED")); | ||
sp_server_strip(svars, ZEND_STRL("ORIG_PATH_TRANSLATED")); | ||
sp_server_strip(svars, ZEND_STRL("ORIG_PATH_INFO")); | ||
} | ||
} | ||
|
||
void sp_hook_register_server_variables() | ||
{ | ||
if (sapi_module.register_server_variables) { | ||
orig_register_server_variables = sapi_module.register_server_variables; | ||
sapi_module.register_server_variables = sp_register_server_variables; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
void sp_hook_register_server_variables(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
sp.global.server_encode.enable(); | ||
sp.global.server_strip.enable(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
input filter: server_encode | ||
--SKIPIF-- | ||
<?php if (!extension_loaded("snuffleupagus")) print "skip"; ?> | ||
--INI-- | ||
sp.configuration_file={PWD}/config/filter.ini | ||
display_errors=1 | ||
display_startup_errors=1 | ||
error_reporting=E_ALL | ||
--ENV-- | ||
return <<<EOF | ||
REQUEST_URI=AAA<>"'`!AAA | ||
EOF; | ||
--COOKIE-- | ||
--GET-- | ||
BBB<>"'`!BBB | ||
--POST-- | ||
--FILE-- | ||
<?php | ||
var_dump($_SERVER['REQUEST_URI']); | ||
var_dump($_SERVER['QUERY_STRING']); | ||
--EXPECT-- | ||
string(22) "AAA%3C%3E%22%27%60!AAA" | ||
string(22) "BBB%3C%3E%22%27%60!BBB" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
input filter: server_strip | ||
--SKIPIF-- | ||
<?php if (!extension_loaded("snuffleupagus")) print "skip"; ?> | ||
--INI-- | ||
sp.configuration_file={PWD}/config/filter.ini | ||
display_errors=1 | ||
display_startup_errors=1 | ||
error_reporting=E_ALL | ||
--ENV-- | ||
return <<<EOF | ||
HTTP_USER_AGENT=Mozilla/5.0 (Windows NT 6.0; rv:29.0) <script>alert('123');</script>Gecko/20100101 Firefox/29.0 | ||
EOF; | ||
--COOKIE-- | ||
--GET-- | ||
--POST-- | ||
--FILE-- | ||
<?php | ||
var_dump($_SERVER['HTTP_USER_AGENT']); | ||
--EXPECT-- | ||
string(95) "Mozilla/5.0 (Windows NT 6.0; rv:29.0) _script_alert(_123_);_/script_Gecko/20100101 Firefox/29.0" |