如何在 Windows 通用应用程序中设置内容安全策略

2023-04-19前端开发问题
10

本文介绍了如何在 Windows 通用应用程序中设置内容安全策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我什至不知道这是否是我需要的,但经过几天 MSDN 论坛帖子 完全没有答案 我想我会试一试在 SO.

I don't even know if that's what I need, but after several days of this MSDN Forum post with no answers at all I thought I'd give a shot in SO.

我的问题:我有许多 Windows 8.1 和 Windows Phone 8.1 HTML/Javascripts 应用程序在 <head> 中有一个小的 <script> 句子; 每个 html 页面.我开始将我的应用程序作为单个通用 Windows 应用程序迁移到 Windows 10,但出现以下错误:

My problem: I have many Windows 8.1 and Windows Phone 8.1 HTML/Javascripts apps that have a little <script> sentence in the <head> of every html page. I started migrating my apps to Windows 10 as a single Universal Windows app but I get the following error:

CSP14312: Resource violated directive 'script-src ms-appx: data: 'unsafe-eval'' in Host Defined Policy: inline script. Resource will be blocked

当然,什么都没有被执行……我错过了什么吗?

and of course, nothing gets executed... am I missing anything?

要重现,只需使用 VS2015 RC 创建一个空白的 Windows 通用应用程序并添加

edit: To repro just create a blank Windows Universal app with VS2015 RC and add

<script>
    console.log('hello');
</script>

就在 head 标签关闭之前

right before the head tag closes

推荐答案

Rob 说得对,默认情况下你不能在 ms-appx:/// 协议中使用内联脚本.这是应用程序的默认协议,并且具有不允许内联脚本的默认 CSP 策略.

Rob has it right, by default you can't have inline script in ms-appx:/// protocol. This is the default protocol for an application and has a default CSP policy that doesn't allow inline script.

如果您真的希望使用内联脚本,您可以通过 ms-appx-web:/// 协议导航到没有默认 CSP 策略的内容.

If you really wish to use inline script you can navigate to the content via ms-appx-web:/// protocol where there is no default CSP policy.

需要注意的是,您无权访问此协议中的某些功能.

The one note is that you do not have access to some capabilities in this protocol.

除了 Rob 所说的之外,我唯一的区别是您很可能希望像这样设置应用程序内容 URI 规则 (ACUR)

The only difference I have beyond what Rob said is that you most likely want to set the Application Content URI Rule (ACUR) like this

<uap:ApplicationContentUriRules>
   <uap:Rule Type="include" Match ="ms-appx-web:///" WindowsRuntimeAccess="all"/>
</uap:ApplicationContentUriRules>

要导航到您的内容,您可以将清单中的 StartPage 设置为 ms-appx-web:///default.html

To navigate to your content you can set the StartPage in the manifest to ms-appx-web:///default.html

这篇关于如何在 Windows 通用应用程序中设置内容安全策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Fatal error: Call to a member function fetch_assoc() on a no
业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass...
2024-12-13 前端开发问题
136

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui table数据表格reload where参数保留
1、创建表格对象 layui.use('table', function () { var table = layui.table; tableObj = table.render({ id: "tableId", url: 'url', //数据接口 elem: '#tableId', page: { limit: 15, limits: [15, 30, 50, 100] }, //开启分页 cols: [[ //表头 ... ]], w...
2024-07-18 前端开发问题
385