web-dev-qa-db-ja.com

<pre>タグ内のエスケープされていないHTMLフィルタリングを緩和しますか?

デフォルトでは、WordPressは登録されていないユーザーからのコメントでHTMLのエスケープされていない可能性があるコンテンツを削除します。これはXSSから保護するのには良いですが、不必要にそのフィルタリングを<pre>要素にも拡張します。私のブログでは、ほとんどすべての投稿がHTMLコードスニペットの恩恵を受けるコメントを生成していますが、そのフィルタリングは私のユーザー(そして私自身)に多くのイライラさせる問題を引き起こしています。

未登録のコメント内の<pre>要素内の過度に積極的なフィルタリングを、残りのコメントに対して無効にすることなく "修正"する方法はありますか?できれば、アップグレードを生き残るような方法で。

2
Dave Ward

小さな解決策強調表示は私のブログでJavaScriptを介していた

function pre_esc_html($content) {
  return preg_replace_callback(
    '#(<pre.*?>)(.*?)(</pre>)#imsu',
    create_function(
      '$i',
      'return $i[1].esc_html($i[2]).$i[3];'
    ),
    $content
  );
}

add_filter(
  'the_content',
  'pre_esc_html',
  9
);
5
bueltge

これはあなたが探しているよりももう少しだけかもしれませんが、 WP-Syntax は投稿やコメント内の<pre>タグ内のHTMLフィルタリングを無効にします(AFAIK)。 Webサイトでは2.8でのみ機能すると言われていますが、Wordpress 3.0でも機能します。

もっとシンプルにしたい場合は、プラグイン内のwp-syntax.php(特に<pre>タグ内でWordpressの自動HTMLフィルタリングを無効にする方法を確認するためのadd_filters()を使用する一番下の部分)を確認することをお勧めします。

編集:私はファイルを見てきた、と彼らは<pre>タグ内の元のHTMLを維持するために正規表現とPHPの preg_replace_callback() を使用しています。あなたはあなたのニーズに合うようにそれを修正しなければならないかもしれません。

たとえば、次のようになります(注:未テストのコード)。

<?php
// Unique string for placeholder
$custom_token = md5(uniqid(Rand()));

// Store all the matches in an array
$custom_matches = array();

function custom_substitute(&$match) {
    global $custom_token, $custom_matches;

    $i = count($custom_matches);

    // Store the match for later use
    $custom_matches[$i] = $match;

    // Unique placeholder so that we know where to put the code that was ripped out
    return '<p>' . $custom_token . '</p>';
}

function custom_replace($match) {
    global $custom_matches;

    $i = intval($match[1]);
    $match = $custom_matches[$i];

    // The index might be off - you might want to double-check it
    return htmlentities($match[1]);
}

function custom_before_content_filter($content) {
    return preg_replace_callback("/\s*<pre+>(.*)<\/pre>\s*/siU", 'custom_substitute', $content);
}

function custom_after_content_filter($content) {
    global $custom_token;

    return preg_replace_callback("/<p>\s*" . $custom_token . "\s*<\/p>/si", 'custom_replace', $content);
}
// Run the "before" filter first, with priority of 0
add_filter('comment_text', 'custom_before_content_filter', 0);

// Now run the "after" filter
add_filter('comment_text', 'custom_after_content_filter', 99);
1
john010117