【问题标题】:Drupal Custom Templating Security IssuesDrupal 自定义模板安全问题
【发布时间】:2013-07-10 14:48:21
【问题描述】:

我主要是一个 Wordpress 人,正在尝试学习 Drupal 7 的技巧。我的问题与模板最佳实践和安全问题有关。我正在处理极其复杂的设计(是的,设计师对!?)所以我的标记需要干净且恰到好处,我发现 Drupal 使得模板文件和函数的大层次结构非常困难。基本上,我发现一直为我工作的工作流程是覆盖我需要在节点级别真正专门标记的特定内容类型的输出。

例如:node--custom-content-type.tpl.php

就像我说的那样,我是一个 wordpress 人,并且习惯于能够运行数据库查询,获取我想要的确切字段值并按照我想要的方式使用它们。我一直在 kpr 或打印出 $variables 数组,研究它包含的内容,并像这样直接获取值:

$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-->

我想知道这个过程如何与最佳实践相匹配,我做错了什么,我做对了什么,更重要的是我的安全风险是什么以及如何避免它们??

【问题讨论】:

    标签: security drupal drupal-7 drupal-theming


    【解决方案1】:

    每次您将纯文本字符串(即任何不是故意标记的内容)输出到 HTML 页面时,都需要对其进行转义。

    在普通的 PHP 模板中,通常使用 htmlspecialchars() 函数完成。 Drupal 提供check_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); ?>">...
    

    (我不确定print_r 的用途是什么——这通常用于生成可读的结构化对象输出以进行调试,但这种输出格式通常不是您在生产网页中想要的,在您的示例中,它仅用于字符串,无论如何都没有区别。)

    【讨论】:

    • 感谢您的回复,我唯一的另一个问题(不确定您是否会问这个问题)是关于此工作流程如何与 Drupal 最佳实践相匹配?有没有更好的方法来完成相同的任务等?
    • 我对 Drupal 不是很熟悉,但是快速浏览一堆模板似乎表明完全易受 XSS 攻击绝对是行业标准。 :-(
    【解决方案2】:

    使用 Drupal 的正确方法是在输出时清理用户输入。由于 Drupal 有多种输出模式(不仅仅是 HTML),因此对输入进行清理是不合适的,因此在输出 HTML 时,您可以按照 bobince 的建议使用 Drupal 的 check_plain() 函数。 check_plain 是几个可供使用的过滤器函数之一,请参阅https://drupal.org/node/28984 了解更多信息。

    如果您要覆盖输出并访问主题变量,那么最好自己运行 check_plain(或其他过滤器函数)是正确的。如果它是节点属性,那么您也可以使用上面链接中描述的“安全”属性。

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多