首先,我是 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)
我感谢大家必须提供的任何建议。