跳至主要内容

解决 Jenkins 中无法展示 HTML 样式的问题

 原文链接


问题描述

对于测试报告来说,除了内容的简洁精炼,样式的美观也很重要。常用的做法是,采用HTML格式的文档,并搭配CSSJS,实现自定义的样式和动画效果(例如展开、折叠等)。

Jenkins中要展示HTML文档,通常采用的方式有两种:

  • 使用HTML Publisher Plugin
  • 使用Files to archive功能,在Build Artifacts中显示HTML文档链接。

第一种方式配合插件,可以通过图形化操作实现简易配置,并且展示效果也不错;而第二种方式的优势在于使用Jenkins自带的功能,不依赖插件也能实现基本的需求。

然而,不管是采用哪种方式,都有可能会遇到一种情况,就是展示出来的HTML报告样式全无。在浏览器的Network中查看资源加载情况,会发现相关的CSSJS都没法正常加载。

1
2
3
4
Refused to load the stylesheet 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css' because it violates the following Content Security Policy directive: "style-src 'self'".
Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-0EZqoz+oBhx7gF4nvY2bSqoGyy4zLjNF+SDQXGp/ZrY='), or a nonce ('nonce-...') is required to enable inline execution.
Blocked script execution in 'http://10.13.0.146:8888/job/SkyPixel-SmokeTest/34/artifact/reports/SkyPixel-smoketest/34.html' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.
Refused to load the stylesheet 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css' because it violates the following Content Security Policy directive: "style-src 'self'".

问题分析

出现该现象的原因在于Jenkins中配置的CSPContent Security Policy)。

简单地说,这是Jenkins的一个安全策略,默认会设置为一个非常严格的权限集,以防止Jenkins用户在workspace/userContentarchived artifacts中受到恶意HTML/JS文件的攻击。

默认地,该权限集会设置为:

1
sandbox; default-src 'none'; img-src 'self'; style-src 'self';

在该配置下,只允许加载:

  • Jenkins服务器上托管的CSS文件
  • Jenkins服务器上托管的图片文件

而如下形式的内容都会被禁止:

  • JavaScript
  • plugins (object/embed)
  • HTML中的内联样式表(Inline style sheets),以及引用的外站CSS文件
  • HTML中的内联图片(Inline image definitions),以及外站引用的图片文件
  • frames
  • web fonts
  • XHR/AJAX
  • etc.

可以看出,这个限制非常严格,在此限制下也就不难理解为什么我们的HTML没法正常展示样式了。

解决方案

临时解决方案

要解决该问题,方式也比较简单,就是修改Content Security Policy的默认配置。

修改方式为,进入Manage Jenkins->Script console,输入如下命令并进行执行。

1
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

当看到如下结果后,则说明配置修改已经生效。

1
2
Result
Result:

再次进行构建,新生成的HTML就可以正常展示样式了。需要说明的是,该操作对之前构建生成的HTML报告无效。

永久解决方案

不过,该方法还存在一个问题:该配置只是临时生效,当重启Jenkins后,Content Security Policy又会恢复为默认值,从而HTML样式又没法展示了。

当前,Jenkins官方还没有相应的解决方法,我们只能在每次启动或重启Jenkins时,重新修改该安全策略。

如果手工地来重复这项工作,也是可行,但并不是一个好的解决方案。

回到刚才的Script console,会发现我们执行的命令其实就是一段Groovy代码;那么,如果我们可以实现在Jenkins每次启动时自动地执行该Groovy代码,那么也就同样能解决我们的问题了。

好在Jenkins已经有相应的插件:

搜索安装startup-trigger-pluginGroovy插件后,我们就可以进行配置了。

配置方式如下:

  • 新建一个job,该job专门用于Jenkins启动时执行的配置命令;
  • Build Triggers模块下,勾选Build when job nodes start
  • Build模块下,Add build step->Execute system Groovy script,在Groovy Script中输入配置命令,System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

需要注意的是,添加构建步骤的时候,应该选择Execute system Groovy script,而不是Execute Groovy script。关于这两者之间的差异,简单地说,Groovy Script相当于是运行在master/slave系统JVM环境中,而system groovy script,则是运行在Jenkins masterJVM环境中,与前面提到的Jenkins Script Console功能相同。如需了解更多信息,可查看Groovy plugin的详细说明

至此,我们就彻底解决HTML样式展示异常的问题了。

但还有一点需要格外注意,在本文的演示中,我们修改CSPContent Security Policy)配置时关闭了的所有安全保护策略,即将hudson.model.DirectoryBrowserSupport.CSP设置为空,其实这是存在很大的安全隐患的。

正确的做法,我们应该是结合项目的实际情况,选择对应的安全策略。例如,如果我们需要开启脚本文件加载,但是只限于Jenkins服务器上托管的CSS文件,那么就可以采用如下配置。

1
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox; style-src 'self';")

除此之外,CSP可以实现非常精细的权限配置,详细配置可参考Content Security Policy Reference

评论

此博客中的热门博文

Resolving errSecInternalComponent errors during code signing

原文链接 One code signing issue I commonly see, both here on DevForums and in my Day Job™ with DTS, is that the codesign command fails with errSecInternalComponent. This issue crops up in a wide variety of circumstances and the correct fix depends on the specific problem. This post is my attempt to clarify the potential causes of this error and help folks resolve it. If you have any questions or comments about this, please start a new thread, tagging it with Code Signing so that I see it. Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com" Resolving errSecInternalComponent errors during code signing In some circumstances the codesign command might fail with the error errSecInternalComponent. For example: % codesign -s "Apple Development" "MyTrue" MyTrue: errSecInternalComponent This typically affects folks who are signing code in a nonstandard environm...

iOS:检测使用VPN或Proxy

参考链接: https://www.jianshu.com/p/c3b950dbf86a https://gist.github.com/PramodJoshi/4faad4c91f7dcb4eb9b06be8390c01db http://noodlecode.net/2018/04/check-if-ios-app-is-connected-to-vpn 第一种方法 需要导入框架CFNetwork 然后,这个方法是mrc的:需要添加-fno-objc-arc的flag 代码如下: + ( BOOL )getProxyStatus { NSDictionary *proxySettings = NSMakeCollectable ([( NSDictionary *) CFNetworkCopySystemProxySettings () autorelease]); NSArray *proxies = NSMakeCollectable ([( NSArray *) CFNetworkCopyProxiesForURL (( CFURLRef )[ NSURL URLWithString: @"http://www.google.com" ], ( CFDictionaryRef )proxySettings) autorelease]); NSDictionary *settings = [proxies objectAtIndex: 0 ]; NSLog ( @"host=%@" , [settings objectForKey:( NSString *)kCFProxyHostNameKey]); NSLog ( @"port=%@" , [settings objectForKey:( NSString *)kCFProxyPortNumberKey]); NSLog ( @"type=%@" , [settings objectForKey:( NSString *)kCFProxyTypeKey]); if ([[settings object...

去广告DNS设置,国内ADGuard DNS方案,手机电脑iOS去广告,保护隐私

 原文链接 之前分享过使用mac系统搭建adguard home,这几个月用下来零零散散基本上也被弃用了。主要原因是因为需要保持电脑一直开机。但是我的电脑是笔记本,存在移动各个地域的情况,也就是说只能够屏蔽电脑自身,对于手机而言不太现实。今天偶然发现dnspod推出了高级版的公共解析。dnspod背靠腾讯云,肯定是合法合规的公共解析服务,这个高级版用起来不错。 国内自己搭建解析服务是违法行为,所以这也是为什么使用dnspod的原因。 后台截图 开始使用 首先我们先进入dnspod的公共解析页面,点击开始使用。 专业版公共解析 dnspod会提供几种预设,我们选择「开发者」即可 开发者 然后你就成功的申请到自己个人使用的dns了! 更新拦截规则 我们可以将常见的广告过滤规则加入到dns中。我们在顶部选项卡中选择「拦截规则」。 拦截规则设置 打开adguard adguard 绑定iOS设备 推荐使用描述文件的方式,删除配置时删除描述文件即可。 描述文件 绑定macOS 推荐使用描述文件的方式,删除配置时删除描述文件即可。 描述文件 mac需要在「系统偏好设置」的「网络」中查看是否正在运行。 代理 如果没有运行需要点击「···」来启动服务。 启动服务 绑定路由器 找到自己路由器的DHCP设置,修改dns,然后记得绑定自己的ip。 修改dns 绑定ip 费用 目前有300万次/月的免费额度,但没有超出之后的价格。300万次一个人比较难用完,可以放心使用。 我个人使用iOS设备两台、智能家居、电脑两台,日均请求数大致2万/日。 判断是否搭建成功 可以通过查看日志的方式,日志大概有半小时到一小时的延迟,请耐心等待。