【问题标题】:Security Policy errors with Automator running JavaScript with Safari使用 Safari 运行 JavaScript 的 Automator 出现安全策略错误
【发布时间】:2023-05-16 23:45:01
【问题描述】:

我正在使用以下 Automator 脚本:

on run {input, parameters}
    set updateCount to 0
    read (item 1 of input)
    set ps to paragraphs of the result
    set tot to count ps
    set TLFile to (("Users:Admin:Desktop:") as text) & "titleList.txt"
    set TLLines to paragraphs of (read file TLFile as «class utf8»)
    set descFile to (("Users:Admin:Desktop:") as text) & "descList.txt"
    set DescLines to paragraphs of (read file descFile as «class utf8»)
    tell application "Safari"
        reopen
        activate
    end tell
    repeat with i from 1 to tot
        set p to item i of ps
        if p is not "" then
            try
                tell application "Safari"
                    tell front window
                        set r to make new tab with properties {URL:p}
                        set current tab to r
                        set titleVal to item i of TLLines
                        set descVal to item i of DescLines
                        set updateCount to updateCount + 1
                        do shell script "echo The value: " & updateCount
                        delay 12
                        do JavaScript "document.getElementsByName('title')[0].value = '" & titleVal & "'; document.getElementsByName('description')[1].value = '" & descVal & "'; 
                                      document.getElementsByClassName('save-changes-button')[0].removeAttribute('disabled');
                                      document.getElementsByClassName('save-changes-button')[0].click();" in current tab
                        delay 4
                        close current tab
                        if updateCount is equal to 10 then
                            say "hi"
                            set updateCount to 0
                            delay 90
                        end if
                        if i = tot then exit repeat
                        repeat
                            delay 4
                            get URL of r
                        end repeat
                    end tell
                end tell
            end try
        end if
    end repeat
end run

几个月前,我已经能够在 Safari 上使用 YouTube 运行此脚本而没有任何问题。现在它不执行 JavaScript 操作。 Safari 的检查器中显示错误:

  1. “内容安全策略指令 'script-src' 的源列表包含无效源:''strict-dynamic''。它将被忽略。 postmessageRelay:0"
  2. “拒绝执行脚本,因为其哈希值、随机数或‘unsafe-inline’未出现在内容安全策略的 script-src 指令中。”

如何绕过这些错误以便我的脚本可以运行?

【问题讨论】:

    标签: javascript content-security-policy automator security-policy


    【解决方案1】:

    正如问题中的错误 #2 所示,目前使该策略在 Safari 中有效的修复方法是将其更改为为相关脚本指定哈希或随机数 - 或者添加 'unsafe-inline'

    在支持'strict-dynamic' 的浏览器中,'unsafe-inline' 部分将被忽略。

    这两个错误的原因是 Safari 还不支持'strict-dynamic'。请参阅以下内容:

    综上所述,从目前的信息来看,目前尚不清楚实际指定有效的 CSP 政策的位置。所以除非你已经知道那在哪里,否则我想第一步是确定策略的指定位置,并在那里进行更改。

    【讨论】:

    • 在哪里添加 unsafe-inline?如何在此 AppleScript 中将其添加为 JavaScript 命令?我是在编辑一个元素来添加它,还是在文档的顶部?
    • 我刚刚重读了您关于确定在何处指定策略的最后一部分。这是 YouTube 视频的编辑页面,我无法链接,因为它需要帐户并登录。我找不到任何提及“nonce”、“unsafe-inline”或“strict-dynamic”当我搜索检查员时在这里。
    • 我刚刚尝试在旧版本的 Safari (9) 上运行我的 AppleScript,但它给了我更多错误。两个月前我没问题。
    • 如果生效的政策是由 Youtube 页面设置的,那么我想您无法更改它以添加“unsafe-inline”或对其进行任何其他更改
    • @VagueExplanation 很高兴听到你成功了
    最近更新 更多