奇怪的行为打印CGI FieldStorage值(Python)

时间:2019-01-04 10:55:11

标签: python cgi heisenbug

我在下面编写一个简单的Python 3 CGI脚本:

#!/usr/bin/python3

print("Content-Type: text/html")
print()
print()

import cgi
import cgitb; cgitb.enable()

storage = cgi.FieldStorage() 
print(storage)
print()
print(storage.getvalue("command", "Command key not present."))

在不提供key的情况下在浏览器中运行此程序,一切正常:我得到了预期的输出:

Content-Type: text/html

FieldStorage(None, None, [])

Command key not present.

提供?command=abcdefghijklmnoprstuvwxyz的效果也很好:

Content-Type: text/html

FieldStorage(None, None, [MiniFieldStorage('command', 'abcdefghijklmnoprstuvwxyz')])

abcdefghijklmnoprstuvwxyz

但是,当我尝试删除字符时,事情变得很棘手。更改为?command=abcdefghijklmnoprstuvwxy(注意缺少的z):

Content-Type: text/html

FieldStorage(None, None, [MiniFieldStorage('command', 'abcdefghijklmnoprstuvwxy(')])

abcdefghijklmnoprstuvwxy(

现在有一个额外的左括号(,而不是省略的z

更改为?command=abcdefghijklmnoprstuvwx会产生...完全一样的东西!

这一直持续到?command=abcdefghijklmnoprst,此时我们得到:

Content-Type: text/html

FieldStorage(None, None, [MiniFieldStorage('command', 'abcdefghijklmnoprstee')])

abcdefghijklmnoprstee

这再次持续到?command=abcdefghijk,此时我们得到了预期的行为!

Content-Type: text/html

FieldStorage(None, None, [MiniFieldStorage('command', 'abcdefghijk')])

abcdefghijk

但是,我们停留在abcdefghijk直到?command=abc,此时它变为:

Content-Type: text/html

FieldStorage(None, None, [MiniFieldStorage('command', 'abcd)')])

abcd)

尝试ab和最后a都可以产生预期的行为。

这是什么骗术?

0 个答案:

没有答案