Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions extension/src/json-viewer/viewer/expose-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
window.json = JSON.parse(text);

} else {
var script = document.createElement("script") ;
script.innerHTML = 'window.json = ' + text + ';';
var script = document.createElement("script");
script.innerHTML = 'window.json = ' + JSON.stringify(JSON.parse(text)) + ';';

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.

Copilot Autofix

AI 11 months ago

To fix the problem, we need to ensure that the text content is safely handled and not directly inserted into the DOM as HTML. One effective way to do this is to use textContent instead of innerHTML when creating the script element. This will prevent the browser from interpreting the text as HTML, thereby mitigating the risk of XSS.

  • Replace the use of innerHTML with textContent for the script element.
  • Ensure that the JSON data is properly escaped and handled as plain text.
Suggested changeset 1
extension/src/json-viewer/viewer/expose-json.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/extension/src/json-viewer/viewer/expose-json.js b/extension/src/json-viewer/viewer/expose-json.js
--- a/extension/src/json-viewer/viewer/expose-json.js
+++ b/extension/src/json-viewer/viewer/expose-json.js
@@ -8,3 +8,3 @@
     var script = document.createElement("script");
-    script.innerHTML = 'window.json = ' + JSON.stringify(JSON.parse(text)) + ';';
+    script.textContent = 'window.json = ' + JSON.stringify(JSON.parse(text)) + ';';
     document.head.appendChild(script);
EOF
@@ -8,3 +8,3 @@
var script = document.createElement("script");
script.innerHTML = 'window.json = ' + JSON.stringify(JSON.parse(text)) + ';';
script.textContent = 'window.json = ' + JSON.stringify(JSON.parse(text)) + ';';
document.head.appendChild(script);
Copilot is powered by AI and may make mistakes. Always verify output.
document.head.appendChild(script);
}
}
Expand Down