【问题标题】:How to make UI5 content compatible with the FLP setting "Asynchronous Module Loading"?如何让UI5内容兼容FLP设置“Asynchronous Module Loading”?
【发布时间】:2022-12-30 16:16:26
【问题描述】:

“站点设置”页面可通过启动板now allows enabling "Asynchronous Module Loading" 的“管理站点”访问。

但是一旦启用,某些 SAPUI5 应用程序或 FLP 插件将无法启动。浏览器在控制台报告:

执行失败 '<JavaScript 模块>.js':拒绝将字符串评估为 JavaScript,因为“unsafe-eval”不是以下脚本的允许来源内容安全政策指令:"script-scr * 'unsafe-inline' data: blob:"

在这种情况下,CSP 与“异步模块加载”设置有何关系?我们可以做些什么来避免在 UI5 中评估“字符串作为 JavaScript”?

【问题讨论】:

    标签: sapui5 content-security-policy amd sap-fiori ui5-tooling


    【解决方案1】:

    原因

    如果激活“异步模块加载”,SAP Fiori 启动板 (FLP) 不仅会使用 data-sap-ui-async="true" 引导 SAPUI5,还会使用包含一组 CSP 指令的 content-security-policy (CSP) 响应标头提供其 HTML 文档省略unsafe-<strong>eval</strong>script-src。因此,启动调用 eval(因此违反 CSP)的 UI5 应用程序和 FLP 插件将不会被浏览器处理。将来,可能会应用更严格的 CSP 指令,例如 script-src,另外省略 unsafe-<strong>inline</strong>

    在遗留 UI5 代码中,通常调用 eval 是因为应用程序通过已弃用的 API 同步获取 JS 模块。其他原因见下表。

    解析度

    UI5 已经弃用了遗留/同步 API,并且在 1.96 版本中大大改进了对严格 CSP 的支持。 UI5 内容所有者应该相应地调整他们的代码:

    ❌ UI5 content violating the CSP ✅ Making the UI5 content more CSP-compliant
    Application's HTML document bootstrapping SAPUI5 without data-sap-ui-async="true" or with the debug mode activated. Ensure that the HTML document bootstraps SAPUI5 with data-sap-ui-async="true" and that no debug mode is activated unnecessarily.
    Using inline scripts (&lt;script&gt;...&lt;/script&gt;) within the application's HTML document. Use only &lt;script <strong>src="..."</strong> ...&gt;&lt;/script&gt; to comply with the CSP without unsafe-inline. Define the initial component declaratively via sap/ui/core/ComponentSupport.
    Using deprecated APIs and libs such as jQuery.sap.*, sap.ui.requireSync, sap.ui.commons, sap.ca.scfld, ... Review the documented API reference to learn about newer asynchronous APIs that replace the deprecated ones.
    Fetching UI5 libs and components manually but still synchronously despite using non-deprecated APIs Review the documented API reference to learn how to enable loading such resources asynchronously. E.g. when loading a UI5 lib manually:
    Core.loadLibrary("that.lib",/*async:*/true);
    Creating the component content such as the root view, routed views, and nested views synchronously in runtime despite having them defined declaratively. Implement the "sap.ui.core.IAsyncContentCreation" marker interface in Component.js to implicitly create the component content asynchronously.

    Component-preload.js bundling JS modules as string due to:

    • Using the outdated standard Grunt build task. Result:
      "my/Component.js":'sap.ui.define([...'
    • Global instructions before calling sap.ui.define. Result:
      "my/Component.js":'var appID...'

    Generate the Component-preload.js bundle by leveraging UI5 Tooling with e.g. ui5 build -a --clean-dest.

    When defining a UI5 module, avoid global instructions but only use sap.ui.define at top-level of the JS file.

    Result:
    "my/Component.js":function(){//...

    有关 UI5 中 CSP 的当前状态以及存在哪些限制的更多详细信息,请参阅文档主题Content Security Policy


    相关问答

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 2012-06-05
      相关资源
      最近更新 更多