warmcat / libwebsockets

canonical libwebsockets.org networking library
https://libwebsockets.org
Other
4.78k stars 1.49k forks source link

CGI environment variables start with '=' #3222

Closed ribes96 closed 1 month ago

ribes96 commented 2 months ago

Hi I'm running a CGI server using libwebsockets on linux I find the value of all the CGI environment variables is starting with an equal sign =

PATH==/bin:/usr/bin:/usr/local/bin:/var/www/cgi-bin REQUEST_METHOD==GET QUERY_STRING== REQUEST_URI==/cgi/a/b/c PATH_INFO==/a/b/c HTTP_HOST==ntime HTTP_USER_AGENT==Mozilla/5.0 (X11; Linux x86_64; rv:130.0) Gecko/20100101 Firefox/130.0 HTTP_ACCEPT==text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/png,image/svg+xml,/;q=0.8 HTTP_ACCEPT_ENCODING==gzip, deflate SCRIPT_PATH==/usr/bin/my-cgi-script.sh SERVER_SOFTWARE==lws

Seems the following code is wrong:

    while (i->env_array[m]){
        const char *p = strchr(i->env_array[m], '=');
        int naml = lws_ptr_diff(p, i->env_array[m]);
        char enam[32];

        lws_strnncpy(enam, i->env_array[m], naml, sizeof(enam));
        setenv(enam, p, 1);
        m++;
    }

And the fix seems trivial: just add a ++p in setenv:

    while (i->env_array[m]){
        const char *p = strchr(i->env_array[m], '=');
        int naml = lws_ptr_diff(p, i->env_array[m]);
        char enam[32];

        lws_strnncpy(enam, i->env_array[m], naml, sizeof(enam));
        setenv(enam, ++p, 1);
        m++;
    }
lws-team commented 2 months ago

What version of lws is this?

ribes96 commented 2 months ago

Sorry This is libwebsockets-4.3.3, and also current main commit

lws-team commented 1 month ago

Thanks, this is pushed on main and v4-3-stable