web.py:如何有选择地隐藏任何 HTTP 方法的 404 资源?
我想根据 web.py 中某种形式的身份验证有选择地隐藏一些资源,但它们的存在是通过对我尚未实现的任何 HTTP 方法的 405 响应来揭示的。
下面是一个示例:
import web
urls = (
'/secret', 'secret',
)
app = web.application(urls, globals())
class secret():
def GET(self):
if web.cookies().get('password') == 'secretpassword':
return "Dastardly secret plans..."
raise web.notfound()
if __name__ == "__main__":
app.run()
当发出未定义的方法请求时,资源就会被泄露:
$ curl -v -X DELETE http://localhost:8080/secret
...
> DELETE /secret HTTP/1.1
...
< HTTP/1.1 405 Method Not Allowed
< Content-Type: text/html
< Allow: GET
...
我可以对 HTTP 规范中的其他常见方法实现相同的检查,但创造性的恶棍可能会发明自己的方法:
$ curl -v -X SHENANIGANS http://localhost:8080/secret
...
> SHENANIGANS /secret HTTP/1.1
...
< HTTP/1.1 405 Method Not Allowed
< Content-Type: text/html
< Allow: GET
...
是否有一种方法可以实现 catch web.py 类中的所有方法适用于任何 HTTP 方法,这样我就可以确保安全检查将运行?
或者有其他方法来隐藏这些资源吗?
I want to selectively hide some resources based on some form of authentication in web.py, but their existence is revealed by 405 responses to any HTTP method that I haven't implemented.
Here's an example:
import web
urls = (
'/secret', 'secret',
)
app = web.application(urls, globals())
class secret():
def GET(self):
if web.cookies().get('password') == 'secretpassword':
return "Dastardly secret plans..."
raise web.notfound()
if __name__ == "__main__":
app.run()
When an undefined method request is issued, the resource is revealed:
$ curl -v -X DELETE http://localhost:8080/secret
...
> DELETE /secret HTTP/1.1
...
< HTTP/1.1 405 Method Not Allowed
< Content-Type: text/html
< Allow: GET
...
I could implement the same check for the other common methods in the HTTP specification, but a creative miscreant might invent their own:
$ curl -v -X SHENANIGANS http://localhost:8080/secret
...
> SHENANIGANS /secret HTTP/1.1
...
< HTTP/1.1 405 Method Not Allowed
< Content-Type: text/html
< Allow: GET
...
Is there a way to implement a catch all method in a web.py class for any HTTP method, so I can ensure the security check will be run?
Or is there an alternative way to hide these resources?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
受到 Daniel Kluev 的回答的启发,我最终从
web.application
派生,在_delegate
方法中添加对默认方法的支持:实例化:
页面类:
我更喜欢这个解决方案,因为它使页面类保持干净,并在一个地方提供对委派过程的进一步定制。例如,我想要的另一个功能是透明重载 POST(例如,使用
method=DELETE
将 POST 请求重定向到页面类的 DELETE 方法),在这里添加它也很简单:Enlightened by Daniel Kluev's answer, I ended up deriving from
web.application
to add support for a default method in the_delegate
method:Instantiation:
Page class:
I prefer this solution because it keeps the page classes clean and affords further customisation of the delegation process in a single place. For example, another feature I wanted was transparent overloaded POST (eg. redirecting a POST request with
method=DELETE
to the DELETE method of the page class) and it's simple to add that here too:您可以像这样实现handle-all-methods方法:
由于web.py检查方法存在的方式,
__getattribute__
被实现了两次:You can implement handle-all-methods method like this:
__getattribute__
is implemented twice due to the way web.py checks for method existence:您可以在“秘密”类中定义任何方法,例如 DELETE 或 SHENANIGANS,如下所示:
you can define any method in your 'secret' class, such as DELETE or SHENANIGANS, like this: