Skip to content
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

CDPT-2079 AJAX search fix & refactor #738

Merged
merged 9 commits into from
Oct 16, 2024
Merged

Conversation

EarthlingDavey
Copy link
Contributor

@EarthlingDavey EarthlingDavey commented Oct 15, 2024

This PR includes a refactor of archive pages.

AJAX filter/search results are pulled in via ajax.

The underlying WP_Query for the AJAX results is the same as the WP_Query used for loading the first page of results (without AJAX).

Some bugs have been ironed out and unnecessary code has been removed.

There are still a lot of potential improvements to be made, e.g.

  • remove get-news-rest-api.php and re-write functions that rely on it.
  • remove pagination.php and modify templates that use it, as a 2nd WP_Query should not be run just for pagination.

^ having removed the file get-blog-rest-api.php and refactoring pagination on page_news.php and page_blog.php shows that the above improvements are feasible.

const resultsHtml = posts.map((props) => t.renderHtml(props));

// Append the html to the content section.
$("#content").append(resultsHtml);

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

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

Copilot Autofix AI 2 months ago

To fix the problem, we need to ensure that any HTML content generated from the template is properly sanitized before being inserted into the DOM. We can use a library like DOMPurify to sanitize the HTML content. This will remove any potentially malicious scripts or tags from the HTML content.

  1. Install the DOMPurify library.
  2. Import DOMPurify in the relevant files.
  3. Use DOMPurify.sanitize to sanitize the HTML content before appending it to the DOM.
Suggested changeset 2
public/app/themes/clarity/src/globals/js/ajax-utils.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/public/app/themes/clarity/src/globals/js/ajax-utils.js b/public/app/themes/clarity/src/globals/js/ajax-utils.js
--- a/public/app/themes/clarity/src/globals/js/ajax-utils.js
+++ b/public/app/themes/clarity/src/globals/js/ajax-utils.js
@@ -1,2 +1,3 @@
 import AjaxTemplating from "./ajax-templating.js";
+import DOMPurify from "dompurify";
 
@@ -89,4 +90,7 @@
 
+  // Sanitize the html content.
+  const sanitizedHtml = resultsHtml.map(html => DOMPurify.sanitize(html));
+
   // Append the html to the content section.
-  $("#content").append(resultsHtml);
+  $("#content").append(sanitizedHtml);
 
EOF
@@ -1,2 +1,3 @@
import AjaxTemplating from "./ajax-templating.js";
import DOMPurify from "dompurify";

@@ -89,4 +90,7 @@

// Sanitize the html content.
const sanitizedHtml = resultsHtml.map(html => DOMPurify.sanitize(html));

// Append the html to the content section.
$("#content").append(resultsHtml);
$("#content").append(sanitizedHtml);

public/app/themes/clarity/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/public/app/themes/clarity/package.json b/public/app/themes/clarity/package.json
--- a/public/app/themes/clarity/package.json
+++ b/public/app/themes/clarity/package.json
@@ -30,3 +30,4 @@
         "jquery": "^3.7.1",
-        "node-notifier": "^10.0.1"
+        "node-notifier": "^10.0.1",
+        "dompurify": "^3.1.7"
     },
EOF
@@ -30,3 +30,4 @@
"jquery": "^3.7.1",
"node-notifier": "^10.0.1"
"node-notifier": "^10.0.1",
"dompurify": "^3.1.7"
},
This fix introduces these dependencies
Package Version Security advisories
dompurify (npm) 3.1.7 None
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is OK as I do see a way that an attacker could inject malicious code into resultsHtml.

Would you mind offering a second opinion please @wilson1000 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we test this? It looks good 😄

totalPages: !totalResults ? 1 : Math.ceil(totalResults / resultsPerPage),
});

$(".c-pagination").html(paginationHtml);

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

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

Copilot Autofix AI 2 months ago

