【发布时间】:2012-07-22 05:33:20
【问题描述】:
首先,我是 Web 开发的新手。我正在制作一个简单的 mod_wsgi webapp,它有两个文本字段来接受用户输入。
第一个输入 nnodes 必须是 0-30 之间的整数。
第二个输入,大小,必须是整数或介于 0-20 之间的浮点数。
这是迄今为止我在脚本中的验证/清理。看到我稍后在脚本中如何使用输入重定向,我希望有人可以评论我是否容易受到任何重大恶意威胁:
nnodes = escape(nnodes)
size = escape(size)
if nnodes.isdigit() and int(nnodes) in range(31):
pass
elif nnodes=='':
response_body=html % ' '
status='200 OK'
response_headers = [('Content-Type', 'text/html'),('Content-Length',str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
else:
response_body=html % 'Please enter the number of malignant nodes as a whole number between 0 and 30.'
status='200 OK'
response_headers = [('Content-Type', 'text/html'),('Content-Length',str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
###Validate that the tumorsize is a float between 0-25.
try:
size=='' or float(size)
pass
except:
response_body=html % 'Please enter the tumor size as a number between 0 and 25.'
status='200 OK'
response_headers = [('Content-Type', 'text/html'),('Content-Length',str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
if 0<=float(size)<=25:
pass
elif size=='':
response_body=html % ' '
status='200 OK'
response_headers = [('Content-Type', 'text/html'),('Content-Length',str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
else:
response_body=html % 'Please enter the tumor size as a number between 0 and 25.'
status='200 OK'
response_headers = [('Content-Type', 'text/html'),('Content-Length',str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
###After the validation, I use input redirection to pass the input to an R script. I know this is not optimal but I can't get the Rpy2 module to work on my server.
###I also know that input redirection can open an app up to shell injection, but that is why I am asking you all if I've done sufficient validation and sanitization.
commandString="/home/usr/bin/R --no-save --quiet --slave --args " + str(nnodes) + " " + str(size) + " </home/usr/webapps/simple/htdocs/webcalc.R"
subprocess.call(commandString,shell=True)
感谢大家提供的任何建议。
【问题讨论】:
-
顺便说一句,您确实应该使用框架。 Flask 很好很简单,可以让你摆脱很多样板文件。如果您需要更多由数据库驱动的东西,您可以考虑使用 Django,因为它有一个很好的内置 ORM 和许多其他功能。
标签: python web-applications security