Skip to content

Conversation

@raphael-istari
Copy link
Contributor

migrate site to docusaurus-docs


// Escape backticks and dollar signs for template literals
const escapeForTemplate = (str: string) => {
return str.replace(/`/g, '\\`').replace(/\$/g, '\\$');

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.

Copilot Autofix

AI 4 days ago

The code block at line 43 defines escapeForTemplate, which attempts to escape backticks and dollar signs from the code content that will be injected into a template literal of the form `${...}`. However, it does not escape backslashes, which could result in a malformed string. The best way to fix this is to first escape all backslashes, then escape backticks and dollar signs. The fix is to update the escapeForTemplate function to use:

str.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$/g, '\\$')

This order ensures that pre-existing escape sequences in str are handled correctly and no malformed escapes are introduced.

Edit only escapeForTemplate in docusaurus-docs/src/components/RunnableCodeBlock/index.tsx, and make no changes elsewhere. No external dependencies are needed.


Suggested changeset 1
docusaurus-docs/src/components/RunnableCodeBlock/index.tsx

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/docusaurus-docs/src/components/RunnableCodeBlock/index.tsx b/docusaurus-docs/src/components/RunnableCodeBlock/index.tsx
--- a/docusaurus-docs/src/components/RunnableCodeBlock/index.tsx
+++ b/docusaurus-docs/src/components/RunnableCodeBlock/index.tsx
@@ -40,7 +40,7 @@
   
   // Escape backticks and dollar signs for template literals
   const escapeForTemplate = (str: string) => {
-    return str.replace(/`/g, '\\`').replace(/\$/g, '\\$');
+    return str.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$/g, '\\$');
   };
   
   const escapedCode = escapeForTemplate(codeContent);
EOF
@@ -40,7 +40,7 @@

// Escape backticks and dollar signs for template literals
const escapeForTemplate = (str: string) => {
return str.replace(/`/g, '\\`').replace(/\$/g, '\\$');
return str.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$/g, '\\$');
};

const escapedCode = escapeForTemplate(codeContent);
Copilot is powered by AI and may make mistakes. Always verify output.

// Escape backticks and dollar signs for template literals
const escapeForTemplate = (str: string) => {
return str.replace(/`/g, '\\`').replace(/\$/g, '\\$');

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.

Copilot Autofix

AI 4 days ago

To correctly escape input for inclusion in a Go string literal using backticks (which start/end with backticks and in which only the backtick itself cannot appear unescaped), one does not generally need to escape backslashes—because Go's raw string literals (backtick) will treat backslashes as normal characters. However, in template literals in JavaScript, backslashes can cause escaping bugs if not properly handled. Since the intention is to embed arbitrary code into a Go raw string literal (inside backticks), we're only required to escape the backtick itself. The original code also escaped $ for use inside a JS template literal (so that ${} would not be evaluated). But, the current logic might still fail if there are backslashes followed by backticks or dollar signs. The safest approach, for JS template literal embedding, is to escape all three: backtick, dollar sign, and backslash in that order—and use a single replace with a function, or at least escape backslash first to avoid double-escaping.

The best fix is:

  • Escape backslashes (\) first, then backticks (`), then dollar signs ($).
  • Use a single replace, or chain in the correct order.
  • Use a well-tested library like lodash.escape if you want robust HTML/joined escaping, but for this specific context, a simple regex suffices.

Required changes:

  • In escapeForTemplate, change the implementation to escape backslash first, then backtick, then dollar sign.
  • No additional dependencies are needed.

Suggested changeset 1
docusaurus-docs/src/components/RunnableCodeBlock/index.tsx

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/docusaurus-docs/src/components/RunnableCodeBlock/index.tsx b/docusaurus-docs/src/components/RunnableCodeBlock/index.tsx
--- a/docusaurus-docs/src/components/RunnableCodeBlock/index.tsx
+++ b/docusaurus-docs/src/components/RunnableCodeBlock/index.tsx
@@ -40,7 +40,11 @@
   
   // Escape backticks and dollar signs for template literals
   const escapeForTemplate = (str: string) => {
-    return str.replace(/`/g, '\\`').replace(/\$/g, '\\$');
+    // Escape backslash, then backtick, then dollar sign (in that order)
+    return str
+      .replace(/\\/g, '\\\\')
+      .replace(/`/g, '\\`')
+      .replace(/\$/g, '\\$');
   };
   
   const escapedCode = escapeForTemplate(codeContent);
EOF
@@ -40,7 +40,11 @@

// Escape backticks and dollar signs for template literals
const escapeForTemplate = (str: string) => {
return str.replace(/`/g, '\\`').replace(/\$/g, '\\$');
// Escape backslash, then backtick, then dollar sign (in that order)
return str
.replace(/\\/g, '\\\\')
.replace(/`/g, '\\`')
.replace(/\$/g, '\\$');
};

const escapedCode = escapeForTemplate(codeContent);
Copilot is powered by AI and may make mistakes. Always verify output.
DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);

String query = "${escapedCode.replace(/"/g, '\\"')}";

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.

Copilot Autofix

AI 4 days ago

To fix the problem, we must ensure that all special characters in the query string are properly escaped for safe injection in a Java string literal. In Java, both double quotes (") and backslashes (\) need to be escaped (i.e., \" and \\). The best fix is to create a helper function (analogous to escapeForTemplate) that escapes both backslashes and double quotes for Java strings. We should apply this function specifically for the Java code block (line 114), replacing the existing direct .replace(/"/g, '\\"') logic, and ensure comprehensive escaping even if both characters are present.

Modify the file docusaurus-docs/src/components/RunnableCodeBlock/index.tsx as follows:

  • Above the return statement, introduce a helper function, e.g., escapeForJavaString, which:
    • Replaces all \ with \\.
    • Then replaces all " with \".
  • Pass escapedJavaCode (output of this function applied to codeContent) into the Java string literal on line 114.

No external dependency is required; the builtin JavaScript .replace() with the g flag suffices.


Suggested changeset 1
docusaurus-docs/src/components/RunnableCodeBlock/index.tsx

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/docusaurus-docs/src/components/RunnableCodeBlock/index.tsx b/docusaurus-docs/src/components/RunnableCodeBlock/index.tsx
--- a/docusaurus-docs/src/components/RunnableCodeBlock/index.tsx
+++ b/docusaurus-docs/src/components/RunnableCodeBlock/index.tsx
@@ -45,6 +45,12 @@
   
   const escapedCode = escapeForTemplate(codeContent);
 
+  // Helper function to escape input for Java string literals
+  const escapeForJavaString = (str: string) => {
+    return str.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
+  };
+  const escapedJavaCode = escapeForJavaString(codeContent);
+
   return (
     <div className={styles.runnable}>
       <Tabs>
@@ -111,7 +117,7 @@
         DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);
         DgraphClient dgraphClient = new DgraphClient(stub);
         
-        String query = "${escapedCode.replace(/"/g, '\\"')}";
+        String query = "${escapedJavaCode}";
         
         Transaction txn = dgraphClient.newTransaction();
         try {
EOF
@@ -45,6 +45,12 @@

const escapedCode = escapeForTemplate(codeContent);

// Helper function to escape input for Java string literals
const escapeForJavaString = (str: string) => {
return str.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
};
const escapedJavaCode = escapeForJavaString(codeContent);

return (
<div className={styles.runnable}>
<Tabs>
@@ -111,7 +117,7 @@
DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);

String query = "${escapedCode.replace(/"/g, '\\"')}";
String query = "${escapedJavaCode}";

Transaction txn = dgraphClient.newTransaction();
try {
Copilot is powered by AI and may make mistakes. Always verify output.
@raphael-istari raphael-istari merged commit a6de249 into main Nov 7, 2025
3 of 4 checks passed
@raphael-istari raphael-istari deleted the raphael/docusaurus-migration branch November 7, 2025 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants