金字塔2.0的新增功能¶
本文解释了 Pyramid 版本2.0与前一版本相比, Pyramid 1.10条。它还记录了两个版本之间的向后不兼容以及添加到 Pyramid 2.0,以及软件依赖关系的更改和显著的文档添加。
备注
这是的第一个版本 Pyramid 这不支持Python2,Python2现已停产,不再接收PSF的关键安全更新。
功能添加¶
Pyramid 2.0中的新增功能如下:
金字塔1.x的身份验证和授权策略已合并到单个 security policy 在金字塔2.0中。有关如何迁移到新安全策略的详细信息,请参阅 升级身份验证/授权 。身份验证和授权策略仍然可以使用,并且暂时将继续正常运行。
添加了新的安全API,以支持身份验证和授权系统的全面改革。朗读 升级身份验证/授权 获取有关使用此新系统的信息。
pyramid.authentication.AuthTktCookieHelper
(在金字塔1.x中提供)
请参阅https://github.com/Pylons/pyramid/pull/3465
暴露的
pyramid.authorization.ALL_PERMISSIONS
和pyramid.authorization.DENY_ALL
这样,所有与ACL相关的常量现在都可以从pyramid.authorization
命名空间。请参阅https://github.com/Pylons/pyramid/pull/3563更改了默认值
serializer
在……上面pyramid.session.SignedCookieSessionFactory
要使用pyramid.session.JSONSerializer
而不是pyramid.session.PickleSerializer
。朗读 升级会话序列化 有关进行此更改的原因的详细信息,请参阅。请参阅https://github.com/Pylons/pyramid/pull/3413现在可以使用以下命令控制路由模式在与路由前缀组成时是否包含尾随劈开
config.include(..., route_prefix=...)
或with config.route_prefix_context(...)
。这可以通过指定空模式并设置新参数来实现inherit_slash=True
。例如:with config.route_prefix_context('/users'): config.add_route('users', '', inherit_slash=True)
在本例中,生成的模式将为
/users
。同样,如果路由前缀是/users/
那么最终的图案将是/users/
。如果pattern
曾经是'/'
,那么最终的图案将始终是/users/
。此新设置仅在以下情况下可用:提供给的图案add_route
是空字符串 (''
)。请参阅https://github.com/Pylons/pyramid/pull/3420一个新参数,
allow_no_origin
,已添加到pyramid.config.Configurator.set_default_csrf_options()
以及pyramid.csrf.check_csrf_origin()
。此选项控制是否拒绝请求(如果没有Origin
或Referer
标头-通常是用户将其浏览器配置为不发送Referer
出于隐私原因,即使是在相同的域请求上也会显示标头。默认情况下,拒绝来源未知的请求。也可以允许特殊的Origin: null
标头,将其添加到pyramid.csrf_trusted_origins
在设置中列出。请参阅https://github.com/Pylons/pyramid/pull/3512和https://github.com/Pylons/pyramid/pull/3518一个新参数,
check_origin
,已添加到pyramid.config.Configurator.set_default_csrf_options()
这将完全禁用来源检查。请参阅https://github.com/Pylons/pyramid/pull/3518已添加
pyramid.interfaces.IPredicateInfo
它将传递给谓词工厂的对象定义为它们的第二个参数。请参阅https://github.com/Pylons/pyramid/pull/3514添加了对服务压缩前的静电资产的支持,方法是使用
content_encodings
论证pyramid.config.Configurator.add_static_view()
和pyramid.static.static_view()
。请参阅https://github.com/Pylons/pyramid/pull/3537修复
DeprecationWarning
通过使用imp
模块。请参阅https://github.com/Pylons/pyramid/pull/3553通过以下方式创建的特性
config.add_request_method(..., property=True)
orrequest.set_property
used to be readonly. They can now be overridden viarequest.foo = ...
在删除该值之前,它将返回被覆盖的值。这在模拟测试中的请求属性时最有用。请参阅https://github.com/Pylons/pyramid/pull/3559完成的回调现在作为
closer
作为以下内容的一部分调用的pyramid.scripting.prepare()
和pyramid.paster.bootstrap()
。请参阅https://github.com/Pylons/pyramid/pull/3561已添加
pyramid.request.RequestLocalCache
它可用于创建跨请求共享的简单对象,并可用于存储每个请求的数据。当数据源在请求本身的外部时,这很有用。通常,实例化属性通过以下方式在请求上使用pyramid.config.Configurator.add_request_method()
,或pyramid.decorator.reify
。当数据在访问请求属性时按需生成时,这些功能非常有用。但是,通常情况是,数据是在访问其他系统时生成的,然后我们希望在请求期间缓存数据。请参阅https://github.com/Pylons/pyramid/pull/3561不再定义
pyramid.request.Request.json_body
它已经由WebOb提供。这使得现在可以设置该属性。请参阅https://github.com/Pylons/pyramid/pull/3447从以下位置改进调试信息
pyramid.view.view_config
装饰师。请参阅https://github.com/Pylons/pyramid/pull/3483pserve
现在将详细消息输出到 stderr 而不是 stdout 要规避默认情况下存在的缓冲问题,请执行以下操作 stdout 。请参阅https://github.com/Pylons/pyramid/pull/3593
废弃¶
不建议使用身份验证和授权接口以及基于主体的支持。看见 升级身份验证/授权 有关等效API的信息和升级注意事项,请参阅。由于此更改,以下API已弃用:
这个
effective_principals
查看和路由谓词。
已弃用
pyramid.security.principals_allowed_by_permission()
。此方法继续对已弃用的 :class:`pyramid.interfaces.IAuthorizationPolicy 接口,但不能与新的pyramid.interfaces.ISecurityPolicy
。请参阅https://github.com/Pylons/pyramid/pull/3465已弃用的与ACL相关的几个方面
pyramid.security
。等价对象现在应该从pyramid.authorization
模块。这包括:请参阅https://github.com/Pylons/pyramid/pull/3563
已弃用
pyramid.session.PickleSerializer
。看见 升级会话序列化 有关更多信息,以及https://github.com/pylons/pyramid/issues/2709,https://github.com/pylons/pyramid/pull/3353,和https://github.com/pylons/pyramid/pull/3413
升级会话序列化¶
在……里面 Pyramid 2.0The pyramid.interfaces.ISession
接口进行了更改,要求会话实现只需要支持JSON可序列化的数据类型。与之前要求所有对象都是可拾取的相比,这是一个更严格的合同,而且这样做是出于安全目的。这是一个向后不兼容的更改。以前,如果客户端会话实现受到威胁,应用程序很容易受到远程代码执行攻击,使用精心编制的会话在反序列化时执行代码。
如果需要有关这些更改的详细信息,请参考以下票证:
2.0 feature request: Require that sessions are JSON serializable #2709 <https://github.com/pylons/pyramid/issues/2709> _.
deprecate pickleable sessions, recommend json #3353 <https://github.com/pylons/pyramid/pull/3353> _.
change to use JSONSerializer for SignedCookieSessionFactory #3413 <https://github.com/pylons/pyramid/pull/3413> _.
对于有兼容性问题的用户,可以创建一个可以同时处理两种格式的序列化程序,直到您满意客户端有时间进行合理升级为止。请记住,会话应该是短暂的,因此受影响的客户端数量应该很小(最长不超过一个身份验证令牌)。示例序列化程序:
1import pickle
2from pyramid.session import JSONSerializer
3from pyramid.session import SignedCookieSessionFactory
4
5
6class JSONSerializerWithPickleFallback(object):
7 def __init__(self):
8 self.json = JSONSerializer()
9
10 def dumps(self, appstruct):
11 """
12 Accept a Python object and return bytes.
13
14 During a migration, you may want to catch serialization errors here,
15 and keep using pickle while finding spots in your app that are not
16 storing JSON-serializable objects. You may also want to integrate
17 a fall-back to pickle serialization here as well.
18 """
19 return self.json.dumps(appstruct)
20
21 def loads(self, bstruct):
22 """Accept bytes and return a Python object."""
23 try:
24 return self.json.loads(bstruct)
25 except ValueError:
26 try:
27 return pickle.loads(bstruct)
28 except Exception:
29 # this block should catch at least:
30 # ValueError, AttributeError, ImportError; but more to be safe
31 raise ValueError
32
33# somewhere in your configuration code
34serializer = JSONSerializerWithPickleFallback()
35session_factory = SignedCookieSessionFactory(..., serializer=serializer)
36config.set_session_factory(session_factory)