To fix the problem, we need to ensure that any dynamic content inserted into the DOM is properly sanitized or escaped to prevent XSS attacks. The best way to fix this is to use a library like DOMPurify to sanitize the HTML content before inserting it into the DOM.

  1. Install the DOMPurify library.
  2. Import DOMPurify in the relevant files.
  3. Use DOMPurify.sanitize to sanitize the HTML content before inserting it into the DOM.
Suggested changeset 3
public/app/themes/clarity/src/globals/js/ajax-utils.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/public/app/themes/clarity/src/globals/js/ajax-utils.js b/public/app/themes/clarity/src/globals/js/ajax-utils.js
--- a/public/app/themes/clarity/src/globals/js/ajax-utils.js
+++ b/public/app/themes/clarity/src/globals/js/ajax-utils.js
@@ -1,2 +1,3 @@
 import AjaxTemplating from "./ajax-templating.js";
+import DOMPurify from 'dompurify';
 
@@ -137,3 +138,3 @@
 
-  $(".c-pagination").html(paginationHtml);
+  $(".c-pagination").html(DOMPurify.sanitize(paginationHtml));
 
EOF
@@ -1,2 +1,3 @@
import AjaxTemplating from "./ajax-templating.js";
import DOMPurify from 'dompurify';

@@ -137,3 +138,3 @@

$(".c-pagination").html(paginationHtml);
$(".c-pagination").html(DOMPurify.sanitize(paginationHtml));

public/app/themes/clarity/src/globals/js/ajax-templating.js
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/public/app/themes/clarity/src/globals/js/ajax-templating.js b/public/app/themes/clarity/src/globals/js/ajax-templating.js
--- a/public/app/themes/clarity/src/globals/js/ajax-templating.js
+++ b/public/app/themes/clarity/src/globals/js/ajax-templating.js
@@ -1 +1,2 @@
+import DOMPurify from 'dompurify';
 /**
@@ -85,3 +86,3 @@
 
-    return parts.join("");
+    return DOMPurify.sanitize(parts.join(""));
   }
EOF
@@ -1 +1,2 @@
import DOMPurify from 'dompurify';
/**
@@ -85,3 +86,3 @@

return parts.join("");
return DOMPurify.sanitize(parts.join(""));
}
public/app/themes/clarity/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/public/app/themes/clarity/package.json b/public/app/themes/clarity/package.json
--- a/public/app/themes/clarity/package.json
+++ b/public/app/themes/clarity/package.json
@@ -30,3 +30,4 @@
         "jquery": "^3.7.1",
-        "node-notifier": "^10.0.1"
+        "node-notifier": "^10.0.1",
+        "dompurify": "^3.1.7"
     },
EOF
@@ -30,3 +30,4 @@
"jquery": "^3.7.1",
"node-notifier": "^10.0.1"
"node-notifier": "^10.0.1",
"dompurify": "^3.1.7"
},
This fix introduces these dependencies
Package Version Security advisories
dompurify (npm) 3.1.7 None
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
@EarthlingDavey EarthlingDavey marked this pull request as ready for review October 15, 2024 16:09
Copy link
Contributor

@wilson1000 wilson1000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @EarthlingDavey , it looks like you ventured down a rabbit hole here 😄
Can you walk me through the update tomorrow? It would be good to get your view of the improvements you've made. Thank you!

@@ -120,6 +119,9 @@
new MOJ\Intranet\WPOffloadMedia();
new MOJ\Intranet\WPElasticPress();

$search = new MOJ\Intranet\Search();
$search->hooks();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to notate it like this? Can we follow the pattern of include and or do we need other $search variables in the functions.php script?

Copy link
Contributor

@wilson1000 wilson1000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@EarthlingDavey EarthlingDavey merged commit 327b7b7 into develop Oct 16, 2024
6 checks passed
@EarthlingDavey EarthlingDavey deleted the ajax-search-refactor branch October 16, 2024 11:21
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.

2 participants