【问题标题】:How to ignore ssl certificate warning or pass a self signed certificate (from inside gruntfile) during accessibility automation?如何在可访问性自动化期间忽略 ssl 证书警告或传递自签名证书(从 gruntfile 内部)?
【发布时间】:2018-01-10 19:17:32
【问题描述】:

我正在使用grunt-accessibility 插件来自动报告accessibility 错误。它正常工作,但是当我在一个有 self signed certificate 的网站上尝试它时(那种显示带有一些证书安全警告的 interim 页面的类型,如果你仍然愿意,还有一个链接可以继续到该站点),它在interim 页面本身报告错误,当然这是一个空页面:

<html>
    <head></head>
    <body></body>
</html>

显然我想绕过这个临时页面并在实际页面上运行accessibility

我在尝试什么?

我尝试了以下方法(从谷歌搜索和其他 SO's Q&A 中找到):

  1. 臭名昭著的黑客攻击

    npm set strict-ssl false
    
  2. 添加导入的认证路径

    npm config set cafile="C:\path\to\cert.cer"
    
  3. 添加process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"(见下文Grunfile

据我所知,grunt-accessibility 使用 AccessSniff,而后者又使用 phantomjs。现在,phantomjs 可以通过

忽略此类警告

--ignore-ssl-errors=[true|false] 忽略 SSL 错误,例如过期或自签名证书错误(默认为 false)。

上面是 CLI 选项,我无法从 Grunfile.js 传递。 有人可以帮我解决或建议解决此问题的另一种方法吗?

这是我的 Gruntfile.js:

module.exports = grunt => {
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

  grunt.initConfig({
    phantomjs: {
      // default: {
        options: {
          "ignore-ssl-errors": true,
          // tested here with different instructions as per comments 
          // below from users on this site, such as
          // "--ignore-ssl-errors": true (-- hyphen)
          // "ignore-ssl-errors": "true" ("true" as string)
          "ssl-protocol": "any",
          "ssl-certificates-path": "C:/path/to/cert.cer"
        }
      // }
    },
    accessibility: {
      options: {
        force: true,
        accessibilityLevel: 'WCAG2AAA',
        browser: true // tested with both true/false, i.e. opt for phantomjs/jsDom
      },
      test: {
        options: {
          urls: ['https://self-signed.badssl.com/']
        },
        src: ['example/test.html']
      }
    }
  });

  grunt.loadNpmTasks('grunt-accessibility');
  grunt.registerTask('default', ['accessibility']);
};

附注:

  • test url是一个实际的自签名ssl站点,所以你可以复制/粘贴上面的代码进行测试

  • 仅在 package.json 中的依赖项

    "devDependencies": {
        "grunt": "^1.0.1",
        "grunt-accessibility": "^5.0.0"
    }
    
  • 节点版本v.8.9.0

【问题讨论】:

  • 好问题!如果您尝试像这样设置 PhantomJS 选项会怎样:options: { "--ignore-ssl-errors": true, "--ssl-protocol": "any" }(是双破折号?)
  • 测试过了,但是没有用。
  • @Vaviloff 还有什么建议吗?
  • 也许将true 中的ignore-ssl-errors 设为字符串? "ignore-ssl-errors": "true",也应该尝试使用 -- 前缀来忽略 SSL 错误,因为 grunt-phantom 似乎只是连接命令行参数,而 PhantomJS 等待带有双破折号的开关。
  • 再试一次,还是不行。

标签: javascript node.js gruntjs phantomjs


【解决方案1】:

我认为您不能直接影响 PhantomJS 在另一个 Grunt 插件中的调用方式在您自己的 Gruntfile 中

如果我没记错的话,唯一的解决方案是提交对 grunt-accessibility 包的更改,该包将 ignore-ssl-errors 选项(在您传递给 grunt-accessibility 的选项中)上游传递给 PhantomJS; 拦截对 PhantomJS 的调用并注入 ignore-ssl-errors 选项。

我认为第二种解决方案将是最快和最方便的。您必须手动修改入口点(node_modules/.bin/phantomjsnode_modules/phantomjs/index.js)或编写一个预运行脚本来修改它。在修改后的 .js 文件中,您可以通过在文件顶部添加代码来注入 ignore-ssl-errors,并将其附加到 process.argv 数组:

process.argv.push("--ignore-ssl-errors=true");

【讨论】:

  • 抱歉,这几天有点忙。手动设置它可能是一个挑战,因为我希望它在不同的环境中自动化。
  • 我尝试将上面的代码添加到一堆文件的顶部,要么不起作用,要么被挂起 -- node_modules\phantomjs-prebuilt\bin\phantomjsnode_modules\phantomjs-prebuilt\lib\phantomjs.jsnode_modules\access-sniff\dist\runners\phantom.jsnode_modules\access-sniff\dist\runners\phantomExec.js , @ 987654332@,
  • 在你最后测试它的要求太高了,你只需要在gruntgrunt-accessibility的gruntifles和dependecies中复制上面的代码。
  • 思考者,有什么想法吗?
  • @n4m31ess_c0d3r,如果这不起作用,那么我建议尝试另一个选项——在 GitHub 上 fork grunt-accessibility,修改它的 gruntfile 以在调用 PhantomJS 时传递该选项,然后更新你的包.json 指向您的分叉 grunt-accessibility 存储库(使用指向它的 URL 代替版本号)。
【解决方案2】:

我总是使用 strict-ssl 命令,但差别很小。

尝试在你的命令中插入一个配置

会有npm config set strict-ssl false

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 2012-08-30
    • 2022-01-03
    • 2019-12-11
    • 2020-09-20
    • 2016-07-27
    相关资源
    最近更新 更多