Skip to content

Potential fix for code scanning alert no. 2: Incomplete multi-character sanitization #339

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 1 commit into
base: main
Choose a base branch
from

Conversation

harlan-zw
Copy link
Collaborator

Potential fix for https://github.com/nuxt-modules/og-image/security/code-scanning/2

To fix the problem, we need to ensure that all instances of <script> tags are removed from the HTML string, even if they are nested or malformed. The best way to achieve this is to apply the regular expression replacement repeatedly until no more replacements can be performed. This approach ensures that all script tags are effectively removed.

We will modify the code on line 117 to repeatedly apply the regular expression replacement until the HTML string no longer changes. This will involve using a loop to check for changes in the HTML string after each replacement.

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

…er sanitization

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
let previousHtml;
do {
previousHtml = html;
html = html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');

Check failure

Code scanning / CodeQL

Bad HTML filtering regexp High

This regular expression does not match script end tags like </script >.

Copilot Autofix

AI 3 months ago

The best way to fix the problem is to use a well-tested HTML sanitization library instead of relying on a custom regular expression. This approach ensures that all edge cases and variations in HTML tags are properly handled, reducing the risk of security vulnerabilities.

To implement this fix, we will:

  1. Install the sanitize-html library.
  2. Replace the custom regular expression with a call to the sanitize-html library to remove all <script> tags from the HTML.
Suggested changeset 2
src/runtime/server/og-image/templates/html.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/runtime/server/og-image/templates/html.ts b/src/runtime/server/og-image/templates/html.ts
--- a/src/runtime/server/og-image/templates/html.ts
+++ b/src/runtime/server/og-image/templates/html.ts
@@ -8,2 +8,3 @@
 import { applyEmojis } from '../satori/transforms/emojis'
+import sanitizeHtml from 'sanitize-html'
 
@@ -116,7 +117,5 @@
   // need to remove ALL script tags from the html
-  let previousHtml;
-  do {
-    previousHtml = html;
-    html = html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
-  } while (html !== previousHtml);
+  html = sanitizeHtml(html, {
+    allowedTags: sanitizeHtml.defaults.allowedTags.filter(tag => tag !== 'script'),
+  });
 
EOF
@@ -8,2 +8,3 @@
import { applyEmojis } from '../satori/transforms/emojis'
import sanitizeHtml from 'sanitize-html'

@@ -116,7 +117,5 @@
// need to remove ALL script tags from the html
let previousHtml;
do {
previousHtml = html;
html = html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
} while (html !== previousHtml);
html = sanitizeHtml(html, {
allowedTags: sanitizeHtml.defaults.allowedTags.filter(tag => tag !== 'script'),
});

package.json
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/package.json b/package.json
--- a/package.json
+++ b/package.json
@@ -99,3 +99,4 @@
     "unwasm": "^0.3.9",
-    "yoga-wasm-web": "^0.3.3"
+    "yoga-wasm-web": "^0.3.3",
+    "sanitize-html": "^2.14.0"
   },
EOF
@@ -99,3 +99,4 @@
"unwasm": "^0.3.9",
"yoga-wasm-web": "^0.3.3"
"yoga-wasm-web": "^0.3.3",
"sanitize-html": "^2.14.0"
},
This fix introduces these dependencies
Package Version Security advisories
sanitize-html (npm) 2.14.0 None
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