Skip to content

Conversation

krishnprakash
Copy link
Owner

Fixes https://github.com/krishnprakash/json-viewer/security/code-scanning/1

To fix the problem, we need to ensure that the text variable is properly sanitized or escaped before being inserted into the innerHTML of the script element. One effective way to do this is to use JSON.stringify to safely encode the text variable, which will escape any potentially harmful characters.

  • In general terms, the problem can be fixed by ensuring that any text content derived from the DOM is properly escaped before being used as HTML.
  • Specifically, we will modify the code in extension/src/json-viewer/viewer/expose-json.js to use JSON.stringify on the text variable before inserting it into the innerHTML of the script element.
  • The changes will be made to the exposeJson function in extension/src/json-viewer/viewer/expose-json.js.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Signed-off-by: Phileco <132178579+krishnprakash@users.noreply.github.com>
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.
@krishnprakash krishnprakash marked this pull request as ready for review November 6, 2024 03:51
@krishnprakash krishnprakash merged commit 4e0fc61 into master Nov 6, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant