Security Drupal自定义模板安全问题
我主要是一个Wordpress的人,正在努力学习Drupal7的诀窍。我的问题与模板化最佳实践和安全问题有关。我正在处理极其复杂的设计(是的,设计师是对的!?),因此我的标记需要干净且恰到好处,我发现Drupal在处理大量模板文件和函数时非常困难。基本上,我发现对我有效的工作流程是覆盖特定内容类型的输出,我需要在节点级别对这些内容类型进行专门的标记 例如:node——custom content type.tpl.php 就像我说的,我是一个wordpress的人,习惯于运行数据库查询,获取我想要的确切字段值,并以我想要的方式使用它们。我一直在kpr或打印$variables数组,研究它包含的内容,并像这样直接获取值:Security Drupal自定义模板安全问题,security,drupal,drupal-7,drupal-theming,Security,Drupal,Drupal 7,Drupal Theming,我主要是一个Wordpress的人,正在努力学习Drupal7的诀窍。我的问题与模板化最佳实践和安全问题有关。我正在处理极其复杂的设计(是的,设计师是对的!?),因此我的标记需要干净且恰到好处,我发现Drupal在处理大量模板文件和函数时非常困难。基本上,我发现对我有效的工作流程是覆盖特定内容类型的输出,我需要在节点级别对这些内容类型进行专门的标记 例如:node——custom content type.tpl.php 就像我说的,我是一个wordpress的人,习惯于运行数据库查询,获取我想
$link = $variables['field_link'][0]['url'];
$link_title = $variables['field_link'][0]['title'];
$target = $variables['field_link'][0]['attributes']['target'];
$text = $variables['field_main_text'][0]['safe_value'];
然后呼出并使用标记中的变量,正如我所希望的:
<article class="getstarted-wrapper">
<a id="tocollege" target="<?php print_r($target); ?>" title="<?php print_r($link_title); ?>" href="<?php print_r($link); ?>"><img src="/sites/all/themes/amped/images/visiticon.png" /></a>
<a id="mapcollege" target="_blank" title="View Location In Google Maps" href="<?php echo $maplink; ?>"><img src="/sites/all/themes/amped/images/mapicon.png" /></a>
<div class="getstarted-top" style="background:<?php print_r($bg); ?>;">
<figure>
<img title="<?php print_r($auth_title); ?>" alt="<?php print_r($auth_alt); ?>" src="<?php print_r($auth_img); ?>" />
</figure>
</div><!--getstarted-top-->
<div class="getstarted-bottom">
<p><?php print_r($text); ?></p>
<a target="<?php print_r($target); ?>" title="<?php print_r($link_title); ?>" href="<?php print_r($link); ?>">Get Started</a>
<span>This will take you to <?php print_r($college_name); ?></span>
</div><!--getstarted-bottom-->
</article><!--getstarted-wrapper-->
每次您将纯文本字符串(即非故意标记的任何内容)输出到HTML页面时,都需要对其进行转义
在普通PHP模板中,通常使用htmlspecialchars()
函数完成。Drupal提供了check\u plain()
作为捷径,尽管不是很短。您可以定义自己的较短切口以减少疼痛:
function h($s) {
echo htmlspecialchars($s, ENT_QUOTES, 'utf-8');
}
<a id="tocollege" target="<?php h($target); ?>" title="<?php h($link_title); ?>" href="<?php h($link); ?>">...
函数h($s){
echo htmlspecialchars($s,ENT_引号,'utf-8');
}
使用Drupal的正确方法是在输出时清理用户输入。由于Drupal有多种输出模式(不仅仅是HTML),因此不适合对输入进行清理,因此在输出HTML时,可以按照bobince的建议使用Drupal的check_plain()函数。check_plain是可供使用的几种过滤器功能之一,有关更多信息,请参阅
如果您要覆盖输出并访问主题变量,那么最好的做法是自己运行check_plain(或其他过滤器函数)。如果是节点属性,那么您也可以使用上面链接中描述的“安全”属性。感谢您的回复,我唯一的另一个问题(不确定您是否是问这个问题的人)是关于此工作流如何与Drupal最佳实践相匹配?是否有更好的方法来完成相同的任务等?我对Drupal不是很熟悉,但快速查看一系列模板似乎表明,完全易受XSS攻击绝对是行业标准-(