webRequest.getSecurityInfo()
使用此函数获取与特定请求相关的 TLS 连接的详细信息。
你需要将相关请求的 requestId
和一些可选的额外参数传递给此函数。它返回一个 Promise
,其会兑现为一个 SecurityInfo
对象。
此函数只能从 webRequest.onHeadersReceived
监听器内部调用。requestId
可以在传递给监听器的 details
对象中找到。
你必须将“blocking”选项传递给 webRequest.onHeadersReceived.addListener()
。因此,要使用此 API,你必须具有“webRequestBlocking”API 权限,以及使用 webRequest
监听器所需的常规权限(“webRequest”权限和主机的主机权限)。
语法
let gettingInfo = browser.webRequest.getSecurityInfo(
requestId, // 字符串
options // 可选对象
)
参数
requestId
-
string
。要获取安全信息的请求的 ID。可以从传递给任何webRequest
事件监听器的details
对象中获取。 options
可选-
object
。一个可以包含以下属性的对象:certificateChain
可选-
boolean
。如果为true
,返回的SecurityInfo
对象将包含完整的证书链和信任根证书。如果为false
则仅包含服务器证书。默认为false
。 rawDER
可选-
boolean
。如果为true
,则SecurityInfo.certificates
属性中的每个CertificateInfo
都将包含一个rawDER
属性。其包含 DER 编码的组成证书数据的 ASN.1。
返回值
一个 Promise
,其会兑现为一个 SecurityInfo
对象。
浏览器兼容性
示例
此示例监听所有对“mozilla.org”及其子域名的 HTTPS 请求,并记录服务器证书中的主题的名称:
async function logSubject(details) {
try {
let securityInfo = await browser.webRequest.getSecurityInfo(
details.requestId,
{},
);
console.log(details.url);
if (securityInfo.state === "secure" || securityInfo.state === "weak") {
console.log(securityInfo.certificates[0].subject);
}
} catch (error) {
console.error(error);
}
}
browser.webRequest.onHeadersReceived.addListener(
logSubject,
{ urls: ["https://*.mozilla.org/*"] },
["blocking"],
);
此示例监听所有对“mozilla.org”及其子域名的 HTTPS 请求,并记录受信任根证书中的名称:
async function logRoot(details) {
try {
let securityInfo = await browser.webRequest.getSecurityInfo(
details.requestId,
{ certificateChain: true },
);
console.log(details.url);
if (securityInfo.state === "secure" || securityInfo.state === "weak") {
console.log(
securityInfo.certificates[securityInfo.certificates.length - 1].issuer,
);
}
} catch (error) {
console.error(error);
}
}
browser.webRequest.onHeadersReceived.addListener(
logRoot,
{ urls: ["https://*.mozilla.org/*"] },
["blocking"],
);