如何在辅助功能自动化期间忽略ssl证书警告或传递自签名证书(来自gruntfile内部)?

n4m*_*d3r 11 javascript node.js phantomjs gruntjs

我正在使用grunt-accessibility插件来自动报告accessibility错误.它通常工作正常,但是当我在一个网站上尝试它时self signed certificate(显示一个interim带有一些证书安全警告的页面并且如果你仍然希望继续访问该网站的链接),它会报告错误interim页面本身,当然是一个空页面:

<html>
    <head></head>
    <body></body>
</html>
Run Code Online (Sandbox Code Playgroud)

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

我在尝试什么?

我曾尝试过以下内容(通过谷歌搜索和其他SO's问答):

  1. 臭名昭着的黑客

    npm set strict-ssl false
    
    Run Code Online (Sandbox Code Playgroud)
  2. 添加导入的证书路径

    npm config set cafile="C:\path\to\cert.cer"
    
    Run Code Online (Sandbox Code Playgroud)
  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']);
};
Run Code Online (Sandbox Code Playgroud)

PS:

  • test url是一个实际的自签名ssl站点,因此您可以复制/粘贴上面的代码并对其进行测试

  • 只有依赖关系 package.json

    "devDependencies": {
        "grunt": "^1.0.1",
        "grunt-accessibility": "^5.0.0"
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 节点版本 v.8.9.0

Roy*_*ker 3

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

如果我没记错的话,唯一的解决方案是要么提交对 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");
Run Code Online (Sandbox Code Playgroud)