我正在使用chrome扩展程序向网站注入AJAX请求。但是我随机收到这个错误:
拒绝连接“http://127.0.0.1:5005/”,因为它违反了“https://media-src.linkedin.com/media/” 遵循内容安全策略指令:“connect-src'self' static.licdn.com media.licdn.com static-exp1.licdn.com static-exp2.licdn.com media-exp1.licdn.com media-exp2.licdn.com
WebMvcConfigurerAdapter
is deprecated as of version 5.0 www.linkedin.com s.c.lnkd.licdn.com m.c.lnkd.licdn.com s.c.exp1.licdn.com s.c.exp2.licdn.com m.c.exp1.licdn.com m.c.exp2.licdn.com wss://*.linkedin.com dms.licdn.com“。
它不会一直发生。只是在某些时候。我真的很困惑。有办法吗?
这是chrome_extension.js
function checkName(){
var fullNameSplit = $('#topcard h1').first().text().split(' ');
var firstName = fullNameSplit[0]
var lastName = fullNameSplit[fullNameSplit.length - 1]
console.log(firstName, lastName)
console.log('checking name')
$.ajax({
type:'POST',
url:'http://127.0.0.1:5005/',
data: JSON.stringify({first: firstName, last: lastName}),
success: function(response) {
data = JSON.parse(response)
firstNameFound = data.first_name
lastNameFound = data.last_name
fullNameFound = data.full_name
$('.profile-info').prepend('Full Name Found: '+fullNameFound+'</br></br>');
//</br></br>First Name Found: '+firstNameFound+'</br></br>Last Name Found: '+lastNameFound+'</br></br>'
},
// dataType: 'json',
contentType: "application/json"
})
}
的manifest.json
{
"manifest_version": 2,
"name": "Upstart Extension",
"version": "1.0",
"browser_action": {
"default_icon": "icons/download.png"
// "default_popup": "popup.html"
},
"background": {
"scripts" : ["background.js"]
},
"permissions": [
"activeTab",
"https://ajax.googleapis.com/",
"storage"
],
"content_scripts": [
{
// "matches":["https://www.linkedin.com/*"],
"matches": ["https://www.linkedin.com/*", "http://www.linkedin.com/*"],
"js":["keypress.js", "jquery.js", "chrome_extension.js"],
"run_at": "document_end"
}
],
"content_security_policy": "script-src 'self' http://127.0.0.1:5005/'; connect-src 'self' http://127.0.0.1:5005/'; object-src 'self'"
}
答案 0 :(得分:1)
这看起来像是被网站Content Security Policy阻止,这是由网络服务器设置的安全策略,它提供了允许连接的服务器列表。基本上,它是一种安全措施,旨在完全阻止您正在做的事情,因为它看起来像是XSS注入攻击。
可能只在某些时候发生,因为只有部分网站启用了CSP。
Is there a way around it?
假设CSP有效,除非相关网站添加了您要连接到其内容政策的来源,否则没有办法绕过它。