AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 78905680
Accepted
timholz
timholz
Asked: 2024-08-23 19:31:04 +0800 CST2024-08-23 19:31:04 +0800 CST 2024-08-23 19:31:04 +0800 CST

CSP script-src 设置拒绝 wordpress 中的内联脚本

  • 772

目前我有:

function add_csp_header() {
    $csp = "script-src 'nonce-".custom_nonce_value()."'"; //and some more
    header("X-Content-Type-Options: nosniff");
    header("Referrer-Policy: strict-origin-when-cross-origin");
    header("Content-Security-Policy: $csp");
    }
add_action('send_headers', 'add_csp_header');

在控制台中我收到几条错误消息:

Content-Security-Policy: the settings of the page have blocked loading a ressource on inline ("script-src").

wp_localize_script()内联脚本按如下方式排队:

wp_register_script( 'dibujos', get_stylesheet_directory_uri() . '/js/dibujos.js',   array('jquery'),'1.0.0', true);
wp_localize_script( 'dibujos', 'archVar', array( 'url' => get_bloginfo('url') )    );
wp_enqueue_script('dibujos');

为了将随机数添加到内联脚本中,我尝试了以下方法:

function toa_inline_script_nonce( array $attr ): array {
    if ( empty( $attr['id'] ) ) {
        return $attr;
    }
    if ( 'dibujos' === $attr['id'] ) {
        $attr['nonce']   = custom_nonce_value();
    }
    return $attr;
}
add_filter( 'wp_script_attributes', 'toa_inline_script_nonce', 10, 1 );

什么也不做,或者...

//nonce for specific scripts
function toa_script_tag_nonce( $tag, $handle ) {
    if ( $handle === 'jquery' OR $handle === 'admin-bar' OR $handle === 'dibujos' ) {
         $nonce = custom_nonce_value();
         $tag = str_replace( '<script ', '<script nonce="'.$nonce.'"', $tag );
    }
    return $tag;
    }
add_filter( 'script_loader_tag', 'toa_script_tag_nonce', 10, 2 );

这会为$handle“jquery”和“admin-bar”添加随机数,但忽略“dibujos”。

最后...

add_filter( 'script_loader_tag', 'add_nonce_to_script_tag', 10, 3 );

function add_nonce_to_script_tag( $tag, $handle, $src ) {

  // Check the $handle and respond accordingly
  if ( $handle === 'dibujos' ) {
    $nonce_value = custom_nonce_value();
    $replace = sprintf("javascript' nonce='%s'>", $nonce_value );
    $tag = str_replace( "javascript'>", $replace, $tag);
  }

  return $tag;
}

// Then... $data is the inline JS from wherever
wp_add_inline_script('dibujos', $data, 'before');

这会引发错误:$data is undefined并且wp_add_inline_script()不应在之前调用wp_enqueue_scripts()。结论:我尝试捕获脚本标记并添加 nonce 属性的所有尝试都失败了。不知何故,内联脚本似乎遥不可及……(至少使用这些方法)

在这篇文章中,有人说:

由于这些是回显的并且是硬编码的,因此您无法向 wp_localize_script() 的 HTML 脚本元素添加属性。

根据此声明,内联脚本对于 wordpress 中的保存脚本源 CSP 来说是死路一条。尽管有这样的说法,但我想知道这是否是事实,如果不是,我该如何将 nonce 添加到所有内联脚本中?

wordpress
  • 1 1 个回答
  • 31 Views

1 个回答

  • Voted
  1. Best Answer
    mmm
    2024-08-24T15:50:59+08:002024-08-24T15:50:59+08:00

    有一个过滤器wp_inline_script_attributes可以自定义内联脚本的属性:
    https://core.trac.wordpress.org/browser/tags/6.6/src/wp-includes/script-loader.php#L2919

    那么你的插件看起来就像这样

    add_filter("MY_PLUGIN/nonce_script", function ($_) {
        
        // put the data in cache to be sure to return the same value on all calls
        if (!isset($GLOBALS["MY_PLUGIN"]["nonce_script"])) {
            $GLOBALS["MY_PLUGIN"]["nonce_script"] = md5("塩" . microtime());
        }
        
        
        return $GLOBALS["MY_PLUGIN"]["nonce_script"];
        
    });
    
    
    add_action("send_headers", function ($wp) {
        
        $nonce_script = apply_filters("MY_PLUGIN/nonce_script", NULL);
        
        header("X-Content-Type-Options: nosniff");
        header("Referrer-Policy: strict-origin-when-cross-origin");
        header("Content-Security-Policy: script-src 'nonce-$nonce_script'");
        
    }, 10, 1);
    
    
    add_filter("wp_script_attributes", function ($attributes) {
        
        $attributes["nonce"] = apply_filters("MY_PLUGIN/nonce_script", NULL);
        
        return $attributes;
        
    }, 10, 1);
    
    
    add_filter("wp_inline_script_attributes", function ($attributes, $data) {
        
        $attributes["nonce"] = apply_filters("MY_PLUGIN/nonce_script", NULL);
        
        return $attributes;
        
    }, 10, 2);
    
    • 1

相关问题

  • 为 WooCommerce 上特定类别或多个类别的用户隐藏产品价格

  • 设置默认“管理库存?” 创建新变体时选择“是”(Woocommerce)

  • 使 WordPress 帖子标题包含使用 wp_insert_post() 创建的新帖子的 id

  • 著名的五分钟 WordPress 安装过程 网站迁移后

  • 我的 WordPress 网站上的输入/输出使用限制已达到最大值

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve