admin管理员组

文章数量:1122832

I have a web interface, that use JavaScript function to create html elements using data from data-* attribute created by web-server. This function will be triggered by onchange event of a checkbox.

JavaScript function and the part for add event listener are both inside an external static JavaScript. Using defer add_event_listener will be triggered after the page has finished loading.

My question is, how do I change the script, so that Content Security Policy with script-src: 'self'; works, without 'unsafe-inline' to avoid XSS attacks?

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Security-Policy" content="script-src 'self'; style-src 'self'">
</head>
<body>
<script  src="./script.js" defer></script>
<div id="data-display">hi</div>
<label for="radio-1">radio-1
    <input class="radio-1" id="radio-1" name="radio" value="1" type="radio">
</label>
<label for="radio-2">radio-2
    <input class="radio-1" id="radio-2" name="radio" value="2" type="radio">
</label>
</body>
</html>

change meta tag content to content="script-src 'unsafe-inline' 'self'; style-src 'self'" to avoid error: "The page’s settings blocked an event handler (script-src-attr) from being executed because it violates the following directive: “script-src 'self'"

function changeData(data) {
    var data_display = document.getElementById('data-display');
    data_display.innerHTML = '';
    p = document.createElement("p");
    p.innerText = data;
    data_display.appendChild(p);
}
document.querySelectorAll('.radio-1').forEach( (element) => {
    element.setAttribute("onchange", "changeData('hi radio')");
});

I am thinking about generate all those html elements server-side, and make them hidden. Only use JavaScript to make them visible. The problem is: I will have 20 to 30 different but similar divs to display/hide, and that is wasteful. I hope there are more elegant solutions out there.

I have a web interface, that use JavaScript function to create html elements using data from data-* attribute created by web-server. This function will be triggered by onchange event of a checkbox.

JavaScript function and the part for add event listener are both inside an external static JavaScript. Using defer add_event_listener will be triggered after the page has finished loading.

My question is, how do I change the script, so that Content Security Policy with script-src: 'self'; works, without 'unsafe-inline' to avoid XSS attacks?

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Security-Policy" content="script-src 'self'; style-src 'self'">
</head>
<body>
<script  src="./script.js" defer></script>
<div id="data-display">hi</div>
<label for="radio-1">radio-1
    <input class="radio-1" id="radio-1" name="radio" value="1" type="radio">
</label>
<label for="radio-2">radio-2
    <input class="radio-1" id="radio-2" name="radio" value="2" type="radio">
</label>
</body>
</html>

change meta tag content to content="script-src 'unsafe-inline' 'self'; style-src 'self'" to avoid error: "The page’s settings blocked an event handler (script-src-attr) from being executed because it violates the following directive: “script-src 'self'"

function changeData(data) {
    var data_display = document.getElementById('data-display');
    data_display.innerHTML = '';
    p = document.createElement("p");
    p.innerText = data;
    data_display.appendChild(p);
}
document.querySelectorAll('.radio-1').forEach( (element) => {
    element.setAttribute("onchange", "changeData('hi radio')");
});

I am thinking about generate all those html elements server-side, and make them hidden. Only use JavaScript to make them visible. The problem is: I will have 20 to 30 different but similar divs to display/hide, and that is wasteful. I hope there are more elegant solutions out there.

Share Improve this question edited 20 hours ago Mister Jojo 22.2k6 gold badges25 silver badges42 bronze badges asked yesterday AvogatroAvogatro 112 bronze badges New contributor Avogatro is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 1

I have tested your code on Windows 11 in Chrome, Edge and Firefox. I can only reproduce the error you report in Firefox.

This might be a bug, or a feature, I can't tell, but I do know the solution:

Instead of changing the DOM using:

element.setAttribute("onchange", "changeData('hi radio')");

You can attach an event handler like this:

element.addEventListener("change", function (event) {
    changeData("hi radio");
});

Firefox likes that, and it is the better way of doing it.

See: addEventListener() method

本文标签: