Does Python CGIHTTPServer decode plus sign(+) in URL into blank space?

100 views Asked by At

In my html, I have below form:

<form method=GET action="/cgi-bin/encry.sh"> 
<table nowrap> 
<tr>
<td>Plain Text:</TD>
<TD><input type="text" name="PlainText"></td>
</tr> 
</table>
<input type="submit" value="Encrypt"> 
</form>

After inputing "aaa +=" and clicking the button, in my cgi-bin/encry.sh, the QUERY_STRING is assigned as "aaa++=" rather than "aaa +=", nor "a+%2B%3D". Is that correct behavior, and if so how can I get the blank space correctly? If not, is that fixed in any later CGIHTTPServer version?

Below provides some info about CGIHTTPServer.py in my CentOS 7.2:

HiAccount-4# md5sum /usr/lib64/python2.7/CGIHTTPServer.py
564afe4defc63001f236b0b2ef899b58  /usr/lib64/python2.7/CGIHTTPServer.py
HiAccount-4# grep __version /usr/lib64/python2.7/CGIHTTPServer.py -i
__version__ = "0.4"
HiAccount-4# grep unquote /usr/lib64/python2.7/CGIHTTPServer.py -i -C 3 -n
84-        path begins with one of the strings in self.cgi_directories
85-        (and the next character is a '/' or the end of the string).
86-        """
87:        collapsed_path = _url_collapse_path(urllib.unquote(self.path))
88-        dir_sep = collapsed_path.find('/', 1)
89-        head, tail = collapsed_path[:dir_sep], collapsed_path[dir_sep+1:]
90-        if head in self.cgi_directories:
--
164-        env['SERVER_PROTOCOL'] = self.protocol_version
165-        env['SERVER_PORT'] = str(self.server.server_port)
166-        env['REQUEST_METHOD'] = self.command
167:        uqrest = urllib.unquote(rest)
168-        env['PATH_INFO'] = uqrest
169-        env['PATH_TRANSLATED'] = self.translate_path(uqrest)
170-        env['SCRIPT_NAME'] = scriptname

Thanks in advance!

1

There are 1 answers

0
Qiu Yangfan On

After trying the CGIHTTPServer from python2.7.18, I think the asked question is a known issue in 2.7.5, which was my version, and not sure which version fixed it.

The problem is in:

/usr/lib64/python2.7/CGIHTTPServer.py: 87: collapsed_path = _url_collapse_path(urllib.unquote(self.path))

In 2.7.18, the QUERY_STRING isn't decoded by CGIHTTPServer, and I need to decode it in my CGI script, but that's OK as it's "correct" encoded QUERY_STRING.

BTW, I didn't upgrade my python in OS from 2.7.5 to 2.7.18, but just extract CGIHTTPServer from python 2.7.18 source code and use it as:

nohup python ./CGIHTTPServer.py 7070 &

rather than

nohup python -m CGIHTTPServer 7070 &