Skip to content

chore(architecture): re-structure files and folders #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft

Conversation

andypf
Copy link
Owner

@andypf andypf commented Jul 30, 2025

No description provided.

@andypf andypf marked this pull request as draft July 30, 2025 14:34
parseButton.addEventListener("click", () => {
const jsonData = textArea.value
const interactiveDemo = document.getElementById("interactive-demo")
interactiveDemo.setAttribute("data", jsonData)

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

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

Copilot Autofix

AI 18 days ago

To fix the problem, we should ensure that any user input from the textarea is properly escaped or sanitized before being set as the value of the data attribute. Since the expected input is JSON, we should first validate that the input is valid JSON before passing it to the component. If the input is not valid JSON, we should not update the attribute and instead show an error. This prevents malicious input from being interpreted as HTML or script. The fix should be applied in the <script> block in src/demo/index.html, specifically in the event listener for the "Parse JSON" button (lines 72–76).

Steps:

  • Parse the input as JSON using JSON.parse.
  • If parsing succeeds, set the attribute to the stringified JSON (which is guaranteed to be safe).
  • If parsing fails, show an error and do not update the attribute.

No external libraries are needed; we can use the built-in JSON.parse and JSON.stringify.


Suggested changeset 1
src/demo/index.html

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/demo/index.html b/src/demo/index.html
--- a/src/demo/index.html
+++ b/src/demo/index.html
@@ -74,3 +74,9 @@
         const interactiveDemo = document.getElementById("interactive-demo")
-        interactiveDemo.setAttribute("data", jsonData)
+        try {
+          const parsed = JSON.parse(jsonData)
+          // Use JSON.stringify to ensure safe serialization
+          interactiveDemo.setAttribute("data", JSON.stringify(parsed))
+        } catch (e) {
+          alert("Invalid JSON. Please enter valid JSON data.")
+        }
       })
EOF
@@ -74,3 +74,9 @@
const interactiveDemo = document.getElementById("interactive-demo")
interactiveDemo.setAttribute("data", jsonData)
try {
const parsed = JSON.parse(jsonData)
// Use JSON.stringify to ensure safe serialization
interactiveDemo.setAttribute("data", JSON.stringify(parsed))
} catch (e) {
alert("Invalid JSON. Please enter valid JSON data.")
}
})
Copilot is powered by AI and may make mistakes. Always verify output.
const contentData = this.getContentData()
if (contentData) {
this.clearContent() // Clear content after reading
this.setAttribute("data", contentData) // Set as attribute for future use

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

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

Copilot Autofix

AI 18 days ago

To fix the problem, we should ensure that any text content read from the DOM and set as an attribute is properly escaped or sanitized, especially if it could later be interpreted as HTML. Since the data attribute is intended to hold JSON, we should ensure that the text content is valid JSON before setting it as the attribute. If the text content is not valid JSON, we should not set it as the data attribute, or we should escape it appropriately.

The best fix is to validate that contentData is valid JSON before setting it as the data attribute. If it is not valid JSON, we should not set it, or we should set a safe default (e.g., "{}"). This prevents any malicious HTML or script from being injected via the text content.

Changes to make:

  • In the data getter, before setting contentData as the data attribute, check if it is valid JSON.
  • If it is valid JSON, set it as the attribute; otherwise, set a safe default (e.g., "{}").
  • Optionally, log a warning if invalid JSON is detected.

No new imports are needed, as JSON parsing is built-in.


Suggested changeset 1
src/package/components/json-viewer.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/package/components/json-viewer.ts b/src/package/components/json-viewer.ts
--- a/src/package/components/json-viewer.ts
+++ b/src/package/components/json-viewer.ts
@@ -230,5 +230,14 @@
       this.clearContent() // Clear content after reading
-      this.setAttribute("data", contentData) // Set as attribute for future use
+      // Only set as attribute if valid JSON, otherwise set to "{}"
+      try {
+        JSON.parse(contentData)
+        this.setAttribute("data", contentData)
+        return contentData
+      } catch (e) {
+        console.warn("Invalid JSON in element content, using default '{}'", e)
+        this.setAttribute("data", "{}")
+        return "{}"
+      }
     }
-    return contentData || "{}"
+    return "{}"
   }
EOF
@@ -230,5 +230,14 @@
this.clearContent() // Clear content after reading
this.setAttribute("data", contentData) // Set as attribute for future use
// Only set as attribute if valid JSON, otherwise set to "{}"
try {
JSON.parse(contentData)
this.setAttribute("data", contentData)
return contentData
} catch (e) {
console.warn("Invalid JSON in element content, using default '{}'", e)
this.setAttribute("data", "{}")
return "{}"
}
}
return contentData || "{}"
return "{}"
}
Copilot is powered by AI and may make mistakes. Always verify output.
Comment on lines +433 to +442
jsonData.innerHTML = `
<div class="error-container">
Invalid JSON data
<details>
<summary>Raw data:</summary>
<pre >${this.data}</pre>
<p>Error: ${result.error || "Unknown error"}</p>
</details>
</div>
`

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

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

Copilot Autofix

AI 18 days ago

To fix this vulnerability, we must ensure that any untrusted data inserted into innerHTML is properly escaped so that HTML meta-characters are not interpreted as markup. The best way to do this is to escape the data before interpolation, converting <, >, &, ", and ' to their corresponding HTML entities. This can be done by introducing a utility function (e.g., escapeHtml) within the file to perform the escaping. The fix should be applied specifically to the interpolation of ${this.data} on line 438, ensuring that only the escaped version of the data is inserted into the HTML template. The rest of the functionality should remain unchanged.

Required changes:

  • Add an escapeHtml function to the file.
  • Use escapeHtml(this.data) instead of ${this.data} in the error template on line 438.

Suggested changeset 1
src/package/components/json-viewer.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/package/components/json-viewer.ts b/src/package/components/json-viewer.ts
--- a/src/package/components/json-viewer.ts
+++ b/src/package/components/json-viewer.ts
@@ -437,3 +437,3 @@
             <summary>Raw data:</summary>
-            <pre >${this.data}</pre>
+            <pre >${escapeHtml(this.data)}</pre>
             <p>Error: ${result.error || "Unknown error"}</p>
@@ -458 +458,11 @@
 }
+
+// Utility function to escape HTML special characters
+function escapeHtml(str: string): string {
+  return str
+    .replace(/&/g, "&amp;")
+    .replace(/</g, "&lt;")
+    .replace(/>/g, "&gt;")
+    .replace(/"/g, "&quot;")
+    .replace(/'/g, "&#39;");
+}
EOF
@@ -437,3 +437,3 @@
<summary>Raw data:</summary>
<pre >${this.data}</pre>
<pre >${escapeHtml(this.data)}</pre>
<p>Error: ${result.error || "Unknown error"}</p>
@@ -458 +458,11 @@
}

// Utility function to escape HTML special characters
function escapeHtml(str: string): string {
return str
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#39;");
}
Copilot is powered by AI and may make mistakes. Always verify output.
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