Repository navigation
SAPI: Convert sapi_getenv to zend_string - #23386
NattyNarwhal wants to merge 1 commit into
Conversation
19195ea to
94c08df
Compare
LamentXU123
left a comment
There was a problem hiding this comment.
Phar part. I am in favor of this.
| entry = path_info; | ||
| if (zs_path_info) { | ||
| entry = estrdup(ZSTR_VAL(zs_path_info)); | ||
| entry_len = strlen(entry); |
There was a problem hiding this comment.
This can just be
| entry_len = strlen(entry); | |
| entry_len = ZSTR_LEN(zs_path_info); |
There was a problem hiding this comment.
estrdup will cut off after the first NUL byte, so if there's an embedded NUL byte then entry_len will go out of bounds. Can't happen with environment variables on their wn, but perhaps via input filters it can
There was a problem hiding this comment.
:| welp I hope that input filter can't mess up like that but this is PHP we're talking about. Might make sense to guard strings that have nul bytes in them as that seems potentially problematic.
There was a problem hiding this comment.
FWIW, I do have a draft branch (not committed/pushed yet) for trying to make input filters use zend_string as well (since it's ugly in sapi_getenv and also the API of the filters are as well), but it's a mild slog that touches a surprising amount of stuff.
For the phar path mangling here, this should also just be zend_stringified the whole way through. Doable, just annoying
There was a problem hiding this comment.
I was working on converting char* to zend_string in phar and there might even still be a PR somewhere that does this (or somewhere on my local machine) but those conversions can be quite tedious yeah.
There was a problem hiding this comment.
The more I read anything input filter related, the more I'm convinced that they're entirely vestigial. The default is a nop, and the only extensions registering one are filter (for the sake of the deprecated filter.default INI option) and ancient security modules like Suhosin. (Modern ones like Snuffleupagus don't seem to bother.) The only docs are also PHP 4 era. They should probably just be removed and simplify a bunch of stuff in SAPI and exts, but that's probably blocking on the filter INI option and thus PHP 9?
As for the other indirected SAPI functions, treat data does seem to be in active use, though filter is the only modern extension I've found registering a handle. POST reader I think is similarly in use and being overridden by mbstring.
| ret = getenv(name); | ||
| } | ||
| return ret ? zend_string_init(ret, strlen(ret), 0) : NULL; | ||
| #else |
There was a problem hiding this comment.
I don't understand the logic. But seems like you've missed something here? if ret is not empty we should use ret, this is also true in the else branch.
There was a problem hiding this comment.
I forgot the logic in the Windows ifdef, that should be fixed.
94c08df to
fc20723
Compare
|
Addressed your comments. |
|
I thought that failure on Windows was related (sapi/cgi/010.phpt), but I can't reproduce it on Windows myself. |
fc20723 to
c5b286f
Compare
c5b286f to
e802aeb
Compare
The old char* return was very ambigious at the SAPI level about ownership per platform (unix returned environ buffer, Win32 returned converted buffer), which it then estrduped at the SAPI frontend level. To clarify this, instead return an allocated zend_string in the SAPIs which we bubble up to consumers. There are some annoyances still; input filters have to shuffle some buffers because input filters expect to work on raw buffers, and phar does some mildly harrowing string mangling without zend_string. These should be cleaned up. There is also the question if SAPIs just bubbling up getenv when they lack a web server specific context i.e. from fcgi or Apache (CGI, FPM, Litespeed), as this seems not quite the right intent; CLI doesn't implement a getenv function for instance.
e802aeb to
39a8aed
Compare
|
@Girgias Figured you might want to take another look before your vacation. |
Girgias
left a comment
There was a problem hiding this comment.
Mostly good, some minor changes are still needed I think.
| if (zs_path_info) { | ||
| entry = estrdup(ZSTR_VAL(zs_path_info)); | ||
| entry_len = strlen(entry); /* not ZSTR_LEN, b/c strdup truncates on nul */ | ||
| spprintf(&path_info, 0, "%s%s", ZSTR_VAL(testit), ZSTR_VAL(zs_path_info)); |
There was a problem hiding this comment.
I was wondering if path_info could be a zend_string* too but that requires changing the SG(request_info).request_uri global to zend_string first.
| /* XXX: pretty ugly because input filters aren't zend_string yet. */ | ||
| char *tmp = estrdup(ZSTR_VAL(value)); | ||
| size_t tmp_len = 0; | ||
| sapi_module.input_filter(PARSE_STRING, name, &tmp, ZSTR_LEN(value), &tmp_len); | ||
| zend_string_release(value); | ||
| value = zend_string_init(tmp, tmp_len, 0); | ||
| efree(tmp); |
There was a problem hiding this comment.
Eh this is fine as this is going to be somewhat temporary I imagine.
| return NULL; | ||
| } | ||
|
|
||
| valw = emalloc((size + 1) * sizeof(wchar_t)); |
There was a problem hiding this comment.
Aside: this might want to be converted to safe_emalloc.
| #ifndef PHP_WIN32 | ||
| if (ret) return ret; | ||
| /* if cgi, or fastcgi and not found in fcgi env | ||
| check the regular environment */ | ||
| return getenv(name); | ||
| if (!ret) { | ||
| /* if cgi, or fastcgi and not found in fcgi env | ||
| check the regular environment */ | ||
| ret = getenv(name); | ||
| } | ||
| return ret ? zend_string_init(ret, strlen(ret), 0) : NULL; | ||
| #else | ||
| if (ret) { | ||
| /* The functions outside here don't know, where does it come | ||
| from. They'll need to free the returned memory as it's | ||
| not necessary from the fcgi env. */ | ||
| return strdup(ret); | ||
| return zend_string_init(ret, strlen(ret), 0); | ||
| } | ||
| /* if cgi, or fastcgi and not found in fcgi env | ||
| check the regular environment */ | ||
| return cgi_getenv_win32(name, name_len); | ||
| #endif | ||
| } |
There was a problem hiding this comment.
Can't this whole ifdef block be replace by sapi_cgi_getenv ? As it seems to be duplicating that exact logic?
| } | ||
|
|
||
| /* if fpm has not started yet, use std env */ | ||
| return getenv(name); | ||
| if (!var) { | ||
| /* if fpm has not started yet, use std env */ | ||
| var = getenv(name); /* XXX: should we? */ | ||
| } | ||
| return var ? zend_string_init(var, strlen(var), 0) : NULL; |
There was a problem hiding this comment.
This feels like it should be an if-else rather than checking that var is NULL.
| if (path_info) { | ||
| php_self_len = script_name_len + ZSTR_LEN(path_info); | ||
| /* Concat script_name and path_info into php_self */ | ||
| php_self = zend_cstr_concat( |
There was a problem hiding this comment.
For a future PR: it probably makes sense to change the php_self global to be a zend_string* as this feels like it's required for the zend_stringification of the input SAPI API anyway.
| char *path, *server_name; | ||
| char *path; | ||
| zend_string *server_name; | ||
| size_t path_len, server_name_len; |
There was a problem hiding this comment.
Is the server_name_len var still needed?
The old char* return was very ambigious at the SAPI level about ownership per platform (unix returned environ buffer, Win32 returned converted buffer), which it then estrduped at the SAPI frontend level. To clarify this, instead return an allocated zend_string in the SAPIs which we bubble up to consumers.
There are some annoyances still; input filters have to shuffle some buffers because input filters expect to work on raw buffers, and phar does some mildly harrowing string mangling without zend_string. These should be cleaned up.
There is also the question if SAPIs just bubbling up getenv when they lack a web server specific context i.e. from fcgi or Apache (CGI, FPM, Litespeed), as this seems not quite the right intent; CLI doesn't implement a getenv function for instance.