drupal/rat

1.0.0 2023-07-19 22:22 UTC

This package is auto-updated.

Last update: 2024-09-02 08:14:01 UTC


README

查看示例

不要与 #access 打交道!

这个库的一个原因是为了 restrictAccess 方法。更改渲染数组的 #access 属性是非常危险的,而 restrictAccess 方法则负责安全地进行这项工作。

那么可能会发生什么。看看这个例子:

$fieldRenderable['#access'] = $fieldIsTranslatable;

可能会发生什么?如果这段代码不是唯一一个修改渲染数组的代码,并且还有其他组件在此代码之前执行,那么可能会发生很多事情。在 Drupal 中,由于构建和渲染过程的复杂性,这种情况通常会发生。

另一个组件可能已将访问设置为 false,而此代码将其覆盖为 true,然后发生爆炸。

更有技巧性的是:另一个组件可能已将 access 设置为包含缓存性的 AccessResult 对象。这被覆盖了,然后发生爆炸。

这两种情况都为信息泄露打开了大门。这不是好事。

结果证明,限制访问并添加缓存性,同时保留来自其他组件的缓存性并不是一件简单的事情。RenderArrayTool 来拯救!

// Add access restriction and cacheability, while preserving any existing access
// restrictions, including cacheability.
\Drupal\rat\v1\RenderArray::alter($fieldRenderable)
  ->restrictAccess($fieldIsTranslatable, $fieldConfig